🛠️ Always show refresh indicator in History

* Shows when searching, refreshing and deleting history
This commit is contained in:
Acclorite 2025-02-11 13:02:59 +02:00
parent 93a06aef59
commit 7be0fe6d0c
3 changed files with 52 additions and 23 deletions

View file

@ -15,7 +15,7 @@ import ua.acclorite.book_story.domain.history.History
@Immutable @Immutable
sealed class HistoryEvent { sealed class HistoryEvent {
data class OnRefreshList( data class OnRefreshList(
val showIndicator: Boolean, val loading: Boolean,
val hideSearch: Boolean val hideSearch: Boolean
) : HistoryEvent() ) : HistoryEvent()

View file

@ -56,12 +56,12 @@ class HistoryModel @Inject constructor(
init { init {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
_state.update { onEvent(
it.copy( HistoryEvent.OnRefreshList(
isLoading = true loading = true,
hideSearch = true
) )
} )
getHistoryFromDatabase()
} }
/* Observe channel - - - - - - - - - - - */ /* Observe channel - - - - - - - - - - - */
@ -70,7 +70,12 @@ class HistoryModel @Inject constructor(
delay(it) delay(it)
yield() yield()
onEvent(HistoryEvent.OnRefreshList(showIndicator = false, hideSearch = false)) onEvent(
HistoryEvent.OnRefreshList(
loading = false,
hideSearch = false
)
)
} }
} }
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
@ -86,7 +91,12 @@ class HistoryModel @Inject constructor(
delay(500) delay(500)
yield() yield()
getHistoryFromDatabase() onEvent(
HistoryEvent.OnRefreshList(
loading = false,
hideSearch = false
)
)
LibraryScreen.refreshListChannel.trySend(0) LibraryScreen.refreshListChannel.trySend(0)
} }
} }
@ -104,8 +114,8 @@ class HistoryModel @Inject constructor(
refreshJob = viewModelScope.launch(Dispatchers.IO) { refreshJob = viewModelScope.launch(Dispatchers.IO) {
_state.update { _state.update {
it.copy( it.copy(
isRefreshing = event.showIndicator, isRefreshing = true,
isLoading = !event.showIndicator, isLoading = event.loading,
showSearch = if (event.hideSearch) false else it.showSearch showSearch = if (event.hideSearch) false else it.showSearch
) )
} }
@ -113,7 +123,7 @@ class HistoryModel @Inject constructor(
yield() yield()
getHistoryFromDatabase() getHistoryFromDatabase()
if (event.showIndicator) delay(500) delay(500)
_state.update { _state.update {
it.copy( it.copy(
isRefreshing = false, isRefreshing = false,
@ -126,7 +136,12 @@ class HistoryModel @Inject constructor(
is HistoryEvent.OnSearchVisibility -> { is HistoryEvent.OnSearchVisibility -> {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
if (!event.show) { if (!event.show) {
getHistoryFromDatabase("") onEvent(
HistoryEvent.OnRefreshList(
loading = false,
hideSearch = true
)
)
} else { } else {
_state.update { _state.update {
it.copy( it.copy(
@ -175,7 +190,12 @@ class HistoryModel @Inject constructor(
is HistoryEvent.OnSearch -> { is HistoryEvent.OnSearch -> {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
getHistoryFromDatabase() onEvent(
HistoryEvent.OnRefreshList(
loading = false,
hideSearch = false
)
)
} }
} }
@ -183,10 +203,12 @@ class HistoryModel @Inject constructor(
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
deleteHistory.execute(event.history) deleteHistory.execute(event.history)
_state.update { it.copy(isRefreshing = true) } onEvent(
getHistoryFromDatabase() HistoryEvent.OnRefreshList(
_state.update { it.copy(isRefreshing = false) } loading = false,
hideSearch = false
)
)
LibraryScreen.refreshListChannel.trySend(0) LibraryScreen.refreshListChannel.trySend(0)
deleteHistoryEntry?.cancel() deleteHistoryEntry?.cancel()
@ -212,10 +234,12 @@ class HistoryModel @Inject constructor(
insertHistory.execute(event.history) insertHistory.execute(event.history)
LibraryScreen.refreshListChannel.trySend(0) LibraryScreen.refreshListChannel.trySend(0)
_state.update { it.copy(isRefreshing = true) } onEvent(
getHistoryFromDatabase() HistoryEvent.OnRefreshList(
delay(500) loading = false,
_state.update { it.copy(isRefreshing = false) } hideSearch = false
)
)
} }
} }
} }
@ -242,7 +266,12 @@ class HistoryModel @Inject constructor(
deleteWholeHistory.execute() deleteWholeHistory.execute()
LibraryScreen.refreshListChannel.trySend(0) LibraryScreen.refreshListChannel.trySend(0)
getHistoryFromDatabase("") onEvent(
HistoryEvent.OnRefreshList(
loading = true,
hideSearch = true
)
)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
event.context event.context

View file

@ -72,7 +72,7 @@ object HistoryScreen : Screen, Parcelable {
onRefresh = { onRefresh = {
screenModel.onEvent( screenModel.onEvent(
HistoryEvent.OnRefreshList( HistoryEvent.OnRefreshList(
showIndicator = true, loading = false,
hideSearch = true hideSearch = true
) )
) )