[M] Added Storing/Loading

This commit is contained in:
choelzl 2023-04-07 02:09:56 +02:00
parent f9dc122af5
commit 77e7893c5f
Signed by: sora
GPG Key ID: A362EA0491E2EEA0
9 changed files with 101 additions and 23 deletions

View File

@ -37,13 +37,13 @@ android {
dependencies { dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation "androidx.preference:preference-ktx:1.2.0"
implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0' implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3' implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3' implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'androidx.core:core-ktx:1.9.0'
implementation "androidx.preference:preference-ktx:1.2.0"
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.ext:junit:1.1.5'

View File

@ -13,10 +13,11 @@ import androidx.recyclerview.widget.RecyclerView
import net.helcel.beendroid.R import net.helcel.beendroid.R
import net.helcel.beendroid.countries.GeoLoc import net.helcel.beendroid.countries.GeoLoc
import net.helcel.beendroid.countries.LocType import net.helcel.beendroid.countries.LocType
import net.helcel.beendroid.countries.Visited
import java.util.* import java.util.*
class FoldingListAdapter(private val ctx: Context, l: List<GeoLoc>) : class FoldingListAdapter(private val ctx: Context, l: List<GeoLoc>, private val visited: Visited) :
RecyclerView.Adapter<FoldingListAdapter.FoldingListViewHolder>() { RecyclerView.Adapter<FoldingListAdapter.FoldingListViewHolder>() {
private var cg : MutableMap<GeoLoc,Boolean> = l.sortedBy { it.fullName }.fold(LinkedHashMap<GeoLoc,Boolean>()) { acc, e -> private var cg : MutableMap<GeoLoc,Boolean> = l.sortedBy { it.fullName }.fold(LinkedHashMap<GeoLoc,Boolean>()) { acc, e ->
@ -28,15 +29,13 @@ class FoldingListAdapter(private val ctx: Context, l: List<GeoLoc>) :
val view: View = LayoutInflater val view: View = LayoutInflater
.from(viewGroup.context) .from(viewGroup.context)
.inflate(R.layout.item_list, viewGroup, false) .inflate(R.layout.item_list, viewGroup, false)
return FoldingListViewHolder(ctx, view) return FoldingListViewHolder(ctx, view, visited)
} }
override fun onBindViewHolder(holder: FoldingListViewHolder, position: Int) { override fun onBindViewHolder(holder: FoldingListViewHolder, position: Int) {
val el = cg.toList()[position] val el = cg.toList()[position]
holder.bind(el) holder.bind(el)
holder.textView.setOnClickListener { holder.checkBox.toggle() }
val expandLambda = { _:View -> val expandLambda = { _:View ->
if (el.first.children.isEmpty() || el.first.type == LocType.STATE) { if (el.first.children.isEmpty() || el.first.type == LocType.STATE) {
false false
@ -53,7 +52,10 @@ class FoldingListAdapter(private val ctx: Context, l: List<GeoLoc>) :
} }
} }
holder.itemView.setOnLongClickListener(expandLambda)
holder.textView.setOnClickListener { holder.checkBox.toggle() }
holder.checkBox.setOnCheckedChangeListener{_,e->visited.setVisited(el.first,e)}
holder.textView.setOnLongClickListener{ expandLambda(it) }
holder.expand.setOnClickListener { expandLambda(it) } holder.expand.setOnClickListener { expandLambda(it) }
} }
@ -61,7 +63,7 @@ class FoldingListAdapter(private val ctx: Context, l: List<GeoLoc>) :
return cg.size return cg.size
} }
class FoldingListViewHolder(private val ctx: Context, itemView: View) : RecyclerView.ViewHolder(itemView) { class FoldingListViewHolder(private val ctx: Context, itemView: View, private val visited: Visited) : RecyclerView.ViewHolder(itemView) {
val textView: TextView val textView: TextView
val expand: ImageView val expand: ImageView
val checkBox: CheckBox val checkBox: CheckBox
@ -85,12 +87,13 @@ class FoldingListAdapter(private val ctx: Context, l: List<GeoLoc>) :
textView.text = el.first.fullName textView.text = el.first.fullName
checkBox.isChecked = visited.visited(el.first)
if(el.first.type == LocType.STATE || el.first.children.isEmpty()){ if(el.first.type == LocType.STATE || el.first.children.isEmpty()){
expand.visibility = View.GONE expand.visibility = View.GONE
return return
} }
textView.parent.parent.requestChildFocus(textView,textView) textView.parent.parent.requestChildFocus(textView,textView)
list.adapter = FoldingListAdapter(ctx, el.first.children) list.adapter = FoldingListAdapter(ctx, el.first.children,visited)
} }
} }

View File

@ -10,7 +10,9 @@ import com.caverock.androidsvg.SVG
import com.caverock.androidsvg.SVGImageView import com.caverock.androidsvg.SVGImageView
import net.helcel.beendroid.R import net.helcel.beendroid.R
import net.helcel.beendroid.countries.Country import net.helcel.beendroid.countries.Country
import net.helcel.beendroid.countries.Visited
import net.helcel.beendroid.countries.WORLD import net.helcel.beendroid.countries.WORLD
import net.helcel.beendroid.countries.World
import net.helcel.beendroid.svg.Level import net.helcel.beendroid.svg.Level
import net.helcel.beendroid.svg.PSVGWrapper import net.helcel.beendroid.svg.PSVGWrapper
@ -20,12 +22,18 @@ class MainActivity : AppCompatActivity() {
private lateinit var map : SVGImageView private lateinit var map : SVGImageView
private lateinit var list : RecyclerView private lateinit var list : RecyclerView
private lateinit var visited : Visited
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
visited = Visited(this)
visited.load()
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
map = findViewById(R.id.map) map = findViewById(R.id.map)
val cm = HashMap<Country, PSVGWrapper>() val cm = HashMap<Country, PSVGWrapper>()
Country.values().forEach { c-> Country.values().forEach { c->
cm[c] = PSVGWrapper(applicationContext,c, Level.ZERO).load() cm[c] = PSVGWrapper(applicationContext,c, Level.ZERO).load()
@ -43,6 +51,6 @@ class MainActivity : AppCompatActivity() {
list = findViewById(R.id.list) list = findViewById(R.id.list)
list.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false) list.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
list.adapter = FoldingListAdapter(this, WORLD) list.adapter = FoldingListAdapter(this, World.WWW.children, visited)
} }
} }

