์ปค๋ฎค๋ํฐ ํ์ด์ง์์ ์กฐ๊ฑด์ ๋ฐ๋ฅธ ๊ธ ์ ๋ ฌํ๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํด๋ณด์. ๋ฉ๋ด ์์ดํ ์ ์ด์ฉํด์ ์ ๋ ฌ ์กฐ๊ฑด์ ์ ํํ๋ฉด ์กฐ๊ฑด์ ๋ฐ๋ผ ๊ฒ์๊ธ์ด ์ ๋ ฌ๋๋ค.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_option_latest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:title="์ต์ ์" />
<item
android:id="@+id/menu_option_deadline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:title="์ธ๊ธฐ์" />
<item
android:id="@+id/menu_option_deadline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:title="๋ง๊ฐ์ผ์" />
<item
android:id="@+id/menu_option_mine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:title="๋ด ๊ธ ์" />
</menu>
๋ฉ๋ด์ ์์ดํ ์ ๋๋ ์ ๋ ๋๋ฆฐ๊ฑด์ง ์๊ธฐ ์ํ ๋ก๊ทธ๋ก ํ์ธํด์ค๋ค.
binding.communityImageOption.setOnClickListener {
Log.d("Options", "๋ฒํผ ๋๋ ๋?")
val menu = PopupMenu(context, it)
menu.menuInflater.inflate(R.menu.sort_option, menu.menu)
menu.setOnMenuItemClickListener {
when(it.itemId) {
R.id.menu_option_latest -> {
Log.d("menu", "์ต์ ์")
true
}
R.id.menu_option_like -> {
Log.d("menu", "์ธ๊ธฐ์")
true
}
R.id.menu_option_deadline -> {
Log.d("menu", "๋ง๊ฐ์ผ์")
true
}
R.id.menu_option_mine -> {
Log.d("menu", "๋ด ๊ธ ์")
true
}
else -> {
false
}
}
}
}
๊ฒ์๋ฌผ์ ์ ๋ก๋ํ ๋ ์ง๊ฐ ์ต์ ์ธ ๊ฒ์๋ฌผ์ ์๋จ์ ํ์๋๋๋ก ์ฌ๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ธฐ ์ํด sortBy ํจ์๋ฅผ ์ด์ฉํด์ฃผ๋ฉด ๋๋ค.
// ์ต์ ์์ผ๋ก ์ ๋ ฌ
private fun sortPostLatest() {
Log.d("menu", "์ต์ ์์ผ๋ก ์ ๋ ฌ")
communityList.sortBy { it.date }
communityPageAdapter.notifyDataSetChanged()
}
// ๋ง๊ฐ์ผ์์ผ๋ก ์ ๋ ฌ
@SuppressLint("NotifyDataSetChanged")
private fun sortPostDeadline() {
Log.d("menu", "๋ง๊ฐ์ผ ์์ผ๋ก ์ ๋ ฌ")
communityList.sortBy { communityPageAdapter.calculateDday(it) }
communityPageAdapter.addItem(communityList)
communityPageAdapter.notifyDataSetChanged()
}
๊ฒ์๋ฌผ์ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ PostModel์ ์ฝ๋์์ deadline์ ๊ทธ๋ฅ ๊ฐ์ ธ์ค๊ณ ์ด๋ก ์ ๋ ฌ์ ํด์ฃผ๋ฉด ๋์ง ์์๊น? ํ์ง๋ง... ์คํจ!
deadlinedate๋ ์ด๋ป๊ฒ ๋์ค๋์ง ํ์ธํ๊ธฐ ์ํด ๋ก๊ทธ๋ฅผ ์ฐ์ด๋ดค๋ค.
์๊ฐํ๋ ๋ก๊ทธ ๊ฒฐ๊ณผ๋ deadlinedate=20231117
์ด์๊ธฐ ๋๋ฌธ์ ์ฒ์์ ์ฝ๋๋ฅผ ์์ฑํ๋ ๊ฑฐ์๋๋ฐ...!!!!
๊ทธ๋๋ ๋ก๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํตํด ๋ง๊ฐ์ผ์ ๊ฐ์ ธ์ฌ๋ ๋
, ์, ์ผ์ ์ ์ธํ Date๊ฐ๋ก ๋ณํํด์ ๊ฐ์ ธ์ค๋ฉด ๋๊ฒ ๋ค!
์ค๋์ ๋ ์ง์์ ์๋ฐํ ์์ผ๋ก ๊ฒ์๋ฌผ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
// ๋ง๊ฐ์ผ์์ผ๋ก ์ ๋ ฌ
@SuppressLint("NotifyDataSetChanged")
private fun sortPostDeadline() {
Log.d("menu", "๋ง๊ฐ์ผ ์์ผ๋ก ์ ๋ ฌ")
val currentDate = System.currentTimeMillis()
// ๋ง๊ฐ์ผ์ ๋ฐ๋ฅธ ์ ๋ ฌ
communityList.sortWith(compareBy { post ->
// ๊ฒ์๋ฌผ ๋ง๊ฐ์ผ ๊ฐ์ ธ์ค๊ธฐ
val deadlineDate = post.deadlinedate
// ๋ง๊ฐ์ผ ๋ฌธ์์ด์ Date ๊ฐ์ฒด๋ก ๋ณํ
val dateFormat = SimpleDateFormat("yyyy๋
MM์ dd์ผ", Locale.getDefault())
// Date ๊ฐ์ฒด๋ก ํ์ฑ
val date = dateFormat.parse(deadlineDate)
// ๋ฐ๋ฆฌ์ด๋ก ๋ณํ
val deadline = date?.time ?: 0
(deadline - currentDate).toInt()
})
communityPageAdapter.addItem(communityList)
communityPageAdapter.notifyDataSetChanged()
}
์ค๋์ ๋ ์ง์์ ์๋ฐํ ์์ผ๋ก ๊ฒ์๋ฌผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
// ๋ง๊ฐ์ผ์์ผ๋ก ์ ๋ ฌ
@SuppressLint("NotifyDataSetChanged")
private fun sortPostDeadline() {
Log.d("menu", "๋ง๊ฐ์ผ ์์ผ๋ก ์ ๋ ฌ")
val currentDate = System.currentTimeMillis()
// ๋ง๊ฐ์ผ์ ๋ฐ๋ฅธ ์ ๋ ฌ
communityList.sortWith(compareBy { post ->
val deadlineDate = post.deadlinedate
val dateFormat = SimpleDateFormat("yyyy๋
MM์ dd์ผ", Locale.getDefault())
val date = dateFormat.parse(deadlineDate)
val deadline = date?.time ?: 0
(currentDate - deadline).toInt()
})
communityPageAdapter.addItem(communityList)
communityPageAdapter.notifyDataSetChanged()
}
ํ์ฌ ๋ก๊ทธ์ธ๋ ๊ณ์ ์ ์ ์ ๊ฐ ์ฌ๋ฆฐ ๊ฒ์๋ฌผ๋ง ์ต์ ์์ผ๋ก ์ ๋ ฌํ๊ธฐ
ํ์ฌ user์ธ์ง ํ์ธํ๊ณ ๊ฒ์๊ธ์ ์์ฑ์๊ฐ ํ์ฌ ๋ก๊ทธ์ธ๋ user์ ๋๋ค์์ด ๋์ผํ ๊ฒฝ์ฐ์ ๊ฒ์๋ฌผ๋ง ๋ณด์ด๋๋ก ์ค์ ํด์ฃผ๊ณ ์ ํ๋ค.
// ๋ด ๊ธ๋ก ์ ๋ ฌ
@SuppressLint("NotifyDataSetChanged")
private fun sortPostMine() {
Log.d("menu", "๋ด ๊ธ๋ก๋ง ์ต์ ์์ผ๋ก ์ ๋ ฌ")
communityList.map { it.nickname }.forEach {
Log.d("menu", it) }
val currentUser = auth.currentUser
if(currentUser != null) {
Log.d("currentUseruid", "$currentUser.uid")
// ๋๋ค์๊ณผ ํ์ฌ ๋ก๊ทธ์ธ๋ ์ ์ ์ ๋์ผํ๋ฉด
val myPost = communityList.filter { it.nickname == currentUser.uid }
myPost.sortedBy { it.date }
Log.d("myPost", "$myPost")
val myPostMutable = myPost.toMutableList()
communityPageAdapter.addItem(myPostMutable)
}
communityPageAdapter.notifyDataSetChanged()
}
์ ๋๋ก ์๋ํ์ง ์์, ๋ก๊ทธ๋ฅผ ์ฐ์ด์ ํ์ธํด๋ณด๋ uid์ ๊ฐ๊ณผ nickname์ด ๋์ผํ์ง๋ฅผ ํ์ธํ๊ณ ์์๊ณ ... ์ด๋ฌ๋ ์๋ฌด๋ฐ ๊ฒ์๊ธ๋ ์๋จ์ง..
ํ์ด์ด๋ฒ ์ด์ค์ RealTimeDB์ ์ ์ฅ๋๋ ๋๋ค์์ ๋ฐ์ดํฐ์ ๊ฒฝ๋ก๋ฅผ ํ์ธํด๋ณด๋ฉด,
users - uid - nickname
์ด๋ค.
users
๊ฒฝ๋ก์์ userId
์ nickname
๋ฐ์ดํฐ๋ฅผ ํ์ธํ๋๋ก firebaseDatabase
ํจ์๋ฅผ ์ด์ฉํด์ snapshot
์ด ์กด์ฌํ๋ฉด ์ด๋ ๋ฐ์ดํฐ๊ฐ ์กด์ฌํ๋ค๋ ๊ฒ์ผ๋ก, ํ์ฌ ๋ก๊ทธ์ธ๋ ๊ณ์ ์ nickname
๊ณผ ๊ฒ์๋ฌผ์ nickname
์ ๋น๊ตํด ๊ฐ์ ๊ฒ์๋ฌผ๋ค๋ง ๋์ค๋๋ก ๊ตฌํํด์ฃผ์๋ค.
@SuppressLint("NotifyDataSetChanged")
private fun sortPostMine() {
Log.d("menu", "๋ด ๊ธ๋ก๋ง ์ต์ ์์ผ๋ก ์ ๋ ฌ")
val currentUser = auth.currentUser
if(currentUser != null) {
val userId = currentUser.uid
firebaseDatabase.child("users").child(userId).child("nickname")
.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if(snapshot.exists()) {
val currentNickname = snapshot.getValue(String ::class.java)
val myPost = communityList.filter { it.nickname == currentNickname }
val sortedMyPost = myPost.sortedBy { it.date }
Log.d("MyPost", "$sortedMyPost")
communityPageAdapter.addItem(sortedMyPost.toMutableList())
communityPageAdapter.notifyDataSetChanged()
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
}