automated scale range setting

This commit is contained in:
Alexey 2025-09-30 12:34:04 +03:00
commit 182f0b29b0
4 changed files with 14 additions and 2 deletions

View file

@ -3,6 +3,7 @@ package com.mirenkov.ktheightmap
import android.app.Application
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
@ -84,6 +85,7 @@ class MainActivity : ComponentActivity() {
@Composable
fun Main(vm: TileViewModel = viewModel()) {
var scale by rememberSaveable { vm.scale }
val maxScale by rememberSaveable { vm.maxLevel }
val coroutineScope = rememberCoroutineScope()
val tileContainer = TileContainer(vm, coroutineScope)
KtHeightMapTheme {
@ -111,7 +113,7 @@ fun Main(vm: TileViewModel = viewModel()) {
Slider(
value = scale,
onValueChange = { scale = it },
valueRange = 1F..14F,
valueRange = 2F..maxScale.toFloat(),
modifier = Modifier.align(Alignment.CenterStart)
)
}

View file

@ -14,4 +14,7 @@ interface TileDao {
@Query("delete from tiles")
fun clearTiles()
@Query("select max(level) from tiles")
fun maxLevel(): Int?
}

View file

@ -21,4 +21,8 @@ class TileRepository(private val tileDao: TileDao) {
fun clearTiles() {
coroutineScope.launch(Dispatchers.IO) { tileDao.clearTiles() }
}
fun getMaxLevel(): Int {
return coroutineScope.future(Dispatchers.IO){ tileDao.maxLevel() }.join() ?: 1
}
}

View file

@ -2,6 +2,7 @@ package com.mirenkov.ktheightmap
import android.app.Application
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.ViewModel
@ -17,7 +18,8 @@ class TileViewModel(application: Application): ViewModel() {
val mapOffsetX = mutableFloatStateOf(-646.65625F)
val mapOffsetY = mutableFloatStateOf(-1157.2814F)
val scale = mutableFloatStateOf(1F)
val scale = mutableFloatStateOf(2F)
var maxLevel = mutableIntStateOf(1)
var halvedOffsetX: Float? = null
var halvedOffsetY: Float? = null
@ -29,5 +31,6 @@ class TileViewModel(application: Application): ViewModel() {
val tileDb = TileDB.getInstance(application)
val tileDao = tileDb.tileDao()
repository = TileRepository(tileDao)
maxLevel.intValue = repository.getMaxLevel()
}
}