Merge pull request #2 from pawelorzech/claude/add-app-refresh-startup-wczRE

Fix sun data refresh on app resume and periodic refresh
This commit is contained in:
Paweł Orzech 2026-01-27 22:23:06 +01:00 committed by GitHub
commit 117a16d571
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 3 deletions

View file

@ -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

View file

@ -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,13 +76,22 @@ 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()
_uiState.value = _uiState.value.copy(locations = locations)
}
} }
} }
} }
fun refresh() {
viewModelScope.launch {
refreshNow()
}
}
private suspend fun refreshNow() {
val locations = getLocationsUseCase().first()
_uiState.value = _uiState.value.copy(locations = locations)
}
fun deleteLocation(id: Long) { fun deleteLocation(id: Long) {
viewModelScope.launch { viewModelScope.launch {
repository.deleteLocation(id) repository.deleteLocation(id)