Skip to content

Commit

Permalink
Merge pull request #102 from yourssu/feature/mint/bottombar
Browse files Browse the repository at this point in the history
[YDS-BottomBar] bottomBar 사용성 개선 작업
  • Loading branch information
islandparadise14 authored May 26, 2022
2 parents d6664c4 + d3dea74 commit b3a071d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.core.content.withStyledAttributes
import androidx.databinding.BindingAdapter
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableInt
import com.yourssu.design.R
import com.yourssu.design.databinding.ItemBottomTabBinding
import com.yourssu.design.databinding.LayoutBottomBarBinding
import com.yourssu.design.system.rule.Vibration
import com.yourssu.design.system.rule.vibe
import com.yourssu.design.undercarriage.animation.startAnim
import java.lang.Exception
import kotlin.collections.List

class BottomBar @JvmOverloads constructor(
Expand All @@ -43,7 +43,6 @@ class BottomBar @JvmOverloads constructor(
LayoutBottomBarBinding.inflate(LayoutInflater.from(context), this, true)
}

private var isCanChangeTab = true
private var tabList = listOf<BottomTabInfo>()
private var bindingMap: MutableMap<Int, ItemBottomTabBinding> = mutableMapOf()
var bottomTabType = ObservableInt(0)
Expand All @@ -59,18 +58,33 @@ class BottomBar @JvmOverloads constructor(
private fun initialize(attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) {

context.withStyledAttributes(attrs, R.styleable.BottomBar, defStyleAttr, defStyleRes) {
bottomTabType.set(getInteger(R.styleable.BottomBar_selectedIndex, 0))
isCanChangeTab = getBoolean(R.styleable.BottomBar_canChangeIndex, true)
isImpactFeedbackEnabled = getBoolean(R.styleable.BottomBar_isImpactFeedbackEnabled, true)
}
}

fun setTabList(list: List<BottomTabInfo>) {
tabList = list
bindingMap = mutableMapOf()

updateTabStatus()
}

fun setSelectedTab(primaryName: String) {
val selectedIndex = try {
tabList.map { it.primaryName }
.indexOf(primaryName)
} catch (e: Exception) {
INVALID_SELECTED_INDEX
}

if (selectedIndex != INVALID_SELECTED_INDEX) {
this.bottomTabType.set(selectedIndex)
tabClickListener?.tabChanged(primaryName)

updateTabStatus()
}
}

private fun updateTabStatus() {
binding.tabArea.removeAllViews()
tabList.forEachIndexed { index, bottomTabInfo ->
Expand All @@ -90,8 +104,6 @@ class BottomBar @JvmOverloads constructor(

item.root.setOnClickListener {
tabClicked(index)
springAnimation(index)
touchVibrationFeedback()
}
item.root.setOnLongClickListener {
longTabClicked(index)
Expand All @@ -107,12 +119,17 @@ class BottomBar @JvmOverloads constructor(

tabClickListener?.tabClicked(primaryName)

if (isCanChangeTab) {
this.bottomTabType.set(index)
tabClickListener?.tabChanged(primaryName)
}
springAnimation(index)
touchVibrationFeedback(Vibration.INTERACT)
}

updateTabStatus()
private fun longTabClicked(index: Int) {
val primaryName = tabList[index].primaryName

springAnimation(index)
touchVibrationFeedback(Vibration.SUCCESS)

tabClickListener?.tabLongClicked(primaryName)
}

fun springAnimation(index: Int) {
Expand All @@ -121,19 +138,15 @@ class BottomBar @JvmOverloads constructor(
}
}

private fun touchVibrationFeedback() {
private fun touchVibrationFeedback(vibration: Vibration) {
if (isImpactFeedbackEnabled) {
context.vibe(Vibration.INTERACT)
context.vibe(vibration)
}
}

private fun longTabClicked(index: Int) {
val primaryName = tabList[index].primaryName

tabClickListener?.tabLongClicked(primaryName)
}

companion object {
private const val INVALID_SELECTED_INDEX = -1

@JvmStatic
@BindingAdapter("setTabClickListener")
fun setTabClickListener(bottomBar: BottomBar, tabClickListener: TabClickListener) {
Expand All @@ -146,6 +159,12 @@ class BottomBar @JvmOverloads constructor(
bottomBar.setTabList(list)
}

@JvmStatic
@BindingAdapter("selectedTabPrimaryName")
fun setSelectedTab(bottomBar: BottomBar, primaryName: String) {
bottomBar.setSelectedTab(primaryName)
}

@JvmStatic
@BindingAdapter("isImpactFeedbackEnabled")
fun setIsImpactFeedbackEnabled(bottomBar: BottomBar, boolean: Boolean) {
Expand Down
4 changes: 2 additions & 2 deletions DesignSystem/src/main/res/values/attrs_bottom_bar.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="BottomBar">
<attr name="selectedIndex" format="integer" />
<attr name="canChangeIndex" format="boolean" />
<attr name="selectedIndex" format="integer" /> <!--TODO remove-->
<attr name="canChangeIndex" format="boolean" /> <!--TODO remove-->
<attr name="isImpactFeedbackEnabled" format="boolean" />
</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yourssu.storybook.component

import android.app.Application
import androidx.databinding.ObservableField
import com.yourssu.design.system.component.BottomBar
import com.yourssu.storybook.BaseViewModel
import com.yourssu.storybook.R
Expand All @@ -14,6 +15,16 @@ class BottomBarViewModel(application: Application): BaseViewModel(application) {
BottomBar.BottomTabInfo(BottomType.SETTING.name, R.drawable.ic_person_line, R.drawable.ic_person_filled)
)

val selectedBottomBarTapName = ObservableField<String>(BottomType.HOME.name)

val tabClickListener = object : BottomBar.TabClickListener {
override fun tabClicked(primaryName: String) {
selectedBottomBarTapName.set(primaryName)
}
override fun tabChanged(primaryName: String) {}
override fun tabLongClicked(primaryName: String) {}
}

enum class BottomType {
HOME,
BOARD,
Expand Down
4 changes: 3 additions & 1 deletion app/storybook/src/main/res/layout/fragment_bottom_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:isImpactFeedbackEnabled="@{true}"
app:setTabInfoList="@{viewModel.bottomType}"/>
app:setTabInfoList="@{viewModel.bottomType}"
app:selectedTabPrimaryName="@{viewModel.selectedBottomBarTapName}"
app:setTabClickListener="@{viewModel.tabClickListener}"/>
</FrameLayout>
</layout>

0 comments on commit b3a071d

Please sign in to comment.