Correct bug when disabling and re-enabling regions
This commit is contained in:
		@@ -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()
 | 
			
		||||
 
 | 
			
		||||
@@ -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,11 +53,26 @@ 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
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        // Open license fragment
 | 
			
		||||
@@ -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
 | 
			
		||||
            refreshPreferences()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun refreshPreferences() {
 | 
			
		||||
        preferenceScreen.removeAll()
 | 
			
		||||
        onCreatePreferences(savedInstanceState, rootKey)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
@@ -10,13 +10,12 @@ 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
 | 
			
		||||
 | 
			
		||||
    private val groupsSerial = Groups.GroupsSerializer()
 | 
			
		||||
    private val visitsSerial = Visits.VisitsSerializer()
 | 
			
		||||
@@ -37,7 +36,6 @@ fun loadData(ctx: Context, id:Int) {
 | 
			
		||||
            groups.setGroup(DEFAULT_GROUP, "Visited", ColorDrawable(ContextCompat.getColor(ctx, R.color.blue)))
 | 
			
		||||
            saveData()
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun saveData() {
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user