#2154 lots of rewrite between viewmodels, working now but should be possible to improve

This commit is contained in:
Frans-Lukas 2020-07-01 21:40:07 +02:00
parent 37418efb53
commit c57b9c9f3b
10 changed files with 61 additions and 68 deletions

View File

@ -50,7 +50,7 @@ import javax.inject.Inject
abstract class PageActivity : OnItemClickListener, BaseActivity() {
val activityComponent by lazy { coreActivityComponent }
abstract val pageViewModel: PageViewModel<PageState>
abstract val pageViewModel: PageViewModel
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
private var actionMode: ActionMode? = null
val compositeDisposable = CompositeDisposable()

View File

@ -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?
}

View File

@ -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) }

View File

@ -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<PageRelated> = 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<Page>): PageState =
copy(pageItems = (newItems as List<BookmarkItem>))
}

View File

@ -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<BookmarkState>(bookmarksDao) {
) : PageViewModel(bookmarksDao) {
override fun initialState(): BookmarkState =
BookmarkState(emptyList(), true, null)
override val state by lazy {
MutableLiveData<PageState>().apply {
value =
BookmarkState(emptyList(), sharedPreferenceUtil.showHistoryAllBooks, zimReaderContainer.id)
}
}
init {
compositeDisposable.addAll(

View File

@ -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) }

View File

@ -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<HistoryListItem> =
HeaderizableList<HistoryListItem, HistoryItem, DateItem>(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<Page>): PageState =
copy(pageItems = (newItems as List<HistoryItem>))
}

View File

@ -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<HistoryState>(historyDao) {
override val sharedPreferenceUtil: SharedPreferenceUtil
) : PageViewModel(pageDao = historyDao) {
override fun initialState(): HistoryState =
HistoryState(emptyList(), true, null)
override val state by lazy {
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 =
(state as HistoryState).copy(searchTerm = action.searchTerm)

View File

@ -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<Page>
val filteredPageItems: List<PageRelated>
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<Page>
val isInSelectionState: Boolean by lazy { pageItems.any(Page::isSelected) }
abstract val numberOfSelectedItems: Int
abstract val filteredPageItems: List<PageRelated>
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<Page>): PageState
}

View File

@ -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<out T : PageState>(val pageDao: PageDao) : ViewModel() {
abstract class PageViewModel(
val pageDao: PageDao
) : ViewModel() {
abstract val zimReaderContainer: ZimReaderContainer
abstract val sharedPreferenceUtil: SharedPreferenceUtil
val state = MutableLiveData<PageState>().apply {
value = initialState()
}
abstract val state: MutableLiveData<PageState>
val compositeDisposable = CompositeDisposable()
val effects = PublishProcessor.create<SideEffect<*>>()
val actions = PublishProcessor.create<Action>()
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)