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