Skip to content

Commit

Permalink
locker ui reload button, more logging on error
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Sep 24, 2024
1 parent 86792d6 commit 8e592fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,29 @@ fun Locker(page: LockerTabs, lockerDao: LockerDao = getKoin().get(), viewModel:
}
}

if (entriesState is LockerViewModel.LockerEntriesState.Loaded) {
when (page) {
LockerTabs.Apps -> {
LockerAppList(viewModel, onOpenModalSheet = { viewModel.openModalSheet(it) })
}
when (entriesState) {
is LockerViewModel.LockerEntriesState.Loaded -> {
when (page) {
LockerTabs.Apps -> {
LockerAppList(viewModel, onOpenModalSheet = { viewModel.openModalSheet(it) })
}

LockerTabs.Watchfaces -> {
LockerWatchfaceList(viewModel, onOpenModalSheet = { viewModel.openModalSheet(it) })
LockerTabs.Watchfaces -> {
LockerWatchfaceList(viewModel, onOpenModalSheet = { viewModel.openModalSheet(it) })
}
}
}
is LockerViewModel.LockerEntriesState.Error -> {
Text("Error loading locker entries")
Button(onClick = {
viewModel.reloadLocker()
}) {
Text("Retry")
}
}
} else {
CircularProgressIndicator(modifier = Modifier.align(CenterHorizontally))
is LockerViewModel.LockerEntriesState.Loading -> {
CircularProgressIndicator(modifier = Modifier.align(CenterHorizontally))
}
}
}
if (modalSheetState is LockerViewModel.ModalSheetState.Open) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.rebble.cobble.shared.ui.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import io.rebble.cobble.shared.Logging
import io.rebble.cobble.shared.database.dao.LockerDao
import io.rebble.cobble.shared.database.entity.SyncedLockerEntryWithPlatforms
import io.rebble.cobble.shared.domain.state.ConnectionStateManager
Expand All @@ -13,14 +14,20 @@ import kotlinx.coroutines.sync.withLock
class LockerViewModel(private val lockerDao: LockerDao): ViewModel() {
open class LockerEntriesState {
object Loading : LockerEntriesState()
object Error : LockerEntriesState()
data class Loaded(val entries: List<SyncedLockerEntryWithPlatforms>) : LockerEntriesState()
}
open class ModalSheetState {
object Closed : ModalSheetState()
data class Open(val viewModel: LockerItemViewModel) : ModalSheetState()
}
val entriesState = lockerDao.getAllEntriesFlow().map {
LockerEntriesState.Loaded(it)
private val entriesFlow = lockerDao.getAllEntriesFlow()
private val reloadFlow = MutableStateFlow(Unit)
val entriesState: StateFlow<LockerEntriesState> = combine(entriesFlow, reloadFlow) { entries, _ ->
LockerEntriesState.Loaded(entries) as LockerEntriesState
}.catch {
Logging.e("Error loading locker entries", it)
emit(LockerEntriesState.Error)
}.stateIn(viewModelScope + Dispatchers.IO, SharingStarted.Eagerly, LockerEntriesState.Loading)

private var mutex = Mutex()
Expand Down Expand Up @@ -49,4 +56,8 @@ class LockerViewModel(private val lockerDao: LockerDao): ViewModel() {
fun closeModalSheet() {
_modalSheetState.value = ModalSheetState.Closed
}

fun reloadLocker() {
reloadFlow.value = Unit
}
}

0 comments on commit 8e592fa

Please sign in to comment.