Add configuration
This commit is contained in:
parent
6a6fdd5471
commit
2ee6d875bf
6 changed files with 95 additions and 15 deletions
|
@ -33,9 +33,11 @@ class BackupWorker @AssistedInject constructor(
|
|||
|
||||
val backup = backupRepository.createBackup()
|
||||
|
||||
// TODO multiple destinations support and remove those !!
|
||||
val settings = settingsRepository.getSettings()
|
||||
val worldIds = settingsRepository.getIncludedWorldIds(settings.worldOrder)
|
||||
|
||||
val backupUri = settingsRepository.getDestinations().first().uri.toUri()
|
||||
// TODO multiple destinations support and remove those !!
|
||||
val backupUri = settings.destinationsList.first().uri.toUri()
|
||||
val backupDirectory = DocumentFile.fromTreeUri(applicationContext, backupUri)!!
|
||||
.createDirectory(DateTimeFormatter.ISO_LOCAL_DATE.format(LocalDate.now()) + " (" + backup.id + ")")!!
|
||||
|
||||
|
@ -43,7 +45,7 @@ class BackupWorker @AssistedInject constructor(
|
|||
statusUpdate("#${backup.id} Initialized")
|
||||
Log.d(TAG, "Initialized backup: ${backup.id}")
|
||||
|
||||
settingsRepository.getIncludedWorldIds().forEach {
|
||||
worldIds.forEach {
|
||||
statusUpdate("#${backup.id} Backing up world $it")
|
||||
Log.d(TAG, "Backing up world $it")
|
||||
|
||||
|
@ -63,7 +65,7 @@ class BackupWorker @AssistedInject constructor(
|
|||
contentResolver = applicationContext.contentResolver,
|
||||
source = world.getOrThrow().documentFile!!,
|
||||
target = it,
|
||||
inflate = true
|
||||
inflate = settings.improvement
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,10 +23,29 @@ class SettingsRepository @Inject constructor(
|
|||
return dataStore.data.first()
|
||||
}
|
||||
|
||||
suspend fun updateSettings(
|
||||
improvement: Boolean? = null
|
||||
) {
|
||||
dataStore.updateData {
|
||||
val builder = it.toBuilder()
|
||||
|
||||
if (improvement != null) {
|
||||
builder.setImprovement(improvement)
|
||||
}
|
||||
|
||||
// add here
|
||||
|
||||
builder.build()
|
||||
}
|
||||
}
|
||||
|
||||
/* Worlds */
|
||||
|
||||
suspend fun getIncludedWorldIds(): List<String> {
|
||||
val worldOrder = getSettings().worldOrder
|
||||
return getIncludedWorldIds(getSettings().worldOrder)
|
||||
}
|
||||
|
||||
fun getIncludedWorldIds(worldOrder: WorldOrder): List<String> {
|
||||
return worldOrder.worldIdsList.subList(0, worldOrder.separatorIndex)
|
||||
}
|
||||
|
||||
|
|
|
@ -2,8 +2,10 @@ package eu.m724.pojavbackup.settings.screen.options
|
|||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Checkbox
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
|
@ -18,6 +20,18 @@ fun OptionsScreen(
|
|||
navController: NavController,
|
||||
) {
|
||||
val viewModel: OptionsScreenViewModel = hiltViewModel()
|
||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
|
||||
Column {
|
||||
Toggle(
|
||||
title = "Optimal mode",
|
||||
description = "Uses less space, but requires special treatment to restore",
|
||||
checked = uiState.improvement,
|
||||
onCheckedChange = {
|
||||
viewModel.setOptions(improvement = it)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
|
@ -32,7 +46,26 @@ fun OptionsScreen(
|
|||
Text("Backup now")
|
||||
}
|
||||
|
||||
val status by viewModel.backupStatus.collectAsStateWithLifecycle()
|
||||
Text(text = status)
|
||||
Text(uiState.backupStatus)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun Toggle(
|
||||
title: String,
|
||||
description: String,
|
||||
checked: Boolean,
|
||||
onCheckedChange: (Boolean) -> Unit
|
||||
) {
|
||||
Row {
|
||||
Checkbox(
|
||||
checked = checked,
|
||||
onCheckedChange = onCheckedChange
|
||||
)
|
||||
|
||||
Column {
|
||||
Text(title)
|
||||
Text(description)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package eu.m724.pojavbackup.settings.screen.options
|
||||
|
||||
data class OptionsScreenUiState(
|
||||
val improvement: Boolean = false,
|
||||
val backupStatus: String = ""
|
||||
)
|
|
@ -9,25 +9,30 @@ import androidx.work.WorkRequest
|
|||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import eu.m724.pojavbackup.core.backup.BackupWorker
|
||||
import eu.m724.pojavbackup.core.datastore.SettingsRepository
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class OptionsScreenViewModel @Inject constructor(
|
||||
@ApplicationContext private val appContext: Context
|
||||
@ApplicationContext private val appContext: Context,
|
||||
private val settingsRepository: SettingsRepository
|
||||
) : ViewModel() {
|
||||
private val _backupStatus = MutableStateFlow("Not running")
|
||||
val backupStatus: StateFlow<String> = _backupStatus.asStateFlow()
|
||||
private val _uiState = MutableStateFlow(OptionsScreenUiState())
|
||||
val uiState: StateFlow<OptionsScreenUiState> = _uiState.asStateFlow()
|
||||
|
||||
private val workManager = WorkManager
|
||||
.getInstance(appContext)
|
||||
|
||||
fun backupNow() {
|
||||
_backupStatus.value = "Starting"
|
||||
_uiState.update {
|
||||
it.copy(backupStatus = "Starting")
|
||||
}
|
||||
|
||||
val uuid = UUID.randomUUID()
|
||||
|
||||
|
@ -40,12 +45,26 @@ class OptionsScreenViewModel @Inject constructor(
|
|||
|
||||
viewModelScope.launch {
|
||||
workManager.getWorkInfoByIdFlow(uuid).collect { workInfo ->
|
||||
_backupStatus.value = workInfo?.state.toString()
|
||||
|
||||
workInfo?.progress?.getString("status")?.let {
|
||||
_backupStatus.value = it
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
backupStatus = workInfo?.state.toString() + workInfo?.progress?.getString("status")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun setOptions(
|
||||
improvement: Boolean? = true
|
||||
) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
improvement = improvement ?: it.improvement
|
||||
)
|
||||
}
|
||||
|
||||
viewModelScope.launch {
|
||||
settingsRepository.updateSettings(improvement = improvement)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -20,6 +20,7 @@ message Settings {
|
|||
repeated BackupDestination destinations = 3;
|
||||
|
||||
string sourceUri = 4;
|
||||
bool improvement = 5;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue