Skip to content

Commit

Permalink
🛠️ Always show refresh indicator in Library
Browse files Browse the repository at this point in the history
* Shows when searching and refreshing
  • Loading branch information
Acclorite committed Feb 11, 2025
1 parent 7be0fe6 commit 14ac7db
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import ua.acclorite.book_story.domain.library.category.CategoryWithBooks
@Immutable
sealed class LibraryEvent {
data class OnRefreshList(
val showIndicator: Boolean,
val loading: Boolean,
val hideSearch: Boolean
) : LibraryEvent()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ class LibraryModel @Inject constructor(

init {
viewModelScope.launch(Dispatchers.IO) {
getBooksFromDatabase("")
onEvent(
LibraryEvent.OnRefreshList(
loading = true,
hideSearch = true
)
)
}

/* Observe channel - - - - - - - - - - - */
Expand All @@ -55,7 +60,12 @@ class LibraryModel @Inject constructor(
delay(it)
yield()

onEvent(LibraryEvent.OnRefreshList(showIndicator = false, hideSearch = false))
onEvent(
LibraryEvent.OnRefreshList(
loading = false,
hideSearch = false
)
)
}
}
/* - - - - - - - - - - - - - - - - - - - */
Expand All @@ -71,17 +81,16 @@ class LibraryModel @Inject constructor(
refreshJob = viewModelScope.launch(Dispatchers.IO) {
_state.update {
it.copy(
isRefreshing = event.showIndicator,
isLoading = !event.showIndicator,
hasSelectedItems = false,
isRefreshing = true,
isLoading = event.loading,
showSearch = if (event.hideSearch) false else it.showSearch
)
}

yield()
getBooksFromDatabase()

if (event.showIndicator) delay(500)
delay(500)
_state.update {
it.copy(
isRefreshing = false,
Expand All @@ -94,7 +103,12 @@ class LibraryModel @Inject constructor(
is LibraryEvent.OnSearchVisibility -> {
viewModelScope.launch(Dispatchers.IO) {
if (!event.show) {
getBooksFromDatabase("")
onEvent(
LibraryEvent.OnRefreshList(
loading = false,
hideSearch = true
)
)
} else {
_state.update {
it.copy(
Expand Down Expand Up @@ -130,7 +144,12 @@ class LibraryModel @Inject constructor(

is LibraryEvent.OnSearch -> {
viewModelScope.launch(Dispatchers.IO) {
getBooksFromDatabase()
onEvent(
LibraryEvent.OnRefreshList(
loading = false,
hideSearch = false
)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ object LibraryScreen : Screen, Parcelable {
onRefresh = {
screenModel.onEvent(
LibraryEvent.OnRefreshList(
showIndicator = true,
loading = false,
hideSearch = true
)
)
Expand Down

0 comments on commit 14ac7db

Please sign in to comment.