Fix region saving bug

This commit is contained in:
soraefir
2026-06-06 19:30:18 +02:00
parent f2a5efcec5
commit cc1a0c1aca
3 changed files with 41 additions and 12 deletions

View File

@@ -82,11 +82,23 @@ fun syncVisited(loc: GeoLoc?=World.WWW): Boolean {
}
}
}
// Sync World from Continents
if (loc != null && Data.visits.getVisited(loc) in listOf(AUTO_GROUP, NO_GROUP)) {
val newState = if(loc.children.any { Data.visits.getVisited(it) != NO_GROUP })
AUTO_GROUP
else
NO_GROUP
if (Data.visits.getVisited(loc) != newState) {
Data.visits.setVisited(loc, newState)
changed = true
}
}
return changed
}
@Composable
fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
val visits by Data.visits.visitsFlow.collectAsState()
var showEdit by remember { mutableStateOf(false) }
val tabs : SnapshotStateList<GeoLoc> = remember { mutableStateListOf(loc) }
val ctx = LocalContext.current
@@ -96,8 +108,11 @@ fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
selectedTab = tabs.lastIndex
}
SideEffect {
if (syncVisited()) {
Data.saveData()
// visits is used to trigger sync whenever data changes
if (visits.isNotEmpty() || true) {
if (syncVisited()) {
Data.saveData()
}
}
}
BackHandler {
@@ -109,6 +124,7 @@ fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
showEdit = false
if (it) {
Data.visits.setVisited(Data.selected_geoloc, NO_GROUP)
syncVisited()
Data.saveData()
if (Data.selected_geoloc!=null && Data.selected_geoloc!!.children.any { itc-> Data.visits.getVisited(itc) != NO_GROUP }) {
@@ -117,6 +133,7 @@ fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
}
if (Data.selected_group != null && Data.selected_geoloc != null) {
Data.visits.setVisited(Data.selected_geoloc, Data.selected_group!!.key)
syncVisited()
Data.saveData()
}
Data.selected_geoloc = null
@@ -164,7 +181,8 @@ fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
Data.selected_group = null
showEdit=true
}
syncVisited()
Data.saveData()
})
}

View File

@@ -35,11 +35,15 @@ class Groups(val id: Int, @SerialName("grps") private val groups: HashMap<Int, G
val old = groups[key]
if (old != null && old.name == name && old.color.color == col.color) return
groups[key] = Group(key, name, col)
_groupsFlow.value = groups.values.toList()
updateFlow()
}
fun deleteGroup(key: Int) {
groups.remove(key)
updateFlow()
}
private fun updateFlow() {
_groupsFlow.value = groups.values.toList()
}

View File

@@ -21,22 +21,23 @@ class Visits(val id: Int, private val locs: HashMap<String, Int>) {
fun setVisited(key: GeoLoc?, b: Int) {
if (key == null || locs[key.code] == b)
return
_visitsFlow.value = _visitsFlow.value.toMutableMap().apply {
this[key.code] = b
}
locs[key.code] = b
updateFlow()
}
private fun updateFlow() {
_visitsFlow.value = locs.toMap()
}
fun deleteVisited(key: Int) {
val keysToDelete = locs
.filter { it.value == key }
.map { it.key }
_visitsFlow.value = _visitsFlow.value.toMutableMap().apply {
keysToDelete.forEach { this.remove(it)}
}
if (keysToDelete.isEmpty()) return
keysToDelete.forEach {
locs.remove(it)
}
updateFlow()
}
fun getVisited(key: GeoLoc): Int {
@@ -60,13 +61,19 @@ class Visits(val id: Int, private val locs: HashMap<String, Int>) {
}
fun reassignAllVisitedToGroup(group: Int) {
var changed = false
val keys = locs.filter { (_, grp) ->
grp !in listOf(NO_GROUP, AUTO_GROUP)
}.keys
keys.forEach {
locs[it] = group
if (locs[it] != group) {
locs[it] = group
changed = true
}
}
if (changed) {
updateFlow()
}
_visitsFlow.value = locs
}
@OptIn(ExperimentalSerializationApi::class)