2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-11 13:11:01 +00:00

No longer require a viewgroup to use FenixSnackbar

This commit is contained in:
Jeff Boek 2019-04-09 16:10:27 -07:00
parent b6abccfb91
commit cd75b28b8f
4 changed files with 40 additions and 6 deletions

View File

@ -339,10 +339,8 @@ class BrowserFragment : Fragment(), BackHandler, CoroutineScope {
val guid = requireComponents.core.bookmarksStorage
.addItem(BookmarkRoot.Mobile.id, session!!.url, session.title, null)
launch(Main) {
context?.asActivity()?.window?.decorView
?.findViewById<View>(android.R.id.content)?.let { view ->
FenixSnackbar.make(
view as ViewGroup,
view!!,
Snackbar.LENGTH_LONG
)
.setAction(getString(R.string.edit_bookmark_snackbar_action)) {
@ -355,7 +353,7 @@ class BrowserFragment : Fragment(), BackHandler, CoroutineScope {
)
}
.setText(getString(R.string.bookmark_created_snackbar))
}!!.show()
.show()
}
}
}

View File

@ -9,7 +9,10 @@ import android.view.View
import android.view.ViewGroup
import com.google.android.material.snackbar.BaseTransientBottomBar
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.widget.TextViewCompat
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.fenix_snackbar.view.*
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.increaseTapArea
@ -48,13 +51,20 @@ class FenixSnackbar private constructor(
}
}
companion object {
private const val minTextSize = 12
private const val maxTextSize = 18
private const val actionButtonIncreaseDps = 16
private const val stepGranularity = 1
fun make(parent: ViewGroup, duration: Int): FenixSnackbar {
fun make(view: View, duration: Int): FenixSnackbar {
val parent = findSuitableParent(view) ?: run {
throw IllegalArgumentException(
"No suitable parent found from the given view. Please provide a valid view."
)
}
val inflater = LayoutInflater.from(parent.context)
val content = inflater.inflate(R.layout.fenix_snackbar, parent, false)
@ -63,6 +73,32 @@ class FenixSnackbar private constructor(
it.duration = duration
}
}
private fun findSuitableParent(_view: View?): ViewGroup? {
var view = _view
var fallback: ViewGroup? = null
do {
if (view is CoordinatorLayout) {
return view
}
if (view is FrameLayout) {
if (view.id == 16908290) {
return view
}
fallback = view
}
if (view != null) {
val parent = view.parent
view = if (parent is View) parent else null
}
} while (view != null)
return fallback
}
}
}

View File

@ -13,7 +13,6 @@ import org.mozilla.fenix.ext.asActivity
import org.mozilla.fenix.ext.components
class ItsNotBrokenSnack(val context: Context) {
fun showSnackbar(issueNumber: String) {
val rootView =
context.asActivity()?.window?.decorView?.findViewById<View>(android.R.id.content) as ViewGroup

View File

@ -33,6 +33,7 @@
android:letterSpacing="0.05"
android:gravity="center"
tools:text="This is a custom Snackbar text"
android:textAlignment="textStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/snackbar_btn"
app:layout_constraintTop_toTopOf="parent"