Skip to content

Commit

Permalink
Fix unread inbox counter badge not properly updating (#1051)
Browse files Browse the repository at this point in the history
* Fix unreadCount not being a state, and properly do fetchesUnread

* Remove duplicate code
  • Loading branch information
MV-GH authored Jul 18, 2023
1 parent 23ffbc8 commit 455e89a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 31 deletions.
14 changes: 10 additions & 4 deletions app/src/main/java/com/jerboa/model/InboxViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ class InboxViewModel : ViewModel(), Initializable {

fun markReplyAsRead(
form: MarkCommentReplyAsRead,
onSuccess: () -> Unit,
) {
viewModelScope.launch {
markReplyAsReadRes = ApiState.Loading
Expand All @@ -384,6 +385,7 @@ class InboxViewModel : ViewModel(), Initializable {
val newRes =
ApiState.Success(existing.data.copy(replies = mutable.toList()))
repliesRes = newRes
onSuccess()
}

else -> {}
Expand All @@ -397,6 +399,7 @@ class InboxViewModel : ViewModel(), Initializable {

fun markPersonMentionAsRead(
form: MarkPersonMentionAsRead,
onSuccess: () -> Unit,
) {
viewModelScope.launch {
markMentionAsReadRes = ApiState.Loading
Expand All @@ -411,9 +414,9 @@ class InboxViewModel : ViewModel(), Initializable {
existing.data.mentions,
readRes.data.person_mention_view,
)
val newRes =
ApiState.Success(existing.data.copy(mentions = newMentions))
val newRes = ApiState.Success(existing.data.copy(mentions = newMentions))
mentionsRes = newRes
onSuccess()
}

else -> {}
Expand All @@ -427,6 +430,7 @@ class InboxViewModel : ViewModel(), Initializable {

fun markPrivateMessageAsRead(
form: MarkPrivateMessageAsRead,
onSuccess: () -> Unit,
) {
viewModelScope.launch {
markMessageAsReadRes = ApiState.Loading
Expand All @@ -441,9 +445,9 @@ class InboxViewModel : ViewModel(), Initializable {
existing.data.private_messages,
readRes.data.private_message_view,
)
val newRes =
ApiState.Success(existing.data.copy(private_messages = newMessages))
val newRes = ApiState.Success(existing.data.copy(private_messages = newMessages))
messagesRes = newRes
onSuccess()
}

else -> {}
Expand Down Expand Up @@ -473,6 +477,7 @@ class InboxViewModel : ViewModel(), Initializable {

fun markAllAsRead(
form: MarkAllAsRead,
onComplete: () -> Unit,
) {
viewModelScope.launch {
markAllAsReadRes = ApiState.Loading
Expand Down Expand Up @@ -507,6 +512,7 @@ class InboxViewModel : ViewModel(), Initializable {

else -> {}
}
onComplete()
}
}

Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/jerboa/model/SiteViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jerboa.model

import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand All @@ -25,6 +26,8 @@ class SiteViewModel : ViewModel() {

private var unreadCountRes: ApiState<GetUnreadCountResponse> by mutableStateOf(ApiState.Empty)

val unreadCount by derivedStateOf { getUnreadCountTotal(unreadCountRes) }

var sortType by mutableStateOf(SortType.Active)
private set
var listingType by mutableStateOf(ListingType.Local)
Expand Down Expand Up @@ -73,7 +76,7 @@ class SiteViewModel : ViewModel() {
}
}

fun getUnreadCountTotal(): Int {
private fun getUnreadCountTotal(unreadCountRes: ApiState<GetUnreadCountResponse>): Int {
return when (val res = unreadCountRes) {
is ApiState.Success -> {
val unreads = res.data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fun MainDrawer(
is ApiState.Success -> res.data.my_user
else -> null
},
unreadCount = siteViewModel.getUnreadCountTotal(),
unreadCount = siteViewModel.unreadCount,
accountViewModel = accountViewModel,
onAddAccount = onClickLogin,
isOpen = drawerState.isOpen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fun BottomNavActivity(
if (appSettings.showBottomNav) {
BottomAppBarAll(
selectedTab = selectedTab,
unreadCounts = siteViewModel.getUnreadCountTotal(),
unreadCounts = siteViewModel.unreadCount,
showTextDescriptionsInNavbar = appSettings.showTextDescriptionsInNavbar,
onSelect = onSelectTab,
)
Expand Down
65 changes: 41 additions & 24 deletions app/src/main/java/com/jerboa/ui/components/inbox/InboxActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fun InboxActivity(
val snackbarHostState = remember { SnackbarHostState() }
val ctx = LocalContext.current
val account = getCurrentAccount(accountViewModel)
val unreadCount = siteViewModel.getUnreadCountTotal()

val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())

val inboxViewModel: InboxViewModel = viewModel()
Expand All @@ -112,6 +112,7 @@ fun InboxActivity(
inboxViewModel.getMessages(
inboxViewModel.getFormMessages(account.jwt),
)
siteViewModel.fetchUnreadCounts(GetUnreadCount(account.jwt))
}
}

Expand All @@ -121,7 +122,7 @@ fun InboxActivity(
topBar = {
InboxHeader(
scrollBehavior = scrollBehavior,
unreadCount = unreadCount,
unreadCount = siteViewModel.unreadCount,
openDrawer = {
scope.launch {
drawerState.open()
Expand Down Expand Up @@ -149,13 +150,23 @@ fun InboxActivity(
MarkAllAsRead(
auth = acct.jwt,
),
)
// TODO test this
// Update site counts
siteViewModel.fetchUnreadCounts(
GetUnreadCount(
auth = acct.jwt,
),
onComplete = {
siteViewModel.fetchUnreadCounts(
GetUnreadCount(
auth = acct.jwt,
),
)
inboxViewModel.resetPages()
inboxViewModel.getReplies(
inboxViewModel.getFormReplies(account.jwt),
)
inboxViewModel.getMentions(
inboxViewModel.getFormMentions(account.jwt),
)
inboxViewModel.getMessages(
inboxViewModel.getFormMessages(account.jwt),
)
},
)
}
},
Expand Down Expand Up @@ -287,11 +298,13 @@ fun InboxTabs(
read = !crv.comment_reply.read,
auth = acct.jwt,
),
)
siteViewModel.fetchUnreadCounts(
GetUnreadCount(
auth = acct.jwt,
),
onSuccess = {
siteViewModel.fetchUnreadCounts(
GetUnreadCount(
auth = acct.jwt,
),
)
},
)
}
}
Expand Down Expand Up @@ -525,11 +538,13 @@ fun InboxTabs(
read = !pm.person_mention.read,
auth = acct.jwt,
),
)
siteViewModel.fetchUnreadCounts(
GetUnreadCount(
auth = acct.jwt,
),
onSuccess = {
siteViewModel.fetchUnreadCounts(
GetUnreadCount(
auth = acct.jwt,
),
)
},
)
}
},
Expand Down Expand Up @@ -658,11 +673,13 @@ fun InboxTabs(
read = !pm.private_message.read,
auth = acct.jwt,
),
)
siteViewModel.fetchUnreadCounts(
GetUnreadCount(
auth = acct.jwt,
),
onSuccess = {
siteViewModel.fetchUnreadCounts(
GetUnreadCount(
auth = acct.jwt,
),
)
},
)
},
onPersonClick = appState::toProfile,
Expand Down

0 comments on commit 455e89a

Please sign in to comment.