diff --git a/app/src/main/java/net/helcel/beans/activity/adapter/GeolocListAdapter.kt b/app/src/main/java/net/helcel/beans/activity/adapter/GeolocListAdapter.kt index c17f3e5..cb5994e 100644 --- a/app/src/main/java/net/helcel/beans/activity/adapter/GeolocListAdapter.kt +++ b/app/src/main/java/net/helcel/beans/activity/adapter/GeolocListAdapter.kt @@ -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() diff --git a/app/src/main/java/net/helcel/beans/activity/fragment/SettingsFragment.kt b/app/src/main/java/net/helcel/beans/activity/fragment/SettingsFragment.kt index e435da1..8e9e440 100644 --- a/app/src/main/java/net/helcel/beans/activity/fragment/SettingsFragment.kt +++ b/app/src/main/java/net/helcel/beans/activity/fragment/SettingsFragment.kt @@ -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(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) + } } \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beans/helper/Data.kt b/app/src/main/java/net/helcel/beans/helper/Data.kt index f426e83..bd59bbc 100644 --- a/app/src/main/java/net/helcel/beans/helper/Data.kt +++ b/app/src/main/java/net/helcel/beans/helper/Data.kt @@ -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() -} - } \ No newline at end of file + 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() + } +} \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beans/svg/CSSWrapper.kt b/app/src/main/java/net/helcel/beans/svg/CSSWrapper.kt index 12ae08a..6bb8930 100644 --- a/app/src/main/java/net/helcel/beans/svg/CSSWrapper.kt +++ b/app/src/main/java/net/helcel/beans/svg/CSSWrapper.kt @@ -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 diff --git a/app/src/main/res/values/en.xml b/app/src/main/res/values/en.xml index 22b57ab..84918ce 100644 --- a/app/src/main/res/values/en.xml +++ b/app/src/main/res/values/en.xml @@ -22,6 +22,7 @@ About the Beans application Are your sure you want to delete this group and remove all its country mappings? Select one group you want to keep. All others will be deleted and its mappings reassigned to the group you choose here. + Are you sure you want to disable regions and reassign all regional mappings to the corresponding countries? Add Clear Logo