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) {
Data.visits.setVisited(Data.selected_geoloc, NO_GROUP)
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) {
Data.visits.setVisited(Data.selected_geoloc, Data.selected_group?.key ?: NO_GROUP)
@ -123,6 +127,14 @@ class GeolocListAdapter(
MaterialCheckBox.STATE_CHECKED
} else if (Data.visits.getVisited(geoLoc) !in listOf(NO_GROUP, AUTO_GROUP)) {
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 {
Data.visits.getVisited(
it
@ -135,6 +147,9 @@ class GeolocListAdapter(
MaterialCheckBox.STATE_INDETERMINATE
} else {
Data.visits.setVisited(geoLoc, NO_GROUP)
if (geoLoc == Data.clearing_geoloc) {
Data.clearing_geoloc = null
}
MaterialCheckBox.STATE_UNCHECKED
}
Data.saveData()

View File

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

View File

@ -10,42 +10,40 @@ import net.helcel.beans.countries.GeoLoc
import java.util.HashMap
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())
var groups : Groups = Groups(0,HashMap())
private val groupsSerial = Groups.GroupsSerializer()
private val visitsSerial = Visits.VisitsSerializer()
var selected_group : Groups.Group? = null
var selected_geoloc: GeoLoc? = null
private lateinit var sharedPreferences: SharedPreferences
private val groupsSerial = Groups.GroupsSerializer()
private val visitsSerial = Visits.VisitsSerializer()
fun loadData(ctx: Context, id:Int) {
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) {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx)
groups = if(!groupsString.isNullOrEmpty()) groupsSerial.readFrom(groupsString.byteInputStream()) else groupsSerial.defaultValue
visits = if(!visitsString.isNullOrEmpty()) visitsSerial.readFrom(visitsString.byteInputStream()) else visitsSerial.defaultValue
val groupsString = sharedPreferences.getString("groups_$id",null)
val visitsString = sharedPreferences.getString("visits_$id",null)
groups = if(!groupsString.isNullOrEmpty()) groupsSerial.readFrom(groupsString.byteInputStream()) else groupsSerial.defaultValue
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()
// 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
val id = groups.id
val editor = sharedPreferences.edit()
editor.putString("groups_$id", groupsSerial.writeTo(groups))
editor.putString("visits_$id", visitsSerial.writeTo(visits))
editor.apply()
}
}
fun saveData() {
if(groups.id != visits.id) return
val id = groups.id
val editor = sharedPreferences.edit()
editor.putString("groups_$id", groupsSerial.writeTo(groups))
editor.putString("visits_$id", visitsSerial.writeTo(visits))
editor.apply()
}
}

View File

@ -1,6 +1,8 @@
package net.helcel.beans.svg
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.helper.AUTO_GROUP
import net.helcel.beans.helper.Data
@ -45,6 +47,18 @@ class CSSWrapper(private val ctx: Context) {
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

View File

@ -22,6 +22,7 @@
<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="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="clear">Clear</string>
<string name="logo">Logo</string>