From 77e7893c5f5ccb437a9288852addb195b9d05f3a Mon Sep 17 00:00:00 2001 From: choelzl Date: Fri, 7 Apr 2023 02:09:56 +0200 Subject: [PATCH] [M] Added Storing/Loading --- app/build.gradle | 4 +-- .../beendroid/activity/FoldingListAdapter.kt | 17 +++++---- .../helcel/beendroid/activity/MainActivity.kt | 10 +++++- .../net/helcel/beendroid/countries/GeoLoc.kt | 1 + .../net/helcel/beendroid/countries/Group.kt | 18 +++++----- .../net/helcel/beendroid/countries/State.kt | 9 +++++ .../net/helcel/beendroid/countries/Visited.kt | 35 +++++++++++++++++++ .../net/helcel/beendroid/countries/World.kt | 20 +++++++++++ .../java/net/helcel/beendroid/CountryTest.kt | 10 +++--- 9 files changed, 101 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/net/helcel/beendroid/countries/State.kt create mode 100644 app/src/main/java/net/helcel/beendroid/countries/Visited.kt create mode 100644 app/src/main/java/net/helcel/beendroid/countries/World.kt diff --git a/app/build.gradle b/app/build.gradle index ff9e2fb..a5bd8c8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,13 +37,13 @@ android { 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 'com.google.android.material:material:1.8.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.navigation:navigation-fragment-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' androidTestImplementation 'androidx.test.ext:junit:1.1.5' diff --git a/app/src/main/java/net/helcel/beendroid/activity/FoldingListAdapter.kt b/app/src/main/java/net/helcel/beendroid/activity/FoldingListAdapter.kt index 8999fe6..750e479 100644 --- a/app/src/main/java/net/helcel/beendroid/activity/FoldingListAdapter.kt +++ b/app/src/main/java/net/helcel/beendroid/activity/FoldingListAdapter.kt @@ -13,10 +13,11 @@ import androidx.recyclerview.widget.RecyclerView import net.helcel.beendroid.R import net.helcel.beendroid.countries.GeoLoc import net.helcel.beendroid.countries.LocType +import net.helcel.beendroid.countries.Visited import java.util.* -class FoldingListAdapter(private val ctx: Context, l: List) : +class FoldingListAdapter(private val ctx: Context, l: List, private val visited: Visited) : RecyclerView.Adapter() { private var cg : MutableMap = l.sortedBy { it.fullName }.fold(LinkedHashMap()) { acc, e -> @@ -28,15 +29,13 @@ class FoldingListAdapter(private val ctx: Context, l: List) : val view: View = LayoutInflater .from(viewGroup.context) .inflate(R.layout.item_list, viewGroup, false) - return FoldingListViewHolder(ctx, view) + return FoldingListViewHolder(ctx, view, visited) } override fun onBindViewHolder(holder: FoldingListViewHolder, position: Int) { val el = cg.toList()[position] holder.bind(el) - holder.textView.setOnClickListener { holder.checkBox.toggle() } - val expandLambda = { _:View -> if (el.first.children.isEmpty() || el.first.type == LocType.STATE) { false @@ -53,7 +52,10 @@ class FoldingListAdapter(private val ctx: Context, l: List) : } } - 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) } } @@ -61,7 +63,7 @@ class FoldingListAdapter(private val ctx: Context, l: List) : 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 expand: ImageView val checkBox: CheckBox @@ -85,12 +87,13 @@ class FoldingListAdapter(private val ctx: Context, l: List) : textView.text = el.first.fullName + checkBox.isChecked = visited.visited(el.first) if(el.first.type == LocType.STATE || el.first.children.isEmpty()){ expand.visibility = View.GONE return } textView.parent.parent.requestChildFocus(textView,textView) - list.adapter = FoldingListAdapter(ctx, el.first.children) + list.adapter = FoldingListAdapter(ctx, el.first.children,visited) } } diff --git a/app/src/main/java/net/helcel/beendroid/activity/MainActivity.kt b/app/src/main/java/net/helcel/beendroid/activity/MainActivity.kt index 690a8b5..30574a5 100644 --- a/app/src/main/java/net/helcel/beendroid/activity/MainActivity.kt +++ b/app/src/main/java/net/helcel/beendroid/activity/MainActivity.kt @@ -10,7 +10,9 @@ import com.caverock.androidsvg.SVG import com.caverock.androidsvg.SVGImageView import net.helcel.beendroid.R 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.svg.Level import net.helcel.beendroid.svg.PSVGWrapper @@ -20,12 +22,18 @@ class MainActivity : AppCompatActivity() { private lateinit var map : SVGImageView private lateinit var list : RecyclerView + private lateinit var visited : Visited + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + visited = Visited(this) + visited.load() + setContentView(R.layout.activity_main) map = findViewById(R.id.map) + val cm = HashMap() Country.values().forEach { c-> cm[c] = PSVGWrapper(applicationContext,c, Level.ZERO).load() @@ -43,6 +51,6 @@ class MainActivity : AppCompatActivity() { list = findViewById(R.id.list) list.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false) - list.adapter = FoldingListAdapter(this, WORLD) + list.adapter = FoldingListAdapter(this, World.WWW.children, visited) } } \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beendroid/countries/GeoLoc.kt b/app/src/main/java/net/helcel/beendroid/countries/GeoLoc.kt index 30931b7..74cb899 100644 --- a/app/src/main/java/net/helcel/beendroid/countries/GeoLoc.kt +++ b/app/src/main/java/net/helcel/beendroid/countries/GeoLoc.kt @@ -11,4 +11,5 @@ interface GeoLoc { val type : LocType val children : List + } \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beendroid/countries/Group.kt b/app/src/main/java/net/helcel/beendroid/countries/Group.kt index 735261b..d057cb6 100644 --- a/app/src/main/java/net/helcel/beendroid/countries/Group.kt +++ b/app/src/main/java/net/helcel/beendroid/countries/Group.kt @@ -3,16 +3,16 @@ package net.helcel.beendroid.countries import net.helcel.beendroid.countries.Country.* val WORLD = listOf( - CountryGroup.EEE, - CountryGroup.ABB, - CountryGroup.FFF, - CountryGroup.NNN, - CountryGroup.SRR, - CountryGroup.UUU, - CountryGroup.XXX + Group.EEE, + Group.ABB, + Group.FFF, + Group.NNN, + Group.SRR, + Group.UUU, + Group.XXX ) -enum class CountryGroup(override val fullName: String, override val children: List) : GeoLoc { +enum class Group(override val fullName: String, override val children: List) : GeoLoc { EEE("Europe",listOf( 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 code = this.name + + } \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beendroid/countries/State.kt b/app/src/main/java/net/helcel/beendroid/countries/State.kt new file mode 100644 index 0000000..ce7f5b9 --- /dev/null +++ b/app/src/main/java/net/helcel/beendroid/countries/State.kt @@ -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() + override val type = LocType.STATE +} \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beendroid/countries/Visited.kt b/app/src/main/java/net/helcel/beendroid/countries/Visited.kt new file mode 100644 index 0000000..07c1a97 --- /dev/null +++ b/app/src/main/java/net/helcel/beendroid/countries/Visited.kt @@ -0,0 +1,35 @@ +package net.helcel.beendroid.countries + +import android.content.Context + + +class Visited(ctx: Context) { + private var locs: MutableMap = 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) + } + +} \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beendroid/countries/World.kt b/app/src/main/java/net/helcel/beendroid/countries/World.kt new file mode 100644 index 0000000..a3a220d --- /dev/null +++ b/app/src/main/java/net/helcel/beendroid/countries/World.kt @@ -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 { + + 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 + + +} \ No newline at end of file diff --git a/app/src/test/java/net/helcel/beendroid/CountryTest.kt b/app/src/test/java/net/helcel/beendroid/CountryTest.kt index 1625d36..3be4ea6 100644 --- a/app/src/test/java/net/helcel/beendroid/CountryTest.kt +++ b/app/src/test/java/net/helcel/beendroid/CountryTest.kt @@ -1,8 +1,8 @@ package net.helcel.beendroid import net.helcel.beendroid.countries.Country -import net.helcel.beendroid.countries.CountryGroup -import net.helcel.beendroid.countries.CountryGroup.* +import net.helcel.beendroid.countries.Group +import net.helcel.beendroid.countries.Group.* import org.junit.Assert import org.junit.Test @@ -33,7 +33,7 @@ class CountryTest { @Test fun allCountriesInAGroup() { Country.values().forEach { c -> - val cnt = CountryGroup.values().none { + val cnt = Group.values().none { it.children.contains((c)) } Assert.assertEquals("$c has no group !",cnt,false) @@ -85,14 +85,14 @@ class CountryTest { @Test fun allCountryGroupsValidName() { - CountryGroup.values().forEach { + Group.values().forEach { Assert.assertEquals("$it has no full_name", it.fullName.isNotEmpty(), true) } } @Test fun allCountryGroupsValidArea() { - CountryGroup.values().forEach { + Group.values().forEach { Assert.assertEquals("$it has an area of 0", it.area >= 0, true) } }