From 3377c0310cbfbd9c68d96d9999a8e5c5838c5302 Mon Sep 17 00:00:00 2001 From: melianor Date: Sat, 25 Apr 2020 16:12:20 +0300 Subject: [PATCH] set menu resource dynamically --- README.md | 2 +- app/build.gradle | 8 ++++---- build.gradle | 4 ++-- lib/build.gradle | 9 ++++----- .../me/ibrahimsn/lib/NavigationComponentHelper.kt | 9 ++++++--- lib/src/main/java/me/ibrahimsn/lib/SmoothBottomBar.kt | 11 +++++++++++ 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 3653a45..43bccdd 100644 --- a/README.md +++ b/README.md @@ -214,7 +214,7 @@ allprojects { } dependencies { - implementation 'com.github.ibrahimsn98:SmoothBottomBar:1.7.4' + implementation 'com.github.ibrahimsn98:SmoothBottomBar:1.7.5' } ``` diff --git a/app/build.gradle b/app/build.gradle index 0e64b70..878c297 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,11 +5,11 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 28 + compileSdkVersion 29 defaultConfig { applicationId "me.ibrahimsn.smoothbottombar" minSdkVersion 16 - targetSdkVersion 28 + targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -27,13 +27,13 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.core:core-ktx:1.1.0' + implementation 'androidx.core:core-ktx:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation project(":lib") implementation 'androidx.legacy:legacy-support-v4:1.0.0' - testImplementation 'junit:junit:4.12' + testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/build.gradle b/build.gradle index c698fc6..7b643f1 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,14 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.3.61' + ext.kotlin_version = '1.3.72' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.6.2' + classpath 'com.android.tools.build:gradle:3.6.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/lib/build.gradle b/lib/build.gradle index 5d28588..daa441d 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -2,12 +2,11 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 28 - + compileSdkVersion 29 defaultConfig { minSdkVersion 16 - targetSdkVersion 28 + targetSdkVersion 29 versionCode 1 versionName "1.0" @@ -29,8 +28,8 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.core:core-ktx:1.1.0' - testImplementation 'junit:junit:4.12' + implementation 'androidx.core:core-ktx:1.2.0' + testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/lib/src/main/java/me/ibrahimsn/lib/NavigationComponentHelper.kt b/lib/src/main/java/me/ibrahimsn/lib/NavigationComponentHelper.kt index a725733..fa36a87 100644 --- a/lib/src/main/java/me/ibrahimsn/lib/NavigationComponentHelper.kt +++ b/lib/src/main/java/me/ibrahimsn/lib/NavigationComponentHelper.kt @@ -3,7 +3,6 @@ package me.ibrahimsn.lib import android.os.Bundle import android.view.Menu import androidx.annotation.IdRes -import androidx.core.view.size import androidx.navigation.NavController import androidx.navigation.NavDestination import androidx.navigation.ui.NavigationUI @@ -26,20 +25,25 @@ class NavigationComponentHelper { return NavigationUI.onNavDestinationSelected(menu.getItem(pos), navController) } }) + val weakReference = WeakReference(smoothBottomBar) + navController.addOnDestinationChangedListener(object : NavController.OnDestinationChangedListener { + override fun onDestinationChanged( controller: NavController, destination: NavDestination, arguments: Bundle? ) { val view = weakReference.get() + if (view == null) { navController.removeOnDestinationChangedListener(this) return } - for (h in 0 until menu.size) { + + for (h in 0 until menu.size()) { val menuItem = menu.getItem(h) if (matchDestination(destination, menuItem.itemId)) { menuItem.isChecked = true @@ -47,7 +51,6 @@ class NavigationComponentHelper { } } } - }) } diff --git a/lib/src/main/java/me/ibrahimsn/lib/SmoothBottomBar.kt b/lib/src/main/java/me/ibrahimsn/lib/SmoothBottomBar.kt index 5d431bc..da4b922 100644 --- a/lib/src/main/java/me/ibrahimsn/lib/SmoothBottomBar.kt +++ b/lib/src/main/java/me/ibrahimsn/lib/SmoothBottomBar.kt @@ -15,6 +15,8 @@ import android.view.MotionEvent import android.view.View import android.view.animation.DecelerateInterpolator import androidx.annotation.FontRes +import androidx.annotation.MenuRes +import androidx.annotation.XmlRes import androidx.core.content.res.ResourcesCompat import androidx.core.graphics.drawable.DrawableCompat import androidx.navigation.NavController @@ -244,6 +246,15 @@ class SmoothBottomBar : View { return true } + fun setMenuRes(@MenuRes menuRes: Int, activeItem: Int? = null) { + items = BottomBarParser(context, menuRes).parse() + invalidate() + + activeItem?.let { + setActiveItem(it) + } + } + fun setActiveItem(pos: Int) { activeItemIndex = pos