Skip to content

Commit

Permalink
- Fix duplicate items in recent list
Browse files Browse the repository at this point in the history
Ensure only distinct items are displayed in the recent list by filtering the source list based on URL.
Move network observation to
 the ViewModel and trigger a reset if the source list is empty, a source is selected, the device is online, and the count is not 1.
  • Loading branch information
jacobrein committed Sep 12, 2024
1 parent 7f524a6 commit a7cbd5b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ fun RecentView(

val isConnected by recentVm.observeNetwork.collectAsState(initial = true)

LaunchedEffect(isConnected) {
if (recentVm.sourceList.isEmpty() && source != null && isConnected && recentVm.count != 1) recentVm.reset()
}

val showButton by remember { derivedStateOf { state.firstVisibleItemIndex > 0 } }

val sourceList = recentVm.sources
Expand Down Expand Up @@ -221,11 +217,11 @@ fun RecentView(
when {
sourceList.isEmpty() -> NoSourcesInstalled(Modifier.fillMaxSize())

recentVm.sourceList.isEmpty() -> Box(Modifier.fillMaxSize()) { info.ComposeShimmerItem() }
recentVm.filteredSourceList.isEmpty() -> Box(Modifier.fillMaxSize()) { info.ComposeShimmerItem() }

else -> {
info.ItemListView(
list = recentVm.sourceList,
list = recentVm.filteredSourceList,
listState = state,
favorites = recentVm.favoriteList,
paddingValues = p,
Expand All @@ -239,7 +235,7 @@ fun RecentView(
}
}

if (source?.canScroll == true && recentVm.sourceList.isNotEmpty()) {
if (source?.canScroll == true && recentVm.filteredSourceList.isNotEmpty()) {
InfiniteListHandler(listState = state, buffer = info.scrollBuffer) {
recentVm.loadMore()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.programmersbox.uiviews.recent

import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.util.fastMaxBy
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
Expand Down Expand Up @@ -41,8 +41,10 @@ class RecentViewModel(
) : ViewModel(), ToastItems by DefaultToastItems() {

var isRefreshing by mutableStateOf(false)
val sourceList = mutableStateListOf<ItemModel>()
var favoriteList = mutableStateListOf<DbModel>()
private val sourceList = mutableStateListOf<ItemModel>()
val favoriteList = mutableStateListOf<DbModel>()

val filteredSourceList by derivedStateOf { sourceList.distinctBy { it.url } }

val observeNetwork = ReactiveNetwork()
.observeInternetConnectivity()
Expand Down Expand Up @@ -70,7 +72,10 @@ class RecentViewModel(
itemListener.getAllShowsFlow(),
dao.getAllFavorites()
) { f, d -> (f + d).groupBy(DbModel::url).map { it.value.fastMaxBy(DbModel::numChapters)!! } }
.onEach { favoriteList = it.toMutableStateList() }
.onEach {
favoriteList.clear()
favoriteList.addAll(it)
}
.launchIn(viewModelScope)

currentSourceRepository.asFlow()
Expand All @@ -88,6 +93,10 @@ class RecentViewModel(
.distinctUntilChanged()
.onEach { gridState.scrollToItem(0) }
.launchIn(viewModelScope)

observeNetwork
.onEach { if (sourceList.isEmpty() && currentSource != null && it && count != 1) reset() }
.launchIn(viewModelScope)
}

fun reset() {
Expand Down

0 comments on commit a7cbd5b

Please sign in to comment.