diff --git a/app/src/main/java/com/aurora/store/Aliases.kt b/app/src/main/java/com/aurora/store/Aliases.kt index a99365f26..7b2929f9f 100644 --- a/app/src/main/java/com/aurora/store/Aliases.kt +++ b/app/src/main/java/com/aurora/store/Aliases.kt @@ -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> -typealias HomeStash = MutableMap \ No newline at end of file +typealias TopChartStash = MutableMap> +typealias HomeStash = MutableMap +typealias CategoryStash = MutableMap> \ No newline at end of file diff --git a/app/src/main/java/com/aurora/store/view/ui/commons/CategoryFragment.kt b/app/src/main/java/com/aurora/store/view/ui/commons/CategoryFragment.kt index 971d1ec2c..b2032fe52 100644 --- a/app/src/main/java/com/aurora/store/view/ui/commons/CategoryFragment.kt +++ b/app/src/main/java/com/aurora/store/view/ui/commons/CategoryFragment.kt @@ -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() + updateController(stash[categoryType]) + } + + else -> { + updateController(emptyList()) + } + } + } + } + + fun updateController(categories: List?) { + 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) diff --git a/app/src/main/java/com/aurora/store/viewmodel/category/CategoryViewModel.kt b/app/src/main/java/com/aurora/store/viewmodel/category/CategoryViewModel.kt index 976d32f1f..f78f8d5c1 100644 --- a/app/src/main/java/com/aurora/store/viewmodel/category/CategoryViewModel.kt +++ b/app/src/main/java/com/aurora/store/viewmodel/category/CategoryViewModel.kt @@ -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> = MutableLiveData() + private var stash: CategoryStash = mutableMapOf( + Category.Type.APPLICATION to emptyList(), + Category.Type.GAME to emptyList() + ) + + val liveData = MutableLiveData() 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 { + return stash.getOrPut(type) { + mutableListOf() + } + } } diff --git a/app/src/main/java/com/aurora/store/viewmodel/homestream/StreamViewModel.kt b/app/src/main/java/com/aurora/store/viewmodel/homestream/StreamViewModel.kt index a67d3f6dd..1c2a544cc 100644 --- a/app/src/main/java/com/aurora/store/viewmodel/homestream/StreamViewModel.kt +++ b/app/src/main/java/com/aurora/store/viewmodel/homestream/StreamViewModel.kt @@ -56,10 +56,7 @@ class StreamViewModel @Inject constructor( val liveData: MutableLiveData = 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 } diff --git a/app/src/main/java/com/aurora/store/viewmodel/topchart/TopChartViewModel.kt b/app/src/main/java/com/aurora/store/viewmodel/topchart/TopChartViewModel.kt index 1d84dbf16..6ed059412 100644 --- a/app/src/main/java/com/aurora/store/viewmodel/topchart/TopChartViewModel.kt +++ b/app/src/main/java/com/aurora/store/viewmodel/topchart/TopChartViewModel.kt @@ -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 = 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 } }