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.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
@ -67,16 +69,25 @@ fun SettingsMain(vm: TileViewModel, launcher: ActivityResultLauncher<DocumentFil
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val filePath by remember { FileLoader.filePath } val filePath by remember { FileLoader.filePath }
val pathIsNotNull = filePath != null
val ctx = LocalContext.current 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 loadToast = Toast.makeText(ctx, "File successfully loaded.", Toast.LENGTH_LONG)
val clearTilesToast = Toast.makeText(ctx, "Tiles removed from database.", 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 clearHeightToast = Toast.makeText(ctx, "Height data removed.", Toast.LENGTH_LONG)
val selectedFileText = if (pathIsNotNull) filePath!!.split('/').last() else "none"
val fileSelectorRow = @Composable () { val fileSelectorRow = @Composable () {
Row(Modifier.fillMaxWidth()) { Row(Modifier.fillMaxWidth()) {
Button({ launcher.launch(null) }) { Text("Select") } 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 { KtHeightMapTheme {
Scaffold { paddingValues -> Scaffold { paddingValues ->
Column(Modifier.padding(paddingValues)) { Column(Modifier.padding(paddingValues)) {
@ -87,29 +98,34 @@ fun SettingsMain(vm: TileViewModel, launcher: ActivityResultLauncher<DocumentFil
Button({ filePath?.let { Button({ filePath?.let {
coroutineScope.launch(Dispatchers.IO) { coroutineScope.launch(Dispatchers.IO) {
SasSQLiteParser.processZip(it, vm.repository, ctx) SasSQLiteParser.processZip(it, vm.repository, ctx)
enableClearTiles = true
coroutineScope.launch(Dispatchers.Main) { loadToast.show() } coroutineScope.launch(Dispatchers.Main) { loadToast.show() }
} }
} }) { Text("SQLite (.zip)") } } }, enabled = pathIsNotNull) { Text("SQLite (.zip)") }
Button({ filePath?.let { Button({ filePath?.let {
coroutineScope.launch(Dispatchers.IO) { coroutineScope.launch(Dispatchers.IO) {
SasJPEGParser.processZip(it, vm.repository) SasJPEGParser.processZip(it, vm.repository)
enableClearTiles = true
coroutineScope.launch(Dispatchers.Main) { loadToast.show() } coroutineScope.launch(Dispatchers.Main) { loadToast.show() }
} }
} }) { Text("JPEG (.zip)") } } }, enabled = pathIsNotNull) { Text("JPEG (.zip)") }
Button({ filePath?.let { Button({ filePath?.let {
KhmParser.load(it, ctx) KhmParser.load(it, ctx)
enableClearHeight = true
loadToast.show() loadToast.show()
} }) { Text("Height data (.khm)") } } }, enabled = pathIsNotNull) { Text("Height data (.khm)") }
} }
LabeledSection("Clear...", Modifier.padding(24.dp, 8.dp)) { LabeledSection("Clear...", Modifier.padding(24.dp, 8.dp)) {
Button({ coroutineScope.launch { Button({ coroutineScope.launch {
vm.repository.clearTiles() vm.repository.clearTiles()
enableClearTiles = false
clearTilesToast.show() clearTilesToast.show()
} }) { Text("Tile data") } } }, enabled = enableClearTiles) { Text("Tile data") }
Button({ Button({
KhmParser.clear(ctx) KhmParser.clear(ctx)
enableClearHeight = false
clearHeightToast.show() 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 { fun getMaxLevel(): Int {
return coroutineScope.future(Dispatchers.IO){ tileDao.maxLevel() }.join() ?: 2 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" const val HEIGHT_FILE: String = "height.khm"
fun load(filePath: String, ctx: Context) { fun load(filePath: String, ctx: Context) {
if (ctx.getFileStreamPath(HEIGHT_FILE).exists()) { if (fileExists(ctx)) {
return return
} }
@ -33,11 +33,15 @@ class KhmParser {
} }
fun clear(ctx: Context) { fun clear(ctx: Context) {
if (ctx.getFileStreamPath(HEIGHT_FILE).exists()) { if (fileExists(ctx)) {
assert(ctx.deleteFile(HEIGHT_FILE)) 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 { private fun getOffset(header: HeightInfo, x: Int, y: Int): Int {
return (x * header.width + y) * 2 return (x * header.width + y) * 2
} }
@ -70,7 +74,7 @@ class KhmParser {
} }
fun getHeight(lon: Float, lat: Float, ctx: Context): UShort { fun getHeight(lon: Float, lat: Float, ctx: Context): UShort {
if (!ctx.getFileStreamPath(HEIGHT_FILE).exists()) if (!fileExists(ctx))
return 0u.toUShort() return 0u.toUShort()
val dis = DataInputStream(ctx.openFileInput(HEIGHT_FILE)) val dis = DataInputStream(ctx.openFileInput(HEIGHT_FILE))
dis.use { dis.use {