feat: UI basics (#6)

This commit is contained in:
Piotr Dec 2024-08-17 00:14:53 +02:00
parent c7fc9e9c09
commit eddcf47205
Signed by: stawros
GPG key ID: F89F27AD8F881A91
5 changed files with 141 additions and 0 deletions

View file

@ -59,4 +59,6 @@ class UI(b: ActivityMainBinding) {
val searchResultsView = b.searchResults
val searchPlaceView = b.searchPlaces
val queryEditText = b.query
val reconnect = b.reconnect
}

View file

@ -0,0 +1,119 @@
package eu.ztsh.garmin.view
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.annotation.StyleRes
import androidx.annotation.UiThread
import androidx.constraintlayout.widget.ConstraintLayout
import com.mapbox.navigation.ui.components.R
import eu.ztsh.garmin.R.styleable.*
import com.mapbox.navigation.ui.components.databinding.ExtendableButtonLayoutBinding
import com.mapbox.navigation.ui.utils.internal.ExtendableButtonHelper
/**
* Mapbox extendable generic.
*/
@UiThread
class ExtendableButton : ConstraintLayout {
private val binding = ExtendableButtonLayoutBinding.inflate(LayoutInflater.from(context), this)
private val helper = ExtendableButtonHelper(
binding.buttonText,
context.resources.getDimensionPixelSize(R.dimen.mapbox_button_size),
context.resources.getDimension(R.dimen.mapbox_recenterButton_minExtendedWidth),
)
private lateinit var expandedText: String
/**
*
* @param context Context
* @constructor
*/
constructor(context: Context) : super(context)
/**
*
* @param context Context
* @param attrs AttributeSet?
* @constructor
*/
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
initAttributes(attrs)
}
/**
*
* @param context Context
* @param attrs AttributeSet?
* @param defStyleAttr Int
* @constructor
*/
constructor(
context: Context,
attrs: AttributeSet?,
defStyleAttr: Int
) : super(context, attrs, defStyleAttr) {
initAttributes(attrs)
}
/**
* Allows you to change the style of [ExtendableButton].
* @param style Int
*/
fun updateStyle(@StyleRes style: Int) {
val typedArray = context.obtainStyledAttributes(
style,
ExtendableButton
)
applyAttributes(typedArray)
typedArray.recycle()
}
/**
* Invoke the function to show optional text associated with the view.
* @param duration for the view to be in the extended mode before it starts to shrink.
* @param text for the view to show in the extended mode.
*/
@JvmOverloads
fun showTextAndExtend(
duration: Long,
text: String = expandedText,
) {
if (!helper.isAnimationRunning) {
helper.showTextAndExtend(text, duration)
}
}
private fun initAttributes(attrs: AttributeSet?) {
val typedArray = context.obtainStyledAttributes(
attrs,
ExtendableButton,
0,
0
)
applyAttributes(typedArray)
typedArray.recycle()
}
private fun applyAttributes(typedArray: TypedArray) {
typedArray.getDrawable(ExtendableButton_btn_icon)
.also { binding.buttonIcon.setImageDrawable(it) }
typedArray.getDrawable(
R.styleable.MapboxRecenterButton_recenterButtonBackground,
)?.let { background ->
binding.buttonIcon.background = background
binding.buttonText.background = background
}
typedArray.getColorStateList(
R.styleable.MapboxRecenterButton_recenterButtonTextColor,
)?.let { binding.buttonText.setTextColor(it) }
typedArray.apply {
expandedText = getString(ExtendableButton_expanded_text) ?: "DUNNO"
}
}
}

View file

@ -127,4 +127,15 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/routeOverview" />
<eu.ztsh.garmin.view.ExtendableButton
android:id="@+id/reconnect"
app:btn_icon="@android:drawable/ic_menu_rotate"
app:expanded_text="@string/reconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/recenter" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ExtendableButton">
<attr name="expanded_text" format="string"/>
<attr name="btn_icon" format="reference"/>
</declare-styleable>
</resources>

View file

@ -7,4 +7,6 @@
<string name="place_autocomplete_selection_error">Error happened during request</string>
<string name="not_implemented_yet">Not implemented yet</string>
<string name="reconnect">Reconnect</string>
</resources>