Update load/clear buttons on data availability change

This commit is contained in:
Alexey 2025-11-11 13:25:50 +03:00
commit d30721cdf6
3 changed files with 33 additions and 9 deletions

View file

@ -18,8 +18,10 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
@ -67,16 +69,25 @@ fun SettingsMain(vm: TileViewModel, launcher: ActivityResultLauncher<DocumentFil
val coroutineScope = rememberCoroutineScope()
val filePath by remember { FileLoader.filePath }
val pathIsNotNull = filePath != null
val ctx = LocalContext.current
var enableClearTiles by remember { mutableStateOf(vm.repository.containsTiles()) }
var enableClearHeight by remember { mutableStateOf(KhmParser.fileExists(ctx)) }
val loadToast = Toast.makeText(ctx, "File successfully loaded.", Toast.LENGTH_LONG)
val clearTilesToast = Toast.makeText(ctx, "Tiles removed from database.", Toast.LENGTH_LONG)
val clearHeightToast = Toast.makeText(ctx, "Height data removed.", Toast.LENGTH_LONG)
val selectedFileText = if (pathIsNotNull) filePath!!.split('/').last() else "none"
val fileSelectorRow = @Composable () {
Row(Modifier.fillMaxWidth()) {
Button({ launcher.launch(null) }) { Text("Select") }
Text("Selected file: none", Modifier.padding(8.dp, 0.dp).align(Alignment.CenterVertically))
Text("Selected file: $selectedFileText", Modifier.padding(8.dp, 0.dp).align(Alignment.CenterVertically))
}
}
KtHeightMapTheme {
Scaffold { paddingValues ->
Column(Modifier.padding(paddingValues)) {
@ -87,29 +98,34 @@ fun SettingsMain(vm: TileViewModel, launcher: ActivityResultLauncher<DocumentFil
Button({ filePath?.let {
coroutineScope.launch(Dispatchers.IO) {
SasSQLiteParser.processZip(it, vm.repository, ctx)
enableClearTiles = true
coroutineScope.launch(Dispatchers.Main) { loadToast.show() }
}
} }) { Text("SQLite (.zip)") }
} }, enabled = pathIsNotNull) { Text("SQLite (.zip)") }
Button({ filePath?.let {
coroutineScope.launch(Dispatchers.IO) {
SasJPEGParser.processZip(it, vm.repository)
enableClearTiles = true
coroutineScope.launch(Dispatchers.Main) { loadToast.show() }
}
} }) { Text("JPEG (.zip)") }
} }, enabled = pathIsNotNull) { Text("JPEG (.zip)") }
Button({ filePath?.let {
KhmParser.load(it, ctx)
enableClearHeight = true
loadToast.show()
} }) { Text("Height data (.khm)") }
} }, enabled = pathIsNotNull) { Text("Height data (.khm)") }
}
LabeledSection("Clear...", Modifier.padding(24.dp, 8.dp)) {
Button({ coroutineScope.launch {
vm.repository.clearTiles()
enableClearTiles = false
clearTilesToast.show()
} }) { Text("Tile data") }
} }, enabled = enableClearTiles) { Text("Tile data") }
Button({
KhmParser.clear(ctx)
enableClearHeight = false
clearHeightToast.show()
}) { Text("Height data") }
}, enabled = enableClearHeight) { Text("Height data") }
}
}
}

View file

@ -25,4 +25,8 @@ class TileRepository(private val tileDao: TileDao) {
fun getMaxLevel(): Int {
return coroutineScope.future(Dispatchers.IO){ tileDao.maxLevel() }.join() ?: 2
}
fun containsTiles(): Boolean {
return coroutineScope.future(Dispatchers.IO){ tileDao.maxLevel() }.join() != null
}
}

View file

@ -21,7 +21,7 @@ class KhmParser {
const val HEIGHT_FILE: String = "height.khm"
fun load(filePath: String, ctx: Context) {
if (ctx.getFileStreamPath(HEIGHT_FILE).exists()) {
if (fileExists(ctx)) {
return
}
@ -33,11 +33,15 @@ class KhmParser {
}
fun clear(ctx: Context) {
if (ctx.getFileStreamPath(HEIGHT_FILE).exists()) {
if (fileExists(ctx)) {
assert(ctx.deleteFile(HEIGHT_FILE))
}
}
fun fileExists(ctx: Context): Boolean {
return (ctx.getFileStreamPath(HEIGHT_FILE).exists())
}
private fun getOffset(header: HeightInfo, x: Int, y: Int): Int {
return (x * header.width + y) * 2
}
@ -70,7 +74,7 @@ class KhmParser {
}
fun getHeight(lon: Float, lat: Float, ctx: Context): UShort {
if (!ctx.getFileStreamPath(HEIGHT_FILE).exists())
if (!fileExists(ctx))
return 0u.toUShort()
val dis = DataInputStream(ctx.openFileInput(HEIGHT_FILE))
dis.use {