[fenix] For https://github.com/mozilla-mobile/fenix/issues/1430 - Adds a styled Snackbar
parent
b42eabc42d
commit
96d4932093
@ -0,0 +1,79 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.components
|
||||
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.google.android.material.snackbar.BaseTransientBottomBar
|
||||
import android.view.LayoutInflater
|
||||
import kotlinx.android.synthetic.main.fenix_snackbar.view.*
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.ext.increaseTapArea
|
||||
|
||||
|
||||
class FenixSnackbar private constructor(
|
||||
parent: ViewGroup,
|
||||
content: View,
|
||||
contentViewCallback: FenixSnackbarCallback
|
||||
) : BaseTransientBottomBar<FenixSnackbar>(parent, content, contentViewCallback) {
|
||||
|
||||
init {
|
||||
view.background = null
|
||||
content.snackbar_btn.increaseTapArea(16)
|
||||
}
|
||||
|
||||
fun setText(text: String) = this.apply {
|
||||
view.snackbar_text.text = text
|
||||
}
|
||||
|
||||
fun setAction(text: String, action: () -> Unit) = this.apply {
|
||||
view.snackbar_btn.apply {
|
||||
setText(text)
|
||||
visibility = View.VISIBLE
|
||||
setOnClickListener {
|
||||
action.invoke()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun make(parent: ViewGroup, duration: Int): FenixSnackbar {
|
||||
val inflater = LayoutInflater.from(parent.context)
|
||||
val content = inflater.inflate(R.layout.fenix_snackbar, parent, false)
|
||||
|
||||
// create snackbar with custom view
|
||||
val callback = FenixSnackbarCallback(content)
|
||||
val fenixSnackbar = FenixSnackbar(parent, content, callback)
|
||||
|
||||
// set snackbar duration
|
||||
fenixSnackbar.duration = duration
|
||||
return fenixSnackbar
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class FenixSnackbarCallback (
|
||||
private val content: View
|
||||
) : com.google.android.material.snackbar.ContentViewCallback {
|
||||
|
||||
override fun animateContentIn(delay: Int, duration: Int) {
|
||||
content.scaleY = 0f
|
||||
content.animate().apply {
|
||||
scaleY(1f)
|
||||
setDuration(duration.toLong())
|
||||
startDelay = delay.toLong()
|
||||
}
|
||||
}
|
||||
|
||||
override fun animateContentOut(delay: Int, duration: Int) {
|
||||
content.scaleY = 1f
|
||||
content.animate().apply {
|
||||
scaleY(0f)
|
||||
setDuration(duration.toLong())
|
||||
startDelay = delay.toLong()
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="@color/fenix_snackbar_background" />
|
||||
<corners android:radius="8dp" />
|
||||
</shape>
|
@ -0,0 +1,64 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_width="match_parent"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:orientation="horizontal"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingBottom="8dp"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:background="@drawable/fenix_snackbar_background"
|
||||
android:layout_margin="8dp"
|
||||
android:elevation="4dp"
|
||||
android:minHeight="48dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/snackbar_text"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:ellipsize="end"
|
||||
android:maxLines="3"
|
||||
android:textColor="@color/photonWhite"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
android:typeface="sans"
|
||||
android:letterSpacing="0.05"
|
||||
android:lineSpacingMultiplier="1.25"
|
||||
tools:text="This is a custom Snackbar text"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@id/snackbar_btn"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/snackbar_btn"
|
||||
style="@style/Widget.AppCompat.Button.Borderless"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="@color/photonWhite"
|
||||
android:textAlignment="textEnd"
|
||||
tools:text="Action text"
|
||||
android:padding="0dp"
|
||||
android:minHeight="0dp"
|
||||
android:minWidth="0dp"
|
||||
android:textSize="14sp"
|
||||
android:textAllCaps="true"
|
||||
android:textStyle="bold"
|
||||
android:typeface="sans"
|
||||
android:letterSpacing="0.05"
|
||||
android:lineSpacingMultiplier="1.25"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintStart_toEndOf="@id/snackbar_text"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</FrameLayout>
|
Loading…
Reference in New Issue