From b8d486412f4f922c12173bbe83a027a8dfd58f38 Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Wed, 25 Oct 2023 21:48:45 +0200 Subject: [PATCH] fix issues with notification issues --- .../ui/activities/NotificationsActivity.kt | 25 +++++++++++-------- .../ui/fragments/NavigationDrawerFragment.kt | 4 +-- .../ui/viewmodels/NotificationsViewModel.kt | 8 +++++- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/activities/NotificationsActivity.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/activities/NotificationsActivity.kt index f5cc2782c..71b6b12ef 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/activities/NotificationsActivity.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/activities/NotificationsActivity.kt @@ -14,6 +14,7 @@ import android.widget.RatingBar import android.widget.TextView import androidx.activity.viewModels import androidx.core.content.ContextCompat +import androidx.core.view.children import androidx.lifecycle.lifecycleScope import com.habitrpg.android.habitica.R import com.habitrpg.android.habitica.data.InventoryRepository @@ -43,7 +44,9 @@ import com.habitrpg.common.habitica.models.notifications.UnallocatedPointsData import com.habitrpg.common.habitica.views.PixelArtView import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -74,6 +77,7 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget private var notifications: List = emptyList() + @OptIn(FlowPreview::class) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -88,7 +92,9 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as? LayoutInflater lifecycleScope.launchCatching { - viewModel.getNotifications().collect { + viewModel.getNotifications() + .debounce(500) + .collect { setNotifications(it) viewModel.markNotificationsAsSeen(it) } @@ -138,9 +144,7 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget binding.notificationItems.showDividers = LinearLayout.SHOW_DIVIDER_MIDDLE or LinearLayout.SHOW_DIVIDER_END val currentViews = mutableSetOf().apply { - for (i in 0 until binding.notificationItems.childCount) { - add(binding.notificationItems.getChildAt(i)) - } + addAll(binding.notificationItems.children) } lifecycleScope.launch(ExceptionHandler.coroutine()) { @@ -160,9 +164,7 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget } item?.let { view -> - if (currentViews.contains(view)) { - currentViews.remove(view) - } else { + if (!currentViews.removeIf { it.tag == view.tag }) { binding.notificationItems.addView(view) } } @@ -227,13 +229,16 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget ) } + private var baileyNewsNotification: Notification? = null + private suspend fun createNewStuffNotification(notification: Notification): View? = withContext(Dispatchers.IO) { var baileyNotification = notification val data = notification.data as? NewStuffData val text = if (data?.title != null) { fromHtml("" + getString(R.string.new_bailey_update) + "
" + data.title) } else { - baileyNotification = userRepository.getNewsNotification() ?: notification + baileyNotification = baileyNewsNotification ?: userRepository.getNewsNotification() ?: notification + baileyNewsNotification = baileyNotification val baileyNewsData = baileyNotification.data as? NewStuffData fromHtml("" + getString(R.string.new_bailey_update) + "
" + baileyNewsData?.title) } @@ -330,6 +335,7 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget textColor: Int? = null ): View? { val item = inflater?.inflate(R.layout.notification_item, binding.notificationItems, false) + item?.tag = notification.id val container = item?.findViewById(R.id.notification_item) as? View container?.setOnClickListener { @@ -442,6 +448,7 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget inviterId: String? = null ): View? { val item = inflater?.inflate(R.layout.notification_item_actionable, binding.notificationItems, false) + item?.tag = notification.id if (openable) { val container = item?.findViewById(R.id.notification_item) as? View @@ -461,7 +468,6 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget val acceptButton = item?.findViewById(R.id.accept_button) as? Button acceptButton?.setOnClickListener { - binding.root.flash() HapticFeedbackManager.tap(it) removeNotificationAndRefresh(notification) viewModel.accept(notification.id) @@ -469,7 +475,6 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget val rejectButton = item?.findViewById(R.id.reject_button) as? Button rejectButton?.setOnClickListener { - binding.root.flash() HapticFeedbackManager.tap(it) removeNotificationAndRefresh(notification) viewModel.reject(notification.id) diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/NavigationDrawerFragment.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/NavigationDrawerFragment.kt index 8f8cb3ca3..66a9209af 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/NavigationDrawerFragment.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/fragments/NavigationDrawerFragment.kt @@ -614,8 +614,8 @@ class NavigationDrawerFragment : DialogFragment() { // set UP the drawer's list view with items and click listener lifecycleScope.launchCatching { - viewModel.getNotifications().collect { - setNotificationsCount(it.count()) + viewModel.getNotificationCount().collect { + setNotificationsCount(it) } } lifecycleScope.launchCatching { diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/viewmodels/NotificationsViewModel.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/viewmodels/NotificationsViewModel.kt index e0e31108b..fb8724bbe 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/viewmodels/NotificationsViewModel.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/viewmodels/NotificationsViewModel.kt @@ -56,6 +56,7 @@ open class NotificationsViewModel @Inject constructor( ) private var party: UserParty? = null + private var hasStats = false private val customNotifications = MutableStateFlow>(emptyList()) @@ -63,6 +64,7 @@ open class NotificationsViewModel @Inject constructor( userViewModel.user.observeForever { if (it == null) return@observeForever party = it.party + hasStats = it.hasClass val notifications = convertInvitationsToNotifications(it) if (it.flags?.newStuff == true) { val notification = Notification() @@ -89,7 +91,11 @@ open class NotificationsViewModel @Inject constructor( } fun getNotificationCount(): Flow { - return getNotifications().map { it.count() }.distinctUntilChanged() + return getNotifications().map { + it.count { notification -> + (notification.type == Notification.Type.UNALLOCATED_STATS_POINTS.type) == hasStats + } + }.distinctUntilChanged() } fun allNotificationsSeen(): Flow {