Skip to content

Commit

Permalink
Enable filtering apps only when data is loaded, unifying handling of …
Browse files Browse the repository at this point in the history
…loading components.
  • Loading branch information
carlo-colombo committed Feb 3, 2025
1 parent 5b8e958 commit 724a903
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ fun Calendar(
@PreviewParameter(SampleEventsProvider::class) calendarUIState: CalendarUIState,
onEventClick: (Event) -> Unit = {}
) {
if (calendarUIState.loading) {
Loading(modifier)
} else {
Loading(modifier, loading = calendarUIState.loading) {
Box(modifier = modifier, contentAlignment = Alignment.Center) {
if (calendarUIState.showCalendar) {
LazyColumn(
Expand All @@ -49,7 +47,7 @@ fun Calendar(

@Preview
@Composable
fun CalendarPreview(){
fun CalendarPreview() {
Calendar(calendarUIState = SampleEventsProvider().values.first())
}

Expand Down
26 changes: 16 additions & 10 deletions app/src/main/java/ovh/litapp/neurhome3/ui/components/Loading.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
fun Loading(modifier: Modifier = Modifier) {
Box(
modifier = modifier
) {
CircularProgressIndicator(
modifier = modifier,
color = MaterialTheme.colorScheme.secondary,
trackColor = MaterialTheme.colorScheme.surfaceVariant,
)
}
fun Loading(
modifier: Modifier = Modifier,
loading: Boolean = true,
content: @Composable () -> Unit
) {
if (loading) {
Box(
modifier = modifier
) {
CircularProgressIndicator(
modifier = modifier,
color = MaterialTheme.colorScheme.secondary,
trackColor = MaterialTheme.colorScheme.surfaceVariant,
)
}
} else (content())
}
19 changes: 11 additions & 8 deletions app/src/main/java/ovh/litapp/neurhome3/ui/home/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ fun Home(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.weight(6f, true)
) {
if (homeUIState.topUIState.loading) {
Loading(Modifier.weight(6f))
} else {
Loading(Modifier.weight(6f), homeUIState.topUIState.loading) {
Box(
modifier = Modifier
.weight(6f, true)
Expand All @@ -124,12 +122,17 @@ fun Home(
)
}
}
Box(modifier = Modifier.weight(3f, true)) {
Keyboard(appsViewModel = viewModel, appsUiState = homeUIState.filteredUiState)

Loading(Modifier.weight(1f, true), homeUIState.filteredUiState.loading) {
Box(modifier = Modifier.weight(3f, true)) {
Keyboard(
appsViewModel = viewModel,
appsUiState = homeUIState.filteredUiState
)
}
}
if (homeUIState.favouriteUIState.loading) {
Loading(Modifier.weight(1f, true))
} else {

Loading(Modifier.weight(1f, true), homeUIState.favouriteUIState.loading) {
Box(modifier = Modifier.weight(1f, true)) {
BottomBar(homeUIState.favouriteUIState.apps, viewModel, navController)
}
Expand Down
52 changes: 18 additions & 34 deletions app/src/main/java/ovh/litapp/neurhome3/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,9 @@ class HomeViewModel(
}, RegexOption.IGNORE_CASE
)

allApps
.filter {
it.visibility != ApplicationVisibility.HIDDEN_FROM_FILTERED
&& (filter matches it.label
|| filter matches it.alias)
}
.sortedBy { -it.score }
.take(6)
.reversed()
allApps.filter {
it.visibility != ApplicationVisibility.HIDDEN_FROM_FILTERED && (filter matches it.label || filter matches it.alias)
}.sortedBy { -it.score }.take(6).reversed()
} else {
listOf()
}
Expand All @@ -114,32 +108,25 @@ class HomeViewModel(
)

private val favouriteUIState: StateFlow<FavouriteUIState> =
favouritesRepository
.favouriteApps
.map { FavouriteUIState(it, false) }
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = FavouriteUIState()
)
favouritesRepository.favouriteApps.map { FavouriteUIState(it, false) }.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = FavouriteUIState()
)

private val topUIState: StateFlow<TopUIState> = neurhomeRepository.getTopApps(6)
.map { TopUIState(it, false) }
.stateIn(
private val topUIState: StateFlow<TopUIState> =
neurhomeRepository.getTopApps(6).map { TopUIState(it, false) }.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = TopUIState()
)

private val alarmUIState: StateFlow<AlarmUIState> =
clockAlarmRepository
.alarm
.map { AlarmUIState(it, false) }
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = AlarmUIState()
)
clockAlarmRepository.alarm.map { AlarmUIState(it, false) }.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = AlarmUIState()
)

val homeUIState: StateFlow<HomeUIState> = combine(
favouriteUIState,
Expand Down Expand Up @@ -203,18 +190,15 @@ data class FilteredUIState(
)

data class FavouriteUIState(
val apps: Map<Int, Application> = mapOf(),
val loading: Boolean = true
val apps: Map<Int, Application> = mapOf(), val loading: Boolean = true
)

class TopUIState(
val apps: List<Application> = listOf(),
val loading: Boolean = true
val apps: List<Application> = listOf(), val loading: Boolean = true
)

data class AlarmUIState(
val next: AlarmManager.AlarmClockInfo? = null,
val loading: Boolean = true
val next: AlarmManager.AlarmClockInfo? = null, val loading: Boolean = true
)

data class HomeUIState(
Expand Down

0 comments on commit 724a903

Please sign in to comment.