Skip to content

Commit

Permalink
Create logic for backContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
Semper-Viventem committed Feb 10, 2019
1 parent 3da95e6 commit a4228e5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
1 change: 1 addition & 0 deletions backdrop/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {

// Android X
implementation "androidx.appcompat:appcompat:1.1.0-alpha02"
implementation "com.google.android.material:material:1.1.0-alpha03"

// Kotlin
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import android.os.Bundle
import android.os.Parcelable
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.appbar.AppBarLayout


class BackdropBehavior : CoordinatorLayout.Behavior<View> {
Expand All @@ -30,12 +32,14 @@ class BackdropBehavior : CoordinatorLayout.Behavior<View> {
private const val ARG_DROP_STATE = "arg_drop_state"
}

private var utils = BackdropUtils()

private var toolbarId: Int? = null
private var backContainerId: Int? = null
private var backLayoutId: Int? = null

private var child: View? = null
private var toolbar: Toolbar? = null
private var backContainer: View? = null
private var backLayout: ViewGroup? = null
private var frontLayout: View? = null

private var closedIconId: Int = R.drawable.ic_menu
private var openedIconRes: Int = R.drawable.ic_close
Expand All @@ -61,25 +65,32 @@ class BackdropBehavior : CoordinatorLayout.Behavior<View> {
}

override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
if (toolbarId == null || backContainerId == null) return false
if (toolbarId == null || backLayoutId == null) return false

return when (dependency.id) {
toolbarId -> true
backContainerId -> true
backLayoutId -> true
else -> false
}
}

override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean {

this.child = child
this.frontLayout = child as? ViewGroup ?: throw IllegalArgumentException("BackLayout must extend a ViewGroup")
when (dependency.id) {
toolbarId -> toolbar = dependency as Toolbar
backContainerId -> backContainer = dependency
toolbarId -> toolbar = dependency as? Toolbar
?: throw IllegalArgumentException("toolbarId doesn't match Toolbar")

backLayoutId -> {
backLayout = dependency as? AppBarLayout
?: throw IllegalArgumentException("appBarLayoutId doesn't match AppBarLayout")
toolbar = utils.findToolbar(backLayout!!)
?: throw IllegalArgumentException("AppBarLayout mast contain a Toolbar!")
}
}

if (toolbar != null && backContainer != null) {
initViews(parent, child, toolbar!!, backContainer!!)
if (toolbar != null && frontLayout != null) {
initViews(parent, backLayout!!, toolbar!!, frontLayout!!)
}

return super.onDependentViewChanged(parent, child, dependency)
Expand All @@ -97,8 +108,20 @@ class BackdropBehavior : CoordinatorLayout.Behavior<View> {
this.toolbarId = toolbarId
}

/**
* Attach back layout to Backdrop.
* BackDropLayout must contain a [Toolbar]
*/
fun attachBackLayout(@IdRes appBarLayoutId: Int) {
this.backLayoutId = appBarLayoutId
}

/**
* @@deprecated — use [BackdropBehavior.attachBackLayout]
*/
@Deprecated("Use BackdropBehavior.attachBackLayout")
fun attachBackContainer(@IdRes backContainerId: Int) {
this.backContainerId = backContainerId
this.backLayoutId = backContainerId
}

fun addOnDropListener(listener: OnDropListener) {
Expand All @@ -113,8 +136,8 @@ class BackdropBehavior : CoordinatorLayout.Behavior<View> {
false
} else {
dropState = DropState.OPEN
if (child != null && toolbar != null && backContainer != null) {
drawDropState(child!!, toolbar!!, backContainer!!, withAnimation)
if (backLayout != null && toolbar != null && frontLayout != null) {
drawDropState(backLayout!!, toolbar!!, frontLayout!!, withAnimation)
} else {
throw IllegalArgumentException("Toolbar and backContainer must be initialized")
}
Expand All @@ -126,27 +149,27 @@ class BackdropBehavior : CoordinatorLayout.Behavior<View> {
false
} else {
dropState = DropState.CLOSE
if (child != null && toolbar != null && backContainer != null) {
drawDropState(child!!, toolbar!!, backContainer!!, withAnimation)
if (backLayout != null && toolbar != null && frontLayout != null) {
drawDropState(backLayout!!, toolbar!!, frontLayout!!, withAnimation)
} else {
throw IllegalArgumentException("Toolbar and backContainer must be initialized")
}
notifyListeners(false)
true
}

private fun initViews(parent: CoordinatorLayout, child: View, toolbar: Toolbar, backContainer: View) {
private fun initViews(parent: CoordinatorLayout, backDropLayout: View, toolbar: Toolbar, backContainer: View) {
backContainer.y = toolbar.y + toolbar.height
child.layoutParams.height = parent.height - toolbar.height
drawDropState(child, toolbar, backContainer, false)
backDropLayout.layoutParams.height = parent.height - toolbar.height
drawDropState(backDropLayout, toolbar, backContainer, false)

with(toolbar) {
setNavigationOnClickListener {
dropState = when (dropState) {
DropState.CLOSE -> DropState.OPEN
DropState.OPEN -> DropState.CLOSE
}
drawDropState(child, toolbar, backContainer)
drawDropState(backDropLayout, toolbar, backContainer)
notifyListeners(true)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.semper_viventem.backdrop

import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar

class BackdropUtils {

fun findToolbar(viewGroup: ViewGroup): Toolbar? {
for (chileId in 0..viewGroup.childCount) {
val childView = viewGroup.getChildAt(chileId)
if (childView is Toolbar) {
return childView
}
}

return null
}
}

0 comments on commit a4228e5

Please sign in to comment.