From 872f31746ed1dff82eebe7d5a290eda082c56ef3 Mon Sep 17 00:00:00 2001 From: fgerber Date: Mon, 4 Mar 2024 22:35:27 +0100 Subject: [PATCH] Create default group and delete group (WIP) --- .../activity/adapter/GroupListAdapter.kt | 23 +++++- .../activity/adapter/ViewPagerAdapter.kt | 8 +-- .../activity/fragment/EditGroupAddFragment.kt | 70 ++++++++++++------- .../java/net/helcel/beendroid/helper/Data.kt | 10 +++ .../net/helcel/beendroid/helper/Groups.kt | 4 ++ app/src/main/res/drawable/add.xml | 9 +++ .../res/drawable/ic_launcher_background.xml | 2 +- .../res/drawable/ic_launcher_foreground.xml | 2 +- .../main/res/layout/fragment_edit_groups.xml | 9 ++- .../res/layout/fragment_edit_groups_add.xml | 2 +- app/src/main/res/values/en.xml | 1 + 11 files changed, 102 insertions(+), 38 deletions(-) create mode 100644 app/src/main/res/drawable/add.xml 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 2d22de0..4ecd1fd 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 @@ -7,11 +7,13 @@ import android.widget.Button import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentActivity import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.dialog.MaterialAlertDialogBuilder import net.helcel.beendroid.R import net.helcel.beendroid.activity.fragment.EditGroupAddFragment import net.helcel.beendroid.helper.Groups 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 class GroupListAdapter(private val activity: FragmentActivity, private val selectDialog: DialogFragment?) : RecyclerView.Adapter() { @@ -29,7 +31,7 @@ class GroupListAdapter(private val activity: FragmentActivity, private val selec return groups!!.size() } - class GroupViewHolder(itemView: View, private val activity: FragmentActivity, private val selectDialog: DialogFragment?) : RecyclerView.ViewHolder(itemView) { + inner class GroupViewHolder(itemView: View, private val activity: FragmentActivity, private val selectDialog: DialogFragment?) : RecyclerView.ViewHolder(itemView) { private val color: Button = itemView.findViewById(R.id.group_color) fun bind(entry: Pair) { @@ -53,6 +55,25 @@ class GroupListAdapter(private val activity: FragmentActivity, private val selec selectDialog.dismiss() } } + + color.setOnLongClickListener { + MaterialAlertDialogBuilder(activity) + .setMessage(R.string.delete_group) + .setPositiveButton(android.R.string.ok) { _, _ -> + // TODO: Remove all countries belonging to that group + + + // 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() + true + } } } } \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beendroid/activity/adapter/ViewPagerAdapter.kt b/app/src/main/java/net/helcel/beendroid/activity/adapter/ViewPagerAdapter.kt index b76c6a8..e21e0d0 100644 --- a/app/src/main/java/net/helcel/beendroid/activity/adapter/ViewPagerAdapter.kt +++ b/app/src/main/java/net/helcel/beendroid/activity/adapter/ViewPagerAdapter.kt @@ -8,8 +8,8 @@ import net.helcel.beendroid.activity.fragment.EditGroupFragment import net.helcel.beendroid.activity.fragment.EditPlaceFragment private val tabArray = arrayOf( + "Places", "Groups", - "Places" ) class ViewPagerAdapter (fragmentManager: FragmentManager, lifecycle: Lifecycle) : FragmentStateAdapter(fragmentManager, lifecycle) { @@ -24,10 +24,10 @@ class ViewPagerAdapter (fragmentManager: FragmentManager, lifecycle: Lifecycle) override fun createFragment(position: Int): Fragment { when (position) { - 0 -> return EditGroupFragment() - 1 -> return EditPlaceFragment() + 0 -> return EditPlaceFragment() + 1 -> return EditGroupFragment() } - return EditGroupFragment() + return EditPlaceFragment() } } diff --git a/app/src/main/java/net/helcel/beendroid/activity/fragment/EditGroupAddFragment.kt b/app/src/main/java/net/helcel/beendroid/activity/fragment/EditGroupAddFragment.kt index 1132d83..a40c645 100644 --- a/app/src/main/java/net/helcel/beendroid/activity/fragment/EditGroupAddFragment.kt +++ b/app/src/main/java/net/helcel/beendroid/activity/fragment/EditGroupAddFragment.kt @@ -23,15 +23,16 @@ import net.helcel.beendroid.helper.saveData import java.lang.Exception -class EditGroupAddFragment(private val key: Int =0, val onAddCb : (Int)->Unit) : DialogFragment() { +class EditGroupAddFragment(private val key: Int = 0, val onAddCb: (Int) -> Unit) : + DialogFragment() { private lateinit var colorNameEditText: TextInputEditText private lateinit var colorEditText: TextInputEditText - private lateinit var colorView : View + private lateinit var colorView: View - private lateinit var colorEditR : Slider - private lateinit var colorEditG : Slider - private lateinit var colorEditB : Slider + private lateinit var colorEditR: Slider + private lateinit var colorEditG: Slider + private lateinit var colorEditB: Slider private val grp = groups!!.getGroupFromKey(key) override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { @@ -48,54 +49,68 @@ class EditGroupAddFragment(private val key: Int =0, val onAddCb : (Int)->Unit) : colorEditG = view.findViewById(R.id.colorG) colorEditB = view.findViewById(R.id.colorB) - setupSlider(colorEditR,(grp?.color?.color?.red ?: 0)/ 255F) - setupSlider(colorEditG,(grp?.color?.color?.green ?: 0)/ 255F) - setupSlider(colorEditB,(grp?.color?.color?.blue ?: 0)/ 255F) + setupSlider(colorEditR, (grp?.color?.color?.red ?: 0) / 255F) + setupSlider(colorEditG, (grp?.color?.color?.green ?: 0) / 255F) + setupSlider(colorEditB, (grp?.color?.color?.blue ?: 0) / 255F) - setupText(colorEditText,grp) + setupText(colorEditText, grp) colorView.background = ColorDrawable(grp?.color?.color ?: 0) - colorNameEditText.setText(grp?.name ?: "") builder.setView(view) - .setPositiveButton("Ok") { _: DialogInterface?, _: Int -> + .setPositiveButton(android.R.string.ok) { _: DialogInterface?, _: Int -> val name = colorNameEditText.text.toString() val color = colorEditText.text.toString() - val key = (if (key!=0) key else groups!!.genKey()) - groups!!.setGroup(key,name, ColorDrawable(Color.parseColor("#$color"))) + val key = (if (key != 0) key else groups!!.genKey()) + groups!!.setGroup(key, name, ColorDrawable(Color.parseColor("#$color"))) saveData() onAddCb(key) } - .setNegativeButton( - "Cancel" - ) { dialog: DialogInterface, _: Int -> dialog.cancel() } + .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> + dialog.cancel() + } return builder.create() } private fun setupText(s: TextInputEditText, grp: Groups.Group?) { - s.setText( colorToHex6(ColorDrawable(grp?.color?.color ?: 0)).substring(1)) - s.addTextChangedListener(EditTextListener(colorEditR, colorEditG, colorEditB, colorEditText, colorView)) + s.setText(colorToHex6(ColorDrawable(grp?.color?.color ?: 0)).substring(1)) + s.addTextChangedListener( + EditTextListener( + colorEditR, + colorEditG, + colorEditB, + colorEditText, + colorView + ) + ) } - private fun setupSlider(s: Slider, v: Float){ + private fun setupSlider(s: Slider, v: Float) { s.valueFrom = 0F s.valueTo = 1F s.value = v - s.addOnChangeListener(SliderOnChangeListener( colorEditR, colorEditG, colorEditB,colorEditText, colorView)) + s.addOnChangeListener( + SliderOnChangeListener( + colorEditR, + colorEditG, + colorEditB, + colorEditText, + colorView + ) + ) } } - private class EditTextListener( private val colorEditR: Slider, private val colorEditG: Slider, private val colorEditB: Slider, private val colorEditText: TextInputEditText, private val colorView: View -): TextWatcher { +) : TextWatcher { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { @@ -105,10 +120,10 @@ private class EditTextListener( } override fun afterTextChanged(s: Editable?) { - val col : Color - try{ + val col: Color + try { col = Color.valueOf(Color.parseColor("#${colorEditText.text}")) - }catch (e:Exception){ + } catch (e: Exception) { return } @@ -126,9 +141,10 @@ private class SliderOnChangeListener( private val colorEditB: Slider, private val colorEditText: TextInputEditText, private val colorView: View -): Slider.OnChangeListener { +) : Slider.OnChangeListener { override fun onValueChange(slider: Slider, value: Float, fromUser: Boolean) { - val rgb = ColorDrawable(Color.argb(1F,colorEditR.value, colorEditG.value, colorEditB.value)) + val rgb = + ColorDrawable(Color.argb(1F, colorEditR.value, colorEditG.value, colorEditB.value)) colorEditText.setText(colorToHex6(rgb).substring(1)) colorView.background = rgb } diff --git a/app/src/main/java/net/helcel/beendroid/helper/Data.kt b/app/src/main/java/net/helcel/beendroid/helper/Data.kt index 58b3e24..c675764 100644 --- a/app/src/main/java/net/helcel/beendroid/helper/Data.kt +++ b/app/src/main/java/net/helcel/beendroid/helper/Data.kt @@ -2,7 +2,10 @@ package net.helcel.beendroid.helper import android.content.Context import android.content.SharedPreferences +import android.graphics.drawable.ColorDrawable +import androidx.core.content.ContextCompat import androidx.preference.PreferenceManager +import net.helcel.beendroid.R import net.helcel.beendroid.countries.GeoLoc var visits : Visits? = null @@ -24,6 +27,13 @@ fun loadData(ctx: Context, id:Int) { groups = if(!groupsString.isNullOrEmpty()) groupsSerial.readFrom(groupsString.byteInputStream()) else groupsSerial.defaultValue visits = if(!visitsString.isNullOrEmpty()) visitsSerial.readFrom(visitsString.byteInputStream()) else visitsSerial.defaultValue + + // Add default group "Visited" with app's color if there is no group already + if (groups!!.size() == 0) { + groups!!.setGroup(1, "Visited", ColorDrawable(ContextCompat.getColor(ctx, R.color.blue))) + saveData() + } + } fun saveData() { 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 e0d30cd..21bebbe 100644 --- a/app/src/main/java/net/helcel/beendroid/helper/Groups.kt +++ b/app/src/main/java/net/helcel/beendroid/helper/Groups.kt @@ -18,6 +18,10 @@ class Groups(val id: Int, private val grps: HashMap) { grps[key] = Group(key,name,col) } + fun deleteGroup(key: Int) { + grps.remove(key) + } + fun getGroupFromKey(key: Int): Group? { return grps.getOrDefault(key,null) } diff --git a/app/src/main/res/drawable/add.xml b/app/src/main/res/drawable/add.xml new file mode 100644 index 0000000..37f4023 --- /dev/null +++ b/app/src/main/res/drawable/add.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml index b4d8b97..b7ca8f2 100644 --- a/app/src/main/res/drawable/ic_launcher_background.xml +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -5,6 +5,6 @@ android:viewportWidth="960" android:viewportHeight="960"> diff --git a/app/src/main/res/drawable/ic_launcher_foreground.xml b/app/src/main/res/drawable/ic_launcher_foreground.xml index 3c49f59..b91a905 100644 --- a/app/src/main/res/drawable/ic_launcher_foreground.xml +++ b/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -5,7 +5,7 @@ android:viewportHeight="1600"> -