Fixed Duplicate items in geoloc children

This commit is contained in:
soraefir 2024-04-09 00:03:34 +02:00
parent 728994a8f3
commit cd999c2a6e
Signed by: sora
GPG Key ID: A362EA0491E2EEA0
7 changed files with 47 additions and 38 deletions

View File

@ -13,11 +13,7 @@ import net.helcel.beans.activity.fragment.EditPlaceColorFragment
import net.helcel.beans.activity.fragment.EditPlaceFragment import net.helcel.beans.activity.fragment.EditPlaceFragment
import net.helcel.beans.countries.GeoLoc import net.helcel.beans.countries.GeoLoc
import net.helcel.beans.databinding.ItemListGeolocBinding import net.helcel.beans.databinding.ItemListGeolocBinding
import net.helcel.beans.helper.AUTO_GROUP import net.helcel.beans.helper.*
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.Theme.colorWrapper import net.helcel.beans.helper.Theme.colorWrapper
class GeolocListAdapter( class GeolocListAdapter(
@ -25,6 +21,8 @@ class GeolocListAdapter(
private val parentHolder: FoldingListViewHolder? private val parentHolder: FoldingListViewHolder?
) : RecyclerView.Adapter<GeolocListAdapter.FoldingListViewHolder>() { ) : RecyclerView.Adapter<GeolocListAdapter.FoldingListViewHolder>() {
private val sortedList = l.children.toList().sortedBy { it.fullName }
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): FoldingListViewHolder { override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): FoldingListViewHolder {
val binding = ItemListGeolocBinding.inflate( val binding = ItemListGeolocBinding.inflate(
LayoutInflater.from(viewGroup.context), LayoutInflater.from(viewGroup.context),
@ -35,7 +33,7 @@ class GeolocListAdapter(
} }
override fun onBindViewHolder(holder: FoldingListViewHolder, position: Int) { override fun onBindViewHolder(holder: FoldingListViewHolder, position: Int) {
val el = l.children[position] val el = sortedList[position]
holder.bind(el) holder.bind(el)
holder.addListeners(el) { holder.addListeners(el) {
if (el.children.isNotEmpty()) if (el.children.isNotEmpty())
@ -122,19 +120,19 @@ class GeolocListAdapter(
_binding.checkBox.checkedState = _binding.checkBox.checkedState =
if (Data.visits.getVisited(geoLoc) == AUTO_GROUP && !Settings.isRegional(ctx) && geoLoc.type == GeoLoc.LocType.COUNTRY) { if (Data.visits.getVisited(geoLoc) == AUTO_GROUP && !Settings.isRegional(ctx) && geoLoc.type == GeoLoc.LocType.COUNTRY) {
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 (geoLoc.children.isNotEmpty() && geoLoc.children.all {
else if (geoLoc.children.isNotEmpty() && geoLoc.children.all { Data.visits.getVisited(it) != NO_GROUP }) { Data.visits.getVisited(
it
) != NO_GROUP
}) {
Data.visits.setVisited(geoLoc, AUTO_GROUP) Data.visits.setVisited(geoLoc, AUTO_GROUP)
MaterialCheckBox.STATE_CHECKED 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) Data.visits.setVisited(geoLoc, AUTO_GROUP)
MaterialCheckBox.STATE_INDETERMINATE MaterialCheckBox.STATE_INDETERMINATE
} } else {
else {
Data.visits.setVisited(geoLoc, NO_GROUP) Data.visits.setVisited(geoLoc, NO_GROUP)
MaterialCheckBox.STATE_UNCHECKED MaterialCheckBox.STATE_UNCHECKED
} }
@ -143,8 +141,7 @@ class GeolocListAdapter(
var col = Data.groups.getGroupFromKey(Data.visits.getVisited(geoLoc)).color var col = Data.groups.getGroupFromKey(Data.visits.getVisited(geoLoc)).color
if (Data.visits.getVisited(geoLoc) == AUTO_GROUP) { if (Data.visits.getVisited(geoLoc) == AUTO_GROUP) {
col = colorWrapper(ctx, android.R.attr.colorPrimary) 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 = colorWrapper(ctx, android.R.attr.panelColorBackground)
col.alpha = 64 col.alpha = 64
} }
@ -152,13 +149,15 @@ class GeolocListAdapter(
} }
private fun refreshCount(geoLoc: GeoLoc) { 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 val denominator = geoLoc.children.size
_binding.count.text = when (Settings.getStatPref(ctx)) { _binding.count.text = when (Settings.getStatPref(ctx)) {
ctx.getString(R.string.percentages) -> ctx.getString( ctx.getString(R.string.percentages) -> ctx.getString(
R.string.percentage, R.string.percentage,
(100 * (numerator.toFloat() / denominator.toFloat())).toInt() (100 * (numerator.toFloat() / denominator.toFloat())).toInt()
) )
else -> ctx.getString(R.string.rate, numerator, denominator) else -> ctx.getString(R.string.rate, numerator, denominator)
} }
} }

View File

@ -1,8 +1,6 @@
package net.helcel.beans.activity.fragment package net.helcel.beans.activity.fragment
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.DialogInterface
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.Preference import androidx.preference.Preference
@ -12,7 +10,6 @@ import net.helcel.beans.R
import net.helcel.beans.countries.GeoLocImporter import net.helcel.beans.countries.GeoLocImporter
import net.helcel.beans.helper.Data import net.helcel.beans.helper.Data
import net.helcel.beans.helper.DialogCloser import net.helcel.beans.helper.DialogCloser
import net.helcel.beans.helper.Settings
class SettingsFragment : PreferenceFragmentCompat(), DialogCloser { class SettingsFragment : PreferenceFragmentCompat(), DialogCloser {
@ -106,7 +103,7 @@ class SettingsFragment : PreferenceFragmentCompat(), DialogCloser {
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))
.commit() .apply()
// Refresh entire preference fragment to reflect changes // Refresh entire preference fragment to reflect changes
preferenceScreen.removeAll() preferenceScreen.removeAll()

View File

@ -263,5 +263,5 @@ enum class Country(
override val code = this.name override val code = this.name
override val type = GeoLoc.LocType.COUNTRY override val type = GeoLoc.LocType.COUNTRY
override val children: MutableList<GeoLoc> = ArrayList() override val children: MutableSet<GeoLoc> = HashSet()
} }

View File

@ -15,7 +15,7 @@ interface GeoLoc {
val area: Int val area: Int
val type: LocType val type: LocType
val children: List<GeoLoc> val children: Set<GeoLoc>
fun shouldShowChildren(ctx: Context): Boolean { fun shouldShowChildren(ctx: Context): Boolean {
if (children.isEmpty()) if (children.isEmpty())

View File

@ -2,10 +2,10 @@ package net.helcel.beans.countries
import net.helcel.beans.countries.Country.* import net.helcel.beans.countries.Country.*
enum class Group(override val fullName: String, override val children: List<GeoLoc>) : GeoLoc { enum class Group(override val fullName: String, override val children: Set<GeoLoc>) : GeoLoc {
EEE( EEE(
"Europe", listOf( "Europe", setOf(
XAD, XAD,
ALA,// Åland Islands: an autonomous region of Finland, but not a member of the EU or UN ALA,// Åland Islands: an autonomous region of Finland, but not a member of the EU or UN
ALB, ALB,
@ -64,7 +64,7 @@ enum class Group(override val fullName: String, override val children: List<GeoL
) )
), ),
ABB( ABB(
"Asia", listOf( "Asia", setOf(
AFG, AFG,
ARM, ARM,
AZE, AZE,
@ -121,7 +121,7 @@ enum class Group(override val fullName: String, override val children: List<GeoL
) )
), ),
FFF( FFF(
"Africa", listOf( "Africa", setOf(
DZA, DZA,
AGO, AGO,
BDI, BDI,
@ -187,7 +187,7 @@ enum class Group(override val fullName: String, override val children: List<GeoL
) )
), ),
NNN( NNN(
"North America", listOf( "North America", setOf(
ABW, ABW,
AIA, AIA,
ATG, ATG,
@ -236,7 +236,7 @@ enum class Group(override val fullName: String, override val children: List<GeoL
) )
), ),
SRR( SRR(
"South America", listOf( "South America", setOf(
ARG, ARG,
BOL, BOL,
BRA, BRA,
@ -254,7 +254,7 @@ enum class Group(override val fullName: String, override val children: List<GeoL
) )
), ),
UUU( UUU(
"Oceania", listOf( "Oceania", setOf(
ASM, ASM,
AUS, AUS,
COK, COK,
@ -284,7 +284,7 @@ enum class Group(override val fullName: String, override val children: List<GeoL
), ),
XXX( XXX(
"Others", listOf( "Others", setOf(
ATA, // Antarctica: not in any other region ATA, // Antarctica: not in any other region
BVT, // Bouvet Island: an uninhabited territory of Norway in the South Atlantic BVT, // Bouvet Island: an uninhabited territory of Norway in the South Atlantic
HMD, // Heard Island and McDonald Islands: an uninhabited Australian external territory in the southern Indian Ocean HMD, // Heard Island and McDonald Islands: an uninhabited Australian external territory in the southern Indian Ocean
@ -293,13 +293,13 @@ enum class Group(override val fullName: String, override val children: List<GeoL
), ),
ZZZ( ZZZ(
"Undefined", listOf( "Undefined", setOf(
) )
), ),
NTT( NTT(
"NATO", listOf( "NATO", setOf(
ALB, BEL, BGR, CAN, HRV, CZE, DNK, EST, FRA, DEU, GRC, HUN, ISL, ITA, LVA, LTU, LUX, ALB, BEL, BGR, CAN, HRV, CZE, DNK, EST, FRA, DEU, GRC, HUN, ISL, ITA, LVA, LTU, LUX,
MNE, NLD, NOR, POL, PRT, ROU, SVK, SVN, ESP, TUR, GBR, USA MNE, NLD, NOR, POL, PRT, ROU, SVK, SVN, ESP, TUR, GBR, USA
) )

View File

@ -3,6 +3,17 @@ package net.helcel.beans.countries
class State(override val code: String, override val fullName: String, override val area: Int) : class State(override val code: String, override val fullName: String, override val area: Int) :
GeoLoc { GeoLoc {
override val children = emptyList<GeoLoc>() override val children = emptySet<GeoLoc>()
override val type = GeoLoc.LocType.STATE 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
}
} }

View File

@ -2,11 +2,13 @@ package net.helcel.beans.countries
import net.helcel.beans.countries.Group.* import net.helcel.beans.countries.Group.*
enum class World(override val fullName: String, override val children: List<GeoLoc>) : GeoLoc { enum class World(override val fullName: String, override val children: Set<GeoLoc>) : GeoLoc {
WWW("World", listOf( WWW(
EEE, ABB, FFF, NNN, SRR, UUU, XXX "World", setOf(
)); EEE, ABB, FFF, NNN, SRR, UUU, XXX
)
);
override val area = children.fold(0) { acc, i -> override val area = children.fold(0) { acc, i ->
acc + i.area acc + i.area