From bf004039a654e8cb85e574a71526bc4790d3e51f Mon Sep 17 00:00:00 2001 From: rumboalla Date: Wed, 3 Apr 2024 17:43:17 +0200 Subject: [PATCH] Play Source * Improve display of progress. * Improve token refresh. * Small refactors. --- .../apkupdater/data/ui/AppInstallProgress.kt | 2 +- .../com/apkupdater/data/ui/AppUpdate.kt | 4 ++-- .../kotlin/com/apkupdater/di/MainModule.kt | 5 ++++- .../apkupdater/repository/PlayRepository.kt | 21 ++++++++++++------- .../apkupdater/util/play/PlayHttpClient.kt | 10 +++++---- .../apkupdater/viewmodel/InstallViewModel.kt | 9 ++++---- .../apkupdater/viewmodel/SearchViewModel.kt | 4 +--- .../apkupdater/viewmodel/UpdatesViewModel.kt | 4 +--- 8 files changed, 32 insertions(+), 27 deletions(-) diff --git a/app/src/main/kotlin/com/apkupdater/data/ui/AppInstallProgress.kt b/app/src/main/kotlin/com/apkupdater/data/ui/AppInstallProgress.kt index ea3aaee8..7999f119 100644 --- a/app/src/main/kotlin/com/apkupdater/data/ui/AppInstallProgress.kt +++ b/app/src/main/kotlin/com/apkupdater/data/ui/AppInstallProgress.kt @@ -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) diff --git a/app/src/main/kotlin/com/apkupdater/data/ui/AppUpdate.kt b/app/src/main/kotlin/com/apkupdater/data/ui/AppUpdate.kt index d53dab6f..77b2b7a6 100644 --- a/app/src/main/kotlin/com/apkupdater/data/ui/AppUpdate.kt +++ b/app/src/main/kotlin/com/apkupdater/data/ui/AppUpdate.kt @@ -38,8 +38,8 @@ fun MutableList.removeId(id: Int): List { fun MutableList.setProgress(progress: AppInstallProgress): MutableList { 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 } diff --git a/app/src/main/kotlin/com/apkupdater/di/MainModule.kt b/app/src/main/kotlin/com/apkupdater/di/MainModule.kt index 06c5f974..4f075f00 100644 --- a/app/src/main/kotlin/com/apkupdater/di/MainModule.kt +++ b/app/src/main/kotlin/com/apkupdater/di/MainModule.kt @@ -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 @@ -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()) } @@ -181,6 +182,8 @@ val mainModule = module { single { InstallLog() } + single { PlayHttpClient() } + viewModel { MainViewModel(get(), get()) } viewModel { AppsViewModel(get(), get(), get()) } diff --git a/app/src/main/kotlin/com/apkupdater/repository/PlayRepository.kt b/app/src/main/kotlin/com/apkupdater/repository/PlayRepository.kt index fa487bba..45f828f2 100644 --- a/app/src/main/kotlin/com/apkupdater/repository/PlayRepository.kt +++ b/app/src/main/kotlin/com/apkupdater/repository/PlayRepository.kt @@ -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 ) { @@ -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) @@ -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() @@ -73,7 +78,7 @@ class PlayRepository( // Normal Search val authData = auth() val updates = SearchHelper(authData) - .using(PlayHttpClient) + .using(playHttpClient) .searchResults(text) .appList .take(10) @@ -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))) @@ -96,7 +101,7 @@ class PlayRepository( suspend fun updates(apps: List) = 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) } @@ -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 } diff --git a/app/src/main/kotlin/com/apkupdater/util/play/PlayHttpClient.kt b/app/src/main/kotlin/com/apkupdater/util/play/PlayHttpClient.kt index 944dc0a6..515ec598 100644 --- a/app/src/main/kotlin/com/apkupdater/util/play/PlayHttpClient.kt +++ b/app/src/main/kotlin/com/apkupdater/util/play/PlayHttpClient.kt @@ -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 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) diff --git a/app/src/main/kotlin/com/apkupdater/viewmodel/InstallViewModel.kt b/app/src/main/kotlin/com/apkupdater/viewmodel/InstallViewModel.kt index eef11109..1adba100 100644 --- a/app/src/main/kotlin/com/apkupdater/viewmodel/InstallViewModel.kt +++ b/app/src/main/kotlin/com/apkupdater/viewmodel/InstallViewModel.kt @@ -44,13 +44,12 @@ abstract class InstallViewModel( } } - protected fun subscribeToInstallStatus( - block: (AppInstallStatus) -> Unit - ) = installLog.status().onEach { - block(it) + protected fun subscribeToInstallStatus(updates: List) = 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) @@ -99,7 +98,7 @@ abstract class InstallViewModel( cancelInstall(id) } - protected fun sendInstallSnack(updates: List, log: AppInstallStatus) { + private fun sendInstallSnack(updates: List, 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 diff --git a/app/src/main/kotlin/com/apkupdater/viewmodel/SearchViewModel.kt b/app/src/main/kotlin/com/apkupdater/viewmodel/SearchViewModel.kt index ad786794..7e0d71eb 100644 --- a/app/src/main/kotlin/com/apkupdater/viewmodel/SearchViewModel.kt +++ b/app/src/main/kotlin/com/apkupdater/viewmodel/SearchViewModel.kt @@ -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)) } diff --git a/app/src/main/kotlin/com/apkupdater/viewmodel/UpdatesViewModel.kt b/app/src/main/kotlin/com/apkupdater/viewmodel/UpdatesViewModel.kt index 909c7e59..190c59e1 100644 --- a/app/src/main/kotlin/com/apkupdater/viewmodel/UpdatesViewModel.kt +++ b/app/src/main/kotlin/com/apkupdater/viewmodel/UpdatesViewModel.kt @@ -37,9 +37,7 @@ class UpdatesViewModel( private val state = MutableStateFlow(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)) }