From 6839de7b7ff7612b18920843fd1b713f80214c20 Mon Sep 17 00:00:00 2001 From: Kyle Corry Date: Mon, 13 Nov 2023 16:42:38 -0500 Subject: [PATCH] Extract tool button --- .../trail_sense/tools/ui/ToolButton.kt | 60 +++++++++++++++++++ .../trail_sense/tools/ui/ToolsFragment.kt | 23 +------ 2 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolButton.kt diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolButton.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolButton.kt new file mode 100644 index 000000000..e5e374c3f --- /dev/null +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolButton.kt @@ -0,0 +1,60 @@ +package com.kylecorry.trail_sense.tools.ui + +import android.content.Context +import android.content.res.ColorStateList +import android.util.AttributeSet +import android.view.Gravity +import android.view.View +import android.widget.FrameLayout +import android.widget.TextView +import com.kylecorry.andromeda.core.system.Resources +import com.kylecorry.andromeda.core.ui.setCompoundDrawables +import com.kylecorry.trail_sense.R +import com.kylecorry.trail_sense.shared.CustomUiUtils + +class ToolButton(context: Context, attrs: AttributeSet? = null) : FrameLayout(context, attrs) { + + private val textView: TextView + + private val iconSize = Resources.dp(context, 24f).toInt() + private val iconPadding = Resources.dp(context, 12f).toInt() + private val iconColor = Resources.androidTextColorPrimary(context) + private val buttonHeight = Resources.dp(context, 64f).toInt() + private val buttonPadding = Resources.dp(context, 16f).toInt() + private val buttonBackgroundColor = + Resources.getAndroidColorAttr(context, android.R.attr.colorBackgroundFloating) + + var text: CharSequence? + get() = textView.text + set(value) { + textView.text = value + } + + fun setIconResource(icon: Int) { + textView.setCompoundDrawables(iconSize, left = icon) + CustomUiUtils.setImageColor(textView, iconColor) + } + + fun setOnClickListener(listener: (View) -> Unit) { + textView.setOnClickListener(listener) + } + + fun setOnLongClickListener(listener: (View) -> Boolean) { + textView.setOnLongClickListener(listener) + } + + init { + textView = TextView(context) + textView.compoundDrawablePadding = iconPadding + textView.elevation = 2f + textView.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, buttonHeight) + textView.gravity = Gravity.CENTER_VERTICAL + textView.setPadding(buttonPadding, 0, buttonPadding, 0) + + textView.setBackgroundResource(R.drawable.rounded_rectangle) + textView.backgroundTintList = ColorStateList.valueOf(buttonBackgroundColor) + + addView(textView) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolsFragment.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolsFragment.kt index ed8ca6290..d6cc25ccb 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolsFragment.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolsFragment.kt @@ -228,38 +228,21 @@ class ToolsFragment : BoundFragment() { } private fun createToolButton(tool: Tool): View { - // TODO: Move this to the class level - val iconSize = Resources.dp(requireContext(), 24f).toInt() - val iconPadding = Resources.dp(requireContext(), 12f).toInt() - val iconColor = Resources.androidTextColorPrimary(requireContext()) - val buttonHeight = Resources.dp(requireContext(), 64f).toInt() val buttonMargins = Resources.dp(requireContext(), 8f).toInt() - val buttonPadding = Resources.dp(requireContext(), 16f).toInt() - val buttonBackgroundColor = Resources.getAndroidColorAttr( - requireContext(), android.R.attr.colorBackgroundFloating - ) - val gridColumnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f) val gridRowSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f) - val button = TextView(requireContext()) + val button = ToolButton(requireContext()) button.text = tool.name.capitalizeWords() - button.setCompoundDrawables(iconSize, left = tool.icon) - button.compoundDrawablePadding = iconPadding - button.elevation = 2f - CustomUiUtils.setImageColor(button, iconColor) + button.setIconResource(tool.icon) button.layoutParams = GridLayout.LayoutParams().apply { width = 0 - height = buttonHeight + height = ViewGroup.LayoutParams.WRAP_CONTENT columnSpec = gridColumnSpec rowSpec = gridRowSpec setMargins(buttonMargins) } - button.gravity = Gravity.CENTER_VERTICAL - button.setPadding(buttonPadding, 0, buttonPadding, 0) - button.setBackgroundResource(R.drawable.rounded_rectangle) - button.backgroundTintList = ColorStateList.valueOf(buttonBackgroundColor) button.setOnClickListener { _ -> findNavController().navigate(tool.navAction) }