diff --git a/app/build.gradle b/app/build.gradle index e532384..c25babb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,6 +11,8 @@ android { targetSdkVersion setup.targetSdk versionCode release.versionCode versionName release.versionName + + multiDexEnabled true } buildTypes { release { @@ -34,14 +36,14 @@ dependencies { implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation "androidx.appcompat:appcompat:${versions.appcompat}" implementation "androidx.legacy:legacy-support-v4:${versions.legacy}" - implementation "com.github.kuba2k2:MaterialDrawer:${versions.materialdrawer}" - //implementation "com.mikepenz:crossfader:1.6.0" // do not update - implementation "com.mikepenz:iconics-core:${versions.iconics}" // do not update. >3.1.0 Breaks jelly bean - implementation "com.mikepenz:iconics-views:${versions.iconics}" // do not update + implementation "com.mikepenz:materialdrawer:${versions.materialdrawer}" + implementation "com.mikepenz:iconics-core:${versions.iconics}" + implementation "com.mikepenz:iconics-views:${versions.iconics}" implementation "com.mikepenz:community-material-typeface:${versions.font_cmd}@aar" implementation "androidx.core:core-ktx:${versions.ktx}" implementation "androidx.constraintlayout:constraintlayout:${versions.constraintLayout}" implementation "com.google.android.material:material:${versions.material}" implementation "pl.droidsonroids.gif:android-gif-drawable:${versions.gifdrawable}" + implementation 'androidx.multidex:multidex:2.0.1' implementation project(":navlib") } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9cb4ef6..feae155 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,7 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" + android:name="androidx.multidex.MultiDexApplication" android:theme="@style/AppTheme.Light"> - \ No newline at end of file + diff --git a/app/src/main/java/pl/szczodrzynski/navigation/MainActivity.kt b/app/src/main/java/pl/szczodrzynski/navigation/MainActivity.kt index f41f583..d550fb4 100644 --- a/app/src/main/java/pl/szczodrzynski/navigation/MainActivity.kt +++ b/app/src/main/java/pl/szczodrzynski/navigation/MainActivity.kt @@ -12,12 +12,10 @@ import com.mikepenz.iconics.IconicsColor import com.mikepenz.iconics.IconicsDrawable import com.mikepenz.iconics.IconicsSize import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial -import com.mikepenz.materialdrawer.Drawer import com.mikepenz.materialdrawer.holder.StringHolder -import com.mikepenz.materialdrawer.model.PrimaryDrawerItem -import com.mikepenz.materialdrawer.model.ProfileDrawerItem import com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem -import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem +import com.mikepenz.materialdrawer.model.interfaces.* +import com.mikepenz.materialdrawer.model.utils.withIsHiddenInMiniDrawer import kotlinx.android.synthetic.main.sample_nav_view.* import pl.szczodrzynski.navlib.SystemBarsUtil import pl.szczodrzynski.navlib.SystemBarsUtil.Companion.COLOR_DO_NOT_CHANGE @@ -29,9 +27,9 @@ import pl.szczodrzynski.navlib.bottomsheet.NavBottomSheet.Companion.SORT_MODE_DE import pl.szczodrzynski.navlib.bottomsheet.NavBottomSheet.Companion.TOGGLE_GROUP_SORTING_ORDER import pl.szczodrzynski.navlib.bottomsheet.items.BottomSheetPrimaryItem import pl.szczodrzynski.navlib.bottomsheet.items.BottomSheetSeparatorItem -import pl.szczodrzynski.navlib.drawer.IDrawerProfile import pl.szczodrzynski.navlib.drawer.items.DrawerPrimaryItem import pl.szczodrzynski.navlib.getColorFromAttr +import pl.szczodrzynski.navlib.withIcon class MainActivity : AppCompatActivity() { @@ -262,7 +260,7 @@ class MainActivity : AppCompatActivity() { DrawerPrimaryItem().withName("Lock screen") .withDescription("aaand not visible in Mini Drawer") - .withHiddenInMiniDrawer(true) + .withIsHiddenInMiniDrawer(true) .withIdentifier(62) .withBadgeStyle(badgeStyle) .withIcon(CommunityMaterial.Icon.cmd_fingerprint), @@ -307,15 +305,13 @@ class MainActivity : AppCompatActivity() { .withIcon( IconicsDrawable(context, CommunityMaterial.Icon2.cmd_plus) .actionBar() - .padding(IconicsSize.dp(5)) - .color(IconicsColor.colorRes(pl.szczodrzynski.navlib.R.color.material_drawer_dark_primary_text)) + .padding { IconicsSize.dp(5) } + .color { IconicsColor.colorInt(getColorFromAttr(context, R.attr.materialDrawerPrimaryText)) } ) - .withOnDrawerItemClickListener(object : Drawer.OnDrawerItemClickListener { - override fun onItemClick(view: View?, position: Int, drawerItem: IDrawerItem<*>): Boolean { - Toast.makeText(context, "Add account", Toast.LENGTH_SHORT).show() - return true - } - }), + .withOnDrawerItemClickListener { v, item, position -> + Toast.makeText(context, "Add account", Toast.LENGTH_SHORT).show() + true + }, ProfileSettingDrawerItem() .withName("Manage Account") .withIcon(CommunityMaterial.Icon2.cmd_settings) diff --git a/build.gradle b/build.gradle index b725199..f951c57 100644 --- a/build.gradle +++ b/build.gradle @@ -6,8 +6,8 @@ buildscript { release = [ // major.minor.patch.rc.beta - versionName: "0.5.0", - versionCode: 50099 + versionName: "0.6.0", + versionCode: 60099 ] setup = [ @@ -18,14 +18,16 @@ buildscript { ] versions = [ - kotlin : "1.3.61", - ktx : "1.1.0", + gradleAndroid : "4.0.0-beta03", + + kotlin : ext.kotlin_version, + ktx : "1.2.0", androidX : '1.0.0', annotation : '1.1.0', recyclerView : '1.2.0-alpha01', - material : '1.2.0-alpha03', - appcompat : '1.2.0-alpha01', + material : '1.2.0-alpha05', + appcompat : '1.2.0-alpha03', constraintLayout : '2.0.0-beta4', cardview : '1.0.0', gridLayout : '1.0.0', @@ -33,22 +35,22 @@ buildscript { navigationFragment: "1.0.0", legacy : "1.0.0", - room : "2.2.3", - lifecycle : "2.2.0-rc03", - work : "2.2.0", + room : "2.2.5", + lifecycle : "2.2.0", + work : "2.3.4", firebase : '17.2.2', - firebasemessaging: "20.1.0", + firebasemessaging: "20.1.3", play_services : "17.0.0", materialdialogs : "0.9.6.0", - materialdrawer : "cad66092a6", + materialdrawer : "8.0.0-rc02", iconics : "4.0.1", font_cmd : "3.5.95.1-kotlin", gifdrawable : "1.2.15", - retrofit : '2.6.2' + retrofit : "2.6.4" ] } @@ -57,8 +59,8 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.3' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath "com.android.tools.build:gradle:${versions.gradleAndroid}" + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}" classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a1a6ddd..f365f83 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-all.zip diff --git a/navlib/build.gradle b/navlib/build.gradle index 77a77d7..cdc06cd 100644 --- a/navlib/build.gradle +++ b/navlib/build.gradle @@ -45,9 +45,10 @@ dependencies { implementation "androidx.recyclerview:recyclerview:${versions.recyclerView}" implementation "com.google.android.material:material:${versions.material}" - api "com.github.kuba2k2:MaterialDrawer:${versions.materialdrawer}" + api "com.mikepenz:materialdrawer:${versions.materialdrawer}" api "com.mikepenz:community-material-typeface:${versions.font_cmd}@aar" api "com.mikepenz:iconics-core:${versions.iconics}" + implementation "com.mikepenz:materialize:1.2.1" implementation "com.mikepenz:itemanimators:1.1.0" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin}" implementation "pl.droidsonroids.gif:android-gif-drawable:${versions.gifdrawable}" diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/BezelGifImageView.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/BezelGifImageView.kt index d53a585..716ee1b 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/BezelGifImageView.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/BezelGifImageView.kt @@ -10,7 +10,6 @@ import android.util.AttributeSet import android.view.MotionEvent import android.view.View import android.view.ViewOutlineProvider -import androidx.appcompat.widget.AppCompatImageView import androidx.core.view.ViewCompat import com.mikepenz.materialdrawer.R import com.mikepenz.materialdrawer.util.DrawerImageLoader @@ -22,7 +21,7 @@ import pl.droidsonroids.gif.GifImageView * drawable on top. This is useful for applying a beveled look to image contents, but is also * flexible enough for use with other desired aesthetics. */ -open class BezelGifImageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) : GifImageView(context, attrs, defStyle) { +open class BezelImageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) : GifImageView(context, attrs, defStyle) { private val mBlackPaint: Paint private val mMaskedPaint: Paint @@ -54,14 +53,14 @@ open class BezelGifImageView @JvmOverloads constructor(context: Context, attrs: // Attribute initialization val a = context.obtainStyledAttributes(attrs, R.styleable.BezelImageView, defStyle, R.style.BezelImageView) - mMaskDrawable = a.getDrawable(R.styleable.BezelImageView_biv_maskDrawable) + mMaskDrawable = a.getDrawable(R.styleable.BezelImageView_materialDrawerMaskDrawable) if (mMaskDrawable != null) { mMaskDrawable.callback = this } - mDrawCircularShadow = a.getBoolean(R.styleable.BezelImageView_biv_drawCircularShadow, true) + mDrawCircularShadow = a.getBoolean(R.styleable.BezelImageView_materialDrawerDrawCircularShadow, true) - mSelectorColor = a.getColor(R.styleable.BezelImageView_biv_selectorOnPress, 0) + mSelectorColor = a.getColor(R.styleable.BezelImageView_materialDrawerSelectorOnPress, 0) a.recycle() diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/DrawerExtensions.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/DrawerExtensions.kt index 30171eb..2ec9084 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/DrawerExtensions.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/DrawerExtensions.kt @@ -1,11 +1,18 @@ package pl.szczodrzynski.navlib import android.view.View +import androidx.recyclerview.widget.RecyclerView +import com.mikepenz.iconics.typeface.IIcon import com.mikepenz.materialdrawer.* -import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem -import com.mikepenz.materialdrawer.model.interfaces.IProfile +import com.mikepenz.materialdrawer.holder.StringHolder +import com.mikepenz.materialdrawer.model.AbstractDrawerItem +import com.mikepenz.materialdrawer.model.BaseDrawerItem +import com.mikepenz.materialdrawer.model.interfaces.* +import com.mikepenz.materialdrawer.util.getDrawerItem +import com.mikepenz.materialdrawer.util.updateItem +import com.mikepenz.materialdrawer.widget.MaterialDrawerSliderView -inline fun DrawerBuilder.withOnDrawerItemClickListener(crossinline listener: (view: View?, position: Int, drawerItem: IDrawerItem<*>) -> Boolean): DrawerBuilder { +/*inline fun DrawerBuilder.withOnDrawerItemClickListener(crossinline listener: (view: View?, position: Int, drawerItem: IDrawerItem<*>) -> Boolean): DrawerBuilder { return this.withOnDrawerItemClickListener(object : Drawer.OnDrawerItemClickListener { override fun onItemClick(view: View?, position: Int, drawerItem: IDrawerItem<*>): Boolean { return listener(view, position, drawerItem) @@ -65,4 +72,14 @@ inline fun MiniDrawer.withOnMiniDrawerItemClickListener(crossinline listener: (v return listener(view, position, drawerItem, type) } }) -} \ No newline at end of file +}*/ + +fun MaterialDrawerSliderView.updateBadge(identifier: Long, badge: StringHolder?) { + val drawerItem = getDrawerItem(identifier) + if (drawerItem is Badgeable) { + drawerItem.withBadge(badge) + updateItem(drawerItem) + } +} + +fun T.withIcon(icon: IIcon) = withIcon(pl.szczodrzynski.navlib.ImageHolder(icon)) diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/ImageHolder.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/ImageHolder.kt index 07173e5..374bc85 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/ImageHolder.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/ImageHolder.kt @@ -1,20 +1,21 @@ package pl.szczodrzynski.navlib import android.content.Context +import android.content.res.ColorStateList import android.graphics.Bitmap import android.graphics.PorterDuff import android.graphics.PorterDuffColorFilter import android.graphics.drawable.Drawable import android.net.Uri -import android.view.View import android.widget.ImageView import androidx.annotation.ColorInt import androidx.annotation.DrawableRes import androidx.appcompat.content.res.AppCompatResources import com.mikepenz.iconics.IconicsColor import com.mikepenz.iconics.IconicsDrawable -import com.mikepenz.iconics.IconicsSize import com.mikepenz.iconics.typeface.IIcon +import com.mikepenz.iconics.utils.paddingDp +import com.mikepenz.iconics.utils.sizeDp import com.mikepenz.materialdrawer.util.DrawerImageLoader import pl.droidsonroids.gif.GifDrawable import java.io.FileNotFoundException @@ -25,31 +26,24 @@ import java.io.FileNotFoundException open class ImageHolder : com.mikepenz.materialdrawer.holder.ImageHolder { - constructor(url: String) : super(url) {} - - constructor(uri: Uri) : super(uri) {} - - constructor(icon: Drawable?) : super(icon) {} - - constructor(bitmap: Bitmap?) : super(bitmap) {} - - constructor(@DrawableRes iconRes: Int) : super(iconRes) {} - constructor(@DrawableRes iconRes: Int, colorFilter: Int?) : super(iconRes) { this.colorFilter = colorFilter } - - constructor(iicon: IIcon) : super(null as Bitmap?) { - this.iIcon = iicon + constructor(iicon: IIcon) : super(null as Drawable?) { + this.iicon = iicon } + constructor() : super() + constructor(url: String) : super(url) + constructor(uri: Uri) : super(uri) + constructor(icon: Drawable?) : super(icon) + constructor(bitmap: Bitmap) : super(bitmap) + constructor(iconRes: Int) : super(iconRes) + var iicon: IIcon? = null @ColorInt var colorFilter: Int? = null var colorFilterMode: PorterDuff.Mode = PorterDuff.Mode.DST_OVER - override fun applyTo(imageView: ImageView): Boolean { - return applyTo(imageView, null) - } /** * sets an existing image to the imageView @@ -59,14 +53,14 @@ open class ImageHolder : com.mikepenz.materialdrawer.holder.ImageHolder { * @return true if an image was set */ override fun applyTo(imageView: ImageView, tag: String?): Boolean { - val ii = iIcon + val ii = iicon if (uri != null) { if (uri.toString().endsWith(".gif", true)) { imageView.setImageDrawable(GifDrawable(uri.toString())) } else { - val consumed = DrawerImageLoader.instance.setImage(imageView, uri, tag) + val consumed = DrawerImageLoader.instance.setImage(imageView, uri!!, tag) if (!consumed) { imageView.setImageURI(uri) } @@ -90,4 +84,39 @@ open class ImageHolder : com.mikepenz.materialdrawer.holder.ImageHolder { return true } + + /** + * this only handles Drawables + * + * @param ctx + * @param iconColor + * @param tint + * @return + */ + override fun decideIcon(ctx: Context, iconColor: ColorStateList, tint: Boolean, paddingDp: Int): Drawable? { + var icon: Drawable? = icon + val ii = iicon + val uri = uri + + when { + ii != null -> icon = IconicsDrawable(ctx, ii).apply { + color(IconicsColor.colorList(iconColor)) + sizeDp(24) + paddingDp(paddingDp) + } + iconRes != -1 -> icon = AppCompatResources.getDrawable(ctx, iconRes) + uri != null -> try { + val inputStream = ctx.contentResolver.openInputStream(uri) + icon = Drawable.createFromStream(inputStream, uri.toString()) + } catch (e: FileNotFoundException) { + //no need to handle this + } + } + //if we got an icon AND we have auto tinting enabled AND it is no IIcon, tint it ;) + if (icon != null && tint && iicon == null) { + icon = icon.mutate() + icon.setColorFilter(iconColor.defaultColor, PorterDuff.Mode.SRC_IN) + } + return icon + } } diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/NavBottomBar.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/NavBottomBar.kt index c09a2f4..2199bae 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/NavBottomBar.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/NavBottomBar.kt @@ -198,4 +198,4 @@ class NavBottomBar : BottomAppBar { override fun setOnMenuItemClickListener(listener: OnMenuItemClickListener?) { onMenuItemClickListener = listener } -} \ No newline at end of file +} diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/NavView.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/NavView.kt index 4e9e78d..496597c 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/NavView.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/NavView.kt @@ -13,15 +13,12 @@ import android.view.ViewGroup.LayoutParams.MATCH_PARENT import android.widget.FrameLayout import android.widget.LinearLayout import androidx.coordinatorlayout.widget.CoordinatorLayout +import androidx.core.view.children import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton import com.google.android.material.floatingactionbutton.FloatingActionButton import kotlinx.android.synthetic.main.nav_view.view.* import pl.szczodrzynski.navlib.bottomsheet.NavBottomSheet import pl.szczodrzynski.navlib.drawer.NavDrawer -import android.graphics.drawable.RippleDrawable -import android.os.Build -import androidx.core.view.children -import com.mikepenz.materialize.util.UIUtils class NavView : FrameLayout { @@ -82,9 +79,8 @@ class NavView : FrameLayout { drawer = NavDrawer( context, - findViewById(R.id.nv_drawerContainer), - findViewById(R.id.nv_fixedDrawerContainer), - findViewById(R.id.nv_miniDrawerContainerLandscape), + findViewById(R.id.nv_drawerLayout), + findViewById(R.id.nv_drawerContainerLandscape), findViewById(R.id.nv_miniDrawerContainerPortrait), findViewById(R.id.nv_miniDrawerElevation) ) @@ -108,11 +104,17 @@ class NavView : FrameLayout { //bottomSheetBehavior.peekHeight = displayHeight } + private fun convertDpToPixel(dp: Float): Float { + val resources = context.resources + val metrics = resources.displayMetrics + return dp * (metrics.densityDpi / 160f) + } + fun gainAttentionOnBottomBar() { var x = ripple.width.toFloat() var y = ripple.height.toFloat() - x -= UIUtils.convertDpToPixel(56f, context) / 2 - y -= UIUtils.convertDpToPixel(56f, context) / 2 + x -= convertDpToPixel(56f) / 2 + y -= convertDpToPixel(56f) / 2 ripple.performRipple(Point(x.toInt(), y.toInt())) } @@ -122,7 +124,7 @@ class NavView : FrameLayout { this.navigationBarBgView = navigationBarBackground this.statusBarDarkView = nv_statusBarDarker //this.navigationBarDarkView = navigationBarBackground - this.insetsListener = nv_drawerContainer + this.insetsListener = nv_drawerLayout this.marginBySystemBars = mainView this.paddingByNavigationBar = bottomSheet.getContentView() } @@ -185,7 +187,7 @@ class NavView : FrameLayout { } fun onBackPressed(): Boolean { - if (drawer.isOpen) { + if (drawer.isOpen && !drawer.fixedDrawerEnabled()) { if (drawer.profileSelectionIsOpen) { drawer.profileSelectionClose() return true diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/SystemBarsUtil.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/SystemBarsUtil.kt index 006e81b..1bcb1f8 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/SystemBarsUtil.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/SystemBarsUtil.kt @@ -295,7 +295,7 @@ class SystemBarsUtil(private val activity: Activity) { // PADDING if (insetsListener != null) { - if (SDK_INT >= LOLLIPOP) { + if (SDK_INT >= LOLLIPOP && false) { ViewCompat.setOnApplyWindowInsetsListener(insetsListener!!) { _, insets -> Log.d("NavLib", "Got insets left = ${insets.systemWindowInsetLeft}, top = ${insets.systemWindowInsetTop}, right = ${insets.systemWindowInsetRight}, bottom = ${insets.systemWindowInsetBottom}") if (insetsApplied) @@ -374,4 +374,4 @@ class SystemBarsUtil(private val activity: Activity) { keyboardUtil?.disable() } } -} \ No newline at end of file +} diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/Utils.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/Utils.kt index ccb7a30..bf47be0 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/Utils.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/Utils.kt @@ -5,20 +5,18 @@ import android.content.Context import android.content.res.Configuration import android.graphics.drawable.Drawable import android.os.Build +import android.util.DisplayMetrics import android.util.TypedValue import android.view.View +import android.view.WindowManager import androidx.annotation.AttrRes +import androidx.annotation.ColorInt +import androidx.annotation.ColorRes import androidx.annotation.DrawableRes import com.google.android.material.elevation.ElevationOverlayProvider import com.mikepenz.iconics.IconicsColor import com.mikepenz.iconics.IconicsDrawable -import android.util.DisplayMetrics -import android.view.WindowManager -import androidx.annotation.ColorInt -import androidx.annotation.ColorRes -import com.mikepenz.materialdrawer.Drawer -import com.mikepenz.materialdrawer.holder.StringHolder -import com.mikepenz.materialdrawer.model.interfaces.Badgeable +import com.mikepenz.iconics.utils.colorInt /*private val displayMetrics by lazy { @@ -122,7 +120,7 @@ fun hasNavigationBar(context: Context): Boolean { return hasNavigationBar } -fun IconicsDrawable.colorAttr(context: Context, @AttrRes attrRes: Int) = color(IconicsColor.colorInt(getColorFromAttr(context, attrRes))) +fun IconicsDrawable.colorAttr(context: Context, @AttrRes attrRes: Int) = colorInt(getColorFromAttr(context, attrRes)) fun getColorFromAttr(context: Context, @AttrRes color: Int): Int { val typedValue = TypedValue() @@ -149,14 +147,6 @@ fun Context.getColorFromRes(@ColorRes id: Int): Int { } } -fun Drawer.updateBadge(identifier: Long, badge: StringHolder?) { - val drawerItem = getDrawerItem(identifier) - if (drawerItem is Badgeable<*>) { - drawerItem.withBadge(badge) - updateItem(drawerItem) - } -} - fun crc16(buffer: String): Int { /* Note the change here */ var crc = 0x1D0F @@ -169,4 +159,4 @@ fun crc16(buffer: String): Int { } crc = crc and 0xffff return crc -} \ No newline at end of file +} diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/NavBottomSheet.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/NavBottomSheet.kt index 15795a1..0557ba4 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/NavBottomSheet.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/NavBottomSheet.kt @@ -431,4 +431,4 @@ class NavBottomSheet : CoordinatorLayout { return isOpen = !isOpen } -} \ No newline at end of file +} diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/items/BottomSheetPrimaryItem.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/items/BottomSheetPrimaryItem.kt index 6903d1b..7608714 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/items/BottomSheetPrimaryItem.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/bottomsheet/items/BottomSheetPrimaryItem.kt @@ -10,9 +10,8 @@ import androidx.recyclerview.widget.RecyclerView import com.mikepenz.iconics.IconicsDrawable import com.mikepenz.iconics.typeface.IIcon import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial -import com.mikepenz.iconics.utils.colorInt import com.mikepenz.iconics.utils.sizeDp -import com.mikepenz.materialize.holder.ImageHolder +import pl.szczodrzynski.navlib.ImageHolder import pl.szczodrzynski.navlib.R import pl.szczodrzynski.navlib.colorAttr import pl.szczodrzynski.navlib.getColorFromAttr @@ -45,7 +44,7 @@ data class BottomSheetPrimaryItem(override val isContextual: Boolean = true) : I viewHolder.image.setImageDrawable(IconicsDrawable(viewHolder.text.context) .icon(iconicsIcon ?: CommunityMaterial.Icon.cmd_android) - .colorAttr(viewHolder.text.context, R.attr.material_drawer_primary_icon) + .colorAttr(viewHolder.text.context, android.R.attr.textColorSecondary) .sizeDp(24)) viewHolder.description.visibility = View.VISIBLE @@ -59,7 +58,7 @@ data class BottomSheetPrimaryItem(override val isContextual: Boolean = true) : I titleRes != null -> viewHolder.text.setText(titleRes!!) else -> viewHolder.text.text = title } - viewHolder.text.setTextColor(getColorFromAttr(viewHolder.text.context, R.attr.material_drawer_primary_text)) + viewHolder.text.setTextColor(getColorFromAttr(viewHolder.text.context, android.R.attr.textColorPrimary)) } /*_____ _ @@ -122,4 +121,4 @@ data class BottomSheetPrimaryItem(override val isContextual: Boolean = true) : I this.onClickListener = onClickListener return this } -} \ No newline at end of file +} diff --git a/navlib/src/main/java/pl/szczodrzynski/navlib/drawer/NavDrawer.kt b/navlib/src/main/java/pl/szczodrzynski/navlib/drawer/NavDrawer.kt index 773efc2..6922313 100644 --- a/navlib/src/main/java/pl/szczodrzynski/navlib/drawer/NavDrawer.kt +++ b/navlib/src/main/java/pl/szczodrzynski/navlib/drawer/NavDrawer.kt @@ -9,33 +9,42 @@ import android.graphics.Color import android.graphics.PorterDuff import android.graphics.drawable.LayerDrawable import android.util.Log +import android.view.Gravity import android.view.View +import android.view.ViewGroup +import android.view.ViewGroup.LayoutParams.MATCH_PARENT +import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import android.widget.FrameLayout import android.widget.LinearLayout import android.widget.Toast import androidx.core.content.ContextCompat import androidx.customview.widget.ViewDragHelper import androidx.drawerlayout.widget.DrawerLayout +import com.mikepenz.fastadapter.IAdapter import com.mikepenz.itemanimators.AlphaCrossFadeAnimator import com.mikepenz.materialdrawer.* import com.mikepenz.materialdrawer.holder.BadgeStyle +import com.mikepenz.materialdrawer.holder.ColorHolder import com.mikepenz.materialdrawer.holder.StringHolder import com.mikepenz.materialdrawer.model.BaseDrawerItem +import com.mikepenz.materialdrawer.model.MiniProfileDrawerItem import com.mikepenz.materialdrawer.model.ProfileDrawerItem import com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem -import com.mikepenz.materialdrawer.model.interfaces.Badgeable -import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem -import com.mikepenz.materialdrawer.model.interfaces.IProfile +import com.mikepenz.materialdrawer.model.interfaces.* +import com.mikepenz.materialdrawer.util.* +import com.mikepenz.materialdrawer.widget.AccountHeaderView +import com.mikepenz.materialdrawer.widget.MaterialDrawerSliderView +import com.mikepenz.materialdrawer.widget.MiniDrawerSliderView +import com.mikepenz.materialize.util.UIUtils import pl.szczodrzynski.navlib.* import pl.szczodrzynski.navlib.R import pl.szczodrzynski.navlib.drawer.items.DrawerPrimaryItem class NavDrawer( val context: Context, - val drawerContainer: LinearLayout, - val fixedDrawerContainer: FrameLayout, - val miniDrawerContainerLandscape: FrameLayout, - val miniDrawerContainerPortrait: FrameLayout, + val drawerLayout: DrawerLayout, + val drawerContainerLandscape: FrameLayout, + val drawerContainerPortrait: FrameLayout, val miniDrawerElevation: View ) { companion object { @@ -51,11 +60,9 @@ class NavDrawer( internal lateinit var toolbar: NavToolbar internal lateinit var bottomBar: NavBottomBar - private var drawer: Drawer? = null - private var drawerView: View? = null - private var accountHeader: AccountHeader? = null - private var miniDrawer: MiniDrawer? = null - private var miniDrawerView: View? = null + private lateinit var drawer: MaterialDrawerSliderView + private lateinit var accountHeader: AccountHeaderView + private lateinit var miniDrawer: MiniDrawerSliderView private var drawerMode: Int = DRAWER_MODE_NORMAL private var selection: Int = -1 @@ -73,123 +80,109 @@ class NavDrawer( getColorFromAttr(context, R.attr.colorOnError) )*/ - badgeStyle = BadgeStyle() - .withTextColor(Color.WHITE) - .withColorRes(R.color.md_red_700) - - val drawerBuilder = DrawerBuilder() - .withActivity(activity) - .withDrawerLayout(R.layout.material_drawer_fits_not) - //.withHasStableIds(true) - .withItemAnimator(AlphaCrossFadeAnimator()) - .withRootView(drawerContainer) - .withFullscreen(true) - .withTranslucentStatusBar(false) - .withTranslucentNavigationBar(true) - .withTranslucentNavigationBarProgrammatically(false) - //.withToolbar(bottomBar) - .withDisplayBelowStatusBar(false) - //.withActionBarDrawerToggleAnimated(true) - .withShowDrawerOnFirstLaunch(true) - //.withShowDrawerUntilDraggedOpened(true) - .withGenerateMiniDrawer(true /* if it is not showing on screen, clicking items throws an exception */) - .withOnDrawerListener(object : Drawer.OnDrawerListener { - override fun onDrawerSlide(drawerView: View, slideOffset: Float) {} - override fun onDrawerOpened(drawerView: View) { - drawerOpenedListener?.invoke() - } - override fun onDrawerClosed(drawerView: View) { - drawerClosedListener?.invoke() - profileSelectionClose() - } - }) - .withOnDrawerItemClickListener { _, position, drawerItem -> - if (drawerItem.identifier.toInt() == selection) { - return@withOnDrawerItemClickListener false - } - when (drawerItemSelectedListener?.invoke(drawerItem.identifier.toInt(), position, drawerItem)) { - true -> { - when { - !drawerItem.isSelectable -> { - setSelection(selection, false) - return@withOnDrawerItemClickListener false - } - drawerItem is DrawerPrimaryItem -> toolbar.title = drawerItem.appTitle ?: drawerItem.name?.getText(context) ?: "" - drawerItem is BaseDrawerItem<*, *> -> toolbar.title = drawerItem.name?.getText(context) ?: "" - } - //setSelection(drawerItem.identifier.toInt(), false) - return@withOnDrawerItemClickListener false - } - false -> { - setSelection(selection, false) - return@withOnDrawerItemClickListener true - } - else -> { - return@withOnDrawerItemClickListener false - } - } - - } - .withOnDrawerItemLongClickListener { _, position, drawerItem -> - drawerItemLongClickListener?.invoke(drawerItem.identifier.toInt(), position, drawerItem) ?: true - } - - - val accountHeaderBuilder = AccountHeaderBuilder() - .withActivity(activity) - .withTranslucentStatusBar(true) - .withOnAccountHeaderListener { view, profile, current -> - if (profile is ProfileSettingDrawerItem) { - return@withOnAccountHeaderListener drawerProfileSettingClickListener?.invoke(profile.identifier.toInt(), view) ?: false - } - updateBadges() - if (current) { - close() - profileSelectionClose() - return@withOnAccountHeaderListener true - } - (drawerProfileSelectedListener?.invoke(profile.identifier.toInt(), profile, current, view) ?: false).also { - setToolbarProfileImage(profileList.singleOrNull { it.id == profile.identifier.toInt() }) - } - } - .withOnAccountHeaderItemLongClickListener { view, profile, current -> - if (profile is ProfileSettingDrawerItem) { - return@withOnAccountHeaderItemLongClickListener drawerProfileSettingLongClickListener?.invoke(profile.identifier.toInt(), view) ?: true - } - drawerProfileLongClickListener?.invoke(profile.identifier.toInt(), profile, current, view) ?: false - } - .withOnAccountHeaderProfileImageListener( - onClick = { view, profile, current -> - drawerProfileImageClickListener?.invoke(profile.identifier.toInt(), profile, current, view) ?: false - }, - onLongClick = { view, profile, current -> - drawerProfileImageLongClickListener?.invoke(profile.identifier.toInt(), profile, current, view) ?: false - } - ) - .withHeaderBackground(R.drawable.header) - .withTextColor(ContextCompat.getColor(context, R.color.material_drawer_dark_primary_text)) - - accountHeader = accountHeaderBuilder.build() - drawerBuilder.withAccountHeader(accountHeader!!) - drawer = drawerBuilder.build() - - drawerView = drawer?.slider - - miniDrawer = drawer?.miniDrawer - miniDrawer?.withOnMiniDrawerItemClickListener { _, _, _, type -> - if (type == MiniDrawer.PROFILE) { - profileSelectionOpen() - open() - return@withOnMiniDrawerItemClickListener true - } - return@withOnMiniDrawerItemClickListener false + badgeStyle = BadgeStyle().apply { + textColor = ColorHolder.fromColor(Color.WHITE) + color = ColorHolder.fromColor(0xffd32f2f.toInt()) + } + + drawerLayout.addDrawerListener(object : DrawerLayout.DrawerListener { + override fun onDrawerStateChanged(newState: Int) {} + override fun onDrawerSlide(drawerView: View, slideOffset: Float) {} + override fun onDrawerClosed(drawerView: View) { + drawerClosedListener?.invoke() + profileSelectionClose() + } + override fun onDrawerOpened(drawerView: View) { + drawerOpenedListener?.invoke() + } + }) + + accountHeader = AccountHeaderView(context).apply { + headerBackground = ImageHolder(R.drawable.header) + + onAccountHeaderListener = { view, profile, current -> + if (profile is ProfileSettingDrawerItem) { + drawerProfileSettingClickListener?.invoke(profile.identifier.toInt(), view) ?: false + } + else { + updateBadges() + if (current) { + close() + profileSelectionClose() + true + } + else { + (drawerProfileSelectedListener?.invoke(profile.identifier.toInt(), profile, current, view) ?: false).also { + setToolbarProfileImage(profileList.singleOrNull { it.id == profile.identifier.toInt() }) + } + } + } + } + + onAccountHeaderItemLongClickListener = { view, profile, current -> + if (profile is ProfileSettingDrawerItem) { + drawerProfileSettingLongClickListener?.invoke(profile.identifier.toInt(), view) ?: true + } + else { + drawerProfileLongClickListener?.invoke(profile.identifier.toInt(), profile, current, view) ?: false + } + } + + onAccountHeaderProfileImageListener = { view, profile, current -> + drawerProfileImageClickListener?.invoke(profile.identifier.toInt(), profile, current, view) ?: false + } + //.withTextColor(ContextCompat.getColor(context, R.color.material_drawer_dark_primary_text)) + } + + drawer = MaterialDrawerSliderView(context).apply { + accountHeader = this@NavDrawer.accountHeader + itemAnimator = AlphaCrossFadeAnimator() + //hasStableIds = true + + onDrawerItemClickListener = { _, drawerItem, position -> + if (drawerItem.identifier.toInt() == selection) { + false + } + else { + val consumed = drawerItemSelectedListener?.invoke(drawerItem.identifier.toInt(), position, drawerItem) + if (consumed == false || !drawerItem.isSelectable) { + setSelection(selection, false) + consumed == false + } + else if (consumed == true) { + when (drawerItem) { + is DrawerPrimaryItem -> toolbar.title = drawerItem.appTitle ?: drawerItem.name?.getText(context) ?: "" + is BaseDrawerItem<*, *> -> toolbar.title = drawerItem.name?.getText(context) ?: "" + } + false + } + else { + false + } + } + } + + onDrawerItemLongClickListener = { _, drawerItem, position -> + drawerItemLongClickListener?.invoke(drawerItem.identifier.toInt(), position, drawerItem) ?: true + } + } + + miniDrawer = MiniDrawerSliderView(context).apply { + drawer = this@NavDrawer.drawer + includeSecondaryDrawerItems = false + this::class.java.getDeclaredField("onMiniDrawerItemClickListener").let { + it.isAccessible = true + it.set(this, { v: View?, position: Int, item: IDrawerItem<*>, type: Int -> + if (item is MiniProfileDrawerItem) { + profileSelectionOpen() + open() + true + } + else false + }) + } } - miniDrawer?.withIncludeSecondaryDrawerItems(false) - // TODO 2019-08-27 build miniDrawerView only if needed - // building in decideDrawerMode causes an exception when clicking drawer items - // also update method updateMiniDrawer... - miniDrawerView = miniDrawer?.build(context) updateMiniDrawer() toolbar.profileImageClickListener = { @@ -215,62 +208,62 @@ class NavDrawer( appendItem(item) } fun appendItem(item: IDrawerItem<*>) { - drawer?.addItem(item) + drawer.addItems(item) updateMiniDrawer() } fun appendItems(vararg items: IDrawerItem<*>) { - drawer?.addItems(*items) + drawer.addItems(*items) updateMiniDrawer() } fun prependItem(item: IDrawerItem<*>) { - drawer?.addItemAtPosition(item, 0) + drawer.addItemAtPosition(0, item) updateMiniDrawer() } fun prependItems(vararg items: IDrawerItem<*>) { - drawer?.addItemsAtPosition(0, *items) + drawer.addItemsAtPosition(0, *items) updateMiniDrawer() } fun addItemAt(index: Int, item: IDrawerItem<*>) { - drawer?.addItemAtPosition(item, index) + drawer.addItemAtPosition(index, item) updateMiniDrawer() } fun addItemsAt(index: Int, vararg items: IDrawerItem<*>) { - drawer?.addItemsAtPosition(index, *items) + drawer.addItemsAtPosition(index, *items) updateMiniDrawer() } fun removeItemById(id: Int) { - drawer?.removeItem(id.toLong()) + drawer.removeItems(id.toLong()) updateMiniDrawer() } fun removeItemAt(index: Int) { - drawer?.removeItemByPosition(index) + drawer.removeItemByPosition(index) updateMiniDrawer() } fun removeAllItems() { - drawer?.removeAllItems() + drawer.removeAllItems() updateMiniDrawer() } fun getItemById(id: Int, run: (it: IDrawerItem<*>?) -> Unit) { - drawer?.getDrawerItem(id.toLong()).also { + drawer.getDrawerItem(id.toLong()).also { run(it) if (it != null) - drawer?.updateItem(it) + drawer.updateItem(it) updateMiniDrawer() } } fun getItemByIndex(index: Int, run: (it: IDrawerItem<*>?) -> Unit) { - drawer?.drawerItems?.getOrNull(index).also { + drawer.itemAdapter.itemList.get(index).also { run(it) if (it != null) - drawer?.updateItem(it) + drawer.updateItem(it) updateMiniDrawer() } } fun setItems(vararg items: IDrawerItem<*>) { - drawer?.removeAllItems() - drawer?.addItems(*items) + drawer.removeAllItems() + drawer.addItems(*items) updateMiniDrawer() } @@ -282,12 +275,12 @@ class NavDrawer( |_| |_| |_| \_/ \__,_|\__\___| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|__*/ private fun drawerSetDragMargin(size: Float) { try { - val mDrawerLayout = drawer?.drawerLayout - val mDragger = mDrawerLayout?.javaClass?.getDeclaredField( + val mDrawerLayout = drawerLayout + val mDragger = mDrawerLayout::class.java.getDeclaredField( "mLeftDragger" )//mRightDragger for right obviously - mDragger?.isAccessible = true - val draggerObj = mDragger?.get(mDrawerLayout) as ViewDragHelper? + mDragger.isAccessible = true + val draggerObj = mDragger.get(mDrawerLayout) as ViewDragHelper? val mEdgeSize = draggerObj?.javaClass?.getDeclaredField( "mEdgeSize" @@ -327,36 +320,37 @@ class NavDrawer( } internal fun decideDrawerMode(orientation: Int, widthDp: Int, heightDp: Int) { + val drawerLayoutParams = DrawerLayout.LayoutParams(WRAP_CONTENT, MATCH_PARENT).apply { + gravity = Gravity.START + } + val fixedLayoutParams = FrameLayout.LayoutParams(UIUtils.convertDpToPixel(300f, context).toInt(), MATCH_PARENT) + Log.d("NavLib", "Deciding drawer mode:") if (orientation == Configuration.ORIENTATION_PORTRAIT) { - if (fixedDrawerContainer.childCount > 0) { - fixedDrawerContainer.removeAllViews() - } Log.d("NavLib", "- fixed container disabled") - drawer?.drawerLayout?.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED) - if (drawer?.drawerLayout?.indexOfChild(drawerView) == -1) { - drawer?.drawerLayout?.addView(drawerView) - } - Log.d("NavLib", "- slider enabled") - if (miniDrawerContainerLandscape.childCount > 0) { - miniDrawerContainerLandscape.removeAllViews() + if (drawerContainerLandscape.childCount > 0) { + drawerContainerLandscape.removeAllViews() } Log.d("NavLib", "- mini drawer land disabled") + if (drawerLayout.indexOfChild(drawer) == -1) { + drawerLayout.addView(drawer, drawerLayoutParams) + } + drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED) + Log.d("NavLib", "- slider enabled") + if ((widthDp >= 480 && miniDrawerVisiblePortrait != false) || miniDrawerVisiblePortrait == true) { - if (miniDrawerView == null) - miniDrawerView = miniDrawer?.build(context) - if (miniDrawerContainerPortrait.indexOfChild(miniDrawerView) == -1) - miniDrawerContainerPortrait.addView(miniDrawerView) + if (drawerContainerPortrait.indexOfChild(miniDrawer) == -1) + drawerContainerPortrait.addView(miniDrawer) Log.d("NavLib", "- mini drawer port enabled") drawerSetDragMargin(72 * resources.displayMetrics.density) drawerMode = DRAWER_MODE_MINI updateMiniDrawer() } else { - if (miniDrawerContainerPortrait.childCount > 0) { - miniDrawerContainerPortrait.removeAllViews() + if (drawerContainerPortrait.childCount > 0) { + drawerContainerPortrait.removeAllViews() } Log.d("NavLib", "- mini drawer port disabled") drawerSetDragMargin(20 * resources.displayMetrics.density) @@ -364,68 +358,67 @@ class NavDrawer( } } else { - if (miniDrawerContainerPortrait.childCount > 0) { - miniDrawerContainerPortrait.removeAllViews() + if (drawerContainerPortrait.childCount > 0) { + drawerContainerPortrait.removeAllViews() } Log.d("NavLib", "- mini drawer port disabled") - if ((widthDp in 480 until 9000 && miniDrawerVisibleLandscape != false) || miniDrawerVisibleLandscape == true) { - if (miniDrawerView == null) - miniDrawerView = miniDrawer?.build(context) - if (miniDrawerContainerLandscape.indexOfChild(miniDrawerView) == -1) - miniDrawerContainerLandscape.addView(miniDrawerView) + if ((widthDp in 480 until 900 && miniDrawerVisibleLandscape != false) || miniDrawerVisibleLandscape == true) { + if (drawerContainerLandscape.indexOfChild(miniDrawer) == -1) + drawerContainerLandscape.addView(miniDrawer) Log.d("NavLib", "- mini drawer land enabled") drawerSetDragMargin(72 * resources.displayMetrics.density) drawerMode = DRAWER_MODE_MINI updateMiniDrawer() } else { - if (miniDrawerContainerLandscape.childCount > 0) { - miniDrawerContainerLandscape.removeAllViews() + if (drawerContainerLandscape.childCount > 0) { + drawerContainerLandscape.removeAllViews() } Log.d("NavLib", "- mini drawer land disabled") drawerSetDragMargin(20 * resources.displayMetrics.density) drawerMode = DRAWER_MODE_NORMAL } - if (widthDp >= 9000) { + if (widthDp >= 900) { // screen is big enough to show fixed drawer - if (drawer?.drawerLayout?.indexOfChild(drawerView) != -1) { + if (drawerLayout.indexOfChild(drawer) != -1) { // remove from slider - drawer?.drawerLayout?.removeView(drawerView) + drawerLayout.removeView(drawer) } // lock the slider - drawer?.drawerLayout?.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) + drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) Log.d("NavLib", "- slider disabled") // add to fixed container - if (fixedDrawerContainer.indexOfChild(drawerView) == -1) - fixedDrawerContainer.addView(drawerView) + if (drawerContainerLandscape.indexOfChild(drawer) == -1) + drawerContainerLandscape.addView(drawer, fixedLayoutParams) + drawer.visibility = View.VISIBLE Log.d("NavLib", "- fixed container enabled") drawerMode = DRAWER_MODE_FIXED } else { // screen is too small for the fixed drawer - if (fixedDrawerContainer.childCount > 0) { + if (drawerContainerLandscape.indexOfChild(drawer) != -1) { // remove from fixed container - fixedDrawerContainer.removeAllViews() + drawerContainerLandscape.removeView(drawer) } Log.d("NavLib", "- fixed container disabled") // unlock the slider - drawer?.drawerLayout?.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED) - if (drawer?.drawerLayout?.indexOfChild(drawerView) == -1) { + drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED) + if (drawerLayout.indexOfChild(drawer) == -1) { // add to slider - drawer?.drawerLayout?.addView(drawerView) + drawerLayout.addView(drawer, drawerLayoutParams) } Log.d("NavLib", "- slider enabled") } } - miniDrawerElevation.visibility = if (drawerMode == DRAWER_MODE_MINI) View.VISIBLE else View.GONE + miniDrawerElevation.visibility = if (drawerMode == DRAWER_MODE_MINI || drawerMode == DRAWER_MODE_FIXED) View.VISIBLE else View.GONE } private fun updateMiniDrawer() { - selection = drawer?.currentSelection?.toInt() ?: -1 + selection = drawer.selectedItemIdentifier.toInt() //if (drawerMode == DRAWER_MODE_MINI) - miniDrawer?.createItems() + miniDrawer.createItems() } /* _____ _ _ _ _ _ _ @@ -435,45 +428,37 @@ class NavDrawer( | | | |_| | |_) | | | (__ | | | | | | __/ |_| | | | (_) | (_| \__ \ |_| \__,_|_.__/|_|_|\___| |_| |_| |_|\___|\__|_| |_|\___/ \__,_|__*/ var isOpen - get() = drawer?.isDrawerOpen ?: false || drawerMode == DRAWER_MODE_FIXED + get() = drawerLayout.isOpen || drawerMode == DRAWER_MODE_FIXED set(value) { if (drawerMode == DRAWER_MODE_FIXED) return - if (value && !isOpen) drawer?.openDrawer() else if (!value && isOpen) drawer?.closeDrawer() + if (value && !isOpen) drawerLayout.open() else if (!value && isOpen) drawerLayout.close() } fun open() { isOpen = true } fun close() { isOpen = false } - fun toggle() { - if (drawer == null) - return - isOpen = !isOpen - } + fun toggle() { isOpen = !isOpen } var profileSelectionIsOpen - get() = accountHeader?.isSelectionListShown == true + get() = accountHeader.selectionListShown set(value) { if (value != profileSelectionIsOpen) profileSelectionToggle() } fun profileSelectionOpen() { profileSelectionIsOpen = true } fun profileSelectionClose() { profileSelectionIsOpen = false } - fun profileSelectionToggle() { - accountHeader?.let { - it.toggleSelectionList(it.view.context) - } - } + fun profileSelectionToggle() { accountHeader.selectionListShown = !accountHeader.selectionListShown } var drawerOpenedListener: (() -> Unit)? = null var drawerClosedListener: (() -> Unit)? = null var drawerItemSelectedListener: ((id: Int, position: Int, drawerItem: IDrawerItem<*>) -> Boolean)? = null var drawerItemLongClickListener: ((id: Int, position: Int, drawerItem: IDrawerItem<*>) -> Boolean)? = null - var drawerProfileSelectedListener: ((id: Int, profile: IProfile<*>, current: Boolean, view: View?) -> Boolean)? = null - var drawerProfileLongClickListener: ((id: Int, profile: IProfile<*>, current: Boolean, view: View) -> Boolean)? = null - var drawerProfileImageClickListener: ((id: Int, profile: IProfile<*>, current: Boolean, view: View) -> Boolean)? = null - var drawerProfileImageLongClickListener: ((id: Int, profile: IProfile<*>, current: Boolean, view: View) -> Boolean)? = null + var drawerProfileSelectedListener: ((id: Int, profile: IProfile, current: Boolean, view: View?) -> Boolean)? = null + var drawerProfileLongClickListener: ((id: Int, profile: IProfile, current: Boolean, view: View?) -> Boolean)? = null + var drawerProfileImageClickListener: ((id: Int, profile: IProfile, current: Boolean, view: View) -> Boolean)? = null + var drawerProfileImageLongClickListener: ((id: Int, profile: IProfile, current: Boolean, view: View) -> Boolean)? = null var drawerProfileListEmptyListener: (() -> Unit)? = null var drawerProfileSettingClickListener: ((id: Int, view: View?) -> Boolean)? = null - var drawerProfileSettingLongClickListener: ((id: Int, view: View) -> Boolean)? = null + var drawerProfileSettingLongClickListener: ((id: Int, view: View?) -> Boolean)? = null fun miniDrawerEnabled(): Boolean = drawerMode == DRAWER_MODE_MINI fun fixedDrawerEnabled(): Boolean = drawerMode == DRAWER_MODE_FIXED @@ -485,26 +470,26 @@ class NavDrawer( profileSelectionClose() selection = id - if (drawer?.currentSelection != id.toLong()) { + if (drawer.selectedItemIdentifier != id.toLong()) { } - if (drawer?.currentSelection != id.toLong() || !fireOnClick) - drawer?.setSelection(id.toLong(), fireOnClick) + if (drawer.selectedItemIdentifier != id.toLong() || !fireOnClick) + drawer.setSelection(id.toLong(), fireOnClick) - miniDrawer?.setSelection(-1L) + miniDrawer.setSelection(-1L) if (drawerMode == DRAWER_MODE_MINI) - miniDrawer?.setSelection(id.toLong()) + miniDrawer.setSelection(id.toLong()) } fun getSelection(): Int = selection // TODO 2019-08-27 add methods for Drawable, @DrawableRes fun setAccountHeaderBackground(path: String?) { if (path == null) { - accountHeader?.setBackgroundRes(R.drawable.header) + accountHeader.headerBackground = ImageHolder(R.drawable.header) return } - accountHeader?.setHeaderBackground(ImageHolder(path)) + accountHeader.headerBackground = ImageHolder(path) } /* _____ __ _ _ @@ -516,12 +501,12 @@ class NavDrawer( private var profileList: MutableList = mutableListOf() fun addProfileSettings(vararg items: ProfileSettingDrawerItem) { - accountHeader?.profiles?.addAll(items) + accountHeader.profiles?.addAll(items) } private fun updateProfileList() { // remove all profile items - val profiles = accountHeader?.profiles?.filterNot { it is ProfileDrawerItem } as MutableList>? + val profiles = accountHeader.profiles?.filterNot { it is ProfileDrawerItem } as MutableList? if (profileList.isEmpty()) drawerProfileListEmptyListener?.invoke() @@ -537,7 +522,7 @@ class NavDrawer( .also { profiles?.add(index, it) } } - accountHeader?.profiles = profiles + accountHeader.profiles = profiles updateMiniDrawer() } @@ -550,10 +535,10 @@ class NavDrawer( val profileListEmpty: Boolean get() = profileList.isEmpty() var currentProfile: Int - get() = accountHeader?.activeProfile?.identifier?.toInt() ?: -1 + get() = accountHeader.activeProfile?.identifier?.toInt() ?: -1 set(value) { Log.d("NavDebug", "currentProfile = $value") - accountHeader?.setActiveProfile(value.toLong(), false) + accountHeader.setActiveProfile(value.toLong(), false) currentProfileObj = profileList.singleOrNull { it.id == value } setToolbarProfileImage(currentProfileObj) updateBadges() @@ -595,7 +580,7 @@ class NavDrawer( updateProfileList() } fun removeAllProfileSettings() { - accountHeader?.profiles = accountHeader?.profiles?.filterNot { it is ProfileSettingDrawerItem }?.toMutableList() + accountHeader.profiles = accountHeader.profiles?.filterNot { it is ProfileSettingDrawerItem }?.toMutableList() } fun getProfileById(id: Int, run: (it: IDrawerProfile?) -> Unit) { @@ -631,10 +616,10 @@ class NavDrawer( currentProfileObj = profileList.singleOrNull { it.id == currentProfile } - drawer?.drawerItems?.forEachIndexed { index, item -> - if (item is Badgeable<*>) { - item.withBadge(null) - drawer!!.updateItem(item) + drawer.itemAdapter.itemList.items.forEachIndexed { index, item -> + if (item is Badgeable) { + item.badge = null + drawer.updateItem(item) } } @@ -652,7 +637,7 @@ class NavDrawer( return@forEach } Log.d("NavDebug", "- Set badge ${it.count} for ${it.drawerItemId}") - drawer?.updateBadge( + drawer.updateBadge( it.drawerItemId?.toLong() ?: 0, when { it.count == 0 -> null @@ -720,4 +705,4 @@ class NavDrawer( unreadCounterList.add(UnreadCounter(profileId, type, null, count)) } } -} \ No newline at end of file +} diff --git a/navlib/src/main/res/layout/nav_view.xml b/navlib/src/main/res/layout/nav_view.xml index 5edb9ab..4b46d11 100644 --- a/navlib/src/main/res/layout/nav_view.xml +++ b/navlib/src/main/res/layout/nav_view.xml @@ -7,8 +7,8 @@ android:orientation="horizontal" tools:parentTag="FrameLayout"> - + android:background="@color/colorSurface_4dp" + tools:layout_width="72dp" /> @@ -94,7 +95,8 @@ android:id="@+id/nv_miniDrawerContainerPortrait" android:layout_width="wrap_content" android:layout_height="match_parent" - android:background="@color/colorSurface_4dp" /> + android:background="@color/colorSurface_4dp" + tools:layout_width="72dp" /> - - - + - \ No newline at end of file + diff --git a/navlib/src/main/res/values/styles.xml b/navlib/src/main/res/values/styles.xml index 94ee5cd..6ad92c2 100644 --- a/navlib/src/main/res/values/styles.xml +++ b/navlib/src/main/res/values/styles.xml @@ -95,16 +95,9 @@ @style/NavView.TextView - ?android:colorBackground - ?android:textColorPrimary - #5F6368 - ?android:textColorSecondary - @color/material_drawer_hint_text - @color/material_drawer_divider - @color/material_drawer_selected - #242424 - @color/material_drawer_header_selection_text - @color/material_drawer_dark_header_selection_subtext + @style/Widget.MaterialDrawerStyle + @style/Widget.MaterialDrawerHeaderStyle + - \ No newline at end of file +