mirror of
https://github.com/pawelorzech/SunZones.git
synced 2026-01-30 04:04:26 +00:00
Fix sun data refresh on app resume and periodic refresh
The periodic refresh was broken because collectLatest on a Room Flow suspends indefinitely, preventing the while loop from advancing. Replace with Flow.first() for one-shot snapshots. Add refresh() method triggered via LifecycleEventEffect(ON_RESUME) so sun times recalculate every time the app comes to foreground. https://claude.ai/code/session_01QD2KVpt2xSRtRALne48g3n
This commit is contained in:
parent
d61cbd28a3
commit
07808be975
2 changed files with 19 additions and 3 deletions
|
|
@ -20,6 +20,8 @@ import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.lifecycle.Lifecycle
|
||||||
|
import androidx.lifecycle.compose.LifecycleEventEffect
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Brush
|
import androidx.compose.ui.graphics.Brush
|
||||||
|
|
@ -43,6 +45,10 @@ fun MainScreen(
|
||||||
val uiState by viewModel.uiState.collectAsState()
|
val uiState by viewModel.uiState.collectAsState()
|
||||||
var showAddSheet by remember { mutableStateOf(false) }
|
var showAddSheet by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
|
||||||
|
viewModel.refresh()
|
||||||
|
}
|
||||||
|
|
||||||
Box(modifier = Modifier.fillMaxSize()) {
|
Box(modifier = Modifier.fillMaxSize()) {
|
||||||
if (uiState.locations.isEmpty() && !uiState.isLoading) {
|
if (uiState.locations.isEmpty() && !uiState.isLoading) {
|
||||||
// Empty state
|
// Empty state
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.isActive
|
import kotlinx.coroutines.isActive
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
@ -75,12 +76,21 @@ class MainViewModel @Inject constructor(
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
delay(60_000L) // refresh every minute for sun progress updates
|
delay(60_000L) // refresh every minute for sun progress updates
|
||||||
getLocationsUseCase().collectLatest { locations ->
|
refreshNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun refresh() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
refreshNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun refreshNow() {
|
||||||
|
val locations = getLocationsUseCase().first()
|
||||||
_uiState.value = _uiState.value.copy(locations = locations)
|
_uiState.value = _uiState.value.copy(locations = locations)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun deleteLocation(id: Long) {
|
fun deleteLocation(id: Long) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue