Skip to content

Commit bf00403

Browse files
committed
Play Source
* Improve display of progress. * Improve token refresh. * Small refactors.
1 parent 0df0554 commit bf00403

File tree

8 files changed

+32
-27
lines changed

8 files changed

+32
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package com.apkupdater.data.ui
22

3-
data class AppInstallProgress(val id: Int, val progress: Long = 0L, val total: Long = 0L)
3+
data class AppInstallProgress(val id: Int, val progress: Long? = null, val total: Long? = null)

app/src/main/kotlin/com/apkupdater/data/ui/AppUpdate.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ fun MutableList<AppUpdate>.removeId(id: Int): List<AppUpdate> {
3838
fun MutableList<AppUpdate>.setProgress(progress: AppInstallProgress): MutableList<AppUpdate> {
3939
val index = this.indexOf(progress.id)
4040
if (index != -1) {
41-
if (progress.progress != 0L) this[index] = this[index].copy(progress = progress.progress)
42-
if (progress.total != 0L) this[index] = this[index].copy(total = progress.total)
41+
progress.progress?.let { this[index] = this[index].copy(progress = it) }
42+
progress.total?.let { this[index] = this[index].copy(total = it) }
4343
}
4444
return this
4545
}

app/src/main/kotlin/com/apkupdater/di/MainModule.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import com.apkupdater.util.Themer
3333
import com.apkupdater.util.UpdatesNotification
3434
import com.apkupdater.util.addUserAgentInterceptor
3535
import com.apkupdater.util.isAndroidTv
36+
import com.apkupdater.util.play.PlayHttpClient
3637
import com.apkupdater.viewmodel.AppsViewModel
3738
import com.apkupdater.viewmodel.MainViewModel
3839
import com.apkupdater.viewmodel.SearchViewModel
@@ -151,7 +152,7 @@ val mainModule = module {
151152

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

154-
single { PlayRepository(get(), get(), get()) }
155+
single { PlayRepository(get(), get(), get(), get()) }
155156

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

@@ -181,6 +182,8 @@ val mainModule = module {
181182

182183
single { InstallLog() }
183184

185+
single { PlayHttpClient() }
186+
184187
viewModel { MainViewModel(get(), get()) }
185188

186189
viewModel { AppsViewModel(get(), get(), get()) }

app/src/main/kotlin/com/apkupdater/repository/PlayRepository.kt

+13-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import kotlinx.coroutines.flow.flow
2626

2727
class PlayRepository(
2828
private val context: Context,
29+
private val playHttpClient: PlayHttpClient,
2930
private val gson: Gson,
3031
private val prefs: Prefs
3132
) {
@@ -36,7 +37,7 @@ class PlayRepository(
3637
private fun refreshAuth(): AuthData {
3738
Log.i("PlayRepository", "Refreshing token.")
3839
val properties = NativeDeviceInfoProvider(context).getNativeDeviceProperties()
39-
val playResponse = PlayHttpClient.postAuth(AUTH_URL, gson.toJson(properties).toByteArray())
40+
val playResponse = playHttpClient.postAuth(AUTH_URL, gson.toJson(properties).toByteArray())
4041
if (playResponse.isSuccessful) {
4142
val authData = gson.fromJson(String(playResponse.responseBytes), AuthData::class.java)
4243
prefs.playAuthData.put(authData)
@@ -56,9 +57,13 @@ class PlayRepository(
5657
Log.i("PlayRepository", "Checking token validity.")
5758

5859
// 1h has passed check if token still works
59-
val app = AppDetailsHelper(savedData)
60-
.using(PlayHttpClient)
61-
.getAppByPackageName("com.google.android.gm")
60+
val app = runCatching {
61+
AppDetailsHelper(savedData)
62+
.using(playHttpClient)
63+
.getAppByPackageName("com.google.android.gm")
64+
}.getOrElse {
65+
return refreshAuth()
66+
}
6267

6368
if (app.packageName.isEmpty()) {
6469
return refreshAuth()
@@ -73,7 +78,7 @@ class PlayRepository(
7378
// Normal Search
7479
val authData = auth()
7580
val updates = SearchHelper(authData)
76-
.using(PlayHttpClient)
81+
.using(playHttpClient)
7782
.searchResults(text)
7883
.appList
7984
.take(10)
@@ -83,7 +88,7 @@ class PlayRepository(
8388
// Package Name Search
8489
val authData = auth()
8590
val update = AppDetailsHelper(authData)
86-
.using(PlayHttpClient)
91+
.using(playHttpClient)
8792
.getAppByPackageName(text)
8893
.toAppUpdate(::getInstallFiles)
8994
emit(Result.success(listOf(update)))
@@ -96,7 +101,7 @@ class PlayRepository(
96101
suspend fun updates(apps: List<AppInstalled>) = flow {
97102
val authData = auth()
98103
val details = AppDetailsHelper(authData)
99-
.using(PlayHttpClient)
104+
.using(playHttpClient)
100105
.getAppByPackageName(apps.getPackageNames())
101106
val updates = details
102107
.filter { it.versionCode > apps.getVersionCode(it.packageName) }
@@ -114,7 +119,7 @@ class PlayRepository(
114119
}
115120

116121
private fun getInstallFiles(app: App) = PurchaseHelper(auth())
117-
.using(PlayHttpClient)
122+
.using(playHttpClient)
118123
.purchase(app.packageName, app.versionCode, app.offerType)
119124
.filter { it.type == File.FileType.BASE || it.type == File.FileType.SPLIT }
120125

app/src/main/kotlin/com/apkupdater/util/play/PlayHttpClient.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ import java.net.Proxy
2121
import java.util.concurrent.TimeUnit
2222

2323

24-
object PlayHttpClient : IProxyHttpClient {
24+
class PlayHttpClient : IProxyHttpClient {
2525

26-
private const val POST = "POST"
27-
private const val GET = "GET"
26+
companion object {
27+
private const val POST = "POST"
28+
private const val GET = "GET"
29+
}
2830

2931
private val _responseCode = MutableStateFlow(100)
3032
override val responseCode: StateFlow<Int> get() = _responseCode.asStateFlow()
3133
private var okHttpClient = OkHttpClient()
32-
val okHttpClientBuilder = OkHttpClient().newBuilder()
34+
private val okHttpClientBuilder = OkHttpClient().newBuilder()
3335
.connectTimeout(25, TimeUnit.SECONDS)
3436
.readTimeout(25, TimeUnit.SECONDS)
3537
.writeTimeout(25, TimeUnit.SECONDS)

app/src/main/kotlin/com/apkupdater/viewmodel/InstallViewModel.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ abstract class InstallViewModel(
4444
}
4545
}
4646

47-
protected fun subscribeToInstallStatus(
48-
block: (AppInstallStatus) -> Unit
49-
) = installLog.status().onEach {
50-
block(it)
47+
protected fun subscribeToInstallStatus(updates: List<AppUpdate>) = installLog.status().onEach {
48+
sendInstallSnack(updates, it)
5149
if (it.success) {
5250
finishInstall(it.id).join()
5351
} else {
52+
installLog.emitProgress(AppInstallProgress(it.id, 0L))
5453
cancelInstall(it.id).join()
5554
}
5655
}.launchIn(viewModelScope)
@@ -99,7 +98,7 @@ abstract class InstallViewModel(
9998
cancelInstall(id)
10099
}
101100

102-
protected fun sendInstallSnack(updates: List<AppUpdate>, log: AppInstallStatus) {
101+
private fun sendInstallSnack(updates: List<AppUpdate>, log: AppInstallStatus) {
103102
if (log.snack) {
104103
updates.find { log.id == it.id }?.let { app ->
105104
val message = if (log.success) R.string.install_success else R.string.install_failure

app/src/main/kotlin/com/apkupdater/viewmodel/SearchViewModel.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ class SearchViewModel(
3939
private var job: Job? = null
4040

4141
init {
42-
subscribeToInstallStatus { status ->
43-
sendInstallSnack(state.value.updates(), status)
44-
}
42+
subscribeToInstallStatus(state.value.updates())
4543
subscribeToInstallProgress { progress ->
4644
state.value = SearchUiState.Success(state.value.mutableUpdates().setProgress(progress))
4745
}

app/src/main/kotlin/com/apkupdater/viewmodel/UpdatesViewModel.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ class UpdatesViewModel(
3737
private val state = MutableStateFlow<UpdatesUiState>(UpdatesUiState.Loading)
3838

3939
init {
40-
subscribeToInstallStatus { status ->
41-
sendInstallSnack(state.value.updates(), status)
42-
}
40+
subscribeToInstallStatus(state.value.updates())
4341
subscribeToInstallProgress { progress ->
4442
state.value = UpdatesUiState.Success(state.value.mutableUpdates().setProgress(progress))
4543
}

0 commit comments

Comments
 (0)