From 173eedf8296542d7e3a0dda05a75bb8b9f458f0e Mon Sep 17 00:00:00 2001 From: berabros <124783808+beradeep@users.noreply.github.com> Date: Fri, 30 Aug 2024 16:09:39 +0530 Subject: [PATCH] update state handling mech (#22) * edit ui state production and consumption mechanism * remove unused imports * fix lint errors --- .../bera/whitehole/ui/main/nav/AppNavHost.kt | 13 ++++-------- .../com/bera/whitehole/ui/main/nav/Screens.kt | 8 ++++---- .../ui/main/screens/local/LocalViewModel.kt | 10 ++++++---- .../ui/main/screens/remote/RemoteViewModel.kt | 20 +++++++++++-------- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/bera/whitehole/ui/main/nav/AppNavHost.kt b/app/src/main/java/com/bera/whitehole/ui/main/nav/AppNavHost.kt index 83176f9..ef6dc6d 100644 --- a/app/src/main/java/com/bera/whitehole/ui/main/nav/AppNavHost.kt +++ b/app/src/main/java/com/bera/whitehole/ui/main/nav/AppNavHost.kt @@ -35,7 +35,7 @@ fun AppNavHost( ) { val viewModel: LocalViewModel = screenScopedViewModel() val localPhotos = viewModel.localPhotosFlow.collectAsLazyPagingItems() - val localPhotosCount by viewModel.localPhotosCount.collectAsStateWithLifecycle(0) + val localPhotosCount by viewModel.localPhotosCount.collectAsStateWithLifecycle() LocalPhotoGrid(localPhotos = localPhotos, totalCount = localPhotosCount) } composable( @@ -43,14 +43,9 @@ fun AppNavHost( ) { val viewModel: RemoteViewModel = screenScopedViewModel() val remotePhotosOnDevice = viewModel.remotePhotosOnDeviceFlow.collectAsLazyPagingItems() - val remotePhotosNotOnDevice = - viewModel.remotePhotosNotOnDeviceFlow.collectAsLazyPagingItems() - val remotePhotosOnDeviceCount by viewModel.remotePhotosOnDeviceCount.collectAsStateWithLifecycle( - 0 - ) - val remotePhotosNotOnDeviceCount by viewModel.remotePhotosNotOnDeviceCount.collectAsStateWithLifecycle( - 0 - ) + val remotePhotosNotOnDevice = viewModel.remotePhotosNotOnDeviceFlow.collectAsLazyPagingItems() + val remotePhotosOnDeviceCount by viewModel.remotePhotosOnDeviceCount.collectAsStateWithLifecycle() + val remotePhotosNotOnDeviceCount by viewModel.remotePhotosNotOnDeviceCount.collectAsStateWithLifecycle() RemotePhotoGrid( remotePhotosOnDevice, remotePhotosNotOnDevice, diff --git a/app/src/main/java/com/bera/whitehole/ui/main/nav/Screens.kt b/app/src/main/java/com/bera/whitehole/ui/main/nav/Screens.kt index 1e7a8be..5d2ba9a 100644 --- a/app/src/main/java/com/bera/whitehole/ui/main/nav/Screens.kt +++ b/app/src/main/java/com/bera/whitehole/ui/main/nav/Screens.kt @@ -8,16 +8,16 @@ import androidx.compose.material.icons.filled.Smartphone import androidx.compose.ui.graphics.vector.ImageVector sealed class Screens(val displayTitle: String, val route: String, val icon: ImageVector? = null) { - object RemotePhotos : + data object RemotePhotos : Screens(displayTitle = "Cloud", route = "cloud", icon = Icons.Default.Cloud) - object LocalPhotos : + data object LocalPhotos : Screens(displayTitle = "Device", route = "device", icon = Icons.Default.Smartphone) - object Settings : + data object Settings : Screens(displayTitle = "Settings", route = "settings", icon = Icons.Default.Settings) - object About : Screens(displayTitle = "About", route = "about", icon = Icons.Default.Info) + data object About : Screens(displayTitle = "About", route = "about", icon = Icons.Default.Info) companion object { val drawerScreens by lazy { listOf(LocalPhotos, RemotePhotos, Settings, About) } diff --git a/app/src/main/java/com/bera/whitehole/ui/main/screens/local/LocalViewModel.kt b/app/src/main/java/com/bera/whitehole/ui/main/screens/local/LocalViewModel.kt index 699d100..61810da 100644 --- a/app/src/main/java/com/bera/whitehole/ui/main/screens/local/LocalViewModel.kt +++ b/app/src/main/java/com/bera/whitehole/ui/main/screens/local/LocalViewModel.kt @@ -13,19 +13,21 @@ import com.bera.whitehole.data.localdb.entities.Photo import com.bera.whitehole.workers.WorkModule import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch class LocalViewModel : ViewModel() { - val localPhotosFlow: Flow> by lazy { + val localPhotosFlow: Flow> = Pager( config = PagingConfig(pageSize = PAGE_SIZE, jumpThreshold = JUMP_THRESHOLD), pagingSourceFactory = { DbHolder.database.photoDao().getAllPaging() } ).flow.cachedIn(viewModelScope) - } - val localPhotosCount: Flow by lazy { + val localPhotosCount: StateFlow = DbHolder.database.photoDao().getAllCountFlow() - } + .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), 0) fun uploadMultiplePhotos(uris: List) { viewModelScope.launch(Dispatchers.IO) { diff --git a/app/src/main/java/com/bera/whitehole/ui/main/screens/remote/RemoteViewModel.kt b/app/src/main/java/com/bera/whitehole/ui/main/screens/remote/RemoteViewModel.kt index efd4003..8f48d3a 100644 --- a/app/src/main/java/com/bera/whitehole/ui/main/screens/remote/RemoteViewModel.kt +++ b/app/src/main/java/com/bera/whitehole/ui/main/screens/remote/RemoteViewModel.kt @@ -10,26 +10,30 @@ import com.bera.whitehole.data.localdb.DbHolder import com.bera.whitehole.data.localdb.entities.Photo import com.bera.whitehole.data.localdb.entities.RemotePhoto import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.stateIn class RemoteViewModel : ViewModel() { - val remotePhotosOnDeviceFlow: Flow> by lazy { + val remotePhotosOnDeviceFlow: Flow> = Pager( config = PagingConfig(pageSize = PAGE_SIZE, jumpThreshold = JUMP_THRESHOLD), pagingSourceFactory = { DbHolder.database.photoDao().getAllUploadedPaging() } ).flow.cachedIn(viewModelScope) - } - val remotePhotosNotOnDeviceFlow: Flow> by lazy { + + val remotePhotosNotOnDeviceFlow: Flow> = Pager( config = PagingConfig(pageSize = PAGE_SIZE, jumpThreshold = JUMP_THRESHOLD), pagingSourceFactory = { DbHolder.database.remotePhotoDao().getNotOnDevicePaging() } ).flow.cachedIn(viewModelScope) - } - val remotePhotosOnDeviceCount: Flow by lazy { + + val remotePhotosOnDeviceCount: StateFlow = DbHolder.database.photoDao().getAllUploadedCountFlow() - } - val remotePhotosNotOnDeviceCount: Flow by lazy { + .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), 0) + + val remotePhotosNotOnDeviceCount: StateFlow = DbHolder.database.remotePhotoDao().getNotOnDeviceCountFlow() - } + .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), 0) companion object { const val PAGE_SIZE = 32