Add: Second products' source to MainNavFragments

This commit is contained in:
machiav3lli 2022-01-25 00:38:38 +01:00
parent d3283cc6e4
commit d64d670173
7 changed files with 57 additions and 26 deletions

View File

@ -218,7 +218,7 @@ class MainActivityX : AppCompatActivity() {
private fun updateUpdateNotificationBlocker(activeSource: Source) { private fun updateUpdateNotificationBlocker(activeSource: Source) {
val blockerFragment = if (activeSource == Source.UPDATES) { val blockerFragment = if (activeSource == Source.UPDATES) {
supportFragmentManager.fragments.asSequence().mapNotNull { it as? MainNavFragmentX } supportFragmentManager.fragments.asSequence().mapNotNull { it as? MainNavFragmentX }
.find { it.source == activeSource } .find { it.primarySource == activeSource }
} else { } else {
null null
} }

View File

@ -171,7 +171,7 @@ class PrefsActivityX : AppCompatActivity() {
private fun updateUpdateNotificationBlocker(activeSource: Source) { private fun updateUpdateNotificationBlocker(activeSource: Source) {
val blockerFragment = if (activeSource == Source.UPDATES) { val blockerFragment = if (activeSource == Source.UPDATES) {
supportFragmentManager.fragments.asSequence().mapNotNull { it as? MainNavFragmentX } supportFragmentManager.fragments.asSequence().mapNotNull { it as? MainNavFragmentX }
.find { it.source == activeSource } .find { it.primarySource == activeSource }
} else { } else {
null null
} }

View File

@ -23,7 +23,8 @@ class ExploreFragment : MainNavFragmentX() {
private lateinit var appsItemAdapter: PagedModelAdapter<Product, VAppItem> private lateinit var appsItemAdapter: PagedModelAdapter<Product, VAppItem>
private var appsFastAdapter: FastAdapter<VAppItem>? = null private var appsFastAdapter: FastAdapter<VAppItem>? = null
override val source = Source.AVAILABLE override val primarySource = Source.AVAILABLE
override val secondarySource = Source.AVAILABLE
private var repositories: Map<Long, Repository> = mapOf() private var repositories: Map<Long, Repository> = mapOf()
@ -62,7 +63,7 @@ class ExploreFragment : MainNavFragmentX() {
} }
override fun setupLayout() { override fun setupLayout() {
viewModel.productsList.observe(viewLifecycleOwner) { viewModel.primaryProducts.observe(viewLifecycleOwner) {
appsItemAdapter.submitList(it) appsItemAdapter.submitList(it)
appsFastAdapter?.notifyDataSetChanged() appsFastAdapter?.notifyDataSetChanged()
} }

View File

@ -27,7 +27,8 @@ class InstalledFragment : MainNavFragmentX() {
private lateinit var updatedItemAdapter: PagedModelAdapter<Product, HAppItem> private lateinit var updatedItemAdapter: PagedModelAdapter<Product, HAppItem>
private var updatedFastAdapter: FastAdapter<HAppItem>? = null private var updatedFastAdapter: FastAdapter<HAppItem>? = null
override val source = Source.INSTALLED override val primarySource = Source.INSTALLED
override val secondarySource = Source.UPDATES
private var repositories: Map<Long, Repository> = mapOf() private var repositories: Map<Long, Repository> = mapOf()
@ -81,11 +82,12 @@ class InstalledFragment : MainNavFragmentX() {
} }
} }
viewModel.productsList.observe(viewLifecycleOwner) { viewModel.primaryProducts.observe(viewLifecycleOwner) {
binding.updatedBar.visibility =
if (it.any { item -> item.data_item?.canUpdate == true }) View.VISIBLE else View.GONE
updatedItemAdapter.submitList(it)
installedItemAdapter.submitList(it) installedItemAdapter.submitList(it)
} }
viewModel.secondaryProducts.observe(viewLifecycleOwner) {
binding.updatedBar.visibility = if (it.isNotEmpty()) View.VISIBLE else View.GONE
updatedItemAdapter.submitList(it)
}
} }
} }

View File

@ -28,7 +28,8 @@ class LatestFragment : MainNavFragmentX() {
private var newFastAdapter: FastAdapter<HAppItem>? = null private var newFastAdapter: FastAdapter<HAppItem>? = null
// TODO replace the source with one that get a certain amount of updated apps // TODO replace the source with one that get a certain amount of updated apps
override val source = Source.UPDATED override val primarySource = Source.UPDATED
override val secondarySource = Source.NEW
private var repositories: Map<Long, Repository> = mapOf() private var repositories: Map<Long, Repository> = mapOf()
@ -76,9 +77,11 @@ class LatestFragment : MainNavFragmentX() {
} }
override fun setupLayout() { override fun setupLayout() {
viewModel.productsList.observe(requireActivity()) { viewModel.primaryProducts.observe(viewLifecycleOwner) {
newItemAdapter.submitList(it)
updatedItemAdapter.submitList(it) updatedItemAdapter.submitList(it)
} }
viewModel.secondaryProducts.observe(viewLifecycleOwner) {
newItemAdapter.submitList(it)
}
} }
} }

