From 7917163a7c45635bc14e8731910c5bef027b1434 Mon Sep 17 00:00:00 2001 From: Rahul Patel Date: Sat, 13 Jul 2024 04:02:16 +0530 Subject: [PATCH] Use WebCategoryStream helper to fetch bundles as default --- .../view/epoxy/groups/CarouselModelGroup.kt | 2 +- .../view/ui/commons/CategoryBrowseFragment.kt | 11 +++-- .../store/view/ui/commons/ForYouFragment.kt | 15 ++----- .../viewmodel/category/CategoryViewModel.kt | 13 ++---- .../viewmodel/homestream/StreamViewModel.kt | 15 +++---- ...iewModel.kt => CategoryStreamViewModel.kt} | 41 +++++++++++-------- gradle/libs.versions.toml | 2 +- 7 files changed, 44 insertions(+), 55 deletions(-) rename app/src/main/java/com/aurora/store/viewmodel/subcategory/{SubCategoryClusterViewModel.kt => CategoryStreamViewModel.kt} (80%) diff --git a/app/src/main/java/com/aurora/store/view/epoxy/groups/CarouselModelGroup.kt b/app/src/main/java/com/aurora/store/view/epoxy/groups/CarouselModelGroup.kt index 1e3675fc2..4dc471284 100644 --- a/app/src/main/java/com/aurora/store/view/epoxy/groups/CarouselModelGroup.kt +++ b/app/src/main/java/com/aurora/store/view/epoxy/groups/CarouselModelGroup.kt @@ -47,7 +47,7 @@ class CarouselModelGroup( val models = ArrayList>() val clusterViewModels = mutableListOf>() - val idPrefix = streamCluster.id + val idPrefix = streamCluster.clusterTitle.hashCode() models.add( HeaderViewModel_() diff --git a/app/src/main/java/com/aurora/store/view/ui/commons/CategoryBrowseFragment.kt b/app/src/main/java/com/aurora/store/view/ui/commons/CategoryBrowseFragment.kt index d54f0baa9..d053ae94c 100644 --- a/app/src/main/java/com/aurora/store/view/ui/commons/CategoryBrowseFragment.kt +++ b/app/src/main/java/com/aurora/store/view/ui/commons/CategoryBrowseFragment.kt @@ -28,20 +28,21 @@ import com.aurora.gplayapi.data.models.App 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.utils.CategoryUtil import com.aurora.store.data.model.ViewState import com.aurora.store.data.model.ViewState.Loading.getDataAs import com.aurora.store.databinding.FragmentGenericWithToolbarBinding import com.aurora.store.view.custom.recycler.EndlessRecyclerOnScrollListener import com.aurora.store.view.epoxy.controller.CategoryCarouselController import com.aurora.store.view.epoxy.controller.GenericCarouselController -import com.aurora.store.viewmodel.subcategory.SubCategoryClusterViewModel +import com.aurora.store.viewmodel.subcategory.CategoryStreamViewModel import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint class CategoryBrowseFragment : BaseFragment(), GenericCarouselController.Callbacks { private val args: CategoryBrowseFragmentArgs by navArgs() - private val viewModel: SubCategoryClusterViewModel by activityViewModels() + private val viewModel: CategoryStreamViewModel by activityViewModels() private lateinit var category: StreamContract.Category private var streamBundle: StreamBundle? = StreamBundle() @@ -49,8 +50,7 @@ class CategoryBrowseFragment : BaseFragment() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - val rawCategory = args.browseUrl.split("/").last() - category = StreamContract.Category.NONE.apply { value = rawCategory } + category = CategoryUtil.getCategoryFromUrl(args.browseUrl) val genericCarouselController = CategoryCarouselController(this) @@ -68,6 +68,7 @@ class CategoryBrowseFragment : BaseFragment() } }) + viewModel.getStreamBundle(category) viewModel.liveData.observe(viewLifecycleOwner) { when (it) { is ViewState.Loading -> { @@ -84,8 +85,6 @@ class CategoryBrowseFragment : BaseFragment() else -> {} } } - - viewModel.observe(category) } override fun onHeaderClicked(streamCluster: StreamCluster) { diff --git a/app/src/main/java/com/aurora/store/view/ui/commons/ForYouFragment.kt b/app/src/main/java/com/aurora/store/view/ui/commons/ForYouFragment.kt index efdbe2c04..316c91e14 100644 --- a/app/src/main/java/com/aurora/store/view/ui/commons/ForYouFragment.kt +++ b/app/src/main/java/com/aurora/store/view/ui/commons/ForYouFragment.kt @@ -67,27 +67,18 @@ class ForYouFragment : BaseFragment(), pageType = bundle.getInt(Constants.PAGE_TYPE, 0) } - when (pageType) { - 0 -> { - category = Category.APPLICATION - viewModel.getStreamBundle(category, Type.HOME) - } - - 1 -> { - category = Category.GAME - viewModel.getStreamBundle(category, Type.HOME) - } - } + category = if (pageType == 0) Category.APPLICATION else Category.GAME binding.recycler.setController(genericCarouselController) binding.recycler.addOnScrollListener( - object : EndlessRecyclerOnScrollListener() { + object : EndlessRecyclerOnScrollListener(visibleThreshold = 4) { override fun onLoadMore(currentPage: Int) { viewModel.observe(category, Type.HOME) } } ) + viewModel.getStreamBundle(category, Type.HOME) viewModel.liveData.observe(viewLifecycleOwner) { when (it) { is ViewState.Loading -> { 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 bde951601..f654e2774 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 @@ -28,7 +28,6 @@ import androidx.lifecycle.viewModelScope 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.model.ViewState import com.aurora.store.data.network.HttpClient @@ -50,9 +49,6 @@ class CategoryViewModel @Inject constructor( private val categoryHelper: CategoryHelper = CategoryHelper(authProvider.authData) .using(HttpClient.getPreferredClient(context)) - private val webCategoryHelper: CategoryContract = WebCategoryHelper() - .using(HttpClient.getPreferredClient(context)) - private var stash: CategoryStash = mutableMapOf( Category.Type.APPLICATION to emptyList(), Category.Type.GAME to emptyList() @@ -61,11 +57,7 @@ class CategoryViewModel @Inject constructor( val liveData = MutableLiveData() private fun contract(): CategoryContract { - return if (authProvider.isAnonymous) { - webCategoryHelper - } else { - categoryHelper - } + return categoryHelper } fun getCategoryList(type: Category.Type) { @@ -74,10 +66,11 @@ class CategoryViewModel @Inject constructor( if (categories.isNotEmpty()) { liveData.postValue(ViewState.Success(stash)) + return@launch } try { - stash[type] = contract().getAllCategoriesList(type) + stash[type] = contract().getAllCategories(type) liveData.postValue(ViewState.Success(stash)) } catch (exception: Exception) { Log.e(TAG, "Failed fetching list of categories", exception) 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 927cbab96..beaccc101 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 @@ -41,10 +41,11 @@ import javax.inject.Inject @HiltViewModel @SuppressLint("StaticFieldLeak") // false positive, see https://github.com/google/dagger/issues/3253 -class StreamViewModel @Inject constructor(@ApplicationContext private val context: Context) : - ViewModel() { +class StreamViewModel @Inject constructor( + @ApplicationContext private val context: Context +) : ViewModel() { - private var webStreamHelper: StreamContract = WebStreamHelper() + private var webStreamHelper = WebStreamHelper() .using(HttpClient.getPreferredClient(context)) val liveData: MutableLiveData = MutableLiveData() @@ -64,7 +65,7 @@ class StreamViewModel @Inject constructor(@ApplicationContext private val contex viewModelScope.launch(Dispatchers.IO) { supervisorScope { val bundle = targetBundle(category) - if (bundle.streamClusters.isNotEmpty()) { + if (bundle.hasCluster()) { liveData.postValue(ViewState.Success(stash)) } @@ -72,13 +73,13 @@ class StreamViewModel @Inject constructor(@ApplicationContext private val contex if (!bundle.hasCluster() || bundle.hasNext()) { //Fetch new stream bundle - val newBundle = if (bundle.streamClusters.isEmpty()) { - contract().fetch(type, category) - } else { + val newBundle = if (bundle.hasCluster()) { contract().nextStreamBundle( category, bundle.streamNextPageUrl ) + } else { + contract().fetch(type, category) } //Update old bundle diff --git a/app/src/main/java/com/aurora/store/viewmodel/subcategory/SubCategoryClusterViewModel.kt b/app/src/main/java/com/aurora/store/viewmodel/subcategory/CategoryStreamViewModel.kt similarity index 80% rename from app/src/main/java/com/aurora/store/viewmodel/subcategory/SubCategoryClusterViewModel.kt rename to app/src/main/java/com/aurora/store/viewmodel/subcategory/CategoryStreamViewModel.kt index 2c0faf43b..03d9c9275 100644 --- a/app/src/main/java/com/aurora/store/viewmodel/subcategory/SubCategoryClusterViewModel.kt +++ b/app/src/main/java/com/aurora/store/viewmodel/subcategory/CategoryStreamViewModel.kt @@ -26,8 +26,9 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.aurora.gplayapi.data.models.StreamBundle import com.aurora.gplayapi.data.models.StreamCluster +import com.aurora.gplayapi.helpers.contracts.CategoryStreamContract import com.aurora.gplayapi.helpers.contracts.StreamContract -import com.aurora.gplayapi.helpers.web.WebStreamHelper +import com.aurora.gplayapi.helpers.web.WebCategoryStreamHelper import com.aurora.store.data.model.ViewState import com.aurora.store.data.network.HttpClient import com.aurora.store.util.Log @@ -40,24 +41,24 @@ import javax.inject.Inject @HiltViewModel @SuppressLint("StaticFieldLeak") // false positive, see https://github.com/google/dagger/issues/3253 -class SubCategoryClusterViewModel @Inject constructor(@ApplicationContext private val context: Context) : - ViewModel() { +class CategoryStreamViewModel @Inject constructor( + @ApplicationContext private val context: Context +) : ViewModel() { - var contract: StreamContract = WebStreamHelper() + private var webCategoryStreamHelper = WebCategoryStreamHelper() .using(HttpClient.getPreferredClient(context)) val liveData: MutableLiveData = MutableLiveData() private var stash: MutableMap = mutableMapOf() - private fun getCategoryStreamBundle( - category: StreamContract.Category, - nextPageUrl: String - ): StreamBundle { - return if (targetBundle(category).streamClusters.isEmpty()) - contract.fetch(StreamContract.Type.HOME, category) - else - contract.nextStreamBundle(category, nextPageUrl) + fun contract(): CategoryStreamContract { + return webCategoryStreamHelper + } + + fun getStreamBundle(category: StreamContract.Category) { + liveData.postValue(ViewState.Loading) + observe(category) } fun observe(category: StreamContract.Category) { @@ -65,18 +66,22 @@ class SubCategoryClusterViewModel @Inject constructor(@ApplicationContext privat viewModelScope.launch(Dispatchers.IO) { supervisorScope { val bundle = targetBundle(category) - if (bundle.streamClusters.isNotEmpty()) { liveData.postValue(ViewState.Success(stash)) } try { if (!bundle.hasCluster() || bundle.hasNext()) { + //Fetch new stream bundle - val newBundle = getCategoryStreamBundle( - category, - bundle.streamNextPageUrl - ) + val newBundle = if (bundle.streamClusters.isEmpty()) { + contract().fetch(category.value) + } else { + contract().nextStreamBundle( + category, + bundle.streamNextPageUrl + ) + } //Update old bundle bundle.apply { @@ -102,7 +107,7 @@ class SubCategoryClusterViewModel @Inject constructor(@ApplicationContext privat try { if (streamCluster.hasNext()) { val newCluster = - contract.nextStreamCluster(streamCluster.clusterNextPageUrl) + contract().nextStreamCluster(streamCluster.clusterNextPageUrl) updateCluster(category, streamCluster.id, newCluster) liveData.postValue(ViewState.Success(stash)) } else { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7bf11c331..74c6ba833 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -7,7 +7,7 @@ composeBom = "2024.06.00" coreVersion = "1.13.1" epoxyVersion = "5.1.4" espressoVersion = "3.6.1" -gplayapiVersion = "3.3.0" +gplayapiVersion = "3.3.1" gsonVersion = "2.10.1" hiddenapibypassVersion = "4.3" hiltVersion = "2.51.1"