Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix navigationbar size #46

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions flashbar/src/main/java/com/andrognito/flashbar/Flashbar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import com.andrognito.flashbar.Flashbar.Gravity.TOP
import com.andrognito.flashbar.anim.FlashAnim
import com.andrognito.flashbar.anim.FlashAnimBarBuilder
import com.andrognito.flashbar.anim.FlashAnimIconBuilder
import com.andrognito.flashbar.util.getContentView
import com.andrognito.flashbar.util.getRootView

private const val DEFAULT_SHADOW_STRENGTH = 4
private const val DEFAUT_ICON_SCALE = 1.0f
Expand All @@ -28,7 +30,13 @@ class Flashbar private constructor(private var builder: Builder) {
* Shows a flashbar
*/
fun show() {
flashbarContainerView.show(builder.activity)
val anchorViewGroup = if (builder.gravity == TOP) {
builder.activity.getRootView()
} else {
builder.activity.getContentView()
} ?: return

flashbarContainerView.show(anchorViewGroup)
}

/**
Expand All @@ -52,7 +60,6 @@ class Flashbar private constructor(private var builder: Builder) {

private fun construct() {
flashbarContainerView = FlashbarContainerView(builder.activity)
flashbarContainerView.adjustOrientation(builder.activity)
flashbarContainerView.addParent(this)

flashbarView = FlashbarView(builder.activity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.andrognito.flashbar.util.NavigationBarPosition.*
import com.andrognito.flashbar.util.afterMeasured
import com.andrognito.flashbar.util.getNavigationBarPosition
import com.andrognito.flashbar.util.getNavigationBarSizeInPx
import com.andrognito.flashbar.util.getRootView

/**
* Container withView matching the height and width of the parent to hold a FlashbarView.
Expand Down Expand Up @@ -56,6 +55,10 @@ internal class FlashbarContainerView(context: Context)
private var showOverlay: Boolean = false
private var overlayBlockable: Boolean = false

init {
layoutParams = RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
}

override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
ACTION_DOWN -> {
Expand Down Expand Up @@ -135,16 +138,14 @@ internal class FlashbarContainerView(context: Context)
layoutParams = flashbarContainerViewLp
}


internal fun show(activity: Activity) {
internal fun show(anchorViewGroup: ViewGroup) {
if (isBarShowing || isBarShown) return

val activityRootView = activity.getRootView() ?: return

// Only add the withView to the parent once
if (this.parent == null) activityRootView.addView(this)
if (this.parent == null) anchorViewGroup.addView(this)

activityRootView.afterMeasured {
anchorViewGroup.afterMeasured {
val enterAnim = enterAnimBuilder.withView(flashbarView).build()
enterAnim.start(object : FlashAnim.InternalAnimListener {
override fun onStart() {
Expand Down
19 changes: 0 additions & 19 deletions flashbar/src/main/java/com/andrognito/flashbar/FlashbarView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,10 @@ import kotlinx.android.synthetic.main.flash_bar_view.view.*
internal class FlashbarView(context: Context) : LinearLayout(context) {

private val TOP_COMPENSATION_MARGIN = resources.getDimension(R.dimen.fb_top_compensation_margin).toInt()
private val BOTTOM_COMPENSATION_MARGIN = resources.getDimension(R.dimen.fb_bottom_compensation_margin).toInt()

private lateinit var parentFlashbarContainer: FlashbarContainerView
private lateinit var gravity: Gravity

private var isMarginCompensationApplied: Boolean = false

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)

if (!isMarginCompensationApplied) {
isMarginCompensationApplied = true

val params = layoutParams as ViewGroup.MarginLayoutParams
when (gravity) {
TOP -> params.topMargin = -TOP_COMPENSATION_MARGIN
BOTTOM -> params.bottomMargin = -BOTTOM_COMPENSATION_MARGIN
}
requestLayout()
}
}

internal fun init(
gravity: Gravity,
castShadow: Boolean,
Expand Down Expand Up @@ -101,7 +83,6 @@ internal class FlashbarView(context: Context) : LinearLayout(context) {
flashbarViewLp.addRule(ALIGN_PARENT_TOP)
}
BOTTOM -> {
flashbarViewContentLp.bottomMargin = BOTTOM_COMPENSATION_MARGIN
flashbarViewLp.addRule(ALIGN_PARENT_BOTTOM)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ internal fun Activity?.getRootView(): ViewGroup? {
if (this == null || window == null || window.decorView == null) {
return null
}
return window.decorView as ViewGroup
return window.decorView as? ViewGroup
}

internal fun Activity?.getContentView(): ViewGroup? {
if (this == null || window == null || window.decorView == null) {
return null
}
return window.decorView.findViewById(android.R.id.content) as? ViewGroup
}

internal fun Context.convertDpToPx(dp: Int): Int {
Expand Down