Use WebCategoryStream helper to fetch bundles as default
This commit is contained in:
@@ -47,7 +47,7 @@ class CarouselModelGroup(
|
||||
val models = ArrayList<EpoxyModel<*>>()
|
||||
val clusterViewModels = mutableListOf<EpoxyModel<*>>()
|
||||
|
||||
val idPrefix = streamCluster.id
|
||||
val idPrefix = streamCluster.clusterTitle.hashCode()
|
||||
|
||||
models.add(
|
||||
HeaderViewModel_()
|
||||
|
||||
@@ -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<FragmentGenericWithToolbarBinding>(),
|
||||
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<FragmentGenericWithToolbarBinding>()
|
||||
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<FragmentGenericWithToolbarBinding>()
|
||||
}
|
||||
})
|
||||
|
||||
viewModel.getStreamBundle(category)
|
||||
viewModel.liveData.observe(viewLifecycleOwner) {
|
||||
when (it) {
|
||||
is ViewState.Loading -> {
|
||||
@@ -84,8 +85,6 @@ class CategoryBrowseFragment : BaseFragment<FragmentGenericWithToolbarBinding>()
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.observe(category)
|
||||
}
|
||||
|
||||
override fun onHeaderClicked(streamCluster: StreamCluster) {
|
||||
|
||||
@@ -67,27 +67,18 @@ class ForYouFragment : BaseFragment<FragmentForYouBinding>(),
|
||||
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 -> {
|
||||
|
||||
@@ -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<ViewState>()
|
||||
|
||||
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)
|
||||
|
||||
@@ -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<ViewState> = 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
|
||||
|
||||
@@ -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<ViewState> = MutableLiveData()
|
||||
|
||||
private var stash: MutableMap<String, StreamBundle> = 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 {
|
||||
Reference in New Issue
Block a user