Skip to content

Commit 9ebc253

Browse files
committed
Fixed updates check not repeating requests when required
1 parent 95e7cbb commit 9ebc253

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/main/kotlin/com/jetpackduba/gitnuro/updates/UpdatesRepository.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package com.jetpackduba.gitnuro.updates
22

33
import com.jetpackduba.gitnuro.AppConstants
4+
import com.jetpackduba.gitnuro.di.qualifiers.AppCoroutineScope
5+
import io.ktor.client.*
6+
import io.ktor.client.call.*
7+
import io.ktor.client.request.*
8+
import kotlinx.coroutines.CoroutineScope
49
import kotlinx.coroutines.delay
10+
import kotlinx.coroutines.flow.SharingStarted
511
import kotlinx.coroutines.flow.flow
12+
import kotlinx.coroutines.flow.stateIn
613
import kotlinx.coroutines.isActive
714
import kotlinx.serialization.json.Json
815
import javax.inject.Inject
@@ -16,12 +23,15 @@ private val updateJson = Json {
1623

1724
@Singleton
1825
class UpdatesRepository @Inject constructor(
19-
private val updatesWebService: UpdatesService,
26+
private val httpClient: HttpClient,
27+
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
2028
) {
2129
val hasUpdatesFlow = flow {
22-
val latestReleaseJson = updatesWebService.release(AppConstants.VERSION_CHECK_URL)
23-
2430
while (coroutineContext.isActive) {
31+
val latestReleaseJson = httpClient
32+
.get(AppConstants.VERSION_CHECK_URL)
33+
.body<String>()
34+
2535
val update = updateJson.decodeFromString<Update?>(latestReleaseJson)
2636

2737
if (update != null && update.appCode > AppConstants.APP_VERSION_CODE) {
@@ -30,5 +40,5 @@ class UpdatesRepository @Inject constructor(
3040

3141
delay(5.minutes)
3242
}
33-
}
43+
}.stateIn(appCoroutineScope, started = SharingStarted.Eagerly, null)
3444
}

src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/RepositoryOpenViewModel.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ class RepositoryOpenViewModel @Inject constructor(
258258
}
259259

260260
val update: StateFlow<Update?> = updatesRepository.hasUpdatesFlow
261-
.stateIn(tabScope, started = SharingStarted.Eagerly, null)
262261

263262
fun blameFile(filePath: String) = tabState.safeProcessing(
264263
refreshType = RefreshType.NONE,

src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/TabViewModel.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ class TabViewModel @Inject constructor(
173173
}
174174

175175
val update: StateFlow<Update?> = updatesRepository.hasUpdatesFlow
176-
.stateIn(tabScope, started = SharingStarted.Eagerly, null)
177176

178177
fun cancelOngoingTask() {
179178
tabState.cancelCurrentTask()

0 commit comments

Comments
 (0)