make notifications reload more reliably

This commit is contained in:
Phillip Thelen 2023-10-30 23:34:50 +01:00
parent 98f3a2957c
commit ef2962f317
4 changed files with 97 additions and 32 deletions

View file

@ -39,15 +39,73 @@
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/notification_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:listDivider"
android:visibility="invisible"
android:orientation="vertical"
android:showDividers="middle" />
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="20dp"
android:paddingTop="10dp"
android:paddingEnd="10dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:orientation="horizontal"
android:paddingTop="@dimen/spacing_medium">
<TextView
android:id="@android:id/title"
style="@style/Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dip"
android:background="@color/transparent"
android:text="@string/notifications"
android:textAllCaps="true"
android:textColor="@color/text_quad"
android:textSize="12sp" />
<TextView
android:id="@+id/notifications_title_badge"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_marginStart="10dp"
android:background="@drawable/badge_gray"
android:gravity="center"
android:minWidth="24dp"
android:textColor="@color/text_quad"
android:textSize="12sp"
tools:text="1" />
</LinearLayout>
<Button
android:id="@+id/dismiss_all_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:text="@string/dismiss_all"
android:textColor="@color/text_brand" />
</LinearLayout>
<androidx.compose.ui.platform.ComposeView
android:id="@+id/progress_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<LinearLayout
android:id="@+id/notification_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:listDivider"
android:visibility="invisible"
android:orientation="vertical"
android:showDividers="middle" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout>

View file

@ -1,9 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingStart="@dimen/spacing_medium"
android:paddingTop="44dp"

View file

@ -13,8 +13,10 @@ import android.widget.LinearLayout
import android.widget.RatingBar
import android.widget.TextView
import androidx.activity.viewModels
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.core.view.children
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.data.InventoryRepository
@ -41,6 +43,7 @@ import com.habitrpg.common.habitica.models.notifications.NewStuffData
import com.habitrpg.common.habitica.models.notifications.PartyInvitationData
import com.habitrpg.common.habitica.models.notifications.QuestInvitationData
import com.habitrpg.common.habitica.models.notifications.UnallocatedPointsData
import com.habitrpg.common.habitica.views.HabiticaCircularProgressView
import com.habitrpg.common.habitica.views.PixelArtView
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
@ -91,19 +94,36 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget
inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as? LayoutInflater
binding.progressView.setContent {
HabiticaCircularProgressView(indicatorSize = 60.dp)
}
lifecycleScope.launchCatching {
viewModel.getNotifications()
.debounce(500)
.debounce(250)
.collect {
setNotifications(it)
viewModel.markNotificationsAsSeen(it)
}
}
lifecycleScope.launchCatching {
viewModel.getNotificationCount()
.collect {
binding.notificationsTitleBadge.text = it.toString()
}
}
binding.notificationsRefreshLayout.setOnRefreshListener(this)
lifecycleScope.launchCatching {
viewModel.refreshNotifications()
}
binding.dismissAllButton.setOnClickListener {
HapticFeedbackManager.tap(it)
viewModel.dismissAllNotifications(notifications)
setNotifications(emptyList())
}
}
override fun onSupportNavigateUp(): Boolean {
@ -137,17 +157,17 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget
binding.notificationItems.removeAllViewsInLayout()
binding.notificationItems.showDividers = LinearLayout.SHOW_DIVIDER_NONE
binding.notificationItems.addView(inflater?.inflate(R.layout.no_notifications, binding.notificationItems, false))
binding.progressView.isVisible = false
}
private fun displayNotificationsListView(notifications: List<Notification>) {
binding.notificationItems.showDividers = LinearLayout.SHOW_DIVIDER_MIDDLE or LinearLayout.SHOW_DIVIDER_END
val currentViews = mutableSetOf<View>().apply {
addAll(binding.notificationItems.children)
}
lifecycleScope.launch(ExceptionHandler.coroutine()) {
val currentViews = mutableSetOf<View>().apply {
addAll(binding.notificationItems.children)
}
notifications.forEach {
val item: View? = when (it.type) {
Notification.Type.NEW_CHAT_MESSAGE.type -> createNewChatMessageNotification(it)
@ -174,6 +194,7 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget
currentViews.forEach { binding.notificationItems.removeView(it) }
lifecycleScope.launch {
binding.progressView.isVisible = false
delay(250)
if (binding.notificationItems.visibility != View.VISIBLE) {
binding.notificationItems.fadeInAnimation(200)
@ -194,22 +215,6 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget
}
}
private fun createNotificationsHeaderView(notificationCount: Int): View? {
val header = inflater?.inflate(R.layout.notifications_header, binding.notificationItems, false)
val badge = header?.findViewById(R.id.notifications_title_badge) as? TextView
badge?.text = notificationCount.toString()
val dismissAllButton = header?.findViewById(R.id.dismiss_all_button) as? Button
dismissAllButton?.setOnClickListener {
binding.root.flash()
HapticFeedbackManager.tap(it)
viewModel.dismissAllNotifications(notifications)
}
return header
}
private fun createNewChatMessageNotification(notification: Notification): View? {
val data = notification.data as? NewChatMessageData
val stringId = if (viewModel.isPartyMessage(data)) R.string.new_msg_party else R.string.new_msg_guild
@ -349,7 +354,7 @@ class NotificationsActivity : BaseActivity(), androidx.swiperefreshlayout.widget
val dismissButton = item?.findViewById(R.id.dismiss_button) as? ImageView
dismissButton?.setOnClickListener {
container?.flash()
it.flash()
HapticFeedbackManager.tap(it)
removeNotificationAndRefresh(notification)
viewModel.dismissNotification(notification)

View file

@ -112,7 +112,11 @@ open class SubscriptionBottomSheetFragment : BottomSheetDialogFragment() {
for (sku in subscriptions) {
updateButtonLabel(sku, sku.subscriptionOfferDetails?.firstOrNull()?.pricingPhases?.pricingPhaseList?.firstOrNull()?.formattedPrice ?: "")
}
subscriptions.minByOrNull { it.subscriptionOfferDetails?.firstOrNull()?.pricingPhases?.pricingPhaseList?.firstOrNull()?.priceAmountMicros ?: 0 }?.let { selectSubscription(it) }
subscriptions
.filter { buttonForSku(it)?.isVisible == true }
.minByOrNull {
it.subscriptionOfferDetails?.firstOrNull()?.pricingPhases?.pricingPhaseList?.firstOrNull()?.priceAmountMicros ?: 0
}?.let { selectSubscription(it) }
hasLoadedSubscriptionOptions = true
updateSubscriptionInfo()
}