Adapt group selection UI

- Enable group deletion (including mapped countries)
- If only one group exists, no need to go through the dialog
This commit is contained in:
fgerber 2024-03-05 18:57:48 +01:00
parent 872f31746e
commit 411bd5c4de
5 changed files with 46 additions and 20 deletions

View File

@ -88,10 +88,15 @@ class GeolocListAdapter(
fun addListeners(el: Pair<GeoLoc, Boolean>, expandLambda: () -> Boolean) { fun addListeners(el: Pair<GeoLoc, Boolean>, expandLambda: () -> Boolean) {
textView.setOnClickListener { expandLambda() } textView.setOnClickListener { expandLambda() }
checkBox.setOnClickListener { checkBox.setOnClickListener {
val dialogFragment = EditPlaceColorFragment(this)
selected_geoloc = el.first selected_geoloc = el.first
if (groups!!.size() != 1) {
val dialogFragment = EditPlaceColorFragment(this)
selected_group = null selected_group = null
dialogFragment.show(ctx.supportFragmentManager, "AddColorDialogFragment") dialogFragment.show(ctx.supportFragmentManager, "AddColorDialogFragment")
} else {
selected_group = groups!!.getUniqueEntry()!!
onColorDialogDismiss(false)
}
} }
} }

View File

@ -15,6 +15,7 @@ import net.helcel.beendroid.helper.getContrastColor
import net.helcel.beendroid.helper.groups import net.helcel.beendroid.helper.groups
import net.helcel.beendroid.helper.saveData import net.helcel.beendroid.helper.saveData
import net.helcel.beendroid.helper.selected_group 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<GroupListAdapter.GroupViewHolder>() { class GroupListAdapter(private val activity: FragmentActivity, private val selectDialog: DialogFragment?) : RecyclerView.Adapter<GroupListAdapter.GroupViewHolder>() {
@ -39,7 +40,7 @@ class GroupListAdapter(private val activity: FragmentActivity, private val selec
color.setBackgroundColor(entry.second.color.color) color.setBackgroundColor(entry.second.color.color)
color.setTextColor(getContrastColor(entry.second.color.color)) color.setTextColor(getContrastColor(entry.second.color.color))
color.setOnClickListener { color.setOnClickListener {
if(selectDialog==null) { if (selectDialog == null) {
val dialogFragment = EditGroupAddFragment(entry.first) { val dialogFragment = EditGroupAddFragment(entry.first) {
val newEntry = groups!!.getGroupFromKey(entry.first)!! val newEntry = groups!!.getGroupFromKey(entry.first)!!
color.text = newEntry.name color.text = newEntry.name
@ -50,21 +51,22 @@ class GroupListAdapter(private val activity: FragmentActivity, private val selec
activity.supportFragmentManager, activity.supportFragmentManager,
"AddColorDialogFragment" "AddColorDialogFragment"
) )
}else{ } else {
selected_group = entry.second selected_group = entry.second
selectDialog.dismiss() selectDialog.dismiss()
} }
} }
color.setOnLongClickListener { color.setOnLongClickListener {
if (selectDialog == null) {
MaterialAlertDialogBuilder(activity) MaterialAlertDialogBuilder(activity)
.setMessage(R.string.delete_group) .setMessage(R.string.delete_group)
.setPositiveButton(android.R.string.ok) { _, _ -> .setPositiveButton(android.R.string.ok) { _, _ ->
// TODO: Remove all countries belonging to that group // Remove all countries belonging to that group
val key = entry.first
visits!!.deleteVisited(key)
// Delete the group // Delete the group
val key = entry.first
val pos = groups!!.findGroupPos(key) val pos = groups!!.findGroupPos(key)
groups!!.deleteGroup(key) groups!!.deleteGroup(key)
saveData() saveData()
@ -72,6 +74,7 @@ class GroupListAdapter(private val activity: FragmentActivity, private val selec
} }
.setNegativeButton(android.R.string.cancel) { _, _ -> } .setNegativeButton(android.R.string.cancel) { _, _ -> }
.show() .show()
}
true true
} }
} }

View File

@ -36,6 +36,15 @@ class Groups(val id: Int, private val grps: HashMap<Int,Group>) {
return grps.size 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<Int,Group> { fun getGroupFromPos(pos: Int): Pair<Int,Group> {
val key = grps.keys.toList()[pos] val key = grps.keys.toList()[pos]
return Pair(key,getGroupFromKey(key)!!) return Pair(key,getGroupFromKey(key)!!)

View File

@ -15,6 +15,15 @@ class Visits(val id: Int, private val locs: HashMap<String,Int>) {
locs[key.code] = b 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 { fun getVisited(key: GeoLoc): Int {
return locs.getOrDefault(key.code,0) return locs.getOrDefault(key.code,0)
} }

View File

@ -17,7 +17,7 @@
<string name="beendroid_repo">Project repository: https://git.helcel.net/helcel/beendroid\n Feel free to report issues or contribute to the project.</string> <string name="beendroid_repo">Project repository: https://git.helcel.net/helcel/beendroid\n Feel free to report issues or contribute to the project.</string>
<string name="foss_licenses">Free and open source dependencies and licenses</string> <string name="foss_licenses">Free and open source dependencies and licenses</string>
<string name="about_beendroid">About the BeenDroid application</string> <string name="about_beendroid">About the BeenDroid application</string>
<string name="delete_group">Do you want to delete this group?</string> <string name="delete_group">Are your sure you want to delete this group and remove all its country mappings?</string>
<string name="group_add">Add</string> <string name="group_add">Add</string>
<string name="logo">Logo</string> <string name="logo">Logo</string>
<string name="name">Name</string> <string name="name">Name</string>