Skip to content

Commit

Permalink
Added loading functionality to the notes dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitMaliDeveloper authored and MohitMaliFtechiz committed Jan 20, 2025
1 parent c1026c7 commit be27f71
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 14 deletions.
22 changes: 15 additions & 7 deletions core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ data class BookmarkState(
override val pageItems: List<LibkiwixBookmarkItem>,
override val showAll: Boolean,
override val currentZimId: String?,
override val searchTerm: String = ""
override val searchTerm: String = "",
override val isLoading: Boolean
) : PageState<LibkiwixBookmarkItem>() {
override val visiblePageItems: List<PageRelated> = filteredPageItems

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ data class HistoryState(
override val pageItems: List<HistoryItem>,
override val showAll: Boolean,
override val currentZimId: String?,
override val searchTerm: String = ""
override val searchTerm: String = "",
override val isLoading: Boolean
) : PageState<HistoryItem>() {
override val visiblePageItems: List<HistoryListItem> =
HeaderizableList<HistoryListItem, HistoryItem, DateItem>(filteredPageItems)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ class HistoryViewModel @Inject constructor(
) : PageViewModel<HistoryItem, HistoryState>(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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ data class NotesState(
override val pageItems: List<NoteListItem>,
override val showAll: Boolean,
override val currentZimId: String?,
override val searchTerm: String = ""
override val searchTerm: String = "",
override val isLoading: Boolean,
) : PageState<NoteListItem>() {
override val visiblePageItems: List<PageRelated> = filteredPageItems

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,6 +45,7 @@ import javax.inject.Inject

data class ShowOpenNoteDialog(
private val effects: PublishProcessor<SideEffect<*>>,
private val actions: PublishProcessor<Action>,
private val page: Page,
private val zimReaderContainer: ZimReaderContainer
) : SideEffect<Unit> {
Expand All @@ -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.
Expand Down Expand Up @@ -83,6 +87,7 @@ data class ShowOpenNoteDialog(
editor.apply()
}
}
actions.offer(LoadingData(false))
effects.offer(OpenNote(item.noteFilePath, item.zimUrl, item.title))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Page>) : Action()
data class LoadingData(val isLoading: Boolean) : Action()
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ abstract class PageState<T : Page> {
.filter { it.title.contains(searchTerm, true) }
}

abstract val isLoading: Boolean
abstract val visiblePageItems: List<PageRelated>
abstract val showAll: Boolean
abstract val currentZimId: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,8 +89,11 @@ abstract class PageViewModel<T : Page, S : PageState<T>>(
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
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/res/layout/fragment_page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/app_bar" />

<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/loading_zimfile_content"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/progress_view_height"
android:indeterminate="false"
android:max="100"
android:theme="@style/ThemeOverlay.KiwixTheme.ProgressBar"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@+id/app_bar"
tools:progress="70" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
Expand Down

0 comments on commit be27f71

Please sign in to comment.