Skip to content

Commit

Permalink
Play Source
Browse files Browse the repository at this point in the history
* Improve display of progress.
* Improve token refresh.
* Small refactors.
  • Loading branch information
rumboalla committed Apr 3, 2024
1 parent 0df0554 commit bf00403
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.apkupdater.data.ui

data class AppInstallProgress(val id: Int, val progress: Long = 0L, val total: Long = 0L)
data class AppInstallProgress(val id: Int, val progress: Long? = null, val total: Long? = null)
4 changes: 2 additions & 2 deletions app/src/main/kotlin/com/apkupdater/data/ui/AppUpdate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ fun MutableList<AppUpdate>.removeId(id: Int): List<AppUpdate> {
fun MutableList<AppUpdate>.setProgress(progress: AppInstallProgress): MutableList<AppUpdate> {
val index = this.indexOf(progress.id)
if (index != -1) {
if (progress.progress != 0L) this[index] = this[index].copy(progress = progress.progress)
if (progress.total != 0L) this[index] = this[index].copy(total = progress.total)
progress.progress?.let { this[index] = this[index].copy(progress = it) }
progress.total?.let { this[index] = this[index].copy(total = it) }
}
return this
}
5 changes: 4 additions & 1 deletion app/src/main/kotlin/com/apkupdater/di/MainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.apkupdater.util.Themer
import com.apkupdater.util.UpdatesNotification
import com.apkupdater.util.addUserAgentInterceptor
import com.apkupdater.util.isAndroidTv
import com.apkupdater.util.play.PlayHttpClient
import com.apkupdater.viewmodel.AppsViewModel
import com.apkupdater.viewmodel.MainViewModel
import com.apkupdater.viewmodel.SearchViewModel
Expand Down Expand Up @@ -151,7 +152,7 @@ val mainModule = module {

single { AptoideRepository(get(), get(), get()) }

single { PlayRepository(get(), get(), get()) }
single { PlayRepository(get(), get(), get(), get()) }

single(named("main")) { FdroidRepository(get(), "https://f-droid.org/repo/", FdroidSource, get()) }

Expand Down Expand Up @@ -181,6 +182,8 @@ val mainModule = module {

single { InstallLog() }

single { PlayHttpClient() }

viewModel { MainViewModel(get(), get()) }

viewModel { AppsViewModel(get(), get(), get()) }
Expand Down
21 changes: 13 additions & 8 deletions app/src/main/kotlin/com/apkupdater/repository/PlayRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlinx.coroutines.flow.flow

class PlayRepository(
private val context: Context,
private val playHttpClient: PlayHttpClient,
private val gson: Gson,
private val prefs: Prefs
) {
Expand All @@ -36,7 +37,7 @@ class PlayRepository(
private fun refreshAuth(): AuthData {
Log.i("PlayRepository", "Refreshing token.")
val properties = NativeDeviceInfoProvider(context).getNativeDeviceProperties()
val playResponse = PlayHttpClient.postAuth(AUTH_URL, gson.toJson(properties).toByteArray())
val playResponse = playHttpClient.postAuth(AUTH_URL, gson.toJson(properties).toByteArray())
if (playResponse.isSuccessful) {
val authData = gson.fromJson(String(playResponse.responseBytes), AuthData::class.java)
prefs.playAuthData.put(authData)
Expand All @@ -56,9 +57,13 @@ class PlayRepository(
Log.i("PlayRepository", "Checking token validity.")

// 1h has passed check if token still works
val app = AppDetailsHelper(savedData)
.using(PlayHttpClient)
.getAppByPackageName("com.google.android.gm")
val app = runCatching {
AppDetailsHelper(savedData)
.using(playHttpClient)
.getAppByPackageName("com.google.android.gm")
}.getOrElse {
return refreshAuth()
}

if (app.packageName.isEmpty()) {
return refreshAuth()
Expand All @@ -73,7 +78,7 @@ class PlayRepository(
// Normal Search
val authData = auth()
val updates = SearchHelper(authData)
.using(PlayHttpClient)
.using(playHttpClient)
.searchResults(text)
.appList
.take(10)
Expand All @@ -83,7 +88,7 @@ class PlayRepository(
// Package Name Search
val authData = auth()
val update = AppDetailsHelper(authData)
.using(PlayHttpClient)
.using(playHttpClient)
.getAppByPackageName(text)
.toAppUpdate(::getInstallFiles)
emit(Result.success(listOf(update)))
Expand All @@ -96,7 +101,7 @@ class PlayRepository(
suspend fun updates(apps: List<AppInstalled>) = flow {
val authData = auth()
val details = AppDetailsHelper(authData)
.using(PlayHttpClient)
.using(playHttpClient)
.getAppByPackageName(apps.getPackageNames())
val updates = details
.filter { it.versionCode > apps.getVersionCode(it.packageName) }
Expand All @@ -114,7 +119,7 @@ class PlayRepository(
}

private fun getInstallFiles(app: App) = PurchaseHelper(auth())
.using(PlayHttpClient)
.using(playHttpClient)
.purchase(app.packageName, app.versionCode, app.offerType)
.filter { it.type == File.FileType.BASE || it.type == File.FileType.SPLIT }

Expand Down
10 changes: 6 additions & 4 deletions app/src/main/kotlin/com/apkupdater/util/play/PlayHttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ import java.net.Proxy
import java.util.concurrent.TimeUnit


object PlayHttpClient : IProxyHttpClient {
class PlayHttpClient : IProxyHttpClient {

private const val POST = "POST"
private const val GET = "GET"
companion object {
private const val POST = "POST"
private const val GET = "GET"
}

private val _responseCode = MutableStateFlow(100)
override val responseCode: StateFlow<Int> get() = _responseCode.asStateFlow()
private var okHttpClient = OkHttpClient()
val okHttpClientBuilder = OkHttpClient().newBuilder()
private val okHttpClientBuilder = OkHttpClient().newBuilder()
.connectTimeout(25, TimeUnit.SECONDS)
.readTimeout(25, TimeUnit.SECONDS)
.writeTimeout(25, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ abstract class InstallViewModel(
}
}

protected fun subscribeToInstallStatus(
block: (AppInstallStatus) -> Unit
) = installLog.status().onEach {
block(it)
protected fun subscribeToInstallStatus(updates: List<AppUpdate>) = installLog.status().onEach {
sendInstallSnack(updates, it)
if (it.success) {
finishInstall(it.id).join()
} else {
installLog.emitProgress(AppInstallProgress(it.id, 0L))
cancelInstall(it.id).join()
}
}.launchIn(viewModelScope)
Expand Down Expand Up @@ -99,7 +98,7 @@ abstract class InstallViewModel(
cancelInstall(id)
}

protected fun sendInstallSnack(updates: List<AppUpdate>, log: AppInstallStatus) {
private fun sendInstallSnack(updates: List<AppUpdate>, log: AppInstallStatus) {
if (log.snack) {
updates.find { log.id == it.id }?.let { app ->
val message = if (log.success) R.string.install_success else R.string.install_failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class SearchViewModel(
private var job: Job? = null

init {
subscribeToInstallStatus { status ->
sendInstallSnack(state.value.updates(), status)
}
subscribeToInstallStatus(state.value.updates())
subscribeToInstallProgress { progress ->
state.value = SearchUiState.Success(state.value.mutableUpdates().setProgress(progress))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ class UpdatesViewModel(
private val state = MutableStateFlow<UpdatesUiState>(UpdatesUiState.Loading)

init {
subscribeToInstallStatus { status ->
sendInstallSnack(state.value.updates(), status)
}
subscribeToInstallStatus(state.value.updates())
subscribeToInstallProgress { progress ->
state.value = UpdatesUiState.Success(state.value.mutableUpdates().setProgress(progress))
}
Expand Down

0 comments on commit bf00403

Please sign in to comment.