Stash categories + code improvements
This commit is contained in:
committed by
Aayush Gupta
parent
8a12ffcd96
commit
c16ae15540
@@ -1,9 +1,11 @@
|
||||
package com.aurora.store
|
||||
|
||||
import com.aurora.gplayapi.data.models.Category
|
||||
import com.aurora.gplayapi.data.models.StreamBundle
|
||||
import com.aurora.gplayapi.data.models.StreamCluster
|
||||
import com.aurora.gplayapi.helpers.contracts.StreamContract
|
||||
import com.aurora.gplayapi.helpers.contracts.TopChartsContract
|
||||
|
||||
typealias TopChartStash = Map<TopChartsContract.Type, Map<TopChartsContract.Chart, StreamCluster>>
|
||||
typealias HomeStash = MutableMap<StreamContract.Category, StreamBundle>
|
||||
typealias TopChartStash = MutableMap<TopChartsContract.Type, MutableMap<TopChartsContract.Chart, StreamCluster>>
|
||||
typealias HomeStash = MutableMap<StreamContract.Category, StreamBundle>
|
||||
typealias CategoryStash = MutableMap<Category.Type, List<Category>>
|
||||
@@ -24,9 +24,13 @@ import android.view.View
|
||||
import androidx.fragment.app.viewModels
|
||||
import com.aurora.Constants
|
||||
import com.aurora.gplayapi.data.models.Category
|
||||
import com.aurora.store.CategoryStash
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.ViewState
|
||||
import com.aurora.store.data.ViewState.Empty.getDataAs
|
||||
import com.aurora.store.databinding.FragmentGenericRecyclerBinding
|
||||
import com.aurora.store.view.epoxy.views.CategoryViewModel_
|
||||
import com.aurora.store.view.epoxy.views.shimmer.AppListViewShimmerModel_
|
||||
import com.aurora.store.viewmodel.category.CategoryViewModel
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
@@ -64,10 +68,37 @@ class CategoryFragment : BaseFragment(R.layout.fragment_generic_recycler) {
|
||||
1 -> viewModel.getCategoryList(Category.Type.GAME)
|
||||
}
|
||||
|
||||
viewModel.liveData.observe(viewLifecycleOwner) { categoryList ->
|
||||
binding.recycler.withModels {
|
||||
val categoryType = when (pageType) {
|
||||
1 -> Category.Type.GAME
|
||||
else -> Category.Type.APPLICATION
|
||||
}
|
||||
|
||||
viewModel.liveData.observe(viewLifecycleOwner) {
|
||||
when (it) {
|
||||
is ViewState.Success<*> -> {
|
||||
val stash = it.getDataAs<CategoryStash>()
|
||||
updateController(stash[categoryType])
|
||||
}
|
||||
|
||||
else -> {
|
||||
updateController(emptyList())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateController(categories: List<Category>?) {
|
||||
binding.recycler.withModels {
|
||||
if (categories == null) {
|
||||
for (i in 1..10) {
|
||||
add(
|
||||
AppListViewShimmerModel_()
|
||||
.id(i)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
setFilterDuplicates(true)
|
||||
categoryList.forEach {
|
||||
categories.forEach {
|
||||
add(
|
||||
CategoryViewModel_()
|
||||
.id(it.title)
|
||||
|
||||
@@ -29,13 +29,15 @@ import com.aurora.gplayapi.data.models.Category
|
||||
import com.aurora.gplayapi.helpers.CategoryHelper
|
||||
import com.aurora.gplayapi.helpers.contracts.CategoryContract
|
||||
import com.aurora.gplayapi.helpers.web.WebCategoryHelper
|
||||
import com.aurora.store.CategoryStash
|
||||
import com.aurora.store.data.ViewState
|
||||
import com.aurora.store.data.network.HttpClient
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@SuppressLint("StaticFieldLeak") // false positive, see https://github.com/google/dagger/issues/3253
|
||||
@@ -43,7 +45,6 @@ class CategoryViewModel @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
private val authProvider: AuthProvider
|
||||
) : ViewModel() {
|
||||
|
||||
private val TAG = CategoryViewModel::class.java.simpleName
|
||||
|
||||
private val categoryHelper: CategoryHelper = CategoryHelper(authProvider.authData)
|
||||
@@ -52,22 +53,41 @@ class CategoryViewModel @Inject constructor(
|
||||
private val webCategoryHelper: CategoryContract = WebCategoryHelper()
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
|
||||
val liveData: MutableLiveData<List<Category>> = MutableLiveData()
|
||||
private var stash: CategoryStash = mutableMapOf(
|
||||
Category.Type.APPLICATION to emptyList(),
|
||||
Category.Type.GAME to emptyList()
|
||||
)
|
||||
|
||||
val liveData = MutableLiveData<ViewState>()
|
||||
|
||||
private fun contract(): CategoryContract {
|
||||
return if(authProvider.isAnonymous){
|
||||
return if (authProvider.isAnonymous) {
|
||||
webCategoryHelper
|
||||
} else {
|
||||
categoryHelper
|
||||
}
|
||||
}
|
||||
|
||||
fun getCategoryList(type: Category.Type) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val categories = getCategories(type)
|
||||
|
||||
if (categories.isNotEmpty()) {
|
||||
liveData.postValue(ViewState.Success(stash))
|
||||
}
|
||||
|
||||
try {
|
||||
liveData.postValue(contract().getAllCategoriesList(type))
|
||||
stash[type] = contract().getAllCategoriesList(type)
|
||||
liveData.postValue(ViewState.Success(stash))
|
||||
} catch (exception: Exception) {
|
||||
Log.e(TAG, "Failed fetching list of categories", exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getCategories(type: Category.Type): List<Category> {
|
||||
return stash.getOrPut(type) {
|
||||
mutableListOf()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,10 +56,7 @@ class StreamViewModel @Inject constructor(
|
||||
|
||||
val liveData: MutableLiveData<ViewState> = MutableLiveData()
|
||||
|
||||
private var stash: HomeStash = mutableMapOf(
|
||||
StreamContract.Category.APPLICATION to StreamBundle(),
|
||||
StreamContract.Category.GAME to StreamBundle()
|
||||
)
|
||||
private var stash: HomeStash = mutableMapOf()
|
||||
|
||||
fun contract(): StreamContract {
|
||||
return if (authProvider.isAnonymous) {
|
||||
@@ -145,8 +142,9 @@ class StreamViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private fun targetBundle(category: StreamContract.Category): StreamBundle {
|
||||
// stash is initialized with empty StreamBundle so this will never return null or throw an exception
|
||||
val streamBundle = stash.getValue(category)
|
||||
val streamBundle = stash.getOrPut(category){
|
||||
StreamBundle()
|
||||
}
|
||||
|
||||
return streamBundle
|
||||
}
|
||||
|
||||
@@ -52,17 +52,7 @@ class TopChartViewModel @Inject constructor(
|
||||
private val webTopChartsHelper: TopChartsContract = WebTopChartsHelper()
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
|
||||
private val dummyChart = mapOf(
|
||||
TopChartsContract.Chart.TOP_GROSSING to StreamCluster(),
|
||||
TopChartsContract.Chart.TOP_SELLING_FREE to StreamCluster(),
|
||||
TopChartsContract.Chart.TOP_SELLING_PAID to StreamCluster(),
|
||||
TopChartsContract.Chart.MOVERS_SHAKERS to StreamCluster(),
|
||||
)
|
||||
|
||||
private var stash: TopChartStash = mapOf(
|
||||
TopChartsContract.Type.APPLICATION to dummyChart,
|
||||
TopChartsContract.Type.GAME to dummyChart
|
||||
)
|
||||
private var stash: TopChartStash = mutableMapOf()
|
||||
|
||||
val liveData: MutableLiveData<ViewState> = MutableLiveData()
|
||||
|
||||
@@ -124,8 +114,9 @@ class TopChartViewModel @Inject constructor(
|
||||
type: TopChartsContract.Type,
|
||||
chart: TopChartsContract.Chart
|
||||
): StreamCluster {
|
||||
// Stash is initialized with empty clusters so this will never return null or throw an exception
|
||||
val cluster = stash.getValue(type).getValue(chart)
|
||||
val cluster = stash
|
||||
.getOrPut(type) { mutableMapOf() }
|
||||
.getOrPut(chart) { StreamCluster() }
|
||||
return cluster
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user