View File

@ -11,4 +11,5 @@ interface GeoLoc {
val type : LocType val type : LocType
val children : List<GeoLoc> val children : List<GeoLoc>
} }

View File

@ -3,16 +3,16 @@ package net.helcel.beendroid.countries
import net.helcel.beendroid.countries.Country.* import net.helcel.beendroid.countries.Country.*
val WORLD = listOf( val WORLD = listOf(
CountryGroup.EEE, Group.EEE,
CountryGroup.ABB, Group.ABB,
CountryGroup.FFF, Group.FFF,
CountryGroup.NNN, Group.NNN,
CountryGroup.SRR, Group.SRR,
CountryGroup.UUU, Group.UUU,
CountryGroup.XXX Group.XXX
) )
enum class CountryGroup(override val fullName: String, override val children: List<Country>) : GeoLoc { enum class Group(override val fullName: String, override val children: List<Country>) : GeoLoc {
EEE("Europe",listOf( EEE("Europe",listOf(
ALB, AND, AUT, BLR, BEL, BIH, BGR, HRV, CYP, CZE, DNK, EST, FIN, FRA, ALB, AND, AUT, BLR, BEL, BIH, BGR, HRV, CYP, CZE, DNK, EST, FIN, FRA,
@ -88,4 +88,6 @@ enum class CountryGroup(override val fullName: String, override val children: Li
override val type: LocType = if (isInWorld) LocType.GROUP else LocType.CUSTOM_GROUP override val type: LocType = if (isInWorld) LocType.GROUP else LocType.CUSTOM_GROUP
override val code = this.name override val code = this.name
} }

View File

@ -0,0 +1,9 @@
package net.helcel.beendroid.countries
enum class State(override val fullName: String, override val area: Int,) : GeoLoc {
UNDEFINED("",0);
override val code = this.name
override val children = emptyList<GeoLoc>()
override val type = LocType.STATE
}

View File

@ -0,0 +1,35 @@
package net.helcel.beendroid.countries
import android.content.Context
class Visited(ctx: Context) {
private var locs: MutableMap<GeoLoc, Boolean> = HashMap()
private val pref = ctx.getSharedPreferences("Visited", Context.MODE_PRIVATE)
private val editor = pref.edit()
fun load() {
Group.values().forEach {
locs[it] = pref.getBoolean(it.code, false)
}
Country.values().forEach {
locs[it] = pref.getBoolean(it.code, false)
}
State.values().forEach {
locs[it] = pref.getBoolean(it.code, false)
}
editor.apply()
}
fun setVisited(key: GeoLoc, b: Boolean){
locs[key] = b
editor.putBoolean(key.code, b)
editor.apply()
}
fun visited(key: GeoLoc): Boolean {
return locs.getOrDefault(key,false)
}
}

View File

@ -0,0 +1,20 @@
package net.helcel.beendroid.countries
import net.helcel.beendroid.countries.Group.*
enum class World(override val fullName: String, override val children: List<GeoLoc>) : GeoLoc {
WWW("World", listOf(
EEE, ABB, FFF, NNN, SRR, UUU, XXX
));
override val area = children.fold(0) { acc, i ->
acc + i.area
}
override val type: LocType = LocType.WORLD
override val code = this.name
}

View File

@ -1,8 +1,8 @@
package net.helcel.beendroid package net.helcel.beendroid
import net.helcel.beendroid.countries.Country import net.helcel.beendroid.countries.Country
import net.helcel.beendroid.countries.CountryGroup import net.helcel.beendroid.countries.Group
import net.helcel.beendroid.countries.CountryGroup.* import net.helcel.beendroid.countries.Group.*
import org.junit.Assert import org.junit.Assert
import org.junit.Test import org.junit.Test
@ -33,7 +33,7 @@ class CountryTest {
@Test @Test
fun allCountriesInAGroup() { fun allCountriesInAGroup() {
Country.values().forEach { c -> Country.values().forEach { c ->
val cnt = CountryGroup.values().none { val cnt = Group.values().none {
it.children.contains((c)) it.children.contains((c))
} }
Assert.assertEquals("$c has no group !",cnt,false) Assert.assertEquals("$c has no group !",cnt,false)
@ -85,14 +85,14 @@ class CountryTest {
@Test @Test
fun allCountryGroupsValidName() { fun allCountryGroupsValidName() {
CountryGroup.values().forEach { Group.values().forEach {
Assert.assertEquals("$it has no full_name", it.fullName.isNotEmpty(), true) Assert.assertEquals("$it has no full_name", it.fullName.isNotEmpty(), true)
} }
} }
@Test @Test
fun allCountryGroupsValidArea() { fun allCountryGroupsValidArea() {
CountryGroup.values().forEach { Group.values().forEach {
Assert.assertEquals("$it has an area of 0", it.area >= 0, true) Assert.assertEquals("$it has an area of 0", it.area >= 0, true)
} }
} }