Skip to content

Commit

Permalink
Fix some issues with the unread counts not being accurate right after…
Browse files Browse the repository at this point in the history
… login, and not updating after logging out. (#537)
  • Loading branch information
camporter authored Jun 12, 2023
1 parent cc75c10 commit 1bcea4f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ fun fetchInitialData(
changeListingType = ListingType.Local,
changeSortType = SortType.Active,
)
homeViewModel.fetchUnreadCounts()
}

siteViewModel.fetchSite(
Expand Down
42 changes: 23 additions & 19 deletions app/src/main/java/com/jerboa/ui/components/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,32 @@ class HomeViewModel : ViewModel() {
}

fun fetchUnreadCounts(
account: Account,
account: Account? = null,
ctx: Context? = null,
) {
viewModelScope.launch {
try {
val api = API.getInstance()
val form = GetUnreadCount(
auth = account.jwt,
)
Log.d(
"jerboa",
"Fetching unread counts: $form",
)
unreadCountResponse = retrofitErrorHandler(
api.getUnreadCount(
form = form
.serializeToMap(),
),
)
} catch (e: Exception) {
toastException(ctx = ctx, error = e)
account?.let {
viewModelScope.launch {
try {
val api = API.getInstance()
val form = GetUnreadCount(
auth = account.jwt,
)
Log.d(
"jerboa",
"Fetching unread counts: $form",
)
unreadCountResponse = retrofitErrorHandler(
api.getUnreadCount(
form = form
.serializeToMap(),
),
)
} catch (e: Exception) {
toastException(ctx = ctx, error = e)
}
}
} ?: run {
unreadCountResponse = GetUnreadCountResponse(0, 0, 0)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.jerboa.datatypes.SortType
import com.jerboa.datatypes.api.Login
import com.jerboa.db.Account
import com.jerboa.db.AccountViewModel
import com.jerboa.fetchInitialData
import com.jerboa.ui.components.home.HomeViewModel
import com.jerboa.ui.components.home.SiteViewModel
import kotlinx.coroutines.cancel
Expand Down Expand Up @@ -117,6 +118,12 @@ class LoginViewModel : ViewModel() {
// Save that info in the DB
accountViewModel.insert(account)

fetchInitialData(
account = account,
siteViewModel = siteViewModel,
homeViewModel = homeViewModel,
)

loading = false

navController.navigate(route = "home")
Expand Down

0 comments on commit 1bcea4f

Please sign in to comment.