-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
291 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package ui | ||
|
||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Archive | ||
import androidx.compose.material.icons.sharp.Delete | ||
import androidx.compose.material3.* | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import kotlinx.coroutines.launch | ||
import kotlinx.datetime.Instant | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.toLocalDateTime | ||
import org.koin.compose.koinInject | ||
import ui.model.ModelState | ||
import ui.model.Screen | ||
import viewmodels.BackupScreenViewModel | ||
|
||
object BackupsScreen : Screen { | ||
|
||
private val padding = Modifier.padding(16.dp) | ||
private val paddingSmall = Modifier.padding(8.dp) | ||
private val filePattern = Regex(".*\\.([0-9]+)\\.bkp$") | ||
|
||
override val name: String | ||
get() = "Backups" | ||
|
||
@Composable | ||
override fun icon() { | ||
Icon(Icons.Filled.Archive, contentDescription = name) | ||
} | ||
|
||
@Composable | ||
override fun content() { | ||
Column( | ||
modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
) { | ||
val backupScreenViewModel = koinInject<BackupScreenViewModel>() | ||
val scope = rememberCoroutineScope() | ||
var backups by remember { mutableStateOf(listOf<String>()) } | ||
suspend fun loadBackups() { | ||
backupScreenViewModel.backupFiles.collect { | ||
if (it is ModelState.Success) { | ||
backups = it.result | ||
} | ||
} | ||
} | ||
LaunchedEffect(Unit) { | ||
scope.launch { | ||
loadBackups() | ||
} | ||
} | ||
BackupContent( | ||
backups, | ||
onClear = { | ||
scope.launch { | ||
backupScreenViewModel.clearBackups() | ||
loadBackups() | ||
} | ||
}, | ||
onDelete = { | ||
scope.launch { | ||
backupScreenViewModel.delete(it) | ||
loadBackups() | ||
} | ||
}) | ||
|
||
} | ||
|
||
} | ||
|
||
@Composable | ||
fun BackupContent(backups: List<String>, onDelete: (String) -> Unit, onClear: () -> Unit) { | ||
Card( | ||
modifier = Modifier.width(width = 900.dp).fillMaxHeight().padding(20.dp) | ||
) { | ||
Column(modifier = padding) { | ||
Row { | ||
Text( | ||
text = "Backups", | ||
fontSize = 30.sp, | ||
modifier = padding, | ||
textAlign = TextAlign.Center, | ||
) | ||
} | ||
Column { | ||
backups.forEach { backup -> BackupItem(backup) { onDelete(backup) } } | ||
} | ||
Row(modifier = Modifier.fillMaxSize()) { | ||
Spacer(modifier = Modifier.weight(1f)) | ||
ElevatedButton(onClick = onClear, modifier = padding) { | ||
Text("Clear All Backups") | ||
} | ||
Spacer(modifier = Modifier.weight(1f)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun BackupItem(backup: String, onDelete: () -> Unit) { | ||
Column { | ||
OutlinedCard( | ||
modifier = Modifier.padding(16.dp, 4.dp).fillMaxWidth() | ||
) { | ||
val ts = filePattern.find(backup)?.groupValues?.get(1)?.toLongOrNull() ?: 0 | ||
val modified = Instant.fromEpochSeconds(ts).toLocalDateTime(TimeZone.currentSystemDefault()) | ||
Row(verticalAlignment = Alignment.CenterVertically) { | ||
Column(modifier = Modifier.weight(1f).padding(8.dp)) { | ||
Text(backup.substringAfterLast('/'), modifier = paddingSmall) | ||
Text( | ||
"Completed at ${modified.date} ${modified.time}", | ||
style = MaterialTheme.typography.labelSmall, | ||
modifier = paddingSmall | ||
) | ||
} | ||
IconButton(onClick = onDelete) { | ||
Icon(Icons.Sharp.Delete, "Delete Backup") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
composeApp/src/commonMain/kotlin/viewmodels/BackupScreenViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package viewmodels | ||
|
||
import io.github.jsixface.common.Api | ||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.plugins.resources.* | ||
import io.ktor.http.* | ||
import kotlinx.coroutines.flow.flow | ||
import ui.model.ModelState | ||
import ui.model.ModelState.* | ||
import util.log | ||
|
||
class BackupScreenViewModel(private val client: HttpClient) { | ||
|
||
init { | ||
log("New backupScreenViewModel") | ||
} | ||
|
||
val backupFiles = flow<ModelState<List<String>>> { | ||
emit(Init()) | ||
val result = kotlin.runCatching { client.get(Api.Backups) }.getOrNull() | ||
log("Got result: $result") | ||
if (result?.status?.isSuccess() == true) emit(Success(result.body())) | ||
else emit(Error("Error. Status: ${result?.status}")) | ||
} | ||
|
||
suspend fun delete(backup: String) { | ||
client.delete(Api.Backups.Backup(path = backup)) | ||
} | ||
|
||
suspend fun clearBackups() { | ||
client.delete(Api.Backups) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import androidx.compose.ui.unit.DpSize | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.window.Window | ||
import androidx.compose.ui.window.WindowState | ||
import androidx.compose.ui.window.application | ||
import io.github.aakira.napier.DebugAntilog | ||
import io.github.aakira.napier.Napier | ||
|
||
object DesktopMain { | ||
@JvmStatic | ||
fun main(args: Array<String>) = application { | ||
Napier.base(DebugAntilog()) | ||
Window( | ||
onCloseRequest = ::exitApplication, | ||
title = "CodeXvert", | ||
state = WindowState(size = DpSize(1200.dp, 800.dp)) | ||
) { | ||
App() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
server/src/main/kotlin/io/github/jsixface/codexvert/api/BackupApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.github.jsixface.codexvert.api | ||
|
||
import io.github.jsixface.codexvert.logger | ||
import java.io.File | ||
|
||
class BackupApi { | ||
|
||
private val logger = logger<BackupApi>() | ||
private val backupPattern = ".*[0-9]+\\.bkp$".toRegex() | ||
|
||
fun getBackups(): List<String> { | ||
val settings = SavedData.load().settings | ||
return settings.libraryLocations.flatMap { location -> | ||
File(location).walk().filter { it.name.matches(backupPattern) }.map { it.absolutePath } | ||
} | ||
} | ||
|
||
fun deleteBackup(path: String) { | ||
logger.info("Deleting backup: $path") | ||
File(path).delete() | ||
} | ||
|
||
fun deleteAllBackups() { | ||
val settings = SavedData.load().settings | ||
settings.libraryLocations.forEach { location -> | ||
logger.info("Deleting all backups in $location") | ||
File(location).walk().filter { it.name.matches(backupPattern) }.forEach { it.delete() } | ||
} | ||
} | ||
} |
Oops, something went wrong.