From 411bd5c4ded7bf15556da10c2f642110001777fb Mon Sep 17 00:00:00 2001 From: fgerber Date: Tue, 5 Mar 2024 18:57:48 +0100 Subject: [PATCH] Adapt group selection UI - Enable group deletion (including mapped countries) - If only one group exists, no need to go through the dialog --- .../activity/adapter/GeolocListAdapter.kt | 11 ++++-- .../activity/adapter/GroupListAdapter.kt | 35 ++++++++++--------- .../net/helcel/beendroid/helper/Groups.kt | 9 +++++ .../net/helcel/beendroid/helper/Visits.kt | 9 +++++ app/src/main/res/values/en.xml | 2 +- 5 files changed, 46 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/net/helcel/beendroid/activity/adapter/GeolocListAdapter.kt b/app/src/main/java/net/helcel/beendroid/activity/adapter/GeolocListAdapter.kt index 56fd8a7..f64ede1 100644 --- a/app/src/main/java/net/helcel/beendroid/activity/adapter/GeolocListAdapter.kt +++ b/app/src/main/java/net/helcel/beendroid/activity/adapter/GeolocListAdapter.kt @@ -88,10 +88,15 @@ class GeolocListAdapter( fun addListeners(el: Pair, expandLambda: () -> Boolean) { textView.setOnClickListener { expandLambda() } checkBox.setOnClickListener { - val dialogFragment = EditPlaceColorFragment(this) selected_geoloc = el.first - selected_group = null - dialogFragment.show(ctx.supportFragmentManager, "AddColorDialogFragment") + if (groups!!.size() != 1) { + val dialogFragment = EditPlaceColorFragment(this) + selected_group = null + dialogFragment.show(ctx.supportFragmentManager, "AddColorDialogFragment") + } else { + selected_group = groups!!.getUniqueEntry()!! + onColorDialogDismiss(false) + } } } diff --git a/app/src/main/java/net/helcel/beendroid/activity/adapter/GroupListAdapter.kt b/app/src/main/java/net/helcel/beendroid/activity/adapter/GroupListAdapter.kt index 4ecd1fd..b25f57d 100644 --- a/app/src/main/java/net/helcel/beendroid/activity/adapter/GroupListAdapter.kt +++ b/app/src/main/java/net/helcel/beendroid/activity/adapter/GroupListAdapter.kt @@ -15,6 +15,7 @@ import net.helcel.beendroid.helper.getContrastColor import net.helcel.beendroid.helper.groups import net.helcel.beendroid.helper.saveData import net.helcel.beendroid.helper.selected_group +import net.helcel.beendroid.helper.visits class GroupListAdapter(private val activity: FragmentActivity, private val selectDialog: DialogFragment?) : RecyclerView.Adapter() { @@ -39,7 +40,7 @@ class GroupListAdapter(private val activity: FragmentActivity, private val selec color.setBackgroundColor(entry.second.color.color) color.setTextColor(getContrastColor(entry.second.color.color)) color.setOnClickListener { - if(selectDialog==null) { + if (selectDialog == null) { val dialogFragment = EditGroupAddFragment(entry.first) { val newEntry = groups!!.getGroupFromKey(entry.first)!! color.text = newEntry.name @@ -50,28 +51,30 @@ class GroupListAdapter(private val activity: FragmentActivity, private val selec activity.supportFragmentManager, "AddColorDialogFragment" ) - }else{ + } else { selected_group = entry.second selectDialog.dismiss() } } color.setOnLongClickListener { - MaterialAlertDialogBuilder(activity) - .setMessage(R.string.delete_group) - .setPositiveButton(android.R.string.ok) { _, _ -> - // TODO: Remove all countries belonging to that group + if (selectDialog == null) { + MaterialAlertDialogBuilder(activity) + .setMessage(R.string.delete_group) + .setPositiveButton(android.R.string.ok) { _, _ -> + // Remove all countries belonging to that group + val key = entry.first + visits!!.deleteVisited(key) - - // Delete the group - val key = entry.first - val pos = groups!!.findGroupPos(key) - groups!!.deleteGroup(key) - saveData() - this@GroupListAdapter.notifyItemRemoved(pos) - } - .setNegativeButton(android.R.string.cancel) { _, _ -> } - .show() + // Delete the group + val pos = groups!!.findGroupPos(key) + groups!!.deleteGroup(key) + saveData() + this@GroupListAdapter.notifyItemRemoved(pos) + } + .setNegativeButton(android.R.string.cancel) { _, _ -> } + .show() + } true } } diff --git a/app/src/main/java/net/helcel/beendroid/helper/Groups.kt b/app/src/main/java/net/helcel/beendroid/helper/Groups.kt index 21bebbe..59bd863 100644 --- a/app/src/main/java/net/helcel/beendroid/helper/Groups.kt +++ b/app/src/main/java/net/helcel/beendroid/helper/Groups.kt @@ -36,6 +36,15 @@ class Groups(val id: Int, private val grps: HashMap) { return grps.size } + fun getUniqueEntry(): Group? { + assert(size() == 1) + return if (grps.size == 1) { + grps[grps.keys.first()] + } else { + null + } + } + fun getGroupFromPos(pos: Int): Pair { val key = grps.keys.toList()[pos] return Pair(key,getGroupFromKey(key)!!) diff --git a/app/src/main/java/net/helcel/beendroid/helper/Visits.kt b/app/src/main/java/net/helcel/beendroid/helper/Visits.kt index a5be13a..bfcd9bb 100644 --- a/app/src/main/java/net/helcel/beendroid/helper/Visits.kt +++ b/app/src/main/java/net/helcel/beendroid/helper/Visits.kt @@ -15,6 +15,15 @@ class Visits(val id: Int, private val locs: HashMap) { locs[key.code] = b } + fun deleteVisited(key: Int) { + val keysToDelete = locs + .filter { it.value == key } + .map { it.key } + keysToDelete.forEach { + locs.remove(it) + } + } + fun getVisited(key: GeoLoc): Int { return locs.getOrDefault(key.code,0) } diff --git a/app/src/main/res/values/en.xml b/app/src/main/res/values/en.xml index a86db5a..e7270d3 100644 --- a/app/src/main/res/values/en.xml +++ b/app/src/main/res/values/en.xml @@ -17,7 +17,7 @@ Project repository: https://git.helcel.net/helcel/beendroid\n Feel free to report issues or contribute to the project. Free and open source dependencies and licenses About the BeenDroid application - Do you want to delete this group? + Are your sure you want to delete this group and remove all its country mappings? Add Logo Name