Enable group color change in single group setting

This commit is contained in:
fgerber 2024-04-10 13:08:49 +02:00
parent 98591691a3
commit 99c7f8394c
10 changed files with 85 additions and 10 deletions

View File

@ -1,15 +1,22 @@
package net.helcel.beans.activity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.addCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayoutMediator
import net.helcel.beans.R
import net.helcel.beans.activity.adapter.ViewPagerAdapter
import net.helcel.beans.activity.fragment.EditGroupAddFragment
import net.helcel.beans.activity.fragment.EditPlaceColorFragment
import net.helcel.beans.activity.fragment.EditPlaceFragment
import net.helcel.beans.countries.World
import net.helcel.beans.databinding.ActivityEditBinding
import net.helcel.beans.helper.Data
import net.helcel.beans.helper.DialogCloser
import net.helcel.beans.helper.Settings
import net.helcel.beans.helper.Theme.createActionBar
@ -39,8 +46,24 @@ class EditActivity : AppCompatActivity() {
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
if (Settings.isSingleGroup(this)) {
menuInflater.inflate(R.menu.menu_edit, menu)
}
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
finish()
when (item.itemId) {
R.id.action_color -> {
Data.groups.getUniqueEntry()?.let { group ->
EditGroupAddFragment(group.key, {
(_binding.pager.adapter as ViewPagerAdapter?)?.refreshColors(group.color)
}, {}, false).show(supportFragmentManager, "AddColorDialogFragment")
}
}
else -> finish()
}
return super.onOptionsItemSelected(item)
}

View File

