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.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<GeolocListAdapter.FoldingListViewHolder>() {
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)
}
}

View File

@ -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()

View File

@ -263,5 +263,5 @@ enum class Country(
override val code = this.name
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 type: LocType
val children: List<GeoLoc>
val children: Set<GeoLoc>
fun shouldShowChildren(ctx: Context): Boolean {
if (children.isEmpty())

View File

@ -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>) : GeoLoc {
enum class Group(override val fullName: String, override val children: Set<GeoLoc>) : 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<GeoL
)
),
ABB(
"Asia", listOf(
"Asia", setOf(
AFG,
ARM,
AZE,
@ -121,7 +121,7 @@ enum class Group(override val fullName: String, override val children: List<GeoL
)
),
FFF(
"Africa", listOf(
"Africa", setOf(
DZA,
AGO,
BDI,
@ -187,7 +187,7 @@ enum class Group(override val fullName: String, override val children: List<GeoL
)
),
NNN(
"North America", listOf(
"North America", setOf(
ABW,
AIA,
ATG,
@ -236,7 +236,7 @@ enum class Group(override val fullName: String, override val children: List<GeoL
)
),
SRR(
"South America", listOf(
"South America", setOf(
ARG,
BOL,
BRA,
@ -254,7 +254,7 @@ enum class Group(override val fullName: String, override val children: List<GeoL
)
),
UUU(
"Oceania", listOf(
"Oceania", setOf(
ASM,
AUS,
COK,
@ -284,7 +284,7 @@ enum class Group(override val fullName: String, override val children: List<GeoL
),
XXX(
"Others", listOf(
"Others", setOf(
ATA, // Antarctica: not in any other region
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
@ -293,13 +293,13 @@ enum class Group(override val fullName: String, override val children: List<GeoL
),
ZZZ(
"Undefined", listOf(
"Undefined", setOf(
)
),
NTT(
"NATO", listOf(
"NATO", setOf(
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
)

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) :
GeoLoc {
override val children = emptyList<GeoLoc>()
override val children = emptySet<GeoLoc>()
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.*
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(
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