From cd999c2a6e6cc9394aa5a91d9d8ec7874de6d55d Mon Sep 17 00:00:00 2001 From: soraefir Date: Tue, 9 Apr 2024 00:03:34 +0200 Subject: [PATCH] Fixed Duplicate items in geoloc children --- .../activity/adapter/GeolocListAdapter.kt | 33 +++++++++---------- .../activity/fragment/SettingsFragment.kt | 5 +-- .../net/helcel/beans/countries/Country.kt | 2 +- .../java/net/helcel/beans/countries/GeoLoc.kt | 2 +- .../java/net/helcel/beans/countries/Group.kt | 20 +++++------ .../java/net/helcel/beans/countries/State.kt | 13 +++++++- .../java/net/helcel/beans/countries/World.kt | 10 +++--- 7 files changed, 47 insertions(+), 38 deletions(-) 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 7e31f29..71f2da2 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 @@ -13,11 +13,7 @@ import net.helcel.beans.activity.fragment.EditPlaceColorFragment import net.helcel.beans.activity.fragment.EditPlaceFragment import net.helcel.beans.countries.GeoLoc import net.helcel.beans.databinding.ItemListGeolocBinding -import net.helcel.beans.helper.AUTO_GROUP -import net.helcel.beans.helper.Data -import net.helcel.beans.helper.DialogCloser -import net.helcel.beans.helper.NO_GROUP -import net.helcel.beans.helper.Settings +import net.helcel.beans.helper.* import net.helcel.beans.helper.Theme.colorWrapper class GeolocListAdapter( @@ -25,6 +21,8 @@ class GeolocListAdapter( private val parentHolder: FoldingListViewHolder? ) : RecyclerView.Adapter() { + private val sortedList = l.children.toList().sortedBy { it.fullName } + override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): FoldingListViewHolder { val binding = ItemListGeolocBinding.inflate( LayoutInflater.from(viewGroup.context), @@ -35,7 +33,7 @@ class GeolocListAdapter( } override fun onBindViewHolder(holder: FoldingListViewHolder, position: Int) { - val el = l.children[position] + val el = sortedList[position] holder.bind(el) holder.addListeners(el) { if (el.children.isNotEmpty()) @@ -122,19 +120,19 @@ class GeolocListAdapter( _binding.checkBox.checkedState = if (Data.visits.getVisited(geoLoc) == AUTO_GROUP && !Settings.isRegional(ctx) && geoLoc.type == GeoLoc.LocType.COUNTRY) { 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 - } - else if (geoLoc.children.isNotEmpty() && geoLoc.children.all { Data.visits.getVisited(it) != NO_GROUP }) { + } else if (geoLoc.children.isNotEmpty() && geoLoc.children.all { + Data.visits.getVisited( + it + ) != NO_GROUP + }) { Data.visits.setVisited(geoLoc, AUTO_GROUP) MaterialCheckBox.STATE_CHECKED - } - else if (geoLoc.children.any { Data.visits.getVisited(it) != NO_GROUP }) { + } else if (geoLoc.children.any { Data.visits.getVisited(it) != NO_GROUP }) { Data.visits.setVisited(geoLoc, AUTO_GROUP) MaterialCheckBox.STATE_INDETERMINATE - } - else { + } else { Data.visits.setVisited(geoLoc, NO_GROUP) MaterialCheckBox.STATE_UNCHECKED } @@ -143,8 +141,7 @@ class GeolocListAdapter( var col = Data.groups.getGroupFromKey(Data.visits.getVisited(geoLoc)).color if (Data.visits.getVisited(geoLoc) == AUTO_GROUP) { col = colorWrapper(ctx, android.R.attr.colorPrimary) - } - else if (col.color == Color.TRANSPARENT) { + } else if (col.color == Color.TRANSPARENT) { col = colorWrapper(ctx, android.R.attr.panelColorBackground) col.alpha = 64 } @@ -152,13 +149,15 @@ class GeolocListAdapter( } private fun refreshCount(geoLoc: GeoLoc) { - val numerator = geoLoc.children.map { Data.visits.getVisited(it) != NO_GROUP }.count { it } + val numerator = + geoLoc.children.map { Data.visits.getVisited(it) != NO_GROUP }.count { it } val denominator = geoLoc.children.size _binding.count.text = when (Settings.getStatPref(ctx)) { ctx.getString(R.string.percentages) -> ctx.getString( R.string.percentage, (100 * (numerator.toFloat() / denominator.toFloat())).toInt() ) + else -> ctx.getString(R.string.rate, numerator, denominator) } } 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 e02414b..b9f21e6 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 @@ -1,8 +1,6 @@ package net.helcel.beans.activity.fragment -import android.annotation.SuppressLint import android.content.Context -import android.content.DialogInterface import android.os.Bundle import androidx.appcompat.app.AppCompatDelegate import androidx.preference.Preference @@ -12,7 +10,6 @@ import net.helcel.beans.R import net.helcel.beans.countries.GeoLocImporter import net.helcel.beans.helper.Data import net.helcel.beans.helper.DialogCloser -import net.helcel.beans.helper.Settings class SettingsFragment : PreferenceFragmentCompat(), DialogCloser { @@ -106,7 +103,7 @@ class SettingsFragment : PreferenceFragmentCompat(), DialogCloser { val ctx = requireContext() val sp = PreferenceManager.getDefaultSharedPreferences(ctx) sp.edit().putString(ctx.getString(R.string.key_group), ctx.getString(R.string.off)) - .commit() + .apply() // Refresh entire preference fragment to reflect changes preferenceScreen.removeAll() diff --git a/app/src/main/java/net/helcel/beans/countries/Country.kt b/app/src/main/java/net/helcel/beans/countries/Country.kt index 4a17dff..54e1a23 100644 --- a/app/src/main/java/net/helcel/beans/countries/Country.kt +++ b/app/src/main/java/net/helcel/beans/countries/Country.kt @@ -263,5 +263,5 @@ enum class Country( override val code = this.name override val type = GeoLoc.LocType.COUNTRY - override val children: MutableList = ArrayList() + override val children: MutableSet = HashSet() } diff --git a/app/src/main/java/net/helcel/beans/countries/GeoLoc.kt b/app/src/main/java/net/helcel/beans/countries/GeoLoc.kt index 2994da5..0580c7b 100644 --- a/app/src/main/java/net/helcel/beans/countries/GeoLoc.kt +++ b/app/src/main/java/net/helcel/beans/countries/GeoLoc.kt @@ -15,7 +15,7 @@ interface GeoLoc { val area: Int val type: LocType - val children: List + val children: Set fun shouldShowChildren(ctx: Context): Boolean { if (children.isEmpty()) diff --git a/app/src/main/java/net/helcel/beans/countries/Group.kt b/app/src/main/java/net/helcel/beans/countries/Group.kt index 88195b2..5c814b2 100644 --- a/app/src/main/java/net/helcel/beans/countries/Group.kt +++ b/app/src/main/java/net/helcel/beans/countries/Group.kt @@ -2,10 +2,10 @@ package net.helcel.beans.countries import net.helcel.beans.countries.Country.* -enum class Group(override val fullName: String, override val children: List) : GeoLoc { +enum class Group(override val fullName: String, override val children: Set) : GeoLoc { EEE( - "Europe", listOf( + "Europe", setOf( XAD, ALA,// Ă…land Islands: an autonomous region of Finland, but not a member of the EU or UN ALB, @@ -64,7 +64,7 @@ enum class Group(override val fullName: String, override val children: List() + override val children = emptySet() override val type = GeoLoc.LocType.STATE + + override fun hashCode(): Int { + return code.hashCode() + } + + override fun equals(other: Any?): Boolean { + if (other is GeoLoc) { + return other.code == this.code + } + return false + } } \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beans/countries/World.kt b/app/src/main/java/net/helcel/beans/countries/World.kt index 406fef3..cb5efd2 100644 --- a/app/src/main/java/net/helcel/beans/countries/World.kt +++ b/app/src/main/java/net/helcel/beans/countries/World.kt @@ -2,11 +2,13 @@ package net.helcel.beans.countries import net.helcel.beans.countries.Group.* -enum class World(override val fullName: String, override val children: List) : GeoLoc { +enum class World(override val fullName: String, override val children: Set) : GeoLoc { - WWW("World", listOf( - EEE, ABB, FFF, NNN, SRR, UUU, XXX - )); + WWW( + "World", setOf( + EEE, ABB, FFF, NNN, SRR, UUU, XXX + ) + ); override val area = children.fold(0) { acc, i -> acc + i.area