diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageActivity.kt index 09b713d28..7fbbaaf70 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageActivity.kt @@ -50,7 +50,7 @@ import javax.inject.Inject abstract class PageActivity : OnItemClickListener, BaseActivity() { val activityComponent by lazy { coreActivityComponent } - abstract val pageViewModel: PageViewModel + abstract val pageViewModel: PageViewModel @Inject lateinit var viewModelFactory: ViewModelProvider.Factory private var actionMode: ActionMode? = null val compositeDisposable = CompositeDisposable() diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/Page.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/Page.kt index 22008fc13..bee0fa34c 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/Page.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/adapter/Page.kt @@ -26,6 +26,6 @@ interface Page : PageRelated { val zimFilePath: String? val url: String val title: String - val isSelected: Boolean + var isSelected: Boolean val favicon: String? } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt index 858722f25..95c8da08f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt @@ -1,7 +1,6 @@ package org.kiwix.kiwixmobile.core.page.bookmark import org.kiwix.kiwixmobile.core.R -import org.kiwix.kiwixmobile.core.di.components.CoreComponent import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel import org.kiwix.kiwixmobile.core.page.PageActivity import org.kiwix.kiwixmobile.core.page.adapter.PageAdapter @@ -15,10 +14,6 @@ class BookmarksActivity : PageActivity() { PageAdapter(PageItemDelegate(this)) } - override fun injection(coreComponent: CoreComponent) { - activityComponent.inject(this) - } - override val title: String by lazy { getString(R.string.bookmarks) } override val noItemsString: String by lazy { getString(R.string.no_bookmarks) } override val switchString: String by lazy { getString(R.string.bookmarks_from_current_book) } 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 8bb13fbae..ee395dea6 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 @@ -28,20 +28,13 @@ data class BookmarkState( override val showAll: Boolean, override val currentZimId: String?, override val searchTerm: String = "" -) : PageState { - - override val isInSelectionState = pageItems.any(BookmarkItem::isSelected) - override val numberOfSelectedItems = pageItems.filter(BookmarkItem::isSelected).size +) : PageState() { + override val numberOfSelectedItems: Int = pageItems.filter(Page::isSelected).size override val filteredPageItems: List = pageItems .filter { it.zimId == currentZimId || showAll } .filter { it.title.contains(searchTerm, true) } - override fun toggleSelectionOfItem(page: Page): BookmarkState { - page as BookmarkItem - val newList = pageItems.map { - if (it.databaseId == page.databaseId) it.apply { isSelected = !isSelected } else it - } - return BookmarkState(newList, showAll, currentZimId, searchTerm) - } + override fun copyWithNewItems(newItems: List): PageState = + copy(pageItems = (newItems as List)) } 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 50e401e4f..53c06dc9f 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 @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile.core.page.bookmark.viewmodel +import androidx.lifecycle.MutableLiveData import io.reactivex.schedulers.Schedulers import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem @@ -34,10 +35,14 @@ class BookmarkViewModel @Inject constructor( bookmarksDao: NewBookmarksDao, override val zimReaderContainer: ZimReaderContainer, override val sharedPreferenceUtil: SharedPreferenceUtil -) : PageViewModel(bookmarksDao) { +) : PageViewModel(bookmarksDao) { - override fun initialState(): BookmarkState = - BookmarkState(emptyList(), true, null) + override val state by lazy { + MutableLiveData().apply { + value = + BookmarkState(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id) + } + } init { compositeDisposable.addAll( diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt index b86718f7d..7e0e12738 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt @@ -1,7 +1,6 @@ package org.kiwix.kiwixmobile.core.page.history import org.kiwix.kiwixmobile.core.R -import org.kiwix.kiwixmobile.core.di.components.CoreComponent import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel import org.kiwix.kiwixmobile.core.page.PageActivity import org.kiwix.kiwixmobile.core.page.adapter.PageAdapter @@ -18,10 +17,6 @@ class HistoryActivity : PageActivity() { PageAdapter(PageItemDelegate(this), HistoryDateDelegate()) } - override fun injection(coreComponent: CoreComponent) { - activityComponent.inject(this) - } - override val noItemsString: String by lazy { getString(R.string.no_history) } override val switchString: String by lazy { getString(R.string.history_from_current_book) } override val title: String by lazy { getString(R.string.history) } 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 59e6fc7d2..306b222c3 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 @@ -30,9 +30,8 @@ data class HistoryState( override val showAll: Boolean, override val currentZimId: String?, override val searchTerm: String = "" -) : PageState { - override val isInSelectionState = pageItems.any(HistoryItem::isSelected) - override val numberOfSelectedItems = pageItems.filter(HistoryItem::isSelected).size +) : PageState() { + override val numberOfSelectedItems: Int = pageItems.filter(Page::isSelected).size override val filteredPageItems: List = HeaderizableList(pageItems @@ -43,13 +42,6 @@ data class HistoryState( { current, next -> current.dateString != next.dateString } ) - override fun toggleSelectionOfItem(historyItem: Page): HistoryState { - historyItem as HistoryItem - val newList = pageItems.map { - if (it.id == historyItem.id) it.apply { - isSelected = !isSelected - } else it - } - return copy(pageItems = newList) - } + override fun copyWithNewItems(newItems: List): PageState = + copy(pageItems = (newItems as List)) } 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 ebb7291f6..3172988fc 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 @@ -18,6 +18,8 @@ package org.kiwix.kiwixmobile.core.page.history.viewmodel +import androidx.lifecycle.MutableLiveData +import io.reactivex.schedulers.Schedulers import org.kiwix.kiwixmobile.core.dao.HistoryDao import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem import org.kiwix.kiwixmobile.core.page.history.viewmodel.effects.ShowDeleteHistoryDialog @@ -30,13 +32,25 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import javax.inject.Inject class HistoryViewModel @Inject constructor( + historyDao: HistoryDao, override val zimReaderContainer: ZimReaderContainer, - override val sharedPreferenceUtil: SharedPreferenceUtil, - historyDao: HistoryDao -) : PageViewModel(historyDao) { + override val sharedPreferenceUtil: SharedPreferenceUtil +) : PageViewModel(pageDao = historyDao) { - override fun initialState(): HistoryState = - HistoryState(emptyList(), true, null) + override val state by lazy { + MutableLiveData().apply { + value = + HistoryState(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id) + } + } + + init { + compositeDisposable.addAll( + viewStateReducer(), + pageDao.pages().subscribeOn(Schedulers.io()) + .subscribe({ actions.offer(Action.UpdatePages(it)) }, Throwable::printStackTrace) + ) + } override fun updatePagesBasedOnFilter(state: PageState, action: Action.Filter): PageState = (state as HistoryState).copy(searchTerm = action.searchTerm) 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 e372dd4d6..327ff0704 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 @@ -21,13 +21,22 @@ package org.kiwix.kiwixmobile.core.page.viewmodel import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.adapter.PageRelated -interface PageState { - val pageItems: List - val filteredPageItems: List - val showAll: Boolean - val currentZimId: String? - val searchTerm: String - val isInSelectionState: Boolean - val numberOfSelectedItems: Int - fun toggleSelectionOfItem(page: Page): PageState +abstract class PageState { + abstract val pageItems: List + val isInSelectionState: Boolean by lazy { pageItems.any(Page::isSelected) } + abstract val numberOfSelectedItems: Int + abstract val filteredPageItems: List + abstract val showAll: Boolean + abstract val currentZimId: String? + abstract val searchTerm: String + fun toggleSelectionOfItem(page: Page): PageState { + val newList = pageItems.map { + if (it.id == page.id) it.apply { + isSelected = !isSelected + } else it + } + return copyWithNewItems(newList) + } + + abstract fun copyWithNewItems(newItems: List): PageState } 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 f1a94a0e6..ee35ba741 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 @@ -23,7 +23,6 @@ import androidx.lifecycle.ViewModel import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.Disposable import io.reactivex.processors.PublishProcessor -import io.reactivex.schedulers.Schedulers import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.dao.PageDao import org.kiwix.kiwixmobile.core.page.viewmodel.Action.Exit @@ -40,31 +39,22 @@ import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer import org.kiwix.kiwixmobile.core.search.viewmodel.effects.Finish import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil -abstract class PageViewModel(val pageDao: PageDao) : ViewModel() { +abstract class PageViewModel( + val pageDao: PageDao +) : ViewModel() { + abstract val zimReaderContainer: ZimReaderContainer abstract val sharedPreferenceUtil: SharedPreferenceUtil - val state = MutableLiveData().apply { - value = initialState() - } + abstract val state: MutableLiveData val compositeDisposable = CompositeDisposable() val effects = PublishProcessor.create>() val actions = PublishProcessor.create() - init { - compositeDisposable.addAll( - viewStateReducer(), - pageDao.pages().subscribeOn(Schedulers.io()) - .subscribe({ actions.offer(Action.UpdatePages(it)) }, Throwable::printStackTrace) - ) - } - - fun viewStateReducer(): Disposable = + protected fun viewStateReducer(): Disposable = actions.map { reduce(it, state.value!!) } .subscribe(state::postValue, Throwable::printStackTrace) - abstract fun initialState(): T - private fun reduce(action: Action, state: PageState): PageState = when (action) { Exit -> finishActivity(state) ExitActionModeMenu -> deselectAllPages(state)