Skip to content

Commit

Permalink
update state handling mech (#22)
Browse files Browse the repository at this point in the history
* edit ui state production and consumption mechanism

* remove unused imports

* fix lint errors
  • Loading branch information
beradeep authored Aug 30, 2024
1 parent 7ce7cec commit 173eedf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
13 changes: 4 additions & 9 deletions app/src/main/java/com/bera/whitehole/ui/main/nav/AppNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,17 @@ 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(
route = Screens.RemotePhotos.route
) {
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,
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/bera/whitehole/ui/main/nav/Screens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PagingData<Photo>> by lazy {
val localPhotosFlow: Flow<PagingData<Photo>> =
Pager(
config = PagingConfig(pageSize = PAGE_SIZE, jumpThreshold = JUMP_THRESHOLD),
pagingSourceFactory = { DbHolder.database.photoDao().getAllPaging() }
).flow.cachedIn(viewModelScope)
}

val localPhotosCount: Flow<Int> by lazy {
val localPhotosCount: StateFlow<Int> =
DbHolder.database.photoDao().getAllCountFlow()
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), 0)

fun uploadMultiplePhotos(uris: List<Uri>) {
viewModelScope.launch(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PagingData<Photo>> by lazy {
val remotePhotosOnDeviceFlow: Flow<PagingData<Photo>> =
Pager(
config = PagingConfig(pageSize = PAGE_SIZE, jumpThreshold = JUMP_THRESHOLD),
pagingSourceFactory = { DbHolder.database.photoDao().getAllUploadedPaging() }
).flow.cachedIn(viewModelScope)
}
val remotePhotosNotOnDeviceFlow: Flow<PagingData<RemotePhoto>> by lazy {

val remotePhotosNotOnDeviceFlow: Flow<PagingData<RemotePhoto>> =
Pager(
config = PagingConfig(pageSize = PAGE_SIZE, jumpThreshold = JUMP_THRESHOLD),
pagingSourceFactory = { DbHolder.database.remotePhotoDao().getNotOnDevicePaging() }
).flow.cachedIn(viewModelScope)
}
val remotePhotosOnDeviceCount: Flow<Int> by lazy {

val remotePhotosOnDeviceCount: StateFlow<Int> =
DbHolder.database.photoDao().getAllUploadedCountFlow()
}
val remotePhotosNotOnDeviceCount: Flow<Int> by lazy {
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), 0)

val remotePhotosNotOnDeviceCount: StateFlow<Int> =
DbHolder.database.remotePhotoDao().getNotOnDeviceCountFlow()
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), 0)

companion object {
const val PAGE_SIZE = 32
Expand Down

0 comments on commit 173eedf

Please sign in to comment.