Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
j-roskopf committed Feb 28, 2019
1 parent be81967 commit ee0faab
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 64 deletions.

This file was deleted.

46 changes: 42 additions & 4 deletions bottomnavbar/src/main/java/joetr/com/bottomnavbar/BottomNavBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,38 @@ import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.view.children

/**
* Container for [BottomNavBarIcon]
*/
class BottomNavBar @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

init {
//make sure all children are attached
post {
children.forEach { child ->
if(child is BottomNavBarIcon) {
child.container().setOnClickListener {
handleInput(child, child.parent as BottomNavBar)
handleClick(child, child.parent as BottomNavBar)

//perform custom click if user registered custom click
child.performCustomClick()
}
}
}
}
}

private fun handleInput(bottomNavBarIcon: BottomNavBarIcon, parent: BottomNavBar) {
/**
* Handles click for icon
*
* Responsible for animating the clicked one to be selected, as well as deselecting other tabs.
*
* @param bottomNavBarIcon - Icon that was clicked on
* @param parent - Parent [BottomNavBar]
*/
private fun handleClick(bottomNavBarIcon: BottomNavBarIcon, parent: BottomNavBar) {
val transition = ChangeBounds()
transition.interpolator = OvershootInterpolator(2.0f)
transition.duration = 400
Expand All @@ -38,17 +52,41 @@ class BottomNavBar @JvmOverloads constructor(

val set = ConstraintSet()

selectEnabledIcon(set, bottomNavBarIcon)

disableOtherIcons(parent, set, bottomNavBarIcon)
}

/**
* Selects icon
*
* @param set - constraint set
* @param bottomNavBarIcon - icon
*/
private fun selectEnabledIcon(set: ConstraintSet, bottomNavBarIcon: BottomNavBarIcon) {
set.clone(bottomNavBarIcon.container())
bottomNavBarIcon.setVisibilityOfText(set, View.VISIBLE)
bottomNavBarIcon.updateBackground()
bottomNavBarIcon.setBackgroundTint()
set.applyTo(bottomNavBarIcon.container())
}

/**
* Deselects other icons
*
* @param parent - [BottomNavBar] container
* @param set - ConstraintSet
*/
private fun disableOtherIcons(
parent: BottomNavBar,
set: ConstraintSet,
bottomNavBarIcon: BottomNavBarIcon
) {
parent.children.forEach { view ->
if(view.id != bottomNavBarIcon.id && view is BottomNavBarIcon) {
set.clone(view.container())

view.setVisibilityOfText(set, View.GONE)
DrawableCompat.setTint(view.navBackground(), ContextCompat.getColor(context, android.R.color.transparent))
DrawableCompat.setTint(view.container().background, ContextCompat.getColor(context, android.R.color.transparent))

set.applyTo(view.container())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package joetr.com.bottomnavbar

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
Expand All @@ -11,6 +10,9 @@ import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import kotlinx.android.synthetic.main.nav_bar_icon.view.*

/**
* Icon to be contained in [BottomNavBar]
*/
class BottomNavBarIcon @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
Expand Down Expand Up @@ -46,33 +48,39 @@ class BottomNavBarIcon @JvmOverloads constructor(
}
}

private fun textViewId(): Int {
return bottomNavBarText.id
}


fun navBackground(): Drawable {
return bottomNavBarContainer.background
}

/**
* Set visibility of only TextView
*
* @param set - ConstraintSet
* @param visibility - Visibility Int
*/
fun setVisibilityOfText(set: ConstraintSet, visibility: Int) {
set.setVisibility(textViewId(), visibility)
}

fun container(): ConstraintLayout {
return bottomNavBarContainer
set.setVisibility(bottomNavBarText.id, visibility)
}

fun updateBackground() {
DrawableCompat.setTint(navBackground(), foregroundTint)
/**
* Set background tint
*/
fun setBackgroundTint() {
DrawableCompat.setTint(container().background, foregroundTint)
}

/**
* Register click listener.
*
* This is the mechanism for having a developer get notified of a click event
*/
fun registerClickListener(clickListener: View.OnClickListener) {
this.clickListener = clickListener
}

/**
* If developer registered a click listener, call it.
*/
fun performCustomClick() {
clickListener?.onClick(container())
}

fun container() = bottomNavBarContainer!!
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package joetr.com.bottomnavbar

import org.junit.Assert.assertEquals
import org.junit.Test

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see [Testing documentation](http://d.android.com/tools/testing)
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, (2 + 2).toLong())
}
}

0 comments on commit ee0faab

Please sign in to comment.