[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
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.drawable.PictureDrawable
import android.os.Bundle
import android.view.Menu
@ -22,8 +21,6 @@ import net.helcel.beans.svg.SVGWrapper
class MainActivity : AppCompatActivity() {
private lateinit var sharedPreferences: SharedPreferences
private lateinit var photoView: PhotoView
private lateinit var psvg: SVGWrapper
@ -40,7 +37,7 @@ class MainActivity : AppCompatActivity() {
// Create action bar
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)
// Create menu in action bar
@ -81,7 +78,7 @@ class MainActivity : AppCompatActivity() {
photoView.minimumScale = 1f
photoView.maximumScale = 40f
GeoLocImporter.importState(this)
GeoLocImporter.importStates(this)
loadData(this, Int.MIN_VALUE)
psvg = SVGWrapper(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.EditPlaceFragment
import net.helcel.beans.countries.GeoLoc
import net.helcel.beans.helper.Data.groups
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.Data
import net.helcel.beans.helper.Settings
import net.helcel.beans.helper.Theme.colorWrapper
@ -64,16 +60,10 @@ class GeolocListAdapter(
fun bind(el: GeoLoc) {
textView.text = el.fullName
if (el.children.isEmpty() || (el.type == GeoLoc.LocType.COUNTRY && !Settings.isRegional(
ctx
))
) {
textView.backgroundTintList =
ColorStateList.valueOf(colorWrapper(ctx, android.R.attr.colorBackground).color)
} else {
if (el.shouldShowChildren(ctx)) {
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
progressView.text = when (statsPref) {
@ -91,31 +81,31 @@ class GeolocListAdapter(
).color
).withAlpha(128)
textView.parent.parent.requestChildFocus(textView, textView)
} else {
textView.backgroundTintList =
ColorStateList.valueOf(colorWrapper(ctx, android.R.attr.colorBackground).color)
}
refreshCheck(el)
}
fun addListeners(el: GeoLoc, expandLambda: () -> Boolean) {
if (el.children.isNotEmpty() && (el.type != GeoLoc.LocType.COUNTRY || Settings.isRegional(
ctx
))
) {
if (el.shouldShowChildren(ctx)) {
textView.setOnClickListener { expandLambda() }
}
checkBox.setOnClickListener {
selected_geoloc = el
if (groups.size() == 1 && Settings.isSingleGroup(ctx)) {
Data.selected_geoloc = el
if (Data.groups.size() == 1 && Settings.isSingleGroup(ctx)) {
if (checkBox.isChecked) {
// If one has just checked the box (assign unique group)
selected_group = groups.getUniqueEntry()
Data.selected_group = Data.groups.getUniqueEntry()
onColorDialogDismiss(false)
} else {
// If one has just unchecked the box (unassign unique group)
selected_group = null
Data.selected_group = null
onColorDialogDismiss(true)
}
} else {
selected_group = null
Data.selected_group = null
EditPlaceColorFragment(this).show(
ctx.supportFragmentManager,
"AddColorDialogFragment"
@ -126,25 +116,25 @@ class GeolocListAdapter(
fun onColorDialogDismiss(clear: Boolean) {
if (clear) {
visits.setVisited(selected_geoloc!!, 0)
saveData()
Data.visits.setVisited(Data.selected_geoloc, 0)
Data.saveData()
}
if (selected_group != null && selected_geoloc != null) {
visits.setVisited(selected_geoloc!!, selected_group!!.key)
saveData()
if (Data.selected_group != null && Data.selected_geoloc != null) {
Data.visits.setVisited(Data.selected_geoloc, Data.selected_group?.key ?: 0)
Data.saveData()
}
selected_geoloc?.let { refreshCheck(it) }
selected_geoloc = null
selected_group = null
Data.selected_geoloc?.let { refreshCheck(it) }
Data.selected_geoloc = null
Data.selected_group = null
}
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)
col = Color.GRAY
checkBox.checkedState =
if (visits.getVisited(geoLoc) != 0) MaterialCheckBox.STATE_CHECKED
else if (geoLoc.children.any { visits.getVisited(it) != 0 }) MaterialCheckBox.STATE_INDETERMINATE
if (Data.visits.getVisited(geoLoc) != 0) MaterialCheckBox.STATE_CHECKED
else if (geoLoc.children.any { Data.visits.getVisited(it) != 0 }) MaterialCheckBox.STATE_INDETERMINATE
else MaterialCheckBox.STATE_UNCHECKED
checkBox.buttonTintList = ColorStateList(

View File

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

View File

@ -1,5 +1,8 @@
package net.helcel.beans.countries
import android.content.Context
import net.helcel.beans.helper.Settings
interface GeoLoc {
@ -14,6 +17,14 @@ interface GeoLoc {
val type: LocType
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 {
fun importState(ctx: Context) {
fun importStates(ctx: Context) {
if (!Settings.isRegional(ctx)) {
return
}
@ -18,4 +18,8 @@ object GeoLocImporter {
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
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
}
@ -25,10 +27,11 @@ class Visits(val id: Int, private val locs: HashMap<String,Int>) {
}
fun getVisited(key: GeoLoc): Int {
return locs.getOrDefault(key.code,0)
return locs.getOrDefault(key.code, 0)
}
private fun getVisited(key: String): Int {
return locs.getOrDefault(key,0)
return locs.getOrDefault(key, 0)
}
fun countVisited(key: Int): Int {
@ -43,14 +46,14 @@ class Visits(val id: Int, private val locs: HashMap<String,Int>) {
@Serializer(Visits::class)
class VisitsSerializer {
val defaultValue: Visits
get() = Visits(Int.MIN_VALUE,hashMapOf())
get() = Visits(Int.MIN_VALUE, hashMapOf())
fun readFrom(input: InputStream): Visits {
return Json.decodeFromString(serializer(),input.readBytes().decodeToString())
return Json.decodeFromString(serializer(), input.readBytes().decodeToString())
}
fun writeTo(t: Visits): String {
return Json.encodeToString(serializer(),t).encodeToByteArray().decodeToString()
fun writeTo(t: Visits): String {
return Json.encodeToString(serializer(), t).encodeToByteArray().decodeToString()
}
}

View File

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