From b57ff6e85ebf8e8eb65efeb97e74b6d968f2b763 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Tue, 1 Feb 2022 00:51:25 +0100 Subject: [PATCH] Update: Extract Sections and Order from ProductItem class --- .../looker/droidify/content/Preferences.kt | 11 ++-- .../looker/droidify/database/CursorOwner.kt | 15 ++--- .../com/looker/droidify/database/DAOs.kt | 31 ++++----- .../com/looker/droidify/entity/Enums.kt | 9 +++ .../com/looker/droidify/entity/ProductItem.kt | 63 ++++--------------- .../com/looker/droidify/entity/Section.kt | 44 +++++++++++++ .../looker/droidify/screen/TabsFragment.kt | 44 ++++++------- .../looker/droidify/service/SyncService.kt | 6 +- .../droidify/ui/fragments/AppListFragment.kt | 7 ++- .../droidify/ui/fragments/MainNavFragmentX.kt | 31 ++++----- .../ui/viewmodels/AppListViewModel.kt | 21 ++++--- .../viewmodels/MainNavFragmentViewModelX.kt | 27 ++++---- 12 files changed, 165 insertions(+), 144 deletions(-) create mode 100644 src/main/kotlin/com/looker/droidify/entity/Enums.kt create mode 100644 src/main/kotlin/com/looker/droidify/entity/Section.kt diff --git a/src/main/kotlin/com/looker/droidify/content/Preferences.kt b/src/main/kotlin/com/looker/droidify/content/Preferences.kt index 80156475..dfa75147 100644 --- a/src/main/kotlin/com/looker/droidify/content/Preferences.kt +++ b/src/main/kotlin/com/looker/droidify/content/Preferences.kt @@ -6,7 +6,7 @@ import android.content.res.Configuration import com.looker.droidify.PREFS_LANGUAGE import com.looker.droidify.PREFS_LANGUAGE_DEFAULT import com.looker.droidify.R -import com.looker.droidify.entity.ProductItem +import com.looker.droidify.entity.Order import com.looker.droidify.utility.extension.android.Android import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -135,6 +135,7 @@ object Preferences { "auto_sync", Value.EnumerationValue(Preferences.AutoSync.Wifi) ) + object InstallAfterSync : Key("auto_sync_install", Value.BooleanValue(Android.sdk(31))) @@ -192,14 +193,14 @@ object Preferences { object Socks : ProxyType("socks", Proxy.Type.SOCKS) } - sealed class SortOrder(override val valueString: String, val order: ProductItem.Order) : + sealed class SortOrder(override val valueString: String, val order: Order) : Enumeration { override val values: List get() = listOf(Name, Added, Update) - object Name : SortOrder("name", ProductItem.Order.NAME) - object Added : SortOrder("added", ProductItem.Order.DATE_ADDED) - object Update : SortOrder("update", ProductItem.Order.LAST_UPDATE) + object Name : SortOrder("name", Order.NAME) + object Added : SortOrder("added", Order.DATE_ADDED) + object Update : SortOrder("update", Order.LAST_UPDATE) } sealed class Theme(override val valueString: String) : Enumeration { diff --git a/src/main/kotlin/com/looker/droidify/database/CursorOwner.kt b/src/main/kotlin/com/looker/droidify/database/CursorOwner.kt index 67cc459b..51cd266c 100644 --- a/src/main/kotlin/com/looker/droidify/database/CursorOwner.kt +++ b/src/main/kotlin/com/looker/droidify/database/CursorOwner.kt @@ -5,31 +5,32 @@ import android.os.Bundle import androidx.fragment.app.Fragment import androidx.loader.app.LoaderManager import androidx.loader.content.Loader -import com.looker.droidify.entity.ProductItem +import com.looker.droidify.entity.Order +import com.looker.droidify.entity.Section class CursorOwner : Fragment(), LoaderManager.LoaderCallbacks { sealed class Request { internal abstract val id: Int data class ProductsAvailable( - val searchQuery: String, val section: ProductItem.Section, - val order: ProductItem.Order, + val searchQuery: String, val section: Section, + val order: Order, ) : Request() { override val id: Int get() = 1 } data class ProductsInstalled( - val searchQuery: String, val section: ProductItem.Section, - val order: ProductItem.Order, + val searchQuery: String, val section: Section, + val order: Order, ) : Request() { override val id: Int get() = 2 } data class ProductsUpdates( - val searchQuery: String, val section: ProductItem.Section, - val order: ProductItem.Order, + val searchQuery: String, val section: Section, + val order: Order, ) : Request() { override val id: Int get() = 3 diff --git a/src/main/kotlin/com/looker/droidify/database/DAOs.kt b/src/main/kotlin/com/looker/droidify/database/DAOs.kt index 27e848f4..a63b24ef 100644 --- a/src/main/kotlin/com/looker/droidify/database/DAOs.kt +++ b/src/main/kotlin/com/looker/droidify/database/DAOs.kt @@ -9,7 +9,8 @@ import androidx.sqlite.db.SimpleSQLiteQuery import androidx.sqlite.db.SupportSQLiteQuery import com.looker.droidify.* import com.looker.droidify.database.entity.* -import com.looker.droidify.entity.ProductItem +import com.looker.droidify.entity.Order +import com.looker.droidify.entity.Section import io.reactivex.rxjava3.core.Flowable @@ -91,7 +92,7 @@ interface ProductDao : BaseDao { @Transaction fun query( installed: Boolean, updates: Boolean, searchQuery: String, - section: ProductItem.Section, order: ProductItem.Order, signal: CancellationSignal? + section: Section, order: Order, signal: CancellationSignal? ): Cursor { val builder = QueryBuilder() @@ -132,17 +133,17 @@ interface ProductDao : BaseDao { builder += """JOIN $ROW_INSTALLED_NAME AS installed ON product.${ROW_PACKAGE_NAME} = installed.${ROW_PACKAGE_NAME}""" - if (section is ProductItem.Section.Category) { + if (section is Section.Category) { builder += """JOIN $ROW_CATEGORY_NAME AS category ON product.${ROW_PACKAGE_NAME} = category.${ROW_PACKAGE_NAME}""" } builder += """WHERE repository.${ROW_ENABLED} != 0""" - if (section is ProductItem.Section.Category) { + if (section is Section.Category) { builder += "AND category.${ROW_NAME} = ?" builder %= section.name - } else if (section is ProductItem.Section.Repository) { + } else if (section is Section.Repository) { builder += "AND product.${ROW_REPOSITORY_ID} = ?" builder %= section.id.toString() } @@ -163,9 +164,9 @@ interface ProductDao : BaseDao { } when (order) { - ProductItem.Order.NAME -> Unit - ProductItem.Order.DATE_ADDED -> builder += "product.${ROW_ADDED} DESC," - ProductItem.Order.LAST_UPDATE -> builder += "product.${ROW_UPDATED} DESC," + Order.NAME -> Unit + Order.DATE_ADDED -> builder += "product.${ROW_ADDED} DESC," + Order.LAST_UPDATE -> builder += "product.${ROW_UPDATED} DESC," }::class builder += "product.${ROW_NAME} COLLATE LOCALIZED ASC" @@ -180,7 +181,7 @@ interface ProductDao : BaseDao { // TODO optimize and simplify fun queryList( installed: Boolean, updates: Boolean, searchQuery: String, - section: ProductItem.Section, order: ProductItem.Order, numberOfItems: Int = 0 + section: Section, order: Order, numberOfItems: Int = 0 ): DataSource.Factory { val builder = QueryBuilder() @@ -221,17 +222,17 @@ interface ProductDao : BaseDao { builder += """JOIN $ROW_INSTALLED_NAME AS installed ON product.${ROW_PACKAGE_NAME} = installed.${ROW_PACKAGE_NAME}""" - if (section is ProductItem.Section.Category) { + if (section is Section.Category) { builder += """JOIN $ROW_CATEGORY_NAME AS category ON product.${ROW_PACKAGE_NAME} = category.${ROW_PACKAGE_NAME}""" } builder += """WHERE repository.${ROW_ENABLED} != 0""" - if (section is ProductItem.Section.Category) { + if (section is Section.Category) { builder += "AND category.${ROW_NAME} = ?" builder %= section.name - } else if (section is ProductItem.Section.Repository) { + } else if (section is Section.Repository) { builder += "AND product.${ROW_REPOSITORY_ID} = ?" builder %= section.id.toString() } @@ -252,9 +253,9 @@ interface ProductDao : BaseDao { } when (order) { - ProductItem.Order.NAME -> Unit - ProductItem.Order.DATE_ADDED -> builder += "product.${ROW_ADDED} DESC," - ProductItem.Order.LAST_UPDATE -> builder += "product.${ROW_UPDATED} DESC," + Order.NAME -> Unit + Order.DATE_ADDED -> builder += "product.${ROW_ADDED} DESC," + Order.LAST_UPDATE -> builder += "product.${ROW_UPDATED} DESC," }::class builder += "product.${ROW_NAME} COLLATE LOCALIZED ASC${if (numberOfItems > 0) " LIMIT $numberOfItems" else ""}" diff --git a/src/main/kotlin/com/looker/droidify/entity/Enums.kt b/src/main/kotlin/com/looker/droidify/entity/Enums.kt new file mode 100644 index 00000000..7222b733 --- /dev/null +++ b/src/main/kotlin/com/looker/droidify/entity/Enums.kt @@ -0,0 +1,9 @@ +package com.looker.droidify.entity + +import com.looker.droidify.R + +enum class Order(val titleResId: Int) { + NAME(R.string.name), + DATE_ADDED(R.string.whats_new), + LAST_UPDATE(R.string.recently_updated) +} \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/entity/ProductItem.kt b/src/main/kotlin/com/looker/droidify/entity/ProductItem.kt index e23cf514..c7aa294c 100644 --- a/src/main/kotlin/com/looker/droidify/entity/ProductItem.kt +++ b/src/main/kotlin/com/looker/droidify/entity/ProductItem.kt @@ -1,63 +1,22 @@ package com.looker.droidify.entity -import android.os.Parcel import com.fasterxml.jackson.core.JsonGenerator import com.fasterxml.jackson.core.JsonParser -import com.looker.droidify.R -import com.looker.droidify.utility.KParcelable import com.looker.droidify.utility.extension.json.forEachKey data class ProductItem( - var repositoryId: Long, var packageName: String, var name: String, var summary: String, - val icon: String, val metadataIcon: String, val version: String, var installedVersion: String, - var compatible: Boolean, var canUpdate: Boolean, var matchRank: Int, + var repositoryId: Long, + var packageName: String, + var name: String, + var summary: String, + val icon: String, + val metadataIcon: String, + val version: String, + var installedVersion: String, + var compatible: Boolean, + var canUpdate: Boolean, + var matchRank: Int, ) { - sealed class Section : KParcelable { - object All : Section() { - @Suppress("unused") - @JvmField - val CREATOR = KParcelable.creator { All } - } - - data class Category(val name: String) : Section() { - override fun writeToParcel(dest: Parcel, flags: Int) { - dest.writeString(name) - } - - companion object { - @Suppress("unused") - @JvmField - val CREATOR = KParcelable.creator { - val name = it.readString()!! - Category(name) - } - } - } - - data class Repository(val id: Long, val name: String) : Section() { - override fun writeToParcel(dest: Parcel, flags: Int) { - dest.writeLong(id) - dest.writeString(name) - } - - companion object { - @Suppress("unused") - @JvmField - val CREATOR = KParcelable.creator { - val id = it.readLong() - val name = it.readString()!! - Repository(id, name) - } - } - } - } - - enum class Order(val titleResId: Int) { - NAME(R.string.name), - DATE_ADDED(R.string.whats_new), - LAST_UPDATE(R.string.recently_updated) - } - fun serialize(generator: JsonGenerator) { generator.writeNumberField("serialVersion", 1) generator.writeNumberField("repositoryId", repositoryId) diff --git a/src/main/kotlin/com/looker/droidify/entity/Section.kt b/src/main/kotlin/com/looker/droidify/entity/Section.kt new file mode 100644 index 00000000..3292a819 --- /dev/null +++ b/src/main/kotlin/com/looker/droidify/entity/Section.kt @@ -0,0 +1,44 @@ +package com.looker.droidify.entity + +import android.os.Parcel +import com.looker.droidify.utility.KParcelable + +sealed class Section : KParcelable { + object All : Section() { + @Suppress("unused") + @JvmField + val CREATOR = KParcelable.creator { All } + } + + data class Category(val name: String) : Section() { + override fun writeToParcel(dest: Parcel, flags: Int) { + dest.writeString(name) + } + + companion object { + @Suppress("unused") + @JvmField + val CREATOR = KParcelable.creator { + val name = it.readString()!! + Category(name) + } + } + } + + data class Repository(val id: Long, val name: String) : Section() { + override fun writeToParcel(dest: Parcel, flags: Int) { + dest.writeLong(id) + dest.writeString(name) + } + + companion object { + @Suppress("unused") + @JvmField + val CREATOR = KParcelable.creator { + val id = it.readLong() + val name = it.readString()!! + Repository(id, name) + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt b/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt index 5490ce4e..44254298 100644 --- a/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt +++ b/src/main/kotlin/com/looker/droidify/screen/TabsFragment.kt @@ -23,7 +23,7 @@ import com.google.android.material.textview.MaterialTextView import com.looker.droidify.R import com.looker.droidify.content.Preferences import com.looker.droidify.databinding.TabsToolbarBinding -import com.looker.droidify.entity.ProductItem +import com.looker.droidify.entity.Section import com.looker.droidify.service.Connection import com.looker.droidify.service.SyncService import com.looker.droidify.ui.fragments.AppListFragment @@ -89,8 +89,8 @@ class TabsFragment : ScreenFragment() { } private var searchQuery = "" - private var sections = listOf(ProductItem.Section.All) - private var section: ProductItem.Section = ProductItem.Section.All + private var sections = listOf
(Section.All) + private var section: Section = Section.All private val syncConnection = Connection(SyncService::class.java, onBind = { _, _ -> viewPager?.let { @@ -193,12 +193,12 @@ class TabsFragment : ScreenFragment() { this.layout = layout showSections = savedInstanceState?.getByte(STATE_SHOW_SECTIONS)?.toInt() ?: 0 != 0 - sections = savedInstanceState?.getParcelableArrayList(STATE_SECTIONS) + sections = savedInstanceState?.getParcelableArrayList
(STATE_SECTIONS) .orEmpty() - section = savedInstanceState?.getParcelable(STATE_SECTION) ?: ProductItem.Section.All + section = savedInstanceState?.getParcelable(STATE_SECTION) ?: Section.All layout.sectionChange.setOnClickListener { showSections = sections - .any { it !is ProductItem.Section.All } && !showSections + .any { it !is Section.All } && !showSections } updateOrder() @@ -240,7 +240,7 @@ class TabsFragment : ScreenFragment() { .subscribe { setSectionsAndUpdate( it.asSequence().sorted() - .map(ProductItem.Section::Category).toList(), null + .map(Section::Category).toList(), null ) } repositoriesDisposable = Observable.just(Unit) @@ -250,7 +250,7 @@ class TabsFragment : ScreenFragment() { .observeOn(AndroidSchedulers.mainThread()) .subscribe { it -> setSectionsAndUpdate(null, it.asSequence().filter { it.enabled } - .map { ProductItem.Section.Repository(it.id, it.name) }.toList()) + .map { Section.Repository(it.id, it.name) }.toList()) } updateSection() @@ -383,19 +383,19 @@ class TabsFragment : ScreenFragment() { productFragments.forEach { it.setOrder(order) } } - private inline fun collectOldSections(list: List?): List? { + private inline fun collectOldSections(list: List?): List? { val oldList = sections.mapNotNull { it as? T } return if (list == null || oldList == list) oldList else null } private fun setSectionsAndUpdate( - categories: List?, - repositories: List?, + categories: List?, + repositories: List?, ) { val oldCategories = collectOldSections(categories) val oldRepositories = collectOldSections(repositories) if (oldCategories == null || oldRepositories == null) { - sections = listOf(ProductItem.Section.All) + + sections = listOf(Section.All) + (categories ?: oldCategories).orEmpty() + (repositories ?: oldRepositories).orEmpty() updateSection() @@ -404,15 +404,15 @@ class TabsFragment : ScreenFragment() { private fun updateSection() { if (section !in sections) { - section = ProductItem.Section.All + section = Section.All } layout?.sectionName?.text = when (val section = section) { - is ProductItem.Section.All -> getString(R.string.all_applications) - is ProductItem.Section.Category -> section.name - is ProductItem.Section.Repository -> section.name + is Section.All -> getString(R.string.all_applications) + is Section.Category -> section.name + is Section.Repository -> section.name } layout?.sectionIcon?.visibility = - if (sections.any { it !is ProductItem.Section.All }) View.VISIBLE else View.GONE + if (sections.any { it !is Section.All }) View.VISIBLE else View.GONE productFragments.forEach { it.setSection(section) } sectionsList?.adapter?.notifyDataSetChanged() } @@ -507,8 +507,8 @@ class TabsFragment : ScreenFragment() { } private class SectionsAdapter( - private val sections: () -> List, - private val onClick: (ProductItem.Section) -> Unit, + private val sections: () -> List
, + private val onClick: (Section) -> Unit, ) : StableRecyclerAdapter() { enum class ViewType { SECTION } @@ -589,9 +589,9 @@ class TabsFragment : ScreenFragment() { section.javaClass != nextSection.javaClass ) margin else 0 holder.title.text = when (section) { - is ProductItem.Section.All -> holder.itemView.resources.getString(R.string.all_applications) - is ProductItem.Section.Category -> section.name - is ProductItem.Section.Repository -> section.name + is Section.All -> holder.itemView.resources.getString(R.string.all_applications) + is Section.Category -> section.name + is Section.Repository -> section.name } } } diff --git a/src/main/kotlin/com/looker/droidify/service/SyncService.kt b/src/main/kotlin/com/looker/droidify/service/SyncService.kt index 5c27f235..6aac0ab0 100644 --- a/src/main/kotlin/com/looker/droidify/service/SyncService.kt +++ b/src/main/kotlin/com/looker/droidify/service/SyncService.kt @@ -16,7 +16,9 @@ import com.looker.droidify.* import com.looker.droidify.content.Preferences import com.looker.droidify.database.DatabaseX import com.looker.droidify.database.entity.Repository +import com.looker.droidify.entity.Order import com.looker.droidify.entity.ProductItem +import com.looker.droidify.entity.Section import com.looker.droidify.index.RepositoryUpdater import com.looker.droidify.utility.RxUtils import com.looker.droidify.utility.Utils @@ -391,8 +393,8 @@ class SyncService : ConnectionService() { installed = true, updates = true, searchQuery = "", - section = ProductItem.Section.All, - order = ProductItem.Order.NAME, + section = Section.All, + order = Order.NAME, signal = it ) .use { diff --git a/src/main/kotlin/com/looker/droidify/ui/fragments/AppListFragment.kt b/src/main/kotlin/com/looker/droidify/ui/fragments/AppListFragment.kt index c8f25920..1e2b6320 100644 --- a/src/main/kotlin/com/looker/droidify/ui/fragments/AppListFragment.kt +++ b/src/main/kotlin/com/looker/droidify/ui/fragments/AppListFragment.kt @@ -13,7 +13,8 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.looker.droidify.R import com.looker.droidify.database.CursorOwner -import com.looker.droidify.entity.ProductItem +import com.looker.droidify.entity.Order +import com.looker.droidify.entity.Section import com.looker.droidify.screen.BaseFragment import com.looker.droidify.ui.adapters.AppListAdapter import com.looker.droidify.ui.viewmodels.AppListViewModel @@ -127,7 +128,7 @@ class AppListFragment() : BaseFragment(), CursorOwner.Callback { } } - internal fun setSection(section: ProductItem.Section) { + internal fun setSection(section: Section) { viewModel.setSection(section) { if (view != null) { screenActivity.cursorOwner.attach(this, viewModel.request(source)) @@ -135,7 +136,7 @@ class AppListFragment() : BaseFragment(), CursorOwner.Callback { } } - internal fun setOrder(order: ProductItem.Order) { + internal fun setOrder(order: Order) { viewModel.setOrder(order) { if (view != null) { screenActivity.cursorOwner.attach(this, viewModel.request(source)) diff --git a/src/main/kotlin/com/looker/droidify/ui/fragments/MainNavFragmentX.kt b/src/main/kotlin/com/looker/droidify/ui/fragments/MainNavFragmentX.kt index 1372d64f..9719ee28 100644 --- a/src/main/kotlin/com/looker/droidify/ui/fragments/MainNavFragmentX.kt +++ b/src/main/kotlin/com/looker/droidify/ui/fragments/MainNavFragmentX.kt @@ -3,7 +3,8 @@ package com.looker.droidify.ui.fragments import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import com.looker.droidify.R -import com.looker.droidify.entity.ProductItem +import com.looker.droidify.entity.Order +import com.looker.droidify.entity.Section import com.looker.droidify.ui.activities.MainActivityX import com.looker.droidify.ui.viewmodels.MainNavFragmentViewModelX @@ -26,7 +27,7 @@ abstract class MainNavFragmentX : BaseNavFragment() { } } - internal fun setSection(section: ProductItem.Section) { + internal fun setSection(section: Section) { viewModel.setSection(section) { if (view != null) { //viewModel.fillList(source) @@ -34,7 +35,7 @@ abstract class MainNavFragmentX : BaseNavFragment() { } } - internal fun setOrder(order: ProductItem.Order) { + internal fun setOrder(order: Order) { viewModel.setOrder(order) { if (view != null) { //viewModel.fillList(source) @@ -65,13 +66,13 @@ sealed class Request { internal abstract val installed: Boolean internal abstract val updates: Boolean internal abstract val searchQuery: String - internal abstract val section: ProductItem.Section - internal abstract val order: ProductItem.Order + internal abstract val section: Section + internal abstract val order: Order internal open val numberOfItems: Int = 0 data class ProductsAll( - override val searchQuery: String, override val section: ProductItem.Section, - override val order: ProductItem.Order, + override val searchQuery: String, override val section: Section, + override val order: Order, ) : Request() { override val id: Int get() = 1 @@ -82,8 +83,8 @@ sealed class Request { } data class ProductsInstalled( - override val searchQuery: String, override val section: ProductItem.Section, - override val order: ProductItem.Order, + override val searchQuery: String, override val section: Section, + override val order: Order, ) : Request() { override val id: Int get() = 2 @@ -94,8 +95,8 @@ sealed class Request { } data class ProductsUpdates( - override val searchQuery: String, override val section: ProductItem.Section, - override val order: ProductItem.Order, + override val searchQuery: String, override val section: Section, + override val order: Order, ) : Request() { override val id: Int get() = 3 @@ -106,8 +107,8 @@ sealed class Request { } data class ProductsUpdated( - override val searchQuery: String, override val section: ProductItem.Section, - override val order: ProductItem.Order, override val numberOfItems: Int, + override val searchQuery: String, override val section: Section, + override val order: Order, override val numberOfItems: Int, ) : Request() { override val id: Int get() = 4 @@ -118,8 +119,8 @@ sealed class Request { } data class ProductsNew( - override val searchQuery: String, override val section: ProductItem.Section, - override val order: ProductItem.Order, override val numberOfItems: Int, + override val searchQuery: String, override val section: Section, + override val order: Order, override val numberOfItems: Int, ) : Request() { override val id: Int get() = 5 diff --git a/src/main/kotlin/com/looker/droidify/ui/viewmodels/AppListViewModel.kt b/src/main/kotlin/com/looker/droidify/ui/viewmodels/AppListViewModel.kt index 11c2956b..2c13442c 100644 --- a/src/main/kotlin/com/looker/droidify/ui/viewmodels/AppListViewModel.kt +++ b/src/main/kotlin/com/looker/droidify/ui/viewmodels/AppListViewModel.kt @@ -4,7 +4,8 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.looker.droidify.content.Preferences import com.looker.droidify.database.CursorOwner -import com.looker.droidify.entity.ProductItem +import com.looker.droidify.entity.Order +import com.looker.droidify.entity.Section import com.looker.droidify.ui.fragments.AppListFragment import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow @@ -16,17 +17,17 @@ import kotlinx.coroutines.launch class AppListViewModel : ViewModel() { private val _order = MutableStateFlow(Preferences[Preferences.Key.SortOrder].order) - private val _sections = MutableStateFlow(ProductItem.Section.All) + private val _sections = MutableStateFlow
(Section.All) private val _searchQuery = MutableStateFlow("") - val order: StateFlow = _order.stateIn( - initialValue = ProductItem.Order.LAST_UPDATE, + val order: StateFlow = _order.stateIn( + initialValue = Order.LAST_UPDATE, scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000) ) - val sections: StateFlow = _sections.stateIn( - initialValue = ProductItem.Section.All, + val sections: StateFlow
= _sections.stateIn( + initialValue = Section.All, scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000) ) @@ -38,8 +39,8 @@ class AppListViewModel : ViewModel() { fun request(source: AppListFragment.Source): CursorOwner.Request { var mSearchQuery = "" - var mSections: ProductItem.Section = ProductItem.Section.All - var mOrder: ProductItem.Order = ProductItem.Order.NAME + var mSections: Section = Section.All + var mOrder: Order = Order.NAME viewModelScope.launch { launch { searchQuery.collect { if (source.sections) mSearchQuery = it } } launch { sections.collect { if (source.sections) mSections = it } } @@ -64,7 +65,7 @@ class AppListViewModel : ViewModel() { } } - fun setSection(newSection: ProductItem.Section, perform: () -> Unit) { + fun setSection(newSection: Section, perform: () -> Unit) { viewModelScope.launch { if (newSection != sections.value) { _sections.emit(newSection) @@ -73,7 +74,7 @@ class AppListViewModel : ViewModel() { } } - fun setOrder(newOrder: ProductItem.Order, perform: () -> Unit) { + fun setOrder(newOrder: Order, perform: () -> Unit) { viewModelScope.launch { if (newOrder != order.value) { _order.emit(newOrder) diff --git a/src/main/kotlin/com/looker/droidify/ui/viewmodels/MainNavFragmentViewModelX.kt b/src/main/kotlin/com/looker/droidify/ui/viewmodels/MainNavFragmentViewModelX.kt index f7f03c73..92175249 100644 --- a/src/main/kotlin/com/looker/droidify/ui/viewmodels/MainNavFragmentViewModelX.kt +++ b/src/main/kotlin/com/looker/droidify/ui/viewmodels/MainNavFragmentViewModelX.kt @@ -10,7 +10,8 @@ import androidx.paging.PagedList import com.looker.droidify.content.Preferences import com.looker.droidify.database.DatabaseX import com.looker.droidify.database.entity.Product -import com.looker.droidify.entity.ProductItem +import com.looker.droidify.entity.Order +import com.looker.droidify.entity.Section import com.looker.droidify.ui.fragments.Request import com.looker.droidify.ui.fragments.Source import kotlinx.coroutines.Dispatchers @@ -24,18 +25,18 @@ import kotlinx.coroutines.withContext class MainNavFragmentViewModelX(val db: DatabaseX, primarySource: Source, secondarySource: Source) : ViewModel() { - private val _order = MutableStateFlow(ProductItem.Order.LAST_UPDATE) - private val _sections = MutableStateFlow(ProductItem.Section.All) + private val _order = MutableStateFlow(Order.LAST_UPDATE) + private val _sections = MutableStateFlow
(Section.All) private val _searchQuery = MutableStateFlow("") - val order: StateFlow = _order.stateIn( - initialValue = ProductItem.Order.LAST_UPDATE, + val order: StateFlow = _order.stateIn( + initialValue = Order.LAST_UPDATE, scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000) ) - val sections: StateFlow = _sections.stateIn( - initialValue = ProductItem.Section.All, + val sections: StateFlow
= _sections.stateIn( + initialValue = Section.All, scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000) ) @@ -47,8 +48,8 @@ class MainNavFragmentViewModelX(val db: DatabaseX, primarySource: Source, second fun request(source: Source): Request { var mSearchQuery = "" - var mSections: ProductItem.Section = ProductItem.Section.All - var mOrder: ProductItem.Order = ProductItem.Order.NAME + var mSections: Section = Section.All + var mOrder: Order = Order.NAME viewModelScope.launch { launch { searchQuery.collect { if (source.sections) mSearchQuery = it } } launch { sections.collect { if (source.sections) mSections = it } } @@ -74,13 +75,13 @@ class MainNavFragmentViewModelX(val db: DatabaseX, primarySource: Source, second Source.UPDATED -> Request.ProductsUpdated( mSearchQuery, mSections, - ProductItem.Order.LAST_UPDATE, + Order.LAST_UPDATE, Preferences[Preferences.Key.UpdatedApps] ) Source.NEW -> Request.ProductsNew( mSearchQuery, mSections, - ProductItem.Order.LAST_UPDATE, + Order.LAST_UPDATE, Preferences[Preferences.Key.NewApps] ) } @@ -140,7 +141,7 @@ class MainNavFragmentViewModelX(val db: DatabaseX, primarySource: Source, second } } - fun setSection(newSection: ProductItem.Section, perform: () -> Unit) { + fun setSection(newSection: Section, perform: () -> Unit) { viewModelScope.launch { if (newSection != sections.value) { _sections.emit(newSection) @@ -149,7 +150,7 @@ class MainNavFragmentViewModelX(val db: DatabaseX, primarySource: Source, second } } - fun setOrder(newOrder: ProductItem.Order, perform: () -> Unit) { + fun setOrder(newOrder: Order, perform: () -> Unit) { viewModelScope.launch { if (newOrder != order.value) { _order.emit(newOrder)