Correct bug when disabling and re-enabling regions

This commit is contained in:
fgerber 2024-04-10 11:20:08 +02:00
parent 16faddb602
commit 98591691a3
5 changed files with 84 additions and 37 deletions

View File

@ -106,6 +106,10 @@ class GeolocListAdapter(
if (clear) { if (clear) {
Data.visits.setVisited(Data.selected_geoloc, NO_GROUP) Data.visits.setVisited(Data.selected_geoloc, NO_GROUP)
Data.saveData() Data.saveData()
if (_parentGeoLoc.children.all { Data.visits.getVisited(it) == NO_GROUP }) {
Data.clearing_geoloc = _parentGeoLoc
}
} }
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 ?: NO_GROUP) Data.visits.setVisited(Data.selected_geoloc, Data.selected_group?.key ?: NO_GROUP)
@ -123,6 +127,14 @@ class GeolocListAdapter(
MaterialCheckBox.STATE_CHECKED MaterialCheckBox.STATE_CHECKED
} else if (Data.visits.getVisited(geoLoc) !in listOf(NO_GROUP, AUTO_GROUP)) { } else if (Data.visits.getVisited(geoLoc) !in listOf(NO_GROUP, AUTO_GROUP)) {
MaterialCheckBox.STATE_CHECKED MaterialCheckBox.STATE_CHECKED
} else if (
Data.visits.getVisited(geoLoc) == AUTO_GROUP
&& Settings.isRegional(ctx)
&& geoLoc.type == GeoLoc.LocType.COUNTRY
&& (geoLoc.children.all { Data.visits.getVisited(it) == NO_GROUP })
&& geoLoc != Data.clearing_geoloc
) {
MaterialCheckBox.STATE_CHECKED
} else if (geoLoc.children.isNotEmpty() && geoLoc.children.all { } else if (geoLoc.children.isNotEmpty() && geoLoc.children.all {
Data.visits.getVisited( Data.visits.getVisited(
it it
@ -135,6 +147,9 @@ class GeolocListAdapter(
MaterialCheckBox.STATE_INDETERMINATE MaterialCheckBox.STATE_INDETERMINATE
} else { } else {
Data.visits.setVisited(geoLoc, NO_GROUP) Data.visits.setVisited(geoLoc, NO_GROUP)
if (geoLoc == Data.clearing_geoloc) {
Data.clearing_geoloc = null
}
MaterialCheckBox.STATE_UNCHECKED MaterialCheckBox.STATE_UNCHECKED
} }
Data.saveData() Data.saveData()

View File

@ -6,6 +6,7 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import net.helcel.beans.R import net.helcel.beans.R
import net.helcel.beans.activity.MainActivity import net.helcel.beans.activity.MainActivity
import net.helcel.beans.countries.GeoLocImporter import net.helcel.beans.countries.GeoLocImporter
@ -52,10 +53,25 @@ class SettingsFragment : PreferenceFragmentCompat(), DialogCloser {
// Toggle regional geolocs // Toggle regional geolocs
findPreference<Preference>(getString(R.string.key_regional))?.setOnPreferenceChangeListener { _, key -> findPreference<Preference>(getString(R.string.key_regional))?.setOnPreferenceChangeListener { _, key ->
when (key as String) { when (key as String) {
ctx.getString(R.string.off) -> GeoLocImporter.clearStates() ctx.getString(R.string.off) -> {
ctx.getString(R.string.on) -> GeoLocImporter.importStates(ctx, true) MaterialAlertDialogBuilder(requireActivity())
.setMessage(R.string.delete_regions)
.setPositiveButton(android.R.string.ok) { _, _ ->
GeoLocImporter.clearStates()
val sp = PreferenceManager.getDefaultSharedPreferences(ctx)
sp.edit().putString(ctx.getString(R.string.key_regional), ctx.getString(R.string.off)).apply()
refreshPreferences()
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
false
}
ctx.getString(R.string.on) -> {
GeoLocImporter.importStates(ctx, true)
true
}
else -> false
} }
true
} }
@ -108,12 +124,15 @@ class SettingsFragment : PreferenceFragmentCompat(), DialogCloser {
// Actually change preference // Actually change preference
val ctx = requireContext() val ctx = requireContext()
val sp = PreferenceManager.getDefaultSharedPreferences(ctx) val sp = PreferenceManager.getDefaultSharedPreferences(ctx)
sp.edit().putString(ctx.getString(R.string.key_group), ctx.getString(R.string.off)) sp.edit().putString(ctx.getString(R.string.key_group), ctx.getString(R.string.off)).apply()
.apply()
// Refresh entire preference fragment to reflect changes // Refresh entire preference fragment to reflect changes
preferenceScreen.removeAll() refreshPreferences()
onCreatePreferences(savedInstanceState, rootKey)
} }
} }
private fun refreshPreferences() {
preferenceScreen.removeAll()
onCreatePreferences(savedInstanceState, rootKey)
}
} }

View File

@ -10,42 +10,40 @@ import net.helcel.beans.countries.GeoLoc
import java.util.HashMap import java.util.HashMap
object Data { object Data {
var visits : Visits = Visits(0, HashMap())
var groups : Groups = Groups(0,HashMap())
var selected_group : Groups.Group? = null
var selected_geoloc: GeoLoc? = null
var clearing_geoloc: GeoLoc? = null
var visits : Visits = Visits(0, HashMap()) private val groupsSerial = Groups.GroupsSerializer()
var groups : Groups = Groups(0,HashMap()) private val visitsSerial = Visits.VisitsSerializer()
var selected_group : Groups.Group? = null private lateinit var sharedPreferences: SharedPreferences
var selected_geoloc: GeoLoc? = null
private val groupsSerial = Groups.GroupsSerializer() fun loadData(ctx: Context, id:Int) {
private val visitsSerial = Visits.VisitsSerializer() sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx)
private lateinit var sharedPreferences: SharedPreferences val groupsString = sharedPreferences.getString("groups_$id",null)
val visitsString = sharedPreferences.getString("visits_$id",null)
fun loadData(ctx: Context, id:Int) { groups = if(!groupsString.isNullOrEmpty()) groupsSerial.readFrom(groupsString.byteInputStream()) else groupsSerial.defaultValue
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx) visits = if(!visitsString.isNullOrEmpty()) visitsSerial.readFrom(visitsString.byteInputStream()) else visitsSerial.defaultValue
val groupsString = sharedPreferences.getString("groups_$id",null) // Add default group "Visited" with app's color if there is no group already
val visitsString = sharedPreferences.getString("visits_$id",null) if (groups.size() == 0) {
groups.setGroup(DEFAULT_GROUP, "Visited", ColorDrawable(ContextCompat.getColor(ctx, R.color.blue)))
groups = if(!groupsString.isNullOrEmpty()) groupsSerial.readFrom(groupsString.byteInputStream()) else groupsSerial.defaultValue saveData()
visits = if(!visitsString.isNullOrEmpty()) visitsSerial.readFrom(visitsString.byteInputStream()) else visitsSerial.defaultValue }
// Add default group "Visited" with app's color if there is no group already
if (groups.size() == 0) {
groups.setGroup(DEFAULT_GROUP, "Visited", ColorDrawable(ContextCompat.getColor(ctx, R.color.blue)))
saveData()
} }
} fun saveData() {
if(groups.id != visits.id) return
fun saveData() { val id = groups.id
if(groups.id != visits.id) return val editor = sharedPreferences.edit()
val id = groups.id editor.putString("groups_$id", groupsSerial.writeTo(groups))
val editor = sharedPreferences.edit() editor.putString("visits_$id", visitsSerial.writeTo(visits))
editor.putString("groups_$id", groupsSerial.writeTo(groups)) editor.apply()
editor.putString("visits_$id", visitsSerial.writeTo(visits))
editor.apply()
}
} }
}

