From be27f710be52bfc945d0db99929a2ba6b45e7fa2 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Mon, 20 Jan 2025 14:53:40 +0530 Subject: [PATCH] Added loading functionality to the notes dialog. --- .../kiwixmobile/core/page/PageFragment.kt | 22 +++++++++++++------ .../page/bookmark/viewmodel/BookmarkState.kt | 3 ++- .../bookmark/viewmodel/BookmarkViewModel.kt | 10 ++++++++- .../page/history/viewmodel/HistoryState.kt | 3 ++- .../history/viewmodel/HistoryViewModel.kt | 10 ++++++++- .../core/page/notes/viewmodel/NotesState.kt | 3 ++- .../page/notes/viewmodel/NotesViewModel.kt | 12 ++++++++-- .../viewmodel/effects/ShowOpenNoteDialog.kt | 5 +++++ .../kiwixmobile/core/page/viewmodel/Action.kt | 1 + .../core/page/viewmodel/PageState.kt | 1 + .../core/page/viewmodel/PageViewModel.kt | 4 ++++ core/src/main/res/layout/fragment_page.xml | 12 ++++++++++ 12 files changed, 72 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt index 825f9f56f4..5a9fb51b99 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt @@ -195,14 +195,22 @@ abstract class PageFragment : OnItemClickListener, BaseFragment(), FragmentActiv pageAdapter.items = state.visiblePageItems fragmentPageBinding?.pageSwitch?.isEnabled = !state.isInSelectionState fragmentPageBinding?.noPage?.visibility = if (state.pageItems.isEmpty()) VISIBLE else GONE - if (state.isInSelectionState) { - if (actionMode == null) { - actionMode = - (requireActivity() as AppCompatActivity).startSupportActionMode(actionModeCallback) + when { + state.isInSelectionState -> { + if (actionMode == null) { + actionMode = + (requireActivity() as AppCompatActivity).startSupportActionMode(actionModeCallback) + } + actionMode?.title = getString(R.string.selected_items, state.numberOfSelectedItems()) + } + + state.isLoading -> { + fragmentPageBinding?.loadingZimfileContent?.visibility = View.VISIBLE + } + + else -> { + actionMode?.finish() } - actionMode?.title = getString(R.string.selected_items, state.numberOfSelectedItems()) - } else { - actionMode?.finish() } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkState.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkState.kt index aea012598e..09ed196162 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkState.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkState.kt @@ -26,7 +26,8 @@ data class BookmarkState( override val pageItems: List, override val showAll: Boolean, override val currentZimId: String?, - override val searchTerm: String = "" + override val searchTerm: String = "", + override val isLoading: Boolean ) : PageState() { override val visiblePageItems: List = filteredPageItems diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkViewModel.kt index f491ddc782..cee6592faa 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/viewmodel/BookmarkViewModel.kt @@ -40,7 +40,15 @@ class BookmarkViewModel @Inject constructor( ) { override fun initialState(): BookmarkState = - BookmarkState(emptyList(), sharedPreferenceUtil.showBookmarksAllBooks, zimReaderContainer.id) + BookmarkState( + emptyList(), + sharedPreferenceUtil.showBookmarksAllBooks, + zimReaderContainer.id, + isLoading = false + ) + + override fun loadData(state: BookmarkState, action: Action.LoadingData): BookmarkState = + state.copy(isLoading = action.isLoading) override fun updatePagesBasedOnFilter( state: BookmarkState, diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryState.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryState.kt index 10ea0b37a8..fa925723b2 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryState.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryState.kt @@ -28,7 +28,8 @@ data class HistoryState( override val pageItems: List, override val showAll: Boolean, override val currentZimId: String?, - override val searchTerm: String = "" + override val searchTerm: String = "", + override val isLoading: Boolean ) : PageState() { override val visiblePageItems: List = HeaderizableList(filteredPageItems) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModel.kt index 5a9493cdd4..04892bb173 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/viewmodel/HistoryViewModel.kt @@ -36,7 +36,15 @@ class HistoryViewModel @Inject constructor( ) : PageViewModel(historyRoomDao, sharedPrefs, zimReaderContainer) { override fun initialState(): HistoryState = - HistoryState(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id) + HistoryState( + emptyList(), + sharedPreferenceUtil.showHistoryAllBooks, + zimReaderContainer.id, + isLoading = false + ) + + override fun loadData(state: HistoryState, action: Action.LoadingData): HistoryState = + state.copy(isLoading = action.isLoading) override fun updatePagesBasedOnFilter( state: HistoryState, diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt index 948926b77e..286e35ca40 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt @@ -26,7 +26,8 @@ data class NotesState( override val pageItems: List, override val showAll: Boolean, override val currentZimId: String?, - override val searchTerm: String = "" + override val searchTerm: String = "", + override val isLoading: Boolean, ) : PageState() { override val visiblePageItems: List = filteredPageItems diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt index 11c47ecbfc..a639b8b84c 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt @@ -44,7 +44,15 @@ class NotesViewModel @Inject constructor( } override fun initialState(): NotesState = - NotesState(emptyList(), sharedPreferenceUtil.showNotesAllBooks, zimReaderContainer.id) + NotesState( + emptyList(), + sharedPreferenceUtil.showNotesAllBooks, + zimReaderContainer.id, + isLoading = false + ) + + override fun loadData(state: NotesState, action: Action.LoadingData): NotesState = + state.copy(isLoading = action.isLoading) override fun updatePagesBasedOnFilter(state: NotesState, action: Action.Filter): NotesState = state.copy(searchTerm = action.searchTerm) @@ -70,5 +78,5 @@ class NotesViewModel @Inject constructor( ShowDeleteNotesDialog(effects, state, pageDao, viewModelScope) override fun onItemClick(page: Page) = - ShowOpenNoteDialog(effects, page, zimReaderContainer) + ShowOpenNoteDialog(effects, actions, page, zimReaderContainer) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt index d41e526548..7782cdfac7 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt @@ -28,6 +28,8 @@ import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.isCustomApp import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem +import org.kiwix.kiwixmobile.core.page.viewmodel.Action +import org.kiwix.kiwixmobile.core.page.viewmodel.Action.LoadingData import org.kiwix.kiwixmobile.core.page.viewmodel.effects.OpenNote import org.kiwix.kiwixmobile.core.page.viewmodel.effects.OpenPage import org.kiwix.kiwixmobile.core.reader.ZimFileReader.Companion.CONTENT_PREFIX @@ -43,6 +45,7 @@ import javax.inject.Inject data class ShowOpenNoteDialog( private val effects: PublishProcessor>, + private val actions: PublishProcessor, private val page: Page, private val zimReaderContainer: ZimReaderContainer ) : SideEffect { @@ -53,6 +56,7 @@ data class ShowOpenNoteDialog( ShowNoteDialog, { effects.offer(OpenPage(page, zimReaderContainer)) }, { + actions.offer(LoadingData(true)) activity.lifecycleScope.launch { val item = page as NoteListItem // Check if toDatabase is not null, and then set it in zimReaderContainer. @@ -83,6 +87,7 @@ data class ShowOpenNoteDialog( editor.apply() } } + actions.offer(LoadingData(false)) effects.offer(OpenNote(item.noteFilePath, item.zimUrl, item.title)) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/Action.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/Action.kt index 428d02275b..761933eff0 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/Action.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/Action.kt @@ -13,4 +13,5 @@ sealed class Action { data class UserClickedShowAllToggle(val isChecked: Boolean) : Action() data class Filter(val searchTerm: String) : Action() data class UpdatePages(val pages: List) : Action() + data class LoadingData(val isLoading: Boolean) : Action() } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageState.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageState.kt index 05decb8746..85a4435382 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageState.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageState.kt @@ -30,6 +30,7 @@ abstract class PageState { .filter { it.title.contains(searchTerm, true) } } + abstract val isLoading: Boolean abstract val visiblePageItems: List abstract val showAll: Boolean abstract val currentZimId: String? diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt index 95379f6c3e..a87f679bab 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt @@ -32,6 +32,7 @@ import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.viewmodel.Action.Exit import org.kiwix.kiwixmobile.core.page.viewmodel.Action.ExitActionModeMenu import org.kiwix.kiwixmobile.core.page.viewmodel.Action.Filter +import org.kiwix.kiwixmobile.core.page.viewmodel.Action.LoadingData import org.kiwix.kiwixmobile.core.page.viewmodel.Action.OnItemClick import org.kiwix.kiwixmobile.core.page.viewmodel.Action.OnItemLongClick import org.kiwix.kiwixmobile.core.page.viewmodel.Action.UpdatePages @@ -88,8 +89,11 @@ abstract class PageViewModel>( is OnItemLongClick -> handleItemLongClick(state, action) is Filter -> updatePagesBasedOnFilter(state, action) is UpdatePages -> updatePages(state, action) + is LoadingData -> loadData(state, action) } + abstract fun loadData(state: S, action: LoadingData): S + abstract fun updatePagesBasedOnFilter(state: S, action: Filter): S abstract fun updatePages(state: S, action: UpdatePages): S diff --git a/core/src/main/res/layout/fragment_page.xml b/core/src/main/res/layout/fragment_page.xml index d4e449bd24..ce5a4fb50d 100644 --- a/core/src/main/res/layout/fragment_page.xml +++ b/core/src/main/res/layout/fragment_page.xml @@ -37,6 +37,18 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/app_bar" /> + +