Skip to content

Commit 3b13dbf

Browse files
committed
feat: refresh cache size on profile screen resume
- Implement a lifecycle observer in `ProfileRoot` to trigger a cache size refresh whenever the profile screen enters the `ON_RESUME` state. - Rename `observeCacheSize` to `refreshCacheSize` in `ProfileViewModel` and update it to be manually triggerable via a new `ProfileAction`. - Add `OnRefreshCacheSize` action to `ProfileAction` to support explicit UI-driven refreshes. - Update the cache clearing logic to call `refreshCacheSize` immediately after a successful deletion. - Ensure cache size is refreshed during the initial state initialization of the `ProfileViewModel`.
1 parent 44044aa commit 3b13dbf

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

feature/profile/presentation/src/commonMain/kotlin/zed/rainxch/profile/presentation/ProfileAction.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ sealed interface ProfileAction {
3434

3535
data object OnLoginClick : ProfileAction
3636

37+
data object OnRefreshCacheSize : ProfileAction
38+
3739
data object OnClearCacheClick : ProfileAction
3840

3941
data object OnClearDownloadsConfirm : ProfileAction

feature/profile/presentation/src/commonMain/kotlin/zed/rainxch/profile/presentation/ProfileRoot.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import androidx.compose.material3.SnackbarHostState
1414
import androidx.compose.material3.Text
1515
import androidx.compose.material3.TopAppBar
1616
import androidx.compose.runtime.Composable
17+
import androidx.compose.runtime.DisposableEffect
1718
import androidx.compose.runtime.getValue
1819
import androidx.compose.runtime.remember
1920
import androidx.compose.runtime.rememberCoroutineScope
2021
import androidx.compose.ui.Modifier
2122
import androidx.compose.ui.text.font.FontWeight
2223
import androidx.compose.ui.tooling.preview.Preview
2324
import androidx.compose.ui.unit.dp
25+
import androidx.lifecycle.compose.LocalLifecycleOwner
2426
import androidx.lifecycle.compose.collectAsStateWithLifecycle
2527
import io.github.fletchmckee.liquid.liquefiable
2628
import kotlinx.coroutines.launch
@@ -54,6 +56,20 @@ fun ProfileRoot(
5456
val snackbarState = remember { SnackbarHostState() }
5557
val coroutineScope = rememberCoroutineScope()
5658

59+
val lifecycleOwner = LocalLifecycleOwner.current
60+
DisposableEffect(lifecycleOwner) {
61+
val observer =
62+
androidx.lifecycle.LifecycleEventObserver { _, event ->
63+
if (event == androidx.lifecycle.Lifecycle.Event.ON_RESUME) {
64+
viewModel.onAction(ProfileAction.OnRefreshCacheSize)
65+
}
66+
}
67+
lifecycleOwner.lifecycle.addObserver(observer)
68+
onDispose {
69+
lifecycleOwner.lifecycle.removeObserver(observer)
70+
}
71+
}
72+
5773
ObserveAsEvents(viewModel.events) { event ->
5874
when (event) {
5975
ProfileEvent.OnLogoutSuccessful -> {

feature/profile/presentation/src/commonMain/kotlin/zed/rainxch/profile/presentation/ProfileViewModel.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ class ProfileViewModel(
5757
loadScrollbarEnabled()
5858

5959
observeLoggedInStatus()
60-
61-
observeCacheSize()
6260
observeShizukuStatus()
6361

6462
hasLoadedInitialData = true
6563
}
64+
refreshCacheSize()
6665
}.stateIn(
6766
scope = viewModelScope,
6867
started = SharingStarted.WhileSubscribed(5_000L),
@@ -72,7 +71,7 @@ class ProfileViewModel(
7271
private val _events = Channel<ProfileEvent>()
7372
val events = _events.receiveAsFlow()
7473

75-
private fun observeCacheSize() {
74+
private fun refreshCacheSize() {
7675
viewModelScope.launch {
7776
profileRepository.observeCacheSize().collect { sizeBytes ->
7877
_state.update {
@@ -299,6 +298,10 @@ class ProfileViewModel(
299298
)
300299
}
301300

301+
ProfileAction.OnRefreshCacheSize -> {
302+
refreshCacheSize()
303+
}
304+
302305
ProfileAction.OnClearCacheClick -> {
303306
_state.update { it.copy(isClearDownloadsDialogVisible = true) }
304307
}
@@ -309,7 +312,7 @@ class ProfileViewModel(
309312
runCatching {
310313
profileRepository.clearCache()
311314
}.onSuccess {
312-
observeCacheSize()
315+
refreshCacheSize()
313316
_events.send(ProfileEvent.OnCacheCleared)
314317
}.onFailure { error ->
315318
_events.send(

0 commit comments

Comments
 (0)