mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-18 03:45:33 -04:00
#2154 lots of rewrite between viewmodels, working now but should be possible to improve
This commit is contained in:
parent
37418efb53
commit
c57b9c9f3b
@ -50,7 +50,7 @@ import javax.inject.Inject
|
|||||||
|
|
||||||
abstract class PageActivity : OnItemClickListener, BaseActivity() {
|
abstract class PageActivity : OnItemClickListener, BaseActivity() {
|
||||||
val activityComponent by lazy { coreActivityComponent }
|
val activityComponent by lazy { coreActivityComponent }
|
||||||
abstract val pageViewModel: PageViewModel<PageState>
|
abstract val pageViewModel: PageViewModel
|
||||||
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
||||||
private var actionMode: ActionMode? = null
|
private var actionMode: ActionMode? = null
|
||||||
val compositeDisposable = CompositeDisposable()
|
val compositeDisposable = CompositeDisposable()
|
||||||
|
@ -26,6 +26,6 @@ interface Page : PageRelated {
|
|||||||
val zimFilePath: String?
|
val zimFilePath: String?
|
||||||
val url: String
|
val url: String
|
||||||
val title: String
|
val title: String
|
||||||
val isSelected: Boolean
|
var isSelected: Boolean
|
||||||
val favicon: String?
|
val favicon: String?
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package org.kiwix.kiwixmobile.core.page.bookmark
|
package org.kiwix.kiwixmobile.core.page.bookmark
|
||||||
|
|
||||||
import org.kiwix.kiwixmobile.core.R
|
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.extensions.ActivityExtensions.viewModel
|
||||||
import org.kiwix.kiwixmobile.core.page.PageActivity
|
import org.kiwix.kiwixmobile.core.page.PageActivity
|
||||||
import org.kiwix.kiwixmobile.core.page.adapter.PageAdapter
|
import org.kiwix.kiwixmobile.core.page.adapter.PageAdapter
|
||||||
@ -15,10 +14,6 @@ class BookmarksActivity : PageActivity() {
|
|||||||
PageAdapter(PageItemDelegate(this))
|
PageAdapter(PageItemDelegate(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun injection(coreComponent: CoreComponent) {
|
|
||||||
activityComponent.inject(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
override val title: String by lazy { getString(R.string.bookmarks) }
|
override val title: String by lazy { getString(R.string.bookmarks) }
|
||||||
override val noItemsString: String by lazy { getString(R.string.no_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) }
|
override val switchString: String by lazy { getString(R.string.bookmarks_from_current_book) }
|
||||||
|
@ -28,20 +28,13 @@ data class BookmarkState(
|
|||||||
override val showAll: Boolean,
|
override val showAll: Boolean,
|
||||||
override val currentZimId: String?,
|
override val currentZimId: String?,
|
||||||
override val searchTerm: String = ""
|
override val searchTerm: String = ""
|
||||||
) : PageState {
|
) : PageState() {
|
||||||
|
override val numberOfSelectedItems: Int = pageItems.filter(Page::isSelected).size
|
||||||
override val isInSelectionState = pageItems.any(BookmarkItem::isSelected)
|
|
||||||
override val numberOfSelectedItems = pageItems.filter(BookmarkItem::isSelected).size
|
|
||||||
|
|
||||||
override val filteredPageItems: List<PageRelated> = pageItems
|
override val filteredPageItems: List<PageRelated> = pageItems
|
||||||
.filter { it.zimId == currentZimId || showAll }
|
.filter { it.zimId == currentZimId || showAll }
|
||||||
.filter { it.title.contains(searchTerm, true) }
|
.filter { it.title.contains(searchTerm, true) }
|
||||||
|
|
||||||
override fun toggleSelectionOfItem(page: Page): BookmarkState {
|
override fun copyWithNewItems(newItems: List<Page>): PageState =
|
||||||
page as BookmarkItem
|
copy(pageItems = (newItems as List<BookmarkItem>))
|
||||||
val newList = pageItems.map {
|
|
||||||
if (it.databaseId == page.databaseId) it.apply { isSelected = !isSelected } else it
|
|
||||||
}
|
|
||||||
return BookmarkState(newList, showAll, currentZimId, searchTerm)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
package org.kiwix.kiwixmobile.core.page.bookmark.viewmodel
|
package org.kiwix.kiwixmobile.core.page.bookmark.viewmodel
|
||||||
|
|
||||||
|
import androidx.lifecycle.MutableLiveData
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao
|
import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao
|
||||||
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem
|
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem
|
||||||
@ -34,10 +35,14 @@ class BookmarkViewModel @Inject constructor(
|
|||||||
bookmarksDao: NewBookmarksDao,
|
bookmarksDao: NewBookmarksDao,
|
||||||
override val zimReaderContainer: ZimReaderContainer,
|
override val zimReaderContainer: ZimReaderContainer,
|
||||||
override val sharedPreferenceUtil: SharedPreferenceUtil
|
override val sharedPreferenceUtil: SharedPreferenceUtil
|
||||||
) : PageViewModel<BookmarkState>(bookmarksDao) {
|
) : PageViewModel(bookmarksDao) {
|
||||||
|
|
||||||
override fun initialState(): BookmarkState =
|
override val state by lazy {
|
||||||
BookmarkState(emptyList(), true, null)
|
MutableLiveData<PageState>().apply {
|
||||||
|
value =
|
||||||
|
BookmarkState(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
compositeDisposable.addAll(
|
compositeDisposable.addAll(
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package org.kiwix.kiwixmobile.core.page.history
|
package org.kiwix.kiwixmobile.core.page.history
|
||||||
|
|
||||||
import org.kiwix.kiwixmobile.core.R
|
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.extensions.ActivityExtensions.viewModel
|
||||||
import org.kiwix.kiwixmobile.core.page.PageActivity
|
import org.kiwix.kiwixmobile.core.page.PageActivity
|
||||||
import org.kiwix.kiwixmobile.core.page.adapter.PageAdapter
|
import org.kiwix.kiwixmobile.core.page.adapter.PageAdapter
|
||||||
@ -18,10 +17,6 @@ class HistoryActivity : PageActivity() {
|
|||||||
PageAdapter(PageItemDelegate(this), HistoryDateDelegate())
|
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 noItemsString: String by lazy { getString(R.string.no_history) }
|
||||||
override val switchString: String by lazy { getString(R.string.history_from_current_book) }
|
override val switchString: String by lazy { getString(R.string.history_from_current_book) }
|
||||||
override val title: String by lazy { getString(R.string.history) }
|
override val title: String by lazy { getString(R.string.history) }
|
||||||
|
@ -30,9 +30,8 @@ data class HistoryState(
|
|||||||
override val showAll: Boolean,
|
override val showAll: Boolean,
|
||||||
override val currentZimId: String?,
|
override val currentZimId: String?,
|
||||||
override val searchTerm: String = ""
|
override val searchTerm: String = ""
|
||||||
) : PageState {
|
) : PageState() {
|
||||||
override val isInSelectionState = pageItems.any(HistoryItem::isSelected)
|
override val numberOfSelectedItems: Int = pageItems.filter(Page::isSelected).size
|
||||||
override val numberOfSelectedItems = pageItems.filter(HistoryItem::isSelected).size
|
|
||||||
|
|
||||||
override val filteredPageItems: List<HistoryListItem> =
|
override val filteredPageItems: List<HistoryListItem> =
|
||||||
HeaderizableList<HistoryListItem, HistoryItem, DateItem>(pageItems
|
HeaderizableList<HistoryListItem, HistoryItem, DateItem>(pageItems
|
||||||
@ -43,13 +42,6 @@ data class HistoryState(
|
|||||||
{ current, next -> current.dateString != next.dateString }
|
{ current, next -> current.dateString != next.dateString }
|
||||||
)
|
)
|
||||||
|
|
||||||
override fun toggleSelectionOfItem(historyItem: Page): HistoryState {
|
override fun copyWithNewItems(newItems: List<Page>): PageState =
|
||||||
historyItem as HistoryItem
|
copy(pageItems = (newItems as List<HistoryItem>))
|
||||||
val newList = pageItems.map {
|
|
||||||
if (it.id == historyItem.id) it.apply {
|
|
||||||
isSelected = !isSelected
|
|
||||||
} else it
|
|
||||||
}
|
|
||||||
return copy(pageItems = newList)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
|
|
||||||
package org.kiwix.kiwixmobile.core.page.history.viewmodel
|
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.dao.HistoryDao
|
||||||
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem
|
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem
|
||||||
import org.kiwix.kiwixmobile.core.page.history.viewmodel.effects.ShowDeleteHistoryDialog
|
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
|
import javax.inject.Inject
|
||||||
|
|
||||||
class HistoryViewModel @Inject constructor(
|
class HistoryViewModel @Inject constructor(
|
||||||
|
historyDao: HistoryDao,
|
||||||
override val zimReaderContainer: ZimReaderContainer,
|
override val zimReaderContainer: ZimReaderContainer,
|
||||||
override val sharedPreferenceUtil: SharedPreferenceUtil,
|
override val sharedPreferenceUtil: SharedPreferenceUtil
|
||||||
historyDao: HistoryDao
|
) : PageViewModel(pageDao = historyDao) {
|
||||||
) : PageViewModel<HistoryState>(historyDao) {
|
|
||||||
|
|
||||||
override fun initialState(): HistoryState =
|
override val state by lazy {
|
||||||
HistoryState(emptyList(), true, null)
|
MutableLiveData<PageState>().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 =
|
override fun updatePagesBasedOnFilter(state: PageState, action: Action.Filter): PageState =
|
||||||
(state as HistoryState).copy(searchTerm = action.searchTerm)
|
(state as HistoryState).copy(searchTerm = action.searchTerm)
|
||||||
|
@ -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.Page
|
||||||
import org.kiwix.kiwixmobile.core.page.adapter.PageRelated
|
import org.kiwix.kiwixmobile.core.page.adapter.PageRelated
|
||||||
|
|
||||||
interface PageState {
|
abstract class PageState {
|
||||||
val pageItems: List<Page>
|
abstract val pageItems: List<Page>
|
||||||
val filteredPageItems: List<PageRelated>
|
val isInSelectionState: Boolean by lazy { pageItems.any(Page::isSelected) }
|
||||||
val showAll: Boolean
|
abstract val numberOfSelectedItems: Int
|
||||||
val currentZimId: String?
|
abstract val filteredPageItems: List<PageRelated>
|
||||||
val searchTerm: String
|
abstract val showAll: Boolean
|
||||||
val isInSelectionState: Boolean
|
abstract val currentZimId: String?
|
||||||
val numberOfSelectedItems: Int
|
abstract val searchTerm: String
|
||||||
fun toggleSelectionOfItem(page: Page): PageState
|
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<Page>): PageState
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,6 @@ import androidx.lifecycle.ViewModel
|
|||||||
import io.reactivex.disposables.CompositeDisposable
|
import io.reactivex.disposables.CompositeDisposable
|
||||||
import io.reactivex.disposables.Disposable
|
import io.reactivex.disposables.Disposable
|
||||||
import io.reactivex.processors.PublishProcessor
|
import io.reactivex.processors.PublishProcessor
|
||||||
import io.reactivex.schedulers.Schedulers
|
|
||||||
import org.kiwix.kiwixmobile.core.base.SideEffect
|
import org.kiwix.kiwixmobile.core.base.SideEffect
|
||||||
import org.kiwix.kiwixmobile.core.dao.PageDao
|
import org.kiwix.kiwixmobile.core.dao.PageDao
|
||||||
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.Exit
|
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.search.viewmodel.effects.Finish
|
||||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
||||||
|
|
||||||
abstract class PageViewModel<out T : PageState>(val pageDao: PageDao) : ViewModel() {
|
abstract class PageViewModel(
|
||||||
|
val pageDao: PageDao
|
||||||
|
) : ViewModel() {
|
||||||
|
|
||||||
abstract val zimReaderContainer: ZimReaderContainer
|
abstract val zimReaderContainer: ZimReaderContainer
|
||||||
abstract val sharedPreferenceUtil: SharedPreferenceUtil
|
abstract val sharedPreferenceUtil: SharedPreferenceUtil
|
||||||
val state = MutableLiveData<PageState>().apply {
|
abstract val state: MutableLiveData<PageState>
|
||||||
value = initialState()
|
|
||||||
}
|
|
||||||
|
|
||||||
val compositeDisposable = CompositeDisposable()
|
val compositeDisposable = CompositeDisposable()
|
||||||
val effects = PublishProcessor.create<SideEffect<*>>()
|
val effects = PublishProcessor.create<SideEffect<*>>()
|
||||||
val actions = PublishProcessor.create<Action>()
|
val actions = PublishProcessor.create<Action>()
|
||||||
|
|
||||||
init {
|
protected fun viewStateReducer(): Disposable =
|
||||||
compositeDisposable.addAll(
|
|
||||||
viewStateReducer(),
|
|
||||||
pageDao.pages().subscribeOn(Schedulers.io())
|
|
||||||
.subscribe({ actions.offer(Action.UpdatePages(it)) }, Throwable::printStackTrace)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun viewStateReducer(): Disposable =
|
|
||||||
actions.map { reduce(it, state.value!!) }
|
actions.map { reduce(it, state.value!!) }
|
||||||
.subscribe(state::postValue, Throwable::printStackTrace)
|
.subscribe(state::postValue, Throwable::printStackTrace)
|
||||||
|
|
||||||
abstract fun initialState(): T
|
|
||||||
|
|
||||||
private fun reduce(action: Action, state: PageState): PageState = when (action) {
|
private fun reduce(action: Action, state: PageState): PageState = when (action) {
|
||||||
Exit -> finishActivity(state)
|
Exit -> finishActivity(state)
|
||||||
ExitActionModeMenu -> deselectAllPages(state)
|
ExitActionModeMenu -> deselectAllPages(state)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user