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 return changed
} }
@Composable @Composable
fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) { fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
val visits by Data.visits.visitsFlow.collectAsState()
var showEdit by remember { mutableStateOf(false) } var showEdit by remember { mutableStateOf(false) }
val tabs : SnapshotStateList<GeoLoc> = remember { mutableStateListOf(loc) } val tabs : SnapshotStateList<GeoLoc> = remember { mutableStateListOf(loc) }
val ctx = LocalContext.current val ctx = LocalContext.current
@@ -96,10 +108,13 @@ fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
selectedTab = tabs.lastIndex selectedTab = tabs.lastIndex
} }
SideEffect { SideEffect {
// visits is used to trigger sync whenever data changes
if (visits.isNotEmpty() || true) {
if (syncVisited()) { if (syncVisited()) {
Data.saveData() Data.saveData()
} }
} }
}
BackHandler { BackHandler {
if (tabs.size > 1) tabs.removeAt(tabs.lastIndex) if (tabs.size > 1) tabs.removeAt(tabs.lastIndex)
else onExit() else onExit()
@@ -109,6 +124,7 @@ fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
showEdit = false showEdit = false
if (it) { if (it) {
Data.visits.setVisited(Data.selected_geoloc, NO_GROUP) Data.visits.setVisited(Data.selected_geoloc, NO_GROUP)
syncVisited()
Data.saveData() Data.saveData()
if (Data.selected_geoloc!=null && Data.selected_geoloc!!.children.any { itc-> Data.visits.getVisited(itc) != NO_GROUP }) { 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) { if (Data.selected_group != null && Data.selected_geoloc != null) {
Data.visits.setVisited(Data.selected_geoloc, Data.selected_group!!.key) Data.visits.setVisited(Data.selected_geoloc, Data.selected_group!!.key)
syncVisited()
Data.saveData() Data.saveData()
} }
Data.selected_geoloc = null Data.selected_geoloc = null
@@ -164,7 +181,8 @@ fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
Data.selected_group = null Data.selected_group = null
showEdit=true 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] val old = groups[key]
if (old != null && old.name == name && old.color.color == col.color) return if (old != null && old.name == name && old.color.color == col.color) return
groups[key] = Group(key, name, col) groups[key] = Group(key, name, col)
_groupsFlow.value = groups.values.toList() updateFlow()
} }
fun deleteGroup(key: Int) { fun deleteGroup(key: Int) {
groups.remove(key) groups.remove(key)
updateFlow()
}
private fun updateFlow() {
_groupsFlow.value = groups.values.toList() _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) { fun setVisited(key: GeoLoc?, b: Int) {
if (key == null || locs[key.code] == b) if (key == null || locs[key.code] == b)
return return
_visitsFlow.value = _visitsFlow.value.toMutableMap().apply {
this[key.code] = b
}
locs[key.code] = b locs[key.code] = b
updateFlow()
}
private fun updateFlow() {
_visitsFlow.value = locs.toMap()
} }
fun deleteVisited(key: Int) { fun deleteVisited(key: Int) {
val keysToDelete = locs val keysToDelete = locs
.filter { it.value == key } .filter { it.value == key }
.map { it.key } .map { it.key }
_visitsFlow.value = _visitsFlow.value.toMutableMap().apply { if (keysToDelete.isEmpty()) return
keysToDelete.forEach { this.remove(it)}
}
keysToDelete.forEach { keysToDelete.forEach {
locs.remove(it) locs.remove(it)
} }
updateFlow()
} }
fun getVisited(key: GeoLoc): Int { fun getVisited(key: GeoLoc): Int {
@@ -60,13 +61,19 @@ class Visits(val id: Int, private val locs: HashMap<String, Int>) {
} }
fun reassignAllVisitedToGroup(group: Int) { fun reassignAllVisitedToGroup(group: Int) {
var changed = false
val keys = locs.filter { (_, grp) -> val keys = locs.filter { (_, grp) ->
grp !in listOf(NO_GROUP, AUTO_GROUP) grp !in listOf(NO_GROUP, AUTO_GROUP)
}.keys }.keys
keys.forEach { keys.forEach {
if (locs[it] != group) {
locs[it] = group locs[it] = group
changed = true
}
}
if (changed) {
updateFlow()
} }
_visitsFlow.value = locs
} }
@OptIn(ExperimentalSerializationApi::class) @OptIn(ExperimentalSerializationApi::class)