mirror of
https://github.com/whyorean/AuroraStore.git
synced 2026-06-12 09:48:34 -04:00
Fetch data from web-api for anonymous logins
This commit is contained in:
committed by
Aayush Gupta
parent
ad64bb1055
commit
bdf5658acf
@@ -25,9 +25,10 @@ import android.util.Log
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.aurora.gplayapi.data.models.AuthData
|
||||
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.data.network.HttpClient
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
@@ -45,15 +46,25 @@ class CategoryViewModel @Inject constructor(
|
||||
|
||||
private val TAG = CategoryViewModel::class.java.simpleName
|
||||
|
||||
private val streamHelper: CategoryHelper = CategoryHelper(authProvider.authData)
|
||||
private val categoryHelper: CategoryHelper = CategoryHelper(authProvider.authData)
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
|
||||
private val webCategoryHelper: CategoryContract = WebCategoryHelper()
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
|
||||
val liveData: MutableLiveData<List<Category>> = MutableLiveData()
|
||||
|
||||
private fun contract(): CategoryContract {
|
||||
return if(authProvider.isAnonymous){
|
||||
webCategoryHelper
|
||||
} else {
|
||||
categoryHelper
|
||||
}
|
||||
}
|
||||
fun getCategoryList(type: Category.Type) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
liveData.postValue(streamHelper.getAllCategoriesList(type))
|
||||
liveData.postValue(contract().getAllCategoriesList(type))
|
||||
} catch (exception: Exception) {
|
||||
Log.e(TAG, "Failed fetching list of categories", exception)
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import com.aurora.gplayapi.data.models.StreamCluster
|
||||
import com.aurora.gplayapi.helpers.StreamHelper
|
||||
import com.aurora.store.data.model.ViewState
|
||||
import com.aurora.gplayapi.helpers.contracts.StreamContract
|
||||
import com.aurora.gplayapi.helpers.web.WebStreamHelper
|
||||
import com.aurora.store.data.network.HttpClient
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import com.aurora.store.util.Log
|
||||
@@ -46,7 +47,10 @@ class BaseClusterViewModel @Inject constructor(
|
||||
private val authProvider: AuthProvider
|
||||
) : ViewModel() {
|
||||
|
||||
var streamHelper: StreamHelper = StreamHelper(authProvider.authData)
|
||||
private var streamHelper: StreamContract = StreamHelper(authProvider.authData)
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
|
||||
private var webStreamHelper: StreamContract = WebStreamHelper()
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
|
||||
val liveData: MutableLiveData<ViewState> = MutableLiveData()
|
||||
@@ -55,6 +59,14 @@ class BaseClusterViewModel @Inject constructor(
|
||||
lateinit var type: StreamContract.Type
|
||||
lateinit var category: StreamContract.Category
|
||||
|
||||
fun contract(): StreamContract {
|
||||
return if (authProvider.isAnonymous) {
|
||||
webStreamHelper
|
||||
} else {
|
||||
streamHelper
|
||||
}
|
||||
}
|
||||
|
||||
fun getStreamBundle(category: StreamContract.Category, type: StreamContract.Type) {
|
||||
this.type = type
|
||||
this.category = category
|
||||
@@ -70,9 +82,9 @@ class BaseClusterViewModel @Inject constructor(
|
||||
|
||||
//Fetch new stream bundle
|
||||
val newBundle = if (streamBundle.streamClusters.isEmpty()) {
|
||||
streamHelper.getNavStream(type, category)
|
||||
contract().fetch(type, category)
|
||||
} else {
|
||||
streamHelper.next(streamBundle.streamNextPageUrl)
|
||||
contract().nextStreamBundle(category, streamBundle.streamNextPageUrl)
|
||||
}
|
||||
|
||||
//Update old bundle
|
||||
@@ -99,7 +111,7 @@ class BaseClusterViewModel @Inject constructor(
|
||||
try {
|
||||
if (streamCluster.hasNext()) {
|
||||
val newCluster =
|
||||
streamHelper.getNextStreamCluster(streamCluster.clusterNextPageUrl)
|
||||
contract().nextStreamCluster(streamCluster.clusterNextPageUrl)
|
||||
updateCluster(streamCluster.id, newCluster)
|
||||
liveData.postValue(ViewState.Success(streamBundle))
|
||||
} else {
|
||||
|
||||
@@ -28,30 +28,33 @@ import com.aurora.gplayapi.data.models.StreamBundle
|
||||
import com.aurora.gplayapi.data.models.StreamCluster
|
||||
import com.aurora.gplayapi.helpers.CategoryHelper
|
||||
import com.aurora.store.data.model.ViewState
|
||||
import com.aurora.gplayapi.helpers.contracts.StreamContract
|
||||
import com.aurora.gplayapi.helpers.web.WebStreamHelper
|
||||
import com.aurora.store.data.network.HttpClient
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import com.aurora.store.util.Log
|
||||
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 kotlinx.coroutines.supervisorScope
|
||||
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,
|
||||
private val authProvider: AuthProvider
|
||||
@ApplicationContext private val context: Context
|
||||
) : ViewModel() {
|
||||
|
||||
var categoryHelper: CategoryHelper = CategoryHelper(authProvider.authData)
|
||||
var contract: StreamContract = WebStreamHelper()
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
|
||||
val liveData: MutableLiveData<ViewState> = MutableLiveData()
|
||||
var streamBundle: StreamBundle = StreamBundle()
|
||||
|
||||
lateinit var homeUrl: String
|
||||
private lateinit var homeUrl: String
|
||||
private lateinit var type: StreamContract.Type
|
||||
private lateinit var category: StreamContract.Category
|
||||
|
||||
init {
|
||||
liveData.postValue(ViewState.Loading)
|
||||
@@ -61,13 +64,21 @@ class SubCategoryClusterViewModel @Inject constructor(
|
||||
nextPageUrl: String
|
||||
): StreamBundle {
|
||||
return if (streamBundle.streamClusters.isEmpty())
|
||||
categoryHelper.getSubCategoryBundle(homeUrl)
|
||||
contract.fetch(type, category)
|
||||
else
|
||||
categoryHelper.getSubCategoryBundle(nextPageUrl)
|
||||
contract.nextStreamBundle(category, nextPageUrl)
|
||||
}
|
||||
|
||||
fun observeCategory(homeUrl: String) {
|
||||
this.homeUrl = homeUrl
|
||||
|
||||
val rawCategory = homeUrl.split("/").last()
|
||||
|
||||
type = StreamContract.Type.HOME
|
||||
category = StreamContract.Category.NONE.apply {
|
||||
value = rawCategory
|
||||
}
|
||||
|
||||
observe()
|
||||
}
|
||||
|
||||
@@ -106,7 +117,7 @@ class SubCategoryClusterViewModel @Inject constructor(
|
||||
try {
|
||||
if (streamCluster.hasNext()) {
|
||||
val newCluster =
|
||||
categoryHelper.getNextStreamCluster(streamCluster.clusterNextPageUrl)
|
||||
contract.nextStreamCluster(streamCluster.clusterNextPageUrl)
|
||||
updateCluster(newCluster)
|
||||
liveData.postValue(ViewState.Success(streamBundle))
|
||||
} else {
|
||||
|
||||
@@ -27,6 +27,7 @@ import androidx.lifecycle.viewModelScope
|
||||
import com.aurora.gplayapi.data.models.StreamCluster
|
||||
import com.aurora.gplayapi.helpers.TopChartsHelper
|
||||
import com.aurora.gplayapi.helpers.contracts.TopChartsContract
|
||||
import com.aurora.gplayapi.helpers.web.WebTopChartsHelper
|
||||
import com.aurora.store.data.network.HttpClient
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
@@ -46,13 +47,24 @@ class TopChartViewModel @Inject constructor(
|
||||
private val topChartsHelper: TopChartsHelper = TopChartsHelper(authProvider.authData)
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
|
||||
private val webTopChartsHelper: TopChartsContract = WebTopChartsHelper()
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
|
||||
val liveData: MutableLiveData<StreamCluster> = MutableLiveData()
|
||||
var streamCluster: StreamCluster = StreamCluster()
|
||||
|
||||
private fun contract(): TopChartsContract {
|
||||
return if(authProvider.isAnonymous){
|
||||
webTopChartsHelper
|
||||
} else {
|
||||
topChartsHelper
|
||||
}
|
||||
}
|
||||
|
||||
fun getStreamCluster(type: TopChartsContract.Type, chart: TopChartsContract.Chart) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
streamCluster = topChartsHelper.getCluster(type.value, chart.value)
|
||||
streamCluster = contract().getCluster(type.value, chart.value)
|
||||
liveData.postValue(streamCluster)
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user