From fad7292b3429f57d49396d538e55c000fe84a54a Mon Sep 17 00:00:00 2001 From: Acclorite <140836141+Acclorite@users.noreply.github.com> Date: Sat, 28 Dec 2024 13:25:33 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20Better=20refreshList=20?= =?UTF-8?q?delays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed visual issue with EmptyPlaceholder being shown when it should not be --- .../book_story/ui/book_info/BookInfoModel.kt | 28 +++++++------- .../book_story/ui/browse/BrowseModel.kt | 7 +--- .../book_story/ui/history/HistoryModel.kt | 38 ++++++++++--------- .../book_story/ui/history/HistoryScreen.kt | 2 +- .../book_story/ui/library/LibraryModel.kt | 12 +++--- .../book_story/ui/library/LibraryScreen.kt | 2 +- .../book_story/ui/reader/ReaderModel.kt | 16 ++++---- 7 files changed, 52 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/ua/acclorite/book_story/ui/book_info/BookInfoModel.kt b/app/src/main/java/ua/acclorite/book_story/ui/book_info/BookInfoModel.kt index d24d9fbb..7ef7a0d8 100644 --- a/app/src/main/java/ua/acclorite/book_story/ui/book_info/BookInfoModel.kt +++ b/app/src/main/java/ua/acclorite/book_story/ui/book_info/BookInfoModel.kt @@ -224,8 +224,8 @@ class BookInfoModel @Inject constructor( ) } - LibraryScreen.refreshListChannel.trySend(Unit) - HistoryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) + HistoryScreen.refreshListChannel.trySend(0) if (_state.value.editTitle) { onEvent(BookInfoEvent.OnEditTitleMode(false)) @@ -302,8 +302,8 @@ class BookInfoModel @Inject constructor( ) } - LibraryScreen.refreshListChannel.trySend(Unit) - HistoryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) + HistoryScreen.refreshListChannel.trySend(0) withContext(Dispatchers.Main) { event.context.getString(R.string.cover_image_changed) @@ -347,8 +347,8 @@ class BookInfoModel @Inject constructor( .showToast(context = event.context) } - LibraryScreen.refreshListChannel.trySend(Unit) - HistoryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) + HistoryScreen.refreshListChannel.trySend(0) } } @@ -372,8 +372,8 @@ class BookInfoModel @Inject constructor( ) } - LibraryScreen.refreshListChannel.trySend(Unit) - HistoryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) + HistoryScreen.refreshListChannel.trySend(0) withContext(Dispatchers.Main) { event.context.getString(R.string.cover_image_deleted) @@ -437,8 +437,8 @@ class BookInfoModel @Inject constructor( deleteBooks.execute(listOf(_state.value.book)) - LibraryScreen.refreshListChannel.trySend(Unit) - HistoryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) + HistoryScreen.refreshListChannel.trySend(0) BrowseScreen.refreshListChannel.trySend(Unit) withContext(Dispatchers.Main) { @@ -476,13 +476,13 @@ class BookInfoModel @Inject constructor( } updateBook.execute(_state.value.book) - LibraryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) LibraryScreen.scrollToPageCompositionChannel.trySend( Category.entries.dropLastWhile { it != event.category }.size - 1 ) - HistoryScreen.refreshListChannel.trySend(Unit) + HistoryScreen.refreshListChannel.trySend(0) withContext(Dispatchers.Main) { event.context.getString(R.string.book_moved) @@ -717,8 +717,8 @@ class BookInfoModel @Inject constructor( ) } - LibraryScreen.refreshListChannel.trySend(Unit) - HistoryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) + HistoryScreen.refreshListChannel.trySend(0) onEvent( BookInfoEvent.OnShowSnackbar( diff --git a/app/src/main/java/ua/acclorite/book_story/ui/browse/BrowseModel.kt b/app/src/main/java/ua/acclorite/book_story/ui/browse/BrowseModel.kt index c99d6b49..40bfb30d 100644 --- a/app/src/main/java/ua/acclorite/book_story/ui/browse/BrowseModel.kt +++ b/app/src/main/java/ua/acclorite/book_story/ui/browse/BrowseModel.kt @@ -12,13 +12,11 @@ import com.google.accompanist.permissions.isGranted import com.google.accompanist.permissions.shouldShowRationale import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.collectLatest -import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch @@ -41,7 +39,6 @@ import java.io.File import javax.inject.Inject import kotlin.collections.map -@OptIn(FlowPreview::class) @HiltViewModel class BrowseModel @Inject constructor( private val getFilesFromDevice: GetFilesFromDevice, @@ -60,7 +57,7 @@ class BrowseModel @Inject constructor( /* Observe channel - - - - - - - - - - - */ viewModelScope.launch(Dispatchers.IO) { - BrowseScreen.refreshListChannel.receiveAsFlow().debounce(200).collectLatest { + BrowseScreen.refreshListChannel.receiveAsFlow().collectLatest { onEvent(BrowseEvent.OnRefreshList(showIndicator = false, hideSearch = false)) } } @@ -532,7 +529,7 @@ class BrowseModel @Inject constructor( return@launch } - LibraryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) LibraryScreen.scrollToPageCompositionChannel.trySend(0) event.navigateToLibrary() diff --git a/app/src/main/java/ua/acclorite/book_story/ui/history/HistoryModel.kt b/app/src/main/java/ua/acclorite/book_story/ui/history/HistoryModel.kt index 6ce4cd4a..ceecf8e8 100644 --- a/app/src/main/java/ua/acclorite/book_story/ui/history/HistoryModel.kt +++ b/app/src/main/java/ua/acclorite/book_story/ui/history/HistoryModel.kt @@ -5,13 +5,11 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.collectLatest -import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch @@ -35,7 +33,6 @@ import javax.inject.Inject import kotlin.collections.component1 import kotlin.collections.component2 -@OptIn(FlowPreview::class) @HiltViewModel class HistoryModel @Inject constructor( private val getHistory: GetHistory, @@ -60,24 +57,29 @@ class HistoryModel @Inject constructor( /* Observe channel - - - - - - - - - - - */ viewModelScope.launch(Dispatchers.IO) { - HistoryScreen.refreshListChannel.receiveAsFlow().debounce(200).collectLatest { + HistoryScreen.refreshListChannel.receiveAsFlow().collectLatest { + delay(it) + yield() + onEvent(HistoryEvent.OnRefreshList(showIndicator = false, hideSearch = false)) } } viewModelScope.launch(Dispatchers.IO) { - HistoryScreen.insertHistoryChannel.receiveAsFlow().debounce(200) - .collectLatest { - insertHistory.execute( - History( - bookId = it, - book = null, - time = Date().time - ) + HistoryScreen.insertHistoryChannel.receiveAsFlow().collectLatest { + insertHistory.execute( + History( + bookId = it, + book = null, + time = Date().time ) + ) - getHistoryFromDatabase() - LibraryScreen.refreshListChannel.trySend(Unit) - } + delay(500) + yield() + + getHistoryFromDatabase() + LibraryScreen.refreshListChannel.trySend(0) + } } /* - - - - - - - - - - - - - - - - - - - */ } @@ -172,7 +174,7 @@ class HistoryModel @Inject constructor( getHistoryFromDatabase() _state.update { it.copy(isRefreshing = false) } - LibraryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) deleteHistoryEntry?.cancel() event.snackbarState.currentSnackbarData?.dismiss() @@ -195,7 +197,7 @@ class HistoryModel @Inject constructor( SnackbarResult.Dismissed -> Unit SnackbarResult.ActionPerformed -> { insertHistory.execute(event.history) - LibraryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) _state.update { it.copy(isRefreshing = true) } getHistoryFromDatabase() @@ -224,7 +226,7 @@ class HistoryModel @Inject constructor( } deleteWholeHistory.execute() - LibraryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) getHistoryFromDatabase("") withContext(Dispatchers.Main) { diff --git a/app/src/main/java/ua/acclorite/book_story/ui/history/HistoryScreen.kt b/app/src/main/java/ua/acclorite/book_story/ui/history/HistoryScreen.kt index b67e8788..caa6dc86 100644 --- a/app/src/main/java/ua/acclorite/book_story/ui/history/HistoryScreen.kt +++ b/app/src/main/java/ua/acclorite/book_story/ui/history/HistoryScreen.kt @@ -30,7 +30,7 @@ object HistoryScreen : Screen, Parcelable { const val DELETE_WHOLE_HISTORY_DIALOG = "delete_whole_history_dialog" @IgnoredOnParcel - val refreshListChannel: Channel = Channel(Channel.CONFLATED) + val refreshListChannel: Channel = Channel(Channel.CONFLATED) @IgnoredOnParcel val insertHistoryChannel: Channel = Channel(Channel.CONFLATED) diff --git a/app/src/main/java/ua/acclorite/book_story/ui/library/LibraryModel.kt b/app/src/main/java/ua/acclorite/book_story/ui/library/LibraryModel.kt index 5ed5de9d..ab58b7b0 100644 --- a/app/src/main/java/ua/acclorite/book_story/ui/library/LibraryModel.kt +++ b/app/src/main/java/ua/acclorite/book_story/ui/library/LibraryModel.kt @@ -4,13 +4,11 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.collectLatest -import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch @@ -27,7 +25,6 @@ import ua.acclorite.book_story.ui.browse.BrowseScreen import ua.acclorite.book_story.ui.history.HistoryScreen import javax.inject.Inject -@OptIn(FlowPreview::class) @HiltViewModel class LibraryModel @Inject constructor( private val getBooks: GetBooks, @@ -45,7 +42,10 @@ class LibraryModel @Inject constructor( /* Observe channel - - - - - - - - - - - */ viewModelScope.launch(Dispatchers.IO) { - LibraryScreen.refreshListChannel.receiveAsFlow().debounce(200).collectLatest { + LibraryScreen.refreshListChannel.receiveAsFlow().collectLatest { + delay(it) + yield() + onEvent(LibraryEvent.OnRefreshList(showIndicator = false, hideSearch = false)) } } @@ -197,7 +197,7 @@ class LibraryModel @Inject constructor( ) } - HistoryScreen.refreshListChannel.trySend(Unit) + HistoryScreen.refreshListChannel.trySend(0) LibraryScreen.scrollToPageCompositionChannel.trySend( event.categories.dropLastWhile { it.category != event.selectedCategory @@ -237,7 +237,7 @@ class LibraryModel @Inject constructor( ) } - HistoryScreen.refreshListChannel.trySend(Unit) + HistoryScreen.refreshListChannel.trySend(0) BrowseScreen.refreshListChannel.trySend(Unit) withContext(Dispatchers.Main) { diff --git a/app/src/main/java/ua/acclorite/book_story/ui/library/LibraryScreen.kt b/app/src/main/java/ua/acclorite/book_story/ui/library/LibraryScreen.kt index 0247a2d0..eee90001 100644 --- a/app/src/main/java/ua/acclorite/book_story/ui/library/LibraryScreen.kt +++ b/app/src/main/java/ua/acclorite/book_story/ui/library/LibraryScreen.kt @@ -40,7 +40,7 @@ object LibraryScreen : Screen, Parcelable { const val DELETE_DIALOG = "delete_dialog" @IgnoredOnParcel - val refreshListChannel: Channel = Channel(Channel.CONFLATED) + val refreshListChannel: Channel = Channel(Channel.CONFLATED) @IgnoredOnParcel val scrollToPageCompositionChannel: Channel = Channel(Channel.CONFLATED) diff --git a/app/src/main/java/ua/acclorite/book_story/ui/reader/ReaderModel.kt b/app/src/main/java/ua/acclorite/book_story/ui/reader/ReaderModel.kt index 2432c995..20f735c3 100644 --- a/app/src/main/java/ua/acclorite/book_story/ui/reader/ReaderModel.kt +++ b/app/src/main/java/ua/acclorite/book_story/ui/reader/ReaderModel.kt @@ -89,8 +89,8 @@ class ReaderModel @Inject constructor( updateBook.execute(_state.value.book) - LibraryScreen.refreshListChannel.trySend(Unit) - HistoryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) + HistoryScreen.refreshListChannel.trySend(0) launch { snapshotFlow { @@ -170,8 +170,8 @@ class ReaderModel @Inject constructor( updateBook.execute(_state.value.book) - LibraryScreen.refreshListChannel.trySend(Unit) - HistoryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(300) + HistoryScreen.refreshListChannel.trySend(300) } } @@ -251,8 +251,8 @@ class ReaderModel @Inject constructor( updateBook.execute(_state.value.book) - LibraryScreen.refreshListChannel.trySend(Unit) - HistoryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) + HistoryScreen.refreshListChannel.trySend(0) } WindowCompat.getInsetsController( @@ -599,8 +599,8 @@ class ReaderModel @Inject constructor( updateBook.execute(_state.value.book) - LibraryScreen.refreshListChannel.trySend(Unit) - HistoryScreen.refreshListChannel.trySend(Unit) + LibraryScreen.refreshListChannel.trySend(0) + HistoryScreen.refreshListChannel.trySend(0) } } }