Skip to content

Commit

Permalink
Load notification view individually versus creating entire view list
Browse files Browse the repository at this point in the history
  • Loading branch information
Hafizzle authored and phillipthelen committed Oct 20, 2023
1 parent d4a5275 commit 04e6b95
Showing 1 changed file with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,18 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget
binding.notificationItems.addView(inflater?.inflate(R.layout.no_notifications, binding.notificationItems, false))
}


private fun displayNotificationsListView(notifications: List<Notification>) {
binding.notificationItems.showDividers = LinearLayout.SHOW_DIVIDER_MIDDLE or LinearLayout.SHOW_DIVIDER_END
val viewList = arrayListOf<View>()
createNotificationsHeaderView(notifications.count())?.let { viewList.add(it) }

val currentViews = mutableSetOf<View>().apply {
for (i in 0 until binding.notificationItems.childCount) {
add(binding.notificationItems.getChildAt(i))
}
}

lifecycleScope.launch(ExceptionHandler.coroutine()) {
notifications.map {
notifications.forEach {
val item: View? = when (it.type) {
Notification.Type.NEW_CHAT_MESSAGE.type -> createNewChatMessageNotification(it)
Notification.Type.NEW_STUFF.type -> createNewStuffNotification(it)
Expand All @@ -154,30 +159,23 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget
else -> null
}

if (item != null) {
viewList.add(item)
item?.let { view ->
if (currentViews.contains(view)) {
currentViews.remove(view)
} else {
binding.notificationItems.addView(view)
}
}
}
updateNotificationsAndRefresh(viewList)
}
}

private fun updateNotificationsAndRefresh(newItems: List<View>) {
val currentViews = (0 until binding.notificationItems.childCount).map {
binding.notificationItems.getChildAt(it)
}
val viewsToRemove = currentViews - newItems
viewsToRemove.forEach { binding.notificationItems.removeView(it) }
val viewsToAdd = newItems - currentViews
viewsToAdd.forEach {
binding.notificationItems.addView(it)
}
// Remove views that are no longer valid
currentViews.forEach { binding.notificationItems.removeView(it) }

lifecycleScope.launch {
delay(250)
// Unnecessary but looks clean c:
if (binding.notificationItems.visibility != View.VISIBLE) {
binding.notificationItems.fadeInAnimation(200)
lifecycleScope.launch {
delay(250)
if (binding.notificationItems.visibility != View.VISIBLE) {
binding.notificationItems.fadeInAnimation(200)
}
}
}
}
Expand Down

0 comments on commit 04e6b95

Please sign in to comment.