Update saving logic

This commit is contained in:
soraefir
2026-06-06 18:54:09 +02:00
parent cfa784991b
commit f2a5efcec5
6 changed files with 35 additions and 14 deletions

View File

@@ -50,8 +50,8 @@ class MainScreen : ComponentActivity() {
super.onCreate(savedInstanceState)
actionBar?.hide()
Settings.start(this)
GeoLocImporter.importStates(this)
Data.loadData(this, Int.MIN_VALUE)
GeoLocImporter.importStates(this)
setContent {
SysTheme {

View File

@@ -168,8 +168,10 @@ fun SettingsScreen(navController: NavHostController = settingsNav()) {
EditPlaceDialog(true) {
showEdit = false
val g = Data.selected_group
if (it && g != null)
if (it && g != null) {
Data.visits.reassignAllVisitedToGroup(g.key)
Data.saveData()
}
}
LazyColumn(
@@ -216,8 +218,10 @@ fun SettingsScreen(navController: NavHostController = settingsNav()) {
deleteMode = true,
onDismiss = {
val g = Data.selected_group
if (g != null)
if (g != null) {
Data.visits.reassignAllVisitedToGroup(g.key)
Data.saveData()
}
showDialog = false
})
}

View File

@@ -54,24 +54,36 @@ fun EditPlaceScreenPreview(){
EditPlaceScreen(Group.EEE)
}
fun syncVisited(loc: GeoLoc?=World.WWW){
fun syncVisited(loc: GeoLoc?=World.WWW): Boolean {
var changed = false
loc?.children?.forEach { tt ->
tt.children.forEach {itc->
if(Data.visits.getVisited(itc) in listOf(AUTO_GROUP,NO_GROUP)) {
if(itc.children.any { c -> Data.visits.getVisited(c) != NO_GROUP })
Data.visits.setVisited(itc, AUTO_GROUP)
val newState = if(itc.children.any { c -> Data.visits.getVisited(c) != NO_GROUP })
AUTO_GROUP
else
Data.visits.setVisited(itc, NO_GROUP)
NO_GROUP
if (Data.visits.getVisited(itc) != newState) {
Data.visits.setVisited(itc, newState)
changed = true
}
}
}
if(Data.visits.getVisited(tt) in listOf(AUTO_GROUP,NO_GROUP)) {
if(tt.children.any { itc -> Data.visits.getVisited(itc) != NO_GROUP })
Data.visits.setVisited(tt, AUTO_GROUP)
val newState = if(tt.children.any { itc -> Data.visits.getVisited(itc) != NO_GROUP })
AUTO_GROUP
else
Data.visits.setVisited(tt, NO_GROUP)
NO_GROUP
if (Data.visits.getVisited(tt) != newState) {
Data.visits.setVisited(tt, newState)
changed = true
}
}
}
return changed
}
@Composable
fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
@@ -84,7 +96,9 @@ fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
selectedTab = tabs.lastIndex
}
SideEffect {
syncVisited()
if (syncVisited()) {
Data.saveData()
}
}
BackHandler {
if (tabs.size > 1) tabs.removeAt(tabs.lastIndex)
@@ -144,6 +158,7 @@ fun EditPlaceScreen(loc: GeoLoc, onExit:()->Unit={}) {
Data.visits.getVisited(itc)!= NO_GROUP } == true) AUTO_GROUP
else NO_GROUP
)
Data.saveData()
Data.selected_group = null
} else {
Data.selected_group = null

View File

@@ -43,14 +43,14 @@ object Data {
if (groups.size() == 0) {
groups.setGroup(DEFAULT_GROUP, "Visited",
ContextCompat.getColor(ctx, R.color.blue).toDrawable())
saveData()
}
saveData()
}
fun saveData() {
if(groups.id != visits.id) return
val id = groups.id
sharedPreferences.edit {
sharedPreferences.edit(commit=true) {
putString("groups_$id", groupsSerial.writeTo(groups))
putString("visits_$id", visitsSerial.writeTo(visits))
}

View File

@@ -32,6 +32,8 @@ class Groups(val id: Int, @SerialName("grps") private val groups: HashMap<Int, G
val groupsFlow: StateFlow<List<Group>> = _groupsFlow.asStateFlow()
fun setGroup(key: Int, name: String, col: ColorDrawable) {
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()
}

View File

@@ -19,7 +19,7 @@ class Visits(val id: Int, private val locs: HashMap<String, Int>) {
val visitsFlow: StateFlow<Map<String,Int>> = _visitsFlow
fun setVisited(key: GeoLoc?, b: Int) {
if (key == null)
if (key == null || locs[key.code] == b)
return
_visitsFlow.value = _visitsFlow.value.toMutableMap().apply {
this[key.code] = b