Улучшение кода
This commit is contained in:
@@ -7,7 +7,9 @@ import androidx.lifecycle.viewmodel.compose.SavedStateHandleSaveableApi
|
|||||||
import androidx.lifecycle.viewmodel.compose.saveable
|
import androidx.lifecycle.viewmodel.compose.saveable
|
||||||
import com.github.nullptroma.wallenc.domain.interfaces.IVaultsManager
|
import com.github.nullptroma.wallenc.domain.interfaces.IVaultsManager
|
||||||
import com.github.nullptroma.wallenc.domain.tasks.ITaskOrchestrator
|
import com.github.nullptroma.wallenc.domain.tasks.ITaskOrchestrator
|
||||||
|
import com.github.nullptroma.wallenc.domain.tasks.PipelineState
|
||||||
import com.github.nullptroma.wallenc.domain.tasks.TaskForegroundUiState
|
import com.github.nullptroma.wallenc.domain.tasks.TaskForegroundUiState
|
||||||
|
import com.github.nullptroma.wallenc.domain.tasks.TaskProgress
|
||||||
import com.github.nullptroma.wallenc.domain.tasks.TaskRunState
|
import com.github.nullptroma.wallenc.domain.tasks.TaskRunState
|
||||||
import com.github.nullptroma.wallenc.ui.R
|
import com.github.nullptroma.wallenc.ui.R
|
||||||
import com.github.nullptroma.wallenc.ui.ViewModelBase
|
import com.github.nullptroma.wallenc.ui.ViewModelBase
|
||||||
@@ -76,7 +78,7 @@ class MainViewModel @Inject constructor(
|
|||||||
|
|
||||||
private fun mapWorkStatus(
|
private fun mapWorkStatus(
|
||||||
fg: TaskForegroundUiState,
|
fg: TaskForegroundUiState,
|
||||||
pipe: com.github.nullptroma.wallenc.domain.tasks.PipelineState,
|
pipe: PipelineState,
|
||||||
anyVaultScanning: Boolean,
|
anyVaultScanning: Boolean,
|
||||||
): MainWorkStatus {
|
): MainWorkStatus {
|
||||||
when (fg) {
|
when (fg) {
|
||||||
@@ -85,23 +87,14 @@ class MainViewModel @Inject constructor(
|
|||||||
return mapBackgroundWork(pipe, anyVaultScanning)
|
return mapBackgroundWork(pipe, anyVaultScanning)
|
||||||
}
|
}
|
||||||
val head = fg.tasks.first()
|
val head = fg.tasks.first()
|
||||||
val p = head.progress
|
return activeWorkStatusFromProgress(head.title, head.progress)
|
||||||
val frac = p?.fraction
|
|
||||||
val indeterminate = p == null || frac == null
|
|
||||||
val label = p?.label?.takeIf { it.isNotBlank() }
|
|
||||||
val line = if (label != null) "${head.title} — $label" else head.title
|
|
||||||
return MainWorkStatus.Active(
|
|
||||||
line = line,
|
|
||||||
progressFraction = frac,
|
|
||||||
indeterminate = indeterminate,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
TaskForegroundUiState.Hidden -> return mapBackgroundWork(pipe, anyVaultScanning)
|
TaskForegroundUiState.Hidden -> return mapBackgroundWork(pipe, anyVaultScanning)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun mapBackgroundWork(
|
private fun mapBackgroundWork(
|
||||||
pipe: com.github.nullptroma.wallenc.domain.tasks.PipelineState,
|
pipe: PipelineState,
|
||||||
anyVaultScanning: Boolean,
|
anyVaultScanning: Boolean,
|
||||||
): MainWorkStatus {
|
): MainWorkStatus {
|
||||||
val fromPipeline = mapPipelineRunningOnly(pipe)
|
val fromPipeline = mapPipelineRunningOnly(pipe)
|
||||||
@@ -116,23 +109,13 @@ class MainViewModel @Inject constructor(
|
|||||||
return MainWorkStatus.Idle
|
return MainWorkStatus.Idle
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun mapPipelineRunningOnly(
|
private fun mapPipelineRunningOnly(pipe: PipelineState): MainWorkStatus? {
|
||||||
pipe: com.github.nullptroma.wallenc.domain.tasks.PipelineState,
|
|
||||||
): MainWorkStatus? {
|
|
||||||
val running = pipe.tasks.filter { it.id in pipe.runningTaskIds }
|
val running = pipe.tasks.filter { it.id in pipe.runningTaskIds }
|
||||||
if (running.isEmpty()) return null
|
if (running.isEmpty()) return null
|
||||||
if (running.size == 1) {
|
if (running.size == 1) {
|
||||||
val t = running.first()
|
val t = running.first()
|
||||||
val prog = (t.state as? TaskRunState.Running)?.progress
|
val progress = (t.state as? TaskRunState.Running)?.progress
|
||||||
val frac = prog?.fraction
|
return activeWorkStatusFromProgress(t.title, progress)
|
||||||
val indeterminate = prog == null || frac == null
|
|
||||||
val label = prog?.label?.takeIf { it.isNotBlank() }
|
|
||||||
val line = if (label != null) "${t.title} — $label" else t.title
|
|
||||||
return MainWorkStatus.Active(
|
|
||||||
line = line,
|
|
||||||
progressFraction = frac,
|
|
||||||
indeterminate = indeterminate,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return MainWorkStatus.Active(
|
return MainWorkStatus.Active(
|
||||||
line = uiStrings(R.string.main_status_multiple_tasks, running.size),
|
line = uiStrings(R.string.main_status_multiple_tasks, running.size),
|
||||||
@@ -140,4 +123,20 @@ class MainViewModel @Inject constructor(
|
|||||||
indeterminate = true,
|
indeterminate = true,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun activeWorkStatusFromProgress(title: String, progress: TaskProgress?): MainWorkStatus.Active {
|
||||||
|
val frac = progress?.fraction
|
||||||
|
val indeterminate = progress == null || frac == null
|
||||||
|
val label = progress?.label?.takeIf { it.isNotBlank() }
|
||||||
|
val line = if (label != null) "$title$TITLE_LABEL_SEPARATOR$label" else title
|
||||||
|
return MainWorkStatus.Active(
|
||||||
|
line = line,
|
||||||
|
progressFraction = frac,
|
||||||
|
indeterminate = indeterminate,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private companion object {
|
||||||
|
private const val TITLE_LABEL_SEPARATOR = " — "
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,46 +54,28 @@ class StorageSyncViewModel @Inject constructor(
|
|||||||
|
|
||||||
fun refreshGroups() {
|
fun refreshGroups() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val groups = groupsUseCase.getGroups()
|
updateState(state.value.copy(groups = reloadGroupsUi()))
|
||||||
updateState(
|
|
||||||
state.value.copy(
|
|
||||||
groups = groups.map { StorageSyncGroupUi(it.id, it.storageUuids) },
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createGroup() {
|
fun createGroup() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
updateState(state.value.copy(isBusy = true, userMessage = null))
|
withGroupMutationBusy(clearPicker = true) {
|
||||||
val group = groupsUseCase.createGroup()
|
val group = groupsUseCase.createGroup()
|
||||||
val groups = groupsUseCase.getGroups()
|
UserNotification.TextRes(
|
||||||
updateState(
|
R.string.sync_msg_group_created,
|
||||||
state.value.copy(
|
listOf(group.id),
|
||||||
groups = groups.map { StorageSyncGroupUi(it.id, it.storageUuids) },
|
)
|
||||||
pickerGroupId = null,
|
}
|
||||||
isBusy = false,
|
|
||||||
userMessage = UserNotification.TextRes(
|
|
||||||
R.string.sync_msg_group_created,
|
|
||||||
listOf(group.id),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removeGroup(groupId: String) {
|
fun removeGroup(groupId: String) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
updateState(state.value.copy(isBusy = true, userMessage = null))
|
withGroupMutationBusy {
|
||||||
groupsUseCase.removeGroup(groupId)
|
groupsUseCase.removeGroup(groupId)
|
||||||
val groups = groupsUseCase.getGroups()
|
UserNotification.TextRes(R.string.sync_msg_group_removed)
|
||||||
updateState(
|
}
|
||||||
state.value.copy(
|
|
||||||
groups = groups.map { StorageSyncGroupUi(it.id, it.storageUuids) },
|
|
||||||
isBusy = false,
|
|
||||||
userMessage = UserNotification.TextRes(R.string.sync_msg_group_removed),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,37 +108,25 @@ class StorageSyncViewModel @Inject constructor(
|
|||||||
fun addStorageToCurrentGroup(storageUuid: UUID) {
|
fun addStorageToCurrentGroup(storageUuid: UUID) {
|
||||||
val groupId = state.value.pickerGroupId ?: return
|
val groupId = state.value.pickerGroupId ?: return
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
updateState(state.value.copy(isBusy = true, userMessage = null))
|
withGroupMutationBusy {
|
||||||
groupsUseCase.addStorageToGroup(groupId, storageUuid)
|
groupsUseCase.addStorageToGroup(groupId, storageUuid)
|
||||||
val groups = groupsUseCase.getGroups()
|
UserNotification.TextRes(
|
||||||
updateState(
|
R.string.sync_msg_storage_added,
|
||||||
state.value.copy(
|
listOf(groupId),
|
||||||
groups = groups.map { StorageSyncGroupUi(it.id, it.storageUuids) },
|
)
|
||||||
isBusy = false,
|
}
|
||||||
userMessage = UserNotification.TextRes(
|
|
||||||
R.string.sync_msg_storage_added,
|
|
||||||
listOf(groupId),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removeStorageFromGroup(groupId: String, storageUuid: UUID) {
|
fun removeStorageFromGroup(groupId: String, storageUuid: UUID) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
updateState(state.value.copy(isBusy = true, userMessage = null))
|
withGroupMutationBusy {
|
||||||
groupsUseCase.removeStorageFromGroup(groupId, storageUuid)
|
groupsUseCase.removeStorageFromGroup(groupId, storageUuid)
|
||||||
val groups = groupsUseCase.getGroups()
|
UserNotification.TextRes(
|
||||||
updateState(
|
R.string.sync_msg_storage_removed,
|
||||||
state.value.copy(
|
listOf(groupId),
|
||||||
groups = groups.map { StorageSyncGroupUi(it.id, it.storageUuids) },
|
)
|
||||||
isBusy = false,
|
}
|
||||||
userMessage = UserNotification.TextRes(
|
|
||||||
R.string.sync_msg_storage_removed,
|
|
||||||
listOf(groupId),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,4 +289,24 @@ class StorageSyncViewModel @Inject constructor(
|
|||||||
val storage: IStorage,
|
val storage: IStorage,
|
||||||
val children: List<StorageTreeNode>,
|
val children: List<StorageTreeNode>,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private suspend fun reloadGroupsUi(): List<StorageSyncGroupUi> =
|
||||||
|
groupsUseCase.getGroups().map { StorageSyncGroupUi(it.id, it.storageUuids) }
|
||||||
|
|
||||||
|
private suspend fun withGroupMutationBusy(
|
||||||
|
clearPicker: Boolean = false,
|
||||||
|
block: suspend () -> UserNotification?,
|
||||||
|
) {
|
||||||
|
updateState(state.value.copy(isBusy = true, userMessage = null))
|
||||||
|
val message = block()
|
||||||
|
val groups = reloadGroupsUi()
|
||||||
|
updateState(
|
||||||
|
state.value.copy(
|
||||||
|
groups = groups,
|
||||||
|
pickerGroupId = if (clearPicker) null else state.value.pickerGroupId,
|
||||||
|
isBusy = false,
|
||||||
|
userMessage = message,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user