Skip to content

Commit bbf2eec

Browse files
authored
Local Diffusion models parallel downloads (#231)
* [Configuration] Local Diffusion models parallel downloads * [Configuration] Unit test coverage
1 parent cd4559a commit bbf2eec

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

presentation/src/main/java/com/shifthackz/aisdv1/presentation/screen/setup/ServerSetupViewModel.kt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class ServerSetupViewModel(
6262
else -> AuthorizationCredentials.None
6363
}
6464

65-
private var downloadDisposable: Disposable? = null
65+
private val downloadDisposables: MutableList<Pair<String, Disposable>> = mutableListOf()
6666

6767
init {
6868
!Single.zip(
@@ -94,6 +94,13 @@ class ServerSetupViewModel(
9494
}
9595
}
9696

97+
override fun onCleared() {
98+
downloadDisposables.forEach { (_, disposable) ->
99+
disposable.dispose()
100+
}
101+
super.onCleared()
102+
}
103+
97104
override fun processIntent(intent: ServerSetupIntent) = when (intent) {
98105
is ServerSetupIntent.AllowLocalCustomModel -> updateState {
99106
it.copy(
@@ -383,8 +390,14 @@ class ServerSetupViewModel(
383390
when {
384391
// User cancels download
385392
localModel.downloadState is DownloadState.Downloading -> {
386-
downloadDisposable?.dispose()
387-
downloadDisposable = null
393+
val index = downloadDisposables.indexOfFirst { it.first == localModel.id }
394+
if (index != -1) {
395+
downloadDisposables[index].second.dispose()
396+
downloadDisposables.removeAt(index)
397+
}
398+
!deleteModelUseCase(localModel.id)
399+
.subscribeOnMainThread(schedulersProvider)
400+
.subscribeBy(::errorLog)
388401
updateState {
389402
it.copy(
390403
localModels = currentState.localModels.withNewState(
@@ -408,14 +421,13 @@ class ServerSetupViewModel(
408421
),
409422
)
410423
}
411-
downloadDisposable?.dispose()
412-
downloadDisposable = null
413-
downloadDisposable = downloadModelUseCase(localModel.id)
424+
!downloadModelUseCase(localModel.id)
414425
.distinctUntilChanged()
415426
.doOnSubscribe { wakeLockInterActor.acquireWakelockUseCase() }
416427
.doFinally { wakeLockInterActor.releaseWakeLockUseCase() }
417428
.subscribeOnMainThread(schedulersProvider).subscribeBy(
418429
onError = { t ->
430+
errorLog(t)
419431
val message = t.localizedMessage ?: "Error"
420432
updateState {
421433
it.copy(
@@ -448,7 +460,8 @@ class ServerSetupViewModel(
448460
}
449461
}
450462
},
451-
).addToDisposable()
463+
)
464+
.also { downloadDisposables.add(localModel.id to it) }
452465
}
453466
}
454467
}

presentation/src/main/res/values-ru/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<string name="version">Версия %1$s</string>
2121

2222
<string name="splash_status_initializing">Инициализация…</string>
23-
<string name="splash_status_fetching">Загрузка занных…</string>
23+
<string name="splash_status_fetching">Загрузка данных…</string>
2424
<string name="splash_status_launching">Запуск приложения!</string>
2525

2626
<string name="local_no_img2img_support_title">Не доступно</string>

presentation/src/test/java/com/shifthackz/aisdv1/presentation/screen/setup/ServerSetupViewModelTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ class ServerSetupViewModelTest : CoreViewModelTest<ServerSetupViewModel>() {
182182

183183
@Test
184184
fun `given received LocalModel ClickReduce intent, model is downloading, expected UI state is Unknown`() {
185+
every {
186+
stubDeleteModelUseCase.invoke(any())
187+
} returns Completable.complete()
188+
185189
val localModel = mockServerSetupStateLocalModel.copy(
186190
downloadState = DownloadState.Downloading(22),
187191
)

0 commit comments

Comments
 (0)