Skip to content

Commit

Permalink
Extract tool button
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Nov 13, 2023
1 parent e475298 commit 6839de7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
60 changes: 60 additions & 0 deletions app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolButton.kt
Original file line number Diff line number Diff line change
@@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -228,38 +228,21 @@ class ToolsFragment : BoundFragment<FragmentToolsBinding>() {
}

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)
}
Expand Down

0 comments on commit 6839de7

Please sign in to comment.