Добавлен foreground сервис

This commit is contained in:
2026-04-18 21:38:09 +03:00
parent db9463c2c6
commit d806e3a8a1
33 changed files with 972 additions and 36 deletions

View File

@@ -1,6 +1,7 @@
package com.github.nullptroma.wallenc.domain.interfaces
import com.github.nullptroma.wallenc.domain.datatypes.StorageEncryptionInfo
import com.github.nullptroma.wallenc.domain.tasks.TaskProgress
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import java.time.Instant
@@ -21,7 +22,7 @@ interface IStorage: IStorageInfo {
suspend fun rename(newName: String)
suspend fun setEncInfo(encInfo: StorageEncryptionInfo?)
suspend fun clearAllContent()
suspend fun clearAllContent(onProgress: suspend (TaskProgress) -> Unit = {})
}
interface IStorageMetaInfo {

View File

@@ -0,0 +1,22 @@
package com.github.nullptroma.wallenc.domain.tasks
import kotlinx.coroutines.flow.StateFlow
interface ITaskOrchestrator {
val pipelineState: StateFlow<PipelineState>
val logLines: StateFlow<List<TaskLogLine>>
val foregroundUi: StateFlow<TaskForegroundUiState>
fun enqueue(
title: String,
requiresForeground: Boolean = true,
work: PipelineWork,
): TaskId
fun cancel(taskId: TaskId): Boolean
/** Cancels the currently running task, if any. */
fun cancelCurrent(): Boolean
fun cancelAll()
}

View File

@@ -0,0 +1,6 @@
package com.github.nullptroma.wallenc.domain.tasks
data class PipelineState(
val tasks: List<PipelineTask>,
val currentTaskId: TaskId?,
)

View File

@@ -0,0 +1,8 @@
package com.github.nullptroma.wallenc.domain.tasks
data class PipelineTask(
val id: TaskId,
val title: String,
val requiresForeground: Boolean,
val state: TaskRunState,
)

View File

@@ -0,0 +1,5 @@
package com.github.nullptroma.wallenc.domain.tasks
fun interface PipelineWork {
suspend fun run(ctx: TaskContext)
}

View File

@@ -0,0 +1,11 @@
package com.github.nullptroma.wallenc.domain.tasks
interface TaskContext {
val taskId: TaskId
suspend fun reportProgress(fraction: Float?, label: String?)
suspend fun reportProgress(progress: TaskProgress) = reportProgress(progress.fraction, progress.label)
fun log(level: TaskLogLevel, message: String)
}

View File

@@ -0,0 +1,9 @@
package com.github.nullptroma.wallenc.domain.tasks
sealed class TaskForegroundUiState {
data object Hidden : TaskForegroundUiState()
data class Visible(
val title: String,
val progress: TaskProgress?,
) : TaskForegroundUiState()
}

View File

@@ -0,0 +1,5 @@
package com.github.nullptroma.wallenc.domain.tasks
import java.util.UUID
data class TaskId(val uuid: UUID = UUID.randomUUID())

View File

@@ -0,0 +1,8 @@
package com.github.nullptroma.wallenc.domain.tasks
enum class TaskLogLevel {
Debug,
Info,
Warn,
Error,
}

View File

@@ -0,0 +1,7 @@
package com.github.nullptroma.wallenc.domain.tasks
data class TaskLogLine(
val timestampMs: Long,
val level: TaskLogLevel,
val message: String,
)

View File

@@ -0,0 +1,7 @@
package com.github.nullptroma.wallenc.domain.tasks
data class TaskProgress(
/** 0f..1f or null if indeterminate */
val fraction: Float?,
val label: String?,
)

View File

@@ -0,0 +1,9 @@
package com.github.nullptroma.wallenc.domain.tasks
sealed class TaskRunState {
data object Queued : TaskRunState()
data class Running(val progress: TaskProgress?) : TaskRunState()
data object Completed : TaskRunState()
data object Cancelled : TaskRunState()
data class Failed(val message: String) : TaskRunState()
}

View File

@@ -5,6 +5,7 @@ import com.github.nullptroma.wallenc.domain.encrypt.Encryptor
import com.github.nullptroma.wallenc.domain.interfaces.IStorage
import com.github.nullptroma.wallenc.domain.interfaces.IStorageInfo
import com.github.nullptroma.wallenc.domain.interfaces.IUnlockManager
import com.github.nullptroma.wallenc.domain.tasks.TaskProgress
import kotlinx.coroutines.flow.first
class ManageStoragesEncryptionUseCase(
@@ -53,13 +54,12 @@ class ManageStoragesEncryptionUseCase(
}
}
suspend fun disableEncryption(storage: IStorageInfo) {
clearAndDisableEncryption(storage)
}
suspend fun clearAndDisableEncryption(storage: IStorageInfo) {
suspend fun clearAndDisableEncryption(
storage: IStorageInfo,
onClearProgress: suspend (TaskProgress) -> Unit = {},
) {
if (storage !is IStorage) return
storage.clearAllContent()
storage.clearAllContent(onClearProgress)
storage.setEncInfo(null)
unlockManager.close(storage)
}