Delay load on Wear, used cache data from DB.

This commit is contained in:
Yuri Schimke 2021-10-08 22:31:18 +01:00 committed by John O'Reilly
parent a849ac126d
commit 5aaeea701c
4 changed files with 29 additions and 15 deletions

View file

@ -10,9 +10,8 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
class PeopleInSpaceViewModel(
private val peopleInSpaceRepository: PeopleInSpaceRepositoryInterface
val peopleInSpaceRepository: PeopleInSpaceRepositoryInterface
) : ViewModel() {
val peopleInSpace = peopleInSpaceRepository.fetchPeopleAsFlow()
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

View file

@ -9,6 +9,7 @@ import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@ -30,6 +31,10 @@ fun PersonListScreen(paddingValues: PaddingValues = PaddingValues(),
) {
val peopleState = peopleInSpaceViewModel.peopleInSpace.collectAsState()
LaunchedEffect(true) {
peopleInSpaceViewModel.peopleInSpaceRepository.fetchAndStorePeople()
}
Scaffold(
topBar = {
TopAppBar(title = { Text("People In Space") })
@ -46,9 +51,10 @@ fun PersonListScreen(paddingValues: PaddingValues = PaddingValues(),
@Composable
fun PersonView(person: Assignment, personSelected: (person: Assignment) -> Unit) {
Row(modifier = Modifier.fillMaxWidth()
.clickable(onClick = { personSelected(person) })
.padding(16.dp),
Row(modifier = Modifier
.fillMaxWidth()
.clickable(onClick = { personSelected(person) })
.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {

View file

@ -18,6 +18,7 @@ interface PeopleInSpaceRepositoryInterface {
fun fetchPeopleAsFlow(): Flow<List<Assignment>>
fun pollISSPosition(): Flow<IssPosition>
suspend fun fetchPeople(): List<Assignment>
suspend fun fetchAndStorePeople()
}
class PeopleInSpaceRepository : KoinComponent, PeopleInSpaceRepositoryInterface {
@ -30,11 +31,11 @@ class PeopleInSpaceRepository : KoinComponent, PeopleInSpaceRepositoryInterface
var peopleJob: Job? = null
init {
coroutineScope.launch {
fetchAndStorePeople()
}
}
// init {
// coroutineScope.launch {
// fetchAndStorePeople()
// }
// }
override fun fetchPeopleAsFlow(): Flow<List<Assignment>> {
// the main reason we need to do this check is that sqldelight isn't currently
@ -46,7 +47,7 @@ class PeopleInSpaceRepository : KoinComponent, PeopleInSpaceRepositoryInterface
)?.asFlow()?.mapToList() ?: flowOf(emptyList<Assignment>())
}
private suspend fun fetchAndStorePeople() {
override suspend fun fetchAndStorePeople() {
logger.d { "fetchAndStorePeople" }
val result = peopleInSpaceApi.fetchPeople()
@ -62,7 +63,7 @@ class PeopleInSpaceRepository : KoinComponent, PeopleInSpaceRepositoryInterface
}
// Used by web client atm
override suspend fun fetchPeople() = peopleInSpaceApi.fetchPeople().people
override suspend fun fetchPeople(): List<Assignment> = peopleInSpaceApi.fetchPeople().people
// called from Kotlin/Native clients
fun startObservingPeopleUpdates(success: (List<Assignment>) -> Unit) {
@ -72,6 +73,9 @@ class PeopleInSpaceRepository : KoinComponent, PeopleInSpaceRepositoryInterface
success(it)
}
}
coroutineScope.launch {
fetchAndStorePeople()
}
}
fun stopObservingPeopleUpdates() {

View file

@ -10,6 +10,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@ -35,10 +36,12 @@ fun PersonList(
peopleInSpaceRepository: PeopleInSpaceRepositoryInterface,
personSelected: (person: Assignment) -> Unit
) {
var peopleState by remember { mutableStateOf(emptyList<Assignment>()) }
val peopleState by peopleInSpaceRepository
.fetchPeopleAsFlow()
.collectAsState(initial = listOf())
LaunchedEffect(true) {
peopleState = peopleInSpaceRepository.fetchPeople()
peopleInSpaceRepository.fetchAndStorePeople()
}
val paddingHeight = if (LocalConfiguration.current.isScreenRound) 50.dp else 8.dp
@ -64,7 +67,9 @@ fun PersonView(person: Assignment, personSelected: (person: Assignment) -> Unit)
if (personImageUrl.isNotEmpty()) {
Image(
painter = rememberImagePainter(personImageUrl),
modifier = Modifier.size(50.dp).clip(MaterialTheme.shapes.medium),
modifier = Modifier
.size(50.dp)
.clip(MaterialTheme.shapes.medium),
contentDescription = person.name
)
} else {