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 750e479..4663de3 100644 --- a/app/src/main/java/net/helcel/beendroid/activity/FoldingListAdapter.kt +++ b/app/src/main/java/net/helcel/beendroid/activity/FoldingListAdapter.kt @@ -4,21 +4,23 @@ import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.CheckBox import android.widget.ImageView import android.widget.TextView import androidx.appcompat.content.res.AppCompatResources import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.checkbox.MaterialCheckBox 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, private val visited: Visited) : - RecyclerView.Adapter() { +class FoldingListAdapter( + private val ctx: Context, l: List, + private val visited: Visited, + private val parentLambda: () -> Unit, + ) : RecyclerView.Adapter() { private var cg : MutableMap = l.sortedBy { it.fullName }.fold(LinkedHashMap()) { acc, e -> acc[e] = false @@ -34,39 +36,34 @@ class FoldingListAdapter(private val ctx: Context, l: List, private val override fun onBindViewHolder(holder: FoldingListViewHolder, position: Int) { val el = cg.toList()[position] - holder.bind(el) - - val expandLambda = { _:View -> - if (el.first.children.isEmpty() || el.first.type == LocType.STATE) { - false - } else { - cg[el.first] = !el.second - if (!el.second) - cg.filter { (it.key != el.first) && (cg[it.key] == true) }.keys.forEach { - cg[it] = false - notifyItemChanged(cg.toList().map { e -> e.first }.indexOf(it)) - } - - notifyItemChanged(position) - true - } - + holder.bind(el) { + notifyItemChanged(position) } - 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.addListeners( { + if (!el.first.isEnd) { + cg[el.first] = !el.second + notifyItemChanged(position) + } + !el.first.isEnd + }, { + visited.setVisited(el.first, it) + parentLambda() + }) + + } override fun getItemCount(): Int { return cg.size } - class FoldingListViewHolder(private val ctx: Context, itemView: View, private val visited: Visited) : RecyclerView.ViewHolder(itemView) { - val textView: TextView - val expand: ImageView - val checkBox: CheckBox + class FoldingListViewHolder(private val ctx: Context, itemView: View, + private val visited: Visited, + ) : RecyclerView.ViewHolder(itemView) { + private val textView: TextView + private val expand: ImageView + private val checkBox: MaterialCheckBox private val subItemView: View private val list: RecyclerView @@ -78,22 +75,32 @@ class FoldingListAdapter(private val ctx: Context, l: List, private val subItemView = itemView.findViewById(R.id.sub_item) list = itemView.findViewById(R.id.list_list) list.layoutManager = LinearLayoutManager(ctx, RecyclerView.VERTICAL, false) - } - fun bind(el: Pair) { + fun bind(el: Pair, parentLambda: () -> Unit) { expand.rotation = if(el.second) 90f else 0f subItemView.visibility = if (el.second) View.VISIBLE else View.GONE - + expand.visibility = if(!el.first.isEnd) View.VISIBLE else View.GONE 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 - } + checkBox.checkedState = + if(visited.visited(el.first)) MaterialCheckBox.STATE_CHECKED + else if (el.first.children.any { visited.visited(it) }) MaterialCheckBox.STATE_INDETERMINATE + else MaterialCheckBox.STATE_UNCHECKED + textView.parent.parent.requestChildFocus(textView,textView) - list.adapter = FoldingListAdapter(ctx, el.first.children,visited) + list.adapter = FoldingListAdapter(ctx, el.first.children,visited, parentLambda) + } + + fun addListeners(expandLambda: ()->Boolean, visitedLambda: (Boolean)->Unit) { + + textView.setOnClickListener { checkBox.toggle() } + checkBox.addOnCheckedStateChangedListener { _, e -> + visitedLambda(e == MaterialCheckBox.STATE_CHECKED) + } + + textView.setOnLongClickListener{ expandLambda() } + expand.setOnClickListener { expandLambda() } } } 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 30574a5..71118d0 100644 --- a/app/src/main/java/net/helcel/beendroid/activity/MainActivity.kt +++ b/app/src/main/java/net/helcel/beendroid/activity/MainActivity.kt @@ -6,14 +6,10 @@ import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView -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 @@ -23,6 +19,7 @@ class MainActivity : AppCompatActivity() { private lateinit var list : RecyclerView private lateinit var visited : Visited + private lateinit var psvg : PSVGWrapper override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -30,27 +27,20 @@ class MainActivity : AppCompatActivity() { visited = Visited(this) visited.load() + psvg = PSVGWrapper(this) + 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() - } - - val fm = cm.values.fold("") { acc, e -> acc + e.data } - val svg = SVG.getFromString("$fm") - val bitmap = Bitmap.createBitmap(1200,900, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) canvas.drawRGB(255, 255, 255) - svg.renderToCanvas(canvas) + psvg.get().renderToCanvas(canvas) map.setImageBitmap(bitmap) list = findViewById(R.id.list) list.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false) - list.adapter = FoldingListAdapter(this, World.WWW.children, visited) + 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 74cb899..22f115a 100644 --- a/app/src/main/java/net/helcel/beendroid/countries/GeoLoc.kt +++ b/app/src/main/java/net/helcel/beendroid/countries/GeoLoc.kt @@ -3,7 +3,6 @@ package net.helcel.beendroid.countries enum class LocType { WORLD, GROUP, CUSTOM_GROUP, COUNTRY, STATE; } - interface GeoLoc { val code : String val fullName : String @@ -12,4 +11,6 @@ interface GeoLoc { val type : LocType val children : List + val isEnd: Boolean + get() = children.isEmpty() || type == LocType.STATE } \ 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 d057cb6..aef08f1 100644 --- a/app/src/main/java/net/helcel/beendroid/countries/Group.kt +++ b/app/src/main/java/net/helcel/beendroid/countries/Group.kt @@ -1,17 +1,6 @@ package net.helcel.beendroid.countries import net.helcel.beendroid.countries.Country.* - -val WORLD = listOf( - Group.EEE, - Group.ABB, - Group.FFF, - Group.NNN, - Group.SRR, - Group.UUU, - Group.XXX -) - enum class Group(override val fullName: String, override val children: List) : GeoLoc { EEE("Europe",listOf( diff --git a/app/src/main/java/net/helcel/beendroid/svg/PSVGLoader.kt b/app/src/main/java/net/helcel/beendroid/svg/PSVGLoader.kt new file mode 100644 index 0000000..9058c51 --- /dev/null +++ b/app/src/main/java/net/helcel/beendroid/svg/PSVGLoader.kt @@ -0,0 +1,28 @@ +package net.helcel.beendroid.svg + +import android.content.Context +import net.helcel.beendroid.countries.Country +import java.nio.charset.StandardCharsets + + +class PSVGLoader(private val c: Context, private val country: Country, private var level: Level) { + var data = "" + + fun load(): PSVGLoader { + data = try { + String( + c.assets.open("${country.code}_${level.id}.psvg").readBytes(), + StandardCharsets.UTF_8 + ) + }catch(e: Exception){ + "" + } + return this + } + + fun changeLevel(level: Level): PSVGLoader { + this.level = level + this.load() + return this + } +} \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beendroid/svg/PSVGWrapper.kt b/app/src/main/java/net/helcel/beendroid/svg/PSVGWrapper.kt index 6e698e0..e498361 100644 --- a/app/src/main/java/net/helcel/beendroid/svg/PSVGWrapper.kt +++ b/app/src/main/java/net/helcel/beendroid/svg/PSVGWrapper.kt @@ -1,28 +1,31 @@ package net.helcel.beendroid.svg import android.content.Context +import com.caverock.androidsvg.SVG import net.helcel.beendroid.countries.Country -import java.nio.charset.StandardCharsets + +class PSVGWrapper(ctx: Context) { -class PSVGWrapper(private val c: Context, private val country: Country, private var level: Level) { - var data = "" - fun load(): PSVGWrapper { - data = try { - String( - c.assets.open("${country.code}_${level.id}.psvg").readBytes(), - StandardCharsets.UTF_8 - ) - }catch(e: Exception){ - "" + private val cm = HashMap() + + init { + Country.values().forEach { + cm[it] = PSVGLoader(ctx, it, Level.ZERO).load() } - return this + } + fun level(el: Country, level: Level){ + cm[el]?.changeLevel(level) } - fun changeLevel(level: Level): PSVGWrapper { - this.level = level - this.load() - return this + fun get(): SVG { + val fm = cm.values.fold("") { acc, e -> acc + e.data } + return SVG.getFromString("$fm") } + + + + + } \ No newline at end of file diff --git a/app/src/main/res/layout/item_list.xml b/app/src/main/res/layout/item_list.xml index 0f03410..784e61d 100644 --- a/app/src/main/res/layout/item_list.xml +++ b/app/src/main/res/layout/item_list.xml @@ -26,12 +26,13 @@ android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" + app:checkedState="indeterminate" app:layout_constraintBottom_toBottomOf="@+id/textView" app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintHorizontal_bias="0.844" + app:layout_constraintHorizontal_bias="1" app:layout_constraintStart_toEndOf="@+id/textView" app:layout_constraintTop_toTopOf="@+id/textView" - app:layout_constraintVertical_bias="0.0" /> + app:layout_constraintVertical_bias="0.5" /> - - \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index 7021d99..89c86e9 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -1,17 +1,6 @@ - -