From 960cc86f6bbcce62e127f4153e33ac6e30c15b96 Mon Sep 17 00:00:00 2001 From: melianor Date: Mon, 4 Nov 2019 17:46:25 +0300 Subject: [PATCH] remove traditional callback interface, refactor code, update readme --- README.md | 113 ++++++++++-------- .../ibrahimsn/nicebottombar/MainActivity.kt | 19 ++- app/src/main/res/layout/activity_main.xml | 13 +- .../java/me/ibrahimsn/lib/BottomBarParser.kt | 47 ++++---- .../main/java/me/ibrahimsn/lib/Constants.kt | 15 +++ .../java/me/ibrahimsn/lib/NiceBottomBar.kt | 58 ++++----- lib/src/main/res/values/strings.xml | 2 + 7 files changed, 158 insertions(+), 109 deletions(-) create mode 100644 lib/src/main/java/me/ibrahimsn/lib/Constants.kt diff --git a/README.md b/README.md index c1c7199..77935d0 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,17 @@ # NiceBottomBar + A lightweight Android material bottom navigation bar library [![](https://jitpack.io/v/ibrahimsn98/NiceBottomBar.svg)](https://jitpack.io/#ibrahimsn98/NiceBottomBar) -[![API](https://img.shields.io/badge/API-22%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=22) +[![API](https://img.shields.io/badge/API-16%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=16) ## GIF + ## Usage + - Create menu.xml under your res/menu/ folder ```xml @@ -36,72 +39,81 @@ A lightweight Android material bottom navigation bar library ``` + + - Add view into your layout file ```xml - + ``` -- Use NiceBottomBar functions in your activity -```kotlin - bottomBar.setActiveItem(1) - bottomBar.setBadge(2) - bottomBar.removeBadge(2) - bottomBar.setBottomBarCallback(object: NiceBottomBar.BottomBarCallback { - - override fun onItemLongClick(pos: Int) { - } - - override fun onItemSelect(pos: Int) { +- Use NiceBottomBar functions in your activity +```kotlin +bottomBar.setActiveItem(1) +bottomBar.setBadge(2) +bottomBar.removeBadge(2) - } +bottomBar.onItemSelected = { + status.text = "Item $it selected" +} - override fun onItemReselect(pos: Int) { +bottomBar.onItemReselected = { + status.text = "Item $it re-selected" +} - } - }) +bottomBar.onItemLongClick = { + status.text = "Item $it long click" +} ``` + ## Customization + ```xml - + ``` + + ## Setup + ```gradle - allprojects { - repositories { - ... - maven { url 'https://jitpack.io' } - } - } - - dependencies { - implementation 'com.github.ibrahimsn98:NiceBottomBar:2.1' +allprojects { + repositories { + ... + maven { url 'https://jitpack.io' } } +} + +dependencies { + implementation 'com.github.ibrahimsn98:NiceBottomBar:2.2' +} ``` + + ## License + ``` MIT License @@ -125,3 +137,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ``` + + +> Follow me on Twitter [@ibrahimsn98](https://twitter.com/ibrahimsn98) \ No newline at end of file diff --git a/app/src/main/java/me/ibrahimsn/nicebottombar/MainActivity.kt b/app/src/main/java/me/ibrahimsn/nicebottombar/MainActivity.kt index a1533ce..991d02d 100644 --- a/app/src/main/java/me/ibrahimsn/nicebottombar/MainActivity.kt +++ b/app/src/main/java/me/ibrahimsn/nicebottombar/MainActivity.kt @@ -16,17 +16,16 @@ class MainActivity : AppCompatActivity() { bottomBar.setBadge(2) bottomBar.removeBadge(2) - bottomBar.setBottomBarCallback(object: NiceBottomBar.BottomBarCallback { - override fun onItemLongClick(pos: Int) { - bottomBar.removeBadge(pos) - } - override fun onItemSelect(pos: Int) { + bottomBar.onItemSelected = { + status.text = "Item $it selected" + } - } - override fun onItemReselect(pos: Int) { - bottomBar.setBadge(pos) - } + bottomBar.onItemReselected = { + status.text = "Item $it re-selected" + } - }) + bottomBar.onItemLongClick = { + status.text = "Item $it long click" + } } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index afd7fe0..e74c5f6 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -2,10 +2,21 @@ + + = arrayListOf() - - fun parse(): ArrayList { - try { - var eventType: Int? - do { - eventType = parser.next() - if (eventType == XmlResourceParser.START_TAG && "item" == parser.name) { - items.add(getTabConfig(parser)) - } - } while (eventType != XmlResourceParser.END_DOCUMENT) - } catch (e: XmlPullParserException) { - e.printStackTrace() - throw Exception() - } - return items + + fun parse(): List { + val items: MutableList = mutableListOf() + var eventType: Int? + + do { + eventType = parser.next() + + if (eventType == XmlResourceParser.START_TAG && parser.name == Constants.ITEM_TAG) + items.add(getTabConfig(parser)) + + } while (eventType != XmlResourceParser.END_DOCUMENT) + + return items.toList() } private fun getTabConfig(parser: XmlResourceParser): BottomBarItem { val attributeCount = parser.attributeCount var itemText: String? = null var itemDrawable: Drawable? = null - for (i in 0 until attributeCount) { + + for (i in 0 until attributeCount) when (parser.getAttributeName(i)) { - "title" -> itemText = context.getString(parser.getAttributeResourceValue(i, 0)) - "icon" -> itemDrawable = ContextCompat.getDrawable(context, parser.getAttributeResourceValue(i, 0))!! + Constants.ICON_ATTRIBUTE -> itemDrawable = + ContextCompat.getDrawable(context, parser.getAttributeResourceValue(i, 0)) + Constants.TITLE_ATTRIBUTE -> { + itemText = try { + context.getString(parser.getAttributeResourceValue(i, 0)) + } catch (e: Exception) { + parser.getAttributeValue(i) + } + } } - } + return BottomBarItem(itemText!!, itemDrawable!!) } } \ No newline at end of file diff --git a/lib/src/main/java/me/ibrahimsn/lib/Constants.kt b/lib/src/main/java/me/ibrahimsn/lib/Constants.kt new file mode 100644 index 0000000..49e168d --- /dev/null +++ b/lib/src/main/java/me/ibrahimsn/lib/Constants.kt @@ -0,0 +1,15 @@ +package me.ibrahimsn.lib + +/** + * This is a static class to hold constants + */ +object Constants { + const val ITEM_TAG = "item" + const val ICON_ATTRIBUTE = "icon" + const val TITLE_ATTRIBUTE = "title" + const val WHITE_COLOR_HEX = "#FFFFFF" + + const val DEFAULT_INDICATOR_COLOR = "#426dfe" + const val DEFAULT_TEXT_COLOR = "#444444" + const val DEFAULT_TEXT_COLOR_ACTIVE = "#426dfe" +} diff --git a/lib/src/main/java/me/ibrahimsn/lib/NiceBottomBar.kt b/lib/src/main/java/me/ibrahimsn/lib/NiceBottomBar.kt index a7a3c32..5128da6 100644 --- a/lib/src/main/java/me/ibrahimsn/lib/NiceBottomBar.kt +++ b/lib/src/main/java/me/ibrahimsn/lib/NiceBottomBar.kt @@ -17,35 +17,44 @@ import android.animation.ArgbEvaluator import android.graphics.* import androidx.core.content.res.ResourcesCompat import androidx.core.graphics.drawable.DrawableCompat +import me.ibrahimsn.lib.Constants.DEFAULT_INDICATOR_COLOR +import me.ibrahimsn.lib.Constants.DEFAULT_TEXT_COLOR +import me.ibrahimsn.lib.Constants.DEFAULT_TEXT_COLOR_ACTIVE +import me.ibrahimsn.lib.Constants.WHITE_COLOR_HEX import kotlin.math.abs class NiceBottomBar : View { // Default attribute values - private var barBackgroundColor = Color.parseColor("#ffffff") - private var barIndicatorColor = Color.parseColor("#426dfe") + private var barBackgroundColor = Color.parseColor(WHITE_COLOR_HEX) + private var barIndicatorColor = Color.parseColor(DEFAULT_INDICATOR_COLOR) private var barIndicatorInterpolator = 4 private var barIndicatorWidth = d2p(50f) private var barIndicatorEnabled = true private var barIndicatorGravity = 1 private var itemIconSize = d2p(18f) private var itemIconMargin = d2p(3f) - private var itemTextColor = Color.parseColor("#444444") - private var itemTextColorActive = Color.parseColor("#426dfe") + private var itemTextColor = Color.parseColor(DEFAULT_TEXT_COLOR) + private var itemTextColorActive = Color.parseColor(DEFAULT_TEXT_COLOR_ACTIVE) private var itemTextSize = d2p(11.0f) private var itemBadgeColor = itemTextColorActive private var itemFontFamily = 0 private var activeItem = 0 + /** + * Dynamic variables + */ + private var currentActiveItemColor = itemTextColor + private var indicatorLocation = 0f + // Represent long press time, when press time > longPressTime call the function callback.onItemLongClick private var longPressTime = 500 private val titleSideMargins = d2p(12f) private var items = listOf() - private var callback: BottomBarCallback? = null - - private var currentActiveItemColor = itemTextColor - private var indicatorLocation = 0f + var onItemSelected: (Int) -> Unit = {} + var onItemReselected: (Int) -> Unit = {} + var onItemLongClick: (Int) -> Unit = {} private val paintIndicator = Paint().apply { isAntiAlias = true @@ -110,10 +119,18 @@ class NiceBottomBar : View { val itemWidth = width / items.size for (item in items) { - // Prevent text overflow by shortening the item title - while (paintText.measureText(item.title) > (itemWidth - titleSideMargins)) + var shorted = false + while (paintText.measureText(item.title) > (itemWidth - titleSideMargins)) { + item.title = item.title.dropLast(1) + shorted = true + } + + // Add ellipsis character to item text if it is shorted + if (shorted) { item.title = item.title.dropLast(1) + item.title += context.getString(R.string.ellipsis) + } item.rect = RectF(lastX, 0f, itemWidth + lastX, height.toFloat()) lastX += itemWidth @@ -165,20 +182,19 @@ class NiceBottomBar : View { if (item.rect.contains(event.x, event.y)) if (i != this.activeItem) { setActiveItem(i) - callback?.onItemSelect(i) + onItemSelected(i) } else - callback?.onItemReselect(i) + onItemReselected(i) if (event.action == MotionEvent.ACTION_MOVE || event.action == MotionEvent.ACTION_UP) if (abs(event.downTime-event.eventTime)> longPressTime) for ((i, item) in items.withIndex()) if (item.rect.contains(event.x, event.y)) - callback?.onItemLongClick(i) + onItemLongClick(i) return true } - // Draw item badge private fun drawBadge(canvas: Canvas, item: BottomBarItem) { paintBadge.style = Paint.Style.FILL @@ -258,18 +274,4 @@ class NiceBottomBar : View { private fun d2p(dp: Float): Float { return resources.displayMetrics.densityDpi.toFloat() / 160.toFloat() * dp } - - fun setBottomBarCallback(callback: BottomBarCallback) { - this.callback = callback - } - - fun setLongPressTime(time: Int){ - longPressTime = time - } - - interface BottomBarCallback { - fun onItemSelect(pos: Int) - fun onItemReselect(pos: Int) - fun onItemLongClick(pos:Int) - } } \ No newline at end of file diff --git a/lib/src/main/res/values/strings.xml b/lib/src/main/res/values/strings.xml index d8b1ac9..c670e59 100644 --- a/lib/src/main/res/values/strings.xml +++ b/lib/src/main/res/values/strings.xml @@ -1,3 +1,5 @@ lib + +