@ -3,6 +3,7 @@ package net.helcel.beans.activity.adapter
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
@ -22,6 +23,7 @@ class GeolocListAdapter(
) : RecyclerView.Adapter<GeolocListAdapter.FoldingListViewHolder>() {
private val sortedList = l.children.toList().sortedBy { it.fullName }
private val holders: MutableSet<FoldingListViewHolder> = mutableSetOf()
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): FoldingListViewHolder {
val binding = ItemListGeolocBinding.inflate(
@ -29,7 +31,9 @@ class GeolocListAdapter(
viewGroup,
false
)
return FoldingListViewHolder(ctx.requireActivity(), binding, parentHolder, l)
val holder = FoldingListViewHolder(ctx.requireActivity(), binding, parentHolder, l)
holders.add(holder)
return holder
}
override fun onBindViewHolder(holder: FoldingListViewHolder, position: Int) {
@ -46,12 +50,17 @@ class GeolocListAdapter(
return l.children.size
}
fun refreshColors(colorDrawable: ColorDrawable) {
holders.forEach { it.refreshColor(colorDrawable) }
}
class FoldingListViewHolder(
private val ctx: FragmentActivity,
private val _binding: ItemListGeolocBinding,
private val _parentHolder: FoldingListViewHolder? = null,
private val _parentGeoLoc: GeoLoc,
) : RecyclerView.ViewHolder(_binding.root), DialogCloser {
private lateinit var el: GeoLoc
private fun bindGroup(el: GeoLoc) {
refreshCount(el)
@ -65,6 +74,7 @@ class GeolocListAdapter(
}
fun bind(el: GeoLoc) {
this.el = el
_binding.textView.text = el.fullName
_binding.textView.backgroundTintList =
ColorStateList.valueOf(colorWrapper(ctx, android.R.attr.colorBackground).color)
@ -75,6 +85,14 @@ class GeolocListAdapter(
refreshCheck(el)
}
fun refreshColor(colorDrawable: ColorDrawable) {
if (Data.visits.getVisited(el) !in listOf(NO_GROUP, AUTO_GROUP)) {
_binding.checkBox.buttonTintList =
ColorStateList.valueOf(colorDrawable.color)
refreshCheck(el)
}
}
fun addListeners(el: GeoLoc, expandLambda: () -> Boolean) {
if (el.shouldShowChildren(ctx)) {
_binding.textView.setOnClickListener { expandLambda() }

View File

@ -1,5 +1,6 @@
package net.helcel.beans.activity.adapter
import android.graphics.drawable.ColorDrawable
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
@ -51,6 +52,10 @@ class ViewPagerAdapter(
override fun createFragment(position: Int): Fragment {
return fragmentList[position]
}
fun refreshColors(colorDrawable: ColorDrawable) {
fragmentList.forEach{ it.refreshColors(colorDrawable)}
}
}

View File

@ -24,7 +24,8 @@ import net.helcel.beans.helper.Theme.colorToHex6
class EditGroupAddFragment(
private val key: Int = 0,
val onAddCb: (Int) -> Unit,
val onDelCb: (Int) -> Unit
val onDelCb: (Int) -> Unit,
private val deleteEnabled: Boolean = true
) : DialogFragment() {
private lateinit var _binding: FragmentEditGroupsAddBinding
@ -42,7 +43,7 @@ class EditGroupAddFragment(
_binding.colorView.background = ColorDrawable(grp.color.color)
if (key == 0) {
if (key == 0 || !deleteEnabled) {
_binding.btnDelete.visibility = View.INVISIBLE
_binding.btnDelete.isEnabled = false
}

View File

@ -45,10 +45,9 @@ class EditPlaceColorFragment(private val parent: DialogCloser, private val delet
if (delete) {
_binding.btnAdd.visibility = View.GONE
_binding.btnClear.text = ctx.getString(R.string.cancel)
with (_binding.warningText) {
visibility = View.VISIBLE
text = ctx.getString(R.string.select_group)
}
_binding.warningText.text = ctx.getString(R.string.select_group)
} else {
_binding.warningText.text = ctx.getString(R.string.edit_group)
}
return dialog

View File

@ -1,5 +1,6 @@
package net.helcel.beans.activity.fragment
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -30,4 +31,8 @@ class EditPlaceFragment(val loc: GeoLoc, private val pager: ViewPagerAdapter, pr
_binding.list.adapter = GeolocListAdapter(this, loc, pager, holder)
return _binding.root
}
fun refreshColors(colorDrawable: ColorDrawable) {
(_binding.list.adapter as GeolocListAdapter?)?.refreshColors(colorDrawable)
}
}

View File

@ -0,0 +1,10 @@
<vector
android:height="24dp"
android:viewportHeight="960"
android:viewportWidth="960"
android:width="24dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="?attr/colorOnBackground"
android:pathData="M346,820L100,574Q90,564 85,552Q80,540 80,527Q80,514 85,502Q90,490 100,480L330,251L224,145L286,80L686,480Q696,490 700.5,502Q705,514 705,527Q705,540 700.5,552Q696,564 686,574L440,820Q430,830 418,835Q406,840 393,840Q380,840 368,835Q356,830 346,820ZM393,314L179,528Q179,528 179,528Q179,528 179,528L607,528Q607,528 607,528Q607,528 607,528L393,314ZM792,840Q756,840 731,814.5Q706,789 706,752Q706,725 719.5,701Q733,677 750,654L792,600L836,654Q852,677 866,701Q880,725 880,752Q880,789 854,814.5Q828,840 792,840Z"/>
</vector>

View File

@ -10,8 +10,7 @@
android:id="@+id/warning_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:visibility="gone" />
android:layout_marginBottom="10dp" />
<ScrollView
android:layout_width="match_parent"

View File

@ -0,0 +1,13 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="net.helcel.beans.activity.EditActivity" >
<item
android:id="@+id/action_color"
android:orderInCategory="100"
android:icon="@drawable/color"
android:title="@string/action_color"
app:showAsAction="ifRoom" />
</menu>

View File

@ -5,6 +5,7 @@
<string name="action_settings">Settings</string>
<string name="action_stat">Stats</string>
<string name="action_edit">Edit</string>
<string name="action_color">Color</string>
<string name="key_theme">App theme</string>
<string name="system">System</string>
<string name="light">Light</string>
@ -20,6 +21,7 @@
<string name="beans_repo">Project repository: https://git.helcel.net/helcel/beans\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="about_beans">About the Beans application</string>
<string name="edit_group">Select the group to assign. Long press on a group to edit its name and color.</string>
<string name="delete_group">Are your sure you want to delete this group and remove all its country mappings?</string>
<string name="select_group">Select one group you want to keep. All others will be deleted and its mappings reassigned to the group you choose here.</string>
<string name="delete_regions">Are you sure you want to disable regions and reassign all regional mappings to the corresponding countries?</string>