[m] isRegional handling & more

This commit is contained in:
soraefir 2024-04-03 08:31:02 +02:00
parent 4e232e572e
commit 822a46236d
Signed by: sora
GPG Key ID: A362EA0491E2EEA0
7 changed files with 71 additions and 62 deletions

View File

@ -1,7 +1,6 @@
package net.helcel.beans.activity package net.helcel.beans.activity
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.graphics.drawable.PictureDrawable import android.graphics.drawable.PictureDrawable
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
@ -22,8 +21,6 @@ import net.helcel.beans.svg.SVGWrapper
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private lateinit var sharedPreferences: SharedPreferences
private lateinit var photoView: PhotoView private lateinit var photoView: PhotoView
private lateinit var psvg: SVGWrapper private lateinit var psvg: SVGWrapper
@ -40,7 +37,7 @@ class MainActivity : AppCompatActivity() {
// Create action bar // Create action bar
supportActionBar?.setBackgroundDrawable(colorWrapper(this, android.R.attr.colorPrimary)) supportActionBar?.setBackgroundDrawable(colorWrapper(this, android.R.attr.colorPrimary))
// Fetch shared preferences to restore app theme upon startup // restore app theme & settings upon startup
Settings.start(this) Settings.start(this)
// Create menu in action bar // Create menu in action bar
@ -81,7 +78,7 @@ class MainActivity : AppCompatActivity() {
photoView.minimumScale = 1f photoView.minimumScale = 1f
photoView.maximumScale = 40f photoView.maximumScale = 40f
GeoLocImporter.importState(this) GeoLocImporter.importStates(this)
loadData(this, Int.MIN_VALUE) loadData(this, Int.MIN_VALUE)
psvg = SVGWrapper(this) psvg = SVGWrapper(this)
css = CSSWrapper(this) css = CSSWrapper(this)

View File

@ -15,11 +15,7 @@ import net.helcel.beans.R
import net.helcel.beans.activity.fragment.EditPlaceColorFragment 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.helper.Data.groups import net.helcel.beans.helper.Data
import net.helcel.beans.helper.Data.saveData
import net.helcel.beans.helper.Data.selected_geoloc
import net.helcel.beans.helper.Data.selected_group
import net.helcel.beans.helper.Data.visits
import net.helcel.beans.helper.Settings import net.helcel.beans.helper.Settings
import net.helcel.beans.helper.Theme.colorWrapper import net.helcel.beans.helper.Theme.colorWrapper
@ -64,16 +60,10 @@ class GeolocListAdapter(
fun bind(el: GeoLoc) { fun bind(el: GeoLoc) {
textView.text = el.fullName textView.text = el.fullName
if (el.children.isEmpty() || (el.type == GeoLoc.LocType.COUNTRY && !Settings.isRegional( if (el.shouldShowChildren(ctx)) {
ctx
))
) {
textView.backgroundTintList =
ColorStateList.valueOf(colorWrapper(ctx, android.R.attr.colorBackground).color)
} else {
textView.setTypeface(null, Typeface.BOLD) textView.setTypeface(null, Typeface.BOLD)
val numerator = el.children.map { visits.getVisited(it) != 0 }.count { it } val numerator = el.children.map { Data.visits.getVisited(it) != 0 }.count { it }
val denominator = el.children.size val denominator = el.children.size
progressView.text = when (statsPref) { progressView.text = when (statsPref) {
@ -91,31 +81,31 @@ class GeolocListAdapter(
).color ).color
).withAlpha(128) ).withAlpha(128)
textView.parent.parent.requestChildFocus(textView, textView) textView.parent.parent.requestChildFocus(textView, textView)
} else {
textView.backgroundTintList =
ColorStateList.valueOf(colorWrapper(ctx, android.R.attr.colorBackground).color)
} }
refreshCheck(el) refreshCheck(el)
} }
fun addListeners(el: GeoLoc, expandLambda: () -> Boolean) { fun addListeners(el: GeoLoc, expandLambda: () -> Boolean) {
if (el.children.isNotEmpty() && (el.type != GeoLoc.LocType.COUNTRY || Settings.isRegional( if (el.shouldShowChildren(ctx)) {
ctx
))
) {
textView.setOnClickListener { expandLambda() } textView.setOnClickListener { expandLambda() }
} }
checkBox.setOnClickListener { checkBox.setOnClickListener {
selected_geoloc = el Data.selected_geoloc = el
if (groups.size() == 1 && Settings.isSingleGroup(ctx)) { if (Data.groups.size() == 1 && Settings.isSingleGroup(ctx)) {
if (checkBox.isChecked) { if (checkBox.isChecked) {
// If one has just checked the box (assign unique group) // If one has just checked the box (assign unique group)
selected_group = groups.getUniqueEntry() Data.selected_group = Data.groups.getUniqueEntry()
onColorDialogDismiss(false) onColorDialogDismiss(false)
} else { } else {
// If one has just unchecked the box (unassign unique group) // If one has just unchecked the box (unassign unique group)
selected_group = null Data.selected_group = null
onColorDialogDismiss(true) onColorDialogDismiss(true)
} }
} else { } else {
selected_group = null Data.selected_group = null
EditPlaceColorFragment(this).show( EditPlaceColorFragment(this).show(
ctx.supportFragmentManager, ctx.supportFragmentManager,
"AddColorDialogFragment" "AddColorDialogFragment"
@ -126,25 +116,25 @@ class GeolocListAdapter(
fun onColorDialogDismiss(clear: Boolean) { fun onColorDialogDismiss(clear: Boolean) {
if (clear) { if (clear) {
visits.setVisited(selected_geoloc!!, 0) Data.visits.setVisited(Data.selected_geoloc, 0)
saveData() Data.saveData()
} }
if (selected_group != null && selected_geoloc != null) { if (Data.selected_group != null && Data.selected_geoloc != null) {
visits.setVisited(selected_geoloc!!, selected_group!!.key) Data.visits.setVisited(Data.selected_geoloc, Data.selected_group?.key ?: 0)
saveData() Data.saveData()
} }
selected_geoloc?.let { refreshCheck(it) } Data.selected_geoloc?.let { refreshCheck(it) }
selected_geoloc = null Data.selected_geoloc = null
selected_group = null Data.selected_group = null
} }
private fun refreshCheck(geoLoc: GeoLoc) { private fun refreshCheck(geoLoc: GeoLoc) {
var col = groups.getGroupFromKey(visits.getVisited(geoLoc)).color.color var col = Data.groups.getGroupFromKey(Data.visits.getVisited(geoLoc)).color.color
if (col == Color.TRANSPARENT) if (col == Color.TRANSPARENT)
col = Color.GRAY col = Color.GRAY
checkBox.checkedState = checkBox.checkedState =
if (visits.getVisited(geoLoc) != 0) MaterialCheckBox.STATE_CHECKED if (Data.visits.getVisited(geoLoc) != 0) MaterialCheckBox.STATE_CHECKED
else if (geoLoc.children.any { visits.getVisited(it) != 0 }) MaterialCheckBox.STATE_INDETERMINATE else if (geoLoc.children.any { Data.visits.getVisited(it) != 0 }) MaterialCheckBox.STATE_INDETERMINATE
else MaterialCheckBox.STATE_UNCHECKED else MaterialCheckBox.STATE_UNCHECKED
checkBox.buttonTintList = ColorStateList( checkBox.buttonTintList = ColorStateList(

View File

@ -6,23 +6,30 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import net.helcel.beans.R import net.helcel.beans.R
import net.helcel.beans.countries.GeoLocImporter
class SettingsFragment : PreferenceFragmentCompat() { class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.fragment_settings, rootKey) setPreferencesFromResource(R.xml.fragment_settings, rootKey)
val ctx = requireContext()
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)
else -> GeoLocImporter.clearStates()
}
true
}
// Select Light/Dark/System Mode // Select Light/Dark/System Mode
val themePreference = findPreference<Preference>(getString(R.string.key_theme)) findPreference<Preference>(getString(R.string.key_theme))?.setOnPreferenceChangeListener { _, key ->
themePreference?.setOnPreferenceChangeListener { _, key -> setTheme(ctx, key as String)
setTheme(requireContext(), key as String)
} }
// Open license fragment // Open license fragment
val licensesPreference = findPreference<Preference>(getString(R.string.licenses)) findPreference<Preference>(getString(R.string.licenses))?.setOnPreferenceClickListener {
licensesPreference?.setOnPreferenceClickListener {
requireActivity().supportFragmentManager.beginTransaction() requireActivity().supportFragmentManager.beginTransaction()
.replace(R.id.fragment_view, LicenseFragment(), getString(R.string.licenses)) .replace(R.id.fragment_view, LicenseFragment(), getString(R.string.licenses))
.commit() .commit()
@ -30,12 +37,10 @@ class SettingsFragment : PreferenceFragmentCompat() {
} }
// Open about fragment // Open about fragment
val aboutPreference = findPreference<Preference>(getString(R.string.about)) findPreference<Preference>(getString(R.string.about))?.setOnPreferenceClickListener {
aboutPreference?.setOnPreferenceClickListener {
requireActivity().supportFragmentManager.beginTransaction() requireActivity().supportFragmentManager.beginTransaction()
.replace(R.id.fragment_view, AboutFragment(), getString(R.string.about)) .replace(R.id.fragment_view, AboutFragment(), getString(R.string.about))
.commit() .commit()
true true
} }

View File

@ -1,5 +1,8 @@
package net.helcel.beans.countries package net.helcel.beans.countries
import android.content.Context
import net.helcel.beans.helper.Settings
interface GeoLoc { interface GeoLoc {
@ -14,6 +17,14 @@ interface GeoLoc {
val type: LocType val type: LocType
val children: List<GeoLoc> val children: List<GeoLoc>
fun shouldShowChildren(ctx: Context): Boolean {
if (children.isEmpty())
return false
if (type == LocType.COUNTRY && !Settings.isRegional(ctx))
return false
return true
}
} }

View File

@ -7,7 +7,7 @@ import java.io.InputStreamReader
object GeoLocImporter { object GeoLocImporter {
fun importState(ctx: Context) { fun importStates(ctx: Context) {
if (!Settings.isRegional(ctx)) { if (!Settings.isRegional(ctx)) {
return return
} }
@ -18,4 +18,8 @@ object GeoLocImporter {
Country.entries.find { it.code == line[1] }?.children?.add(state) Country.entries.find { it.code == line[1] }?.children?.add(state)
} }
} }
fun clearStates() {
Country.entries.forEach { it.children.clear() }
}
} }

View File

@ -9,9 +9,11 @@ import java.io.InputStream
@Serializable @Serializable
class Visits(val id: Int, private val locs: HashMap<String,Int>) { class Visits(val id: Int, private val locs: HashMap<String, Int>) {
fun setVisited(key: GeoLoc, b: Int) { fun setVisited(key: GeoLoc?, b: Int) {
if (b == 0 || key == null)
return
locs[key.code] = b locs[key.code] = b
} }
@ -25,10 +27,11 @@ class Visits(val id: Int, private val locs: HashMap<String,Int>) {
} }
fun getVisited(key: GeoLoc): Int { fun getVisited(key: GeoLoc): Int {
return locs.getOrDefault(key.code,0) return locs.getOrDefault(key.code, 0)
} }
private fun getVisited(key: String): Int { private fun getVisited(key: String): Int {
return locs.getOrDefault(key,0) return locs.getOrDefault(key, 0)
} }
fun countVisited(key: Int): Int { fun countVisited(key: Int): Int {
@ -43,14 +46,14 @@ class Visits(val id: Int, private val locs: HashMap<String,Int>) {
@Serializer(Visits::class) @Serializer(Visits::class)
class VisitsSerializer { class VisitsSerializer {
val defaultValue: Visits val defaultValue: Visits
get() = Visits(Int.MIN_VALUE,hashMapOf()) get() = Visits(Int.MIN_VALUE, hashMapOf())
fun readFrom(input: InputStream): Visits { fun readFrom(input: InputStream): Visits {
return Json.decodeFromString(serializer(),input.readBytes().decodeToString()) return Json.decodeFromString(serializer(), input.readBytes().decodeToString())
} }
fun writeTo(t: Visits): String { fun writeTo(t: Visits): String {
return Json.encodeToString(serializer(),t).encodeToByteArray().decodeToString() return Json.encodeToString(serializer(), t).encodeToByteArray().decodeToString()
} }
} }

View File

@ -18,13 +18,12 @@ class CSSWrapper(ctx: Context) {
private var customCSS: String = "" private var customCSS: String = ""
init { init {
val www = World.WWW.children.joinToString(",") { "#${it.code}2" } val www = World.WWW.children.joinToString(",") { "#${it.code}2" }
val ccc = val ccc = World.WWW.children.joinToString(",") { itt ->
World.WWW.children.joinToString(",") { itt -> itt.children.joinToString(",") { "#${it.code}2" } } itt.children.joinToString(",") { "#${it.code}2" }
baseCSS = }
"svg{fill:$colorForeground;stroke:$colorBackground;stroke-width:0.01;}" + baseCSS = "svg{fill:$colorForeground;stroke:$colorBackground;stroke-width:0.01;}" +
"$www,$ccc{stroke-width:0.1;fill:none}" "$www,$ccc{stroke:$colorBackground;stroke-width:0.1;fill:none}"
refresh() refresh()
} }