Initial Web APIs implementation

Introduce a toggle in Settings > Advanced to let user use Web APIs.
Currently implemented for search suggestions and results only.

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2023-10-02 14:05:30 +05:30
parent 4134e1f4f4
commit fd097fbba1
7 changed files with 44 additions and 4 deletions

View File

@@ -44,6 +44,7 @@ import com.aurora.store.MainActivity
import com.aurora.store.R
import com.aurora.store.util.Log
import com.aurora.store.util.Preferences
import com.aurora.store.util.Preferences.PREFERENCE_ADVANCED_USE_WEB_API
import kotlin.system.exitProcess
val Context.inflater: LayoutInflater
@@ -176,3 +177,7 @@ fun Context.accentColor(): Int {
}
return ContextCompat.getColor(this, color)
}
fun Context.shouldUseWebAPI(): Boolean {
return Preferences.getBoolean(this, PREFERENCE_ADVANCED_USE_WEB_API)
}

View File

@@ -59,6 +59,7 @@ object Preferences {
const val PREFERENCE_UPDATES_CHECK = "PREFERENCE_UPDATES_CHECK"
const val PREFERENCE_ADVANCED_SEARCH_IN_CTT = "PREFERENCE_ADVANCED_SEARCH_IN_CTT"
const val PREFERENCE_ADVANCED_USE_WEB_API = "PREFERENCE_ADVANCED_USE_WEB_API"
const val PREFERENCE_UNIQUE_GROUP_IDS = "PREFERENCE_UNIQUE_GROUP_IDS"

View File

@@ -20,17 +20,21 @@
package com.aurora.store.viewmodel.search
import android.app.Application
import android.content.Context
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.aurora.extensions.flushAndAdd
import com.aurora.extensions.shouldUseWebAPI
import com.aurora.gplayapi.data.models.AuthData
import com.aurora.gplayapi.data.models.SearchBundle
import com.aurora.gplayapi.helpers.SearchHelper
import com.aurora.gplayapi.helpers.WebSearchHelper
import com.aurora.store.data.RequestState
import com.aurora.store.data.network.HttpClient
import com.aurora.store.data.providers.AuthProvider
import com.aurora.store.viewmodel.BaseAndroidViewModel
import java.util.UUID
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
@@ -45,6 +49,8 @@ class SearchResultViewModel(application: Application) : BaseAndroidViewModel(app
private val searchHelper: SearchHelper = SearchHelper(authData)
.using(HttpClient.getPreferredClient())
private val webSearchHelper = WebSearchHelper()
val liveData: MutableLiveData<SearchBundle> = MutableLiveData()
private var searchBundle: SearchBundle = SearchBundle()
@@ -69,7 +75,17 @@ class SearchResultViewModel(application: Application) : BaseAndroidViewModel(app
private fun search(
query: String
): SearchBundle {
return searchHelper.searchResults(query)
return if ((getApplication() as Context).shouldUseWebAPI()) {
val result = webSearchHelper.searchResults(query)
val searchBundle = SearchBundle().apply {
this.id = 100
this.query = query
appList = result.values.flatten().toSet().toMutableList()
}
return searchBundle
} else {
searchHelper.searchResults(query)
}
}
@Synchronized

View File

@@ -20,12 +20,15 @@
package com.aurora.store.viewmodel.search
import android.app.Application
import android.content.Context
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.aurora.extensions.shouldUseWebAPI
import com.aurora.gplayapi.SearchSuggestEntry
import com.aurora.gplayapi.data.models.AuthData
import com.aurora.gplayapi.helpers.SearchHelper
import com.aurora.gplayapi.helpers.WebSearchSuggestionHelper
import com.aurora.store.data.network.HttpClient
import com.aurora.store.data.providers.AuthProvider
import kotlinx.coroutines.Dispatchers
@@ -40,6 +43,8 @@ class SearchSuggestionViewModel(application: Application) : AndroidViewModel(app
private val searchHelper: SearchHelper = SearchHelper(authData)
.using(HttpClient.getPreferredClient())
private val webSearchSuggestionHelper = WebSearchSuggestionHelper()
val liveSearchSuggestions: MutableLiveData<List<SearchSuggestEntry>> = MutableLiveData()
fun observeStreamBundles(query: String) {
@@ -51,6 +56,10 @@ class SearchSuggestionViewModel(application: Application) : AndroidViewModel(app
private fun getSearchSuggestions(
query: String
): List<SearchSuggestEntry> {
return searchHelper.searchSuggestions(query)
return if ((getApplication() as Context).shouldUseWebAPI()) {
webSearchSuggestionHelper.searchSuggestions(query)
} else {
searchHelper.searchSuggestions(query)
}
}
}
}

View File

@@ -251,6 +251,8 @@
<string name="pref_updates_extended_desc">Looks for new versions of disabled apps too</string>
<string name="pref_advanced_search_in_ctt">Search in Custom Tab</string>
<string name="pref_advanced_search_in_ctt_desc">Opens search results in a browser\'s custom tab</string>
<string name="pref_advanced_use_web_api">Use Web APIs</string>
<string name="pref_advanced_use_web_api_desc">Switches to Web APIs wherever possible when making a request to Google Play (Increases anonymity, but can be unstable).</string>
<string name="purchase_failed">"Download Failed"</string>
<string name="purchase_invalid">"App not purchased"</string>
<string name="purchase_unsupported">"App not supported"</string>

View File

@@ -8,4 +8,11 @@
app:summary="@string/pref_advanced_search_in_ctt_desc"
app:title="@string/pref_advanced_search_in_ctt" />
<SwitchPreferenceCompat
app:defaultValue="false"
app:iconSpaceReserved="false"
app:key="PREFERENCE_ADVANCED_USE_WEB_API"
app:summary="@string/pref_advanced_use_web_api_desc"
app:title="@string/pref_advanced_use_web_api" />
</PreferenceScreen>