View File

@ -1,6 +1,8 @@
package com.looker.droidify.ui.fragments package com.looker.droidify.ui.fragments
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import com.looker.droidify.R
import com.looker.droidify.entity.ProductItem import com.looker.droidify.entity.ProductItem
import com.looker.droidify.ui.activities.MainActivityX import com.looker.droidify.ui.activities.MainActivityX
import com.looker.droidify.ui.viewmodels.MainNavFragmentViewModelX import com.looker.droidify.ui.viewmodels.MainNavFragmentViewModelX
@ -9,9 +11,10 @@ abstract class MainNavFragmentX : BaseNavFragment() {
private val mainActivityX: MainActivityX private val mainActivityX: MainActivityX
get() = requireActivity() as MainActivityX get() = requireActivity() as MainActivityX
val viewModel: MainNavFragmentViewModelX by viewModels { val viewModel: MainNavFragmentViewModelX by viewModels {
MainNavFragmentViewModelX.Factory(mainActivityX.db, source) MainNavFragmentViewModelX.Factory(mainActivityX.db, primarySource, secondarySource)
} }
abstract val source: Source abstract val primarySource: Source
abstract val secondarySource: Source
open fun onBackPressed(): Boolean = false open fun onBackPressed(): Boolean = false

View File

@ -14,11 +14,15 @@ import com.looker.droidify.entity.ProductItem
import com.looker.droidify.ui.fragments.Request import com.looker.droidify.ui.fragments.Request
import com.looker.droidify.ui.fragments.Source import com.looker.droidify.ui.fragments.Source
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class MainNavFragmentViewModelX(val db: DatabaseX, source: Source) : ViewModel() { class MainNavFragmentViewModelX(val db: DatabaseX, primarySource: Source, secondarySource: Source) :
ViewModel() {
private val _order = MutableStateFlow(ProductItem.Order.LAST_UPDATE) private val _order = MutableStateFlow(ProductItem.Order.LAST_UPDATE)
private val _sections = MutableStateFlow<ProductItem.Section>(ProductItem.Section.All) private val _sections = MutableStateFlow<ProductItem.Section>(ProductItem.Section.All)
@ -88,16 +92,29 @@ class MainNavFragmentViewModelX(val db: DatabaseX, source: Source) : ViewModel()
.setEnablePlaceholders(false) .setEnablePlaceholders(false)
.build() .build()
} }
private val request = request(source) private val primaryRequest = request(primarySource)
val productsList: LiveData<PagedList<Product>> by lazy { val primaryProducts: LiveData<PagedList<Product>> by lazy {
LivePagedListBuilder( LivePagedListBuilder(
db.productDao.queryList( db.productDao.queryList(
installed = request.installed, installed = primaryRequest.installed,
updates = request.updates, updates = primaryRequest.updates,
searchQuery = request.searchQuery, searchQuery = primaryRequest.searchQuery,
section = request.section, section = primaryRequest.section,
order = request.order, order = primaryRequest.order,
numberOfItems = request.numberOfItems numberOfItems = primaryRequest.numberOfItems
), pagedListConfig
).build()
}
private val secondaryRequest = request(secondarySource)
val secondaryProducts: LiveData<PagedList<Product>> by lazy {
LivePagedListBuilder(
db.productDao.queryList(
installed = secondaryRequest.installed,
updates = secondaryRequest.updates,
searchQuery = secondaryRequest.searchQuery,
section = secondaryRequest.section,
order = secondaryRequest.order,
numberOfItems = secondaryRequest.numberOfItems
), pagedListConfig ), pagedListConfig
).build() ).build()
} }
@ -149,11 +166,16 @@ class MainNavFragmentViewModelX(val db: DatabaseX, source: Source) : ViewModel()
} }
} }
class Factory(val db: DatabaseX, val source: Source) : ViewModelProvider.Factory { class Factory(
val db: DatabaseX,
private val primarySource: Source,
private val secondarySource: Source
) :
ViewModelProvider.Factory {
@Suppress("unchecked_cast") @Suppress("unchecked_cast")
override fun <T : ViewModel?> create(modelClass: Class<T>): T { override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(MainNavFragmentViewModelX::class.java)) { if (modelClass.isAssignableFrom(MainNavFragmentViewModelX::class.java)) {
return MainNavFragmentViewModelX(db, source) as T return MainNavFragmentViewModelX(db, primarySource, secondarySource) as T
} }
throw IllegalArgumentException("Unknown ViewModel class") throw IllegalArgumentException("Unknown ViewModel class")
} }