View File

@ -1,6 +1,8 @@
package net.helcel.beans.svg package net.helcel.beans.svg
import android.content.Context import android.content.Context
import net.helcel.beans.countries.Country
import net.helcel.beans.countries.GeoLoc
import net.helcel.beans.countries.World import net.helcel.beans.countries.World
import net.helcel.beans.helper.AUTO_GROUP import net.helcel.beans.helper.AUTO_GROUP
import net.helcel.beans.helper.Data import net.helcel.beans.helper.Data
@ -45,6 +47,18 @@ class CSSWrapper(private val ctx: Context) {
colorToHex6(colorWrapper(ctx, android.R.attr.colorPrimary)) colorToHex6(colorWrapper(ctx, android.R.attr.colorPrimary))
};}" };}"
} }
else if (Settings.isRegional(ctx) && k == AUTO_GROUP) {
Country.entries.filter { it.code in v }
.filter {
it.children.all { itt ->
visits.getVisited(itt) == NO_GROUP
}
}.map {
it.code
}.joinToString(",") { "#${it}$id,#${it}" } + "{fill:${
colorToHex6(colorWrapper(ctx, android.R.attr.colorPrimary))
};}"
}
else if (groups.getGroupFromKey(k).key == NO_GROUP) else if (groups.getGroupFromKey(k).key == NO_GROUP)
"" ""
else else

View File

@ -22,6 +22,7 @@
<string name="about_beans">About the Beans application</string> <string name="about_beans">About the Beans application</string>
<string name="delete_group">Are your sure you want to delete this group and remove all its country mappings?</string> <string name="delete_group">Are your sure you want to delete this group and remove all its country mappings?</string>
<string name="select_group">Select one group you want to keep. All others will be deleted and its mappings reassigned to the group you choose here.</string> <string name="select_group">Select one group you want to keep. All others will be deleted and its mappings reassigned to the group you choose here.</string>
<string name="delete_regions">Are you sure you want to disable regions and reassign all regional mappings to the corresponding countries?</string>
<string name="add">Add</string> <string name="add">Add</string>
<string name="clear">Clear</string> <string name="clear">Clear</string>
<string name="logo">Logo</string> <string name="logo">Logo</string>