Skip to content

Commit

Permalink
Merge pull request #58 from st235/feature/notifications
Browse files Browse the repository at this point in the history
Feature/notifications
  • Loading branch information
st235 authored Sep 20, 2020
2 parents 000be7a + 70cb088 commit 85a39ee
Show file tree
Hide file tree
Showing 32 changed files with 882 additions and 150 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ each item tag has following attributes:
| **exb_color** | reference/color | color of element, it may be color reference or color |
| **icon** | reference | icon reference (vector drawables supported) |
| **title** | reference/text | item title |
| **exb_badgeColor** | color | notification badge background color. **It will override the color from layout attribute** |
| **exb_badgeTextColor** | color | notification badge text color. **It will override the color from layout attribute** |
Just like any Android menu 😉
Expand Down Expand Up @@ -160,6 +162,22 @@ Then you should reference this xml file at the view attributes
| **exb_item_vertical_padding** | dimen | top & bottom item padding |
| **exb_item_horizontal_padding** | dimen | left & right item padding |
| **exb_items** | reference | xml supported menu format |
| **exb_notificationBadgeBackgroundColor** | color | notification badge background color. **Will be applied to all menu items** |
| **exb_notificationBadgeTextColor** | color | notification badge text color. **Will be applied to all menu items** |
## Notification badges
```kotlin
/**
* Returns notification object
*/
val notification = bottomBar.getNotificationFor(i.itemId) // itemId is R.id.action_id
notification.show() // shows simple dot-notification
notification.show("string literal") // shows notification with counter. Counter could not exceed the 4 symbols length
notification.clear() // removes notification badge from menu item
```
## Navigation Components support
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
compileSdkVersion 30
defaultConfig {
applicationId "github.com.st235.expandablebottonbar"
minSdkVersion 21
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
}

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<activity android:name=".screens.ScrollableCoordinatorLayoutActivity" />
<activity android:name=".screens.navigation.NavigationComponentActivity" />
<activity android:name=".screens.StylesActivity" />
<activity android:name=".screens.NotificationBadgeActivity" />
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class ShowCaseActivity : AppCompatActivity() {
createShowCase<StylesActivity>(
title = getString(R.string.styles_title),
description = getString(R.string.styles_description)
),
createShowCase<NotificationBadgeActivity>(
title = getString(R.string.notification_badges_title),
description = getString(R.string.notification_badges_description)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package github.com.st235.expandablebottombar.screens

import android.graphics.Color
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewAnimationUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.ColorUtils
import github.com.st235.expandablebottombar.R
import github.com.st235.lib_expandablebottombar.ExpandableBottomBar
import github.com.st235.lib_expandablebottombar.ExpandableBottomBarNotification
import kotlinx.android.synthetic.main.activity_xml_declared.*

class NotificationBadgeActivity : AppCompatActivity() {

private lateinit var bottomBar: ExpandableBottomBar

private val notificationsLookup = mutableMapOf<Int, ExpandableBottomBarNotification>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_notification_badge)
setSupportActionBar(toolbar)

val color: View = findViewById(R.id.color)
bottomBar = findViewById(R.id.expandable_bottom_bar)

color.setBackgroundColor(ColorUtils.setAlphaComponent(Color.GRAY, 60))

bottomBar.onItemSelectedListener = { v, i ->
val anim = ViewAnimationUtils.createCircularReveal(color,
bottomBar.x.toInt() + v.x.toInt() + v.width / 2,
bottomBar.y.toInt() + v.y.toInt() + v.height / 2, 0F,
findViewById<View>(android.R.id.content).height.toFloat())
color.setBackgroundColor(ColorUtils.setAlphaComponent(i.activeColor, 60))
anim.duration = 420
anim.start()
}

bottomBar.onItemReselectedListener = { v, i ->
if (!notificationsLookup.containsKey(i.itemId)) {
notificationsLookup[i.itemId] = bottomBar.getNotificationFor(i.itemId)
}
val notification = notificationsLookup.getValue(i.itemId)

if (v.tag == null) {
notification.show()
v.tag = 0
} else {
val counter = (v.tag as Int) + 1
notification.show(counter.toString())
v.tag = counter
}
}
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.toolbar_menu, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.clear -> {
for (notification in notificationsLookup.values) {
notification.clear()
}
}
}

return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import github.com.st235.expandablebottombar.R
import github.com.st235.lib_expandablebottombar.ExpandableBottomBar
import kotlinx.android.synthetic.main.activity_xml_declared.*


class XmlDeclaredActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/res/layout/activity_notification_badge.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".screens.ProgrammaticallyCreatedDemoActivity">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

<View
android:id="@+id/color"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<github.com.st235.lib_expandablebottombar.ExpandableBottomBar
android:id="@+id/expandable_bottom_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:exb_items="@menu/notification_badge_menu"
app:exb_itemInactiveColor="#CCCCCC"
app:exb_notificationBadgeBackgroundColor="#757575"
app:exb_notificationBadgeTextColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
22 changes: 22 additions & 0 deletions app/src/main/res/menu/notification_badge_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:title="@string/text"
android:icon="@drawable/ic_home"
app:exb_color="#FF8888"
app:exb_badgeColor="#FF5555"
app:exb_badgeTextColor="#FFFFFF" />

<item
android:id="@+id/settings"
android:title="@string/text4"
android:icon="@drawable/ic_settings"
app:exb_color="@color/colorSettings" />

<item
android:id="@+id/bookmarks"
android:title="@string/text3"
android:icon="@drawable/ic_bookmarks"
app:exb_color="#fa2" />
</menu>
6 changes: 6 additions & 0 deletions app/src/main/res/menu/toolbar_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/clear"
android:title="@string/action_clear" />
</menu>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<string name="text2">Likes</string>
<string name="text3">Bookmarks</string>
<string name="text4">Settings</string>
<string name="action_clear">Clear</string>

<string name="programmatically_title">Manually created</string>
<string name="programmatically_description">This showcase demonstrates the basic usage of library.\nSee: https://github.com/st235/ExpandableBottomBar#usage</string>
Expand All @@ -25,4 +26,7 @@

<string name="styles_title">Styles showcase</string>
<string name="styles_description">Demonstrates styles, introduced at 1.2.0.</string>

<string name="notification_badges_title">Notification badges showcase</string>
<string name="notification_badges_description">Demonstrates usage and look of notification badges\n See: https://github.com/st235/ExpandableBottomBar#notification-badges</string>
</resources>
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
vers = [
versionCode: 38,
versionName: "1.2.4"
versionCode: 39,
versionName: "1.3.0"
]
info = [
name: 'expandablebottombar',
Expand All @@ -14,7 +14,7 @@ ext {
}

buildscript {
ext.kotlin_version = '1.3.72'
ext.kotlin_version = '1.4.10'

repositories {
google()
Expand Down
4 changes: 2 additions & 2 deletions lib-expandablebottombar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ androidExtensions {
}

android {
compileSdkVersion 29
compileSdkVersion 30

defaultConfig {
minSdkVersion 19
targetSdkVersion 29
targetSdkVersion 30
versionCode vers.versionCode
versionName vers.versionName

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.hamcrest.collection.IsIterableContainingInOrder.contains
import org.junit.Assert.assertThat
import org.junit.Test


@RunWith(AndroidJUnit4::class)
@MediumTest
class ExpandableBottomBarParserTest {
Expand Down Expand Up @@ -46,7 +45,7 @@ class ExpandableBottomBarParserTest {
val items = expandableBottomBarParser.inflate(R.menu.valid_menu)
val iconText = appContext.getString(R.string.icon_text)
val actualItem =
ExpandableBottomBarMenuItem(R.id.icon_id, R.drawable.item_icon, iconText, Color.WHITE)
ExpandableBottomBarMenuItem(R.id.icon_id, R.drawable.item_icon, iconText, Color.WHITE, null, null)
assertThat(items, contains(actualItem))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<item
android:id="@+id/icon_id"
android:title="@string/icon_text"
app:icon="@drawable/item_icon"
app:color="@android:color/white" />
android:icon="@drawable/item_icon"
app:exb_color="@android:color/white" />
</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,10 @@ class ExpandableBottomBar @JvmOverloads constructor(
}
}

private val backgroundStates
= arrayOf(
intArrayOf(android.R.attr.state_selected),
intArrayOf(-android.R.attr.state_selected)
)
@ColorInt
private var globalBadgeColor: Int = Color.RED
@ColorInt
private var globalBadgeTextColor: Int = Color.WHITE

private var transitionDuration: Int = 0

Expand Down Expand Up @@ -130,6 +129,9 @@ class ExpandableBottomBar @JvmOverloads constructor(
menuHorizontalPadding = typedArray.getDimension(R.styleable.ExpandableBottomBar_exb_item_horizontal_padding, 15F.toPx()).toInt()
menuVerticalPadding = typedArray.getDimension(R.styleable.ExpandableBottomBar_exb_item_vertical_padding, 10F.toPx()).toInt()

globalBadgeColor = typedArray.getColor(R.styleable.ExpandableBottomBar_exb_notificationBadgeBackgroundColor, Color.RED)
globalBadgeTextColor = typedArray.getColor(R.styleable.ExpandableBottomBar_exb_notificationBadgeTextColor, Color.WHITE)

val rawItemsStyle = typedArray.getInt(R.styleable.ExpandableBottomBar_exb_itemStyle, 0)
styleController = StyleController.create(style = rawItemsStyle.toItemStyle())

Expand Down Expand Up @@ -173,6 +175,24 @@ class ExpandableBottomBar @JvmOverloads constructor(
setBackgroundColor(ContextCompat.getColor(context, colorRes), resources.getDimension(backgroundCornerRadiusRes))
}

fun setNotificationBadgeBackgroundColor(@ColorInt color: Int) {
globalBadgeColor = color
invalidate()
}

fun setNotificationBadgeBackgroundColorRes(@ColorRes colorRes: Int) {
setNotificationBadgeBackgroundColor(ContextCompat.getColor(context, colorRes))
}

fun setNotificationBadgeTextColor(@ColorInt color: Int) {
globalBadgeTextColor = color
invalidate()
}

fun setNotificationBadgeTextColorRes(@ColorRes colorRes: Int) {
setNotificationBadgeTextColor(ContextCompat.getColor(context, colorRes))
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()

Expand Down Expand Up @@ -201,6 +221,15 @@ class ExpandableBottomBar @JvmOverloads constructor(
bounds.set(0, 0, w, h)
}

/**
* Returns notification for passed menu item
*
* @throws NullPointerException when id doesn't exists
*/
fun getNotificationFor(@IdRes id: Int): ExpandableBottomBarNotification {
return viewControllers.getValue(id).notification()
}

/**
* Adds passed items to widget
*
Expand Down Expand Up @@ -290,15 +319,14 @@ class ExpandableBottomBar @JvmOverloads constructor(
}

private fun createItem(menuItem: ExpandableBottomBarMenuItem): ExpandableItemViewController {
val colors = intArrayOf(menuItem.activeColor, itemInactiveColor)
val selectedStateColorList = ColorStateList(backgroundStates, colors)

val viewController =
ExpandableItemViewController.Builder(menuItem)
.styleController(styleController)
.itemMargins(menuHorizontalPadding, menuVerticalPadding)
.itemBackground(itemBackgroundCornerRadius, itemBackgroundOpacity)
.itemsColors(selectedStateColorList)
.itemInactiveColor(itemInactiveColor)
.notificationBadgeColor(globalBadgeColor)
.notificationBadgeTextColor(globalBadgeTextColor)
.onItemClickListener { v: View ->
if (!v.isSelected) {
onItemSelected(menuItem)
Expand Down Expand Up @@ -327,7 +355,7 @@ class ExpandableBottomBar @JvmOverloads constructor(
set.clone(this)

viewControllers.getValue(activeMenuItem.itemId).select()
viewControllers.getValue(selectedItemId).deselect()
viewControllers.getValue(selectedItemId).unselect()
selectedItemId = activeMenuItem.itemId

set.applyTo(this)
Expand Down
Loading

0 comments on commit 85a39ee

Please sign in to comment.