1
0
Fork 0

For #1430 - Adds a styled Snackbar

master
Jeff Boek 2019-04-05 13:33:36 -07:00
parent 395d6f20d5
commit 371fbf7164
4 changed files with 154 additions and 0 deletions

View File

@ -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()
}
}
}

View File

@ -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>

View File

@ -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>

View File

@ -132,4 +132,6 @@
<color name="quick_action_pull_tab">#2915141A</color>
<color name="quick_action_background_normal_theme">@color/toolbar_normal_theme</color>
<color name="quick_action_background_private_theme">@color/toolbar_dark_mode</color>
<color name="fenix_snackbar_background">#232749</color>
</resources>