Update MaterialDrawer to support hiding items from the Mini drawer. Remove GIF files dependency in sample app. Fix profile image colors in default IDrawerProfile implementation.

This commit is contained in:
kubasz 2019-09-06 14:05:37 +02:00
parent 2efa7f4947
commit c26753fdc6
8 changed files with 99 additions and 16 deletions

View File

@ -35,7 +35,7 @@ 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:library:${versions.materialdrawer}"
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
@ -43,5 +43,6 @@ dependencies {
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 project(":navlib")
}

View File

@ -1,11 +1,21 @@
package pl.szczodrzynski.navigation
import android.content.Context
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import pl.droidsonroids.gif.GifDrawable
import pl.szczodrzynski.navlib.ImageHolder
import pl.szczodrzynski.navlib.crc16
import pl.szczodrzynski.navlib.drawer.IDrawerProfile
import pl.szczodrzynski.navlib.getColorFromRes
import pl.szczodrzynski.navlib.getDrawableFromRes
/*
Example IDrawerProfile implementation
*/
class DrawerProfile(
override var id: Int,
override var name: String?,
@ -13,15 +23,70 @@ class DrawerProfile(
override var image: String?
) : IDrawerProfile {
private fun colorFromName(context: Context, name: String?): Int {
var crc = crc16(name ?: "")
crc = (crc and 0xff) or (crc shr 8)
crc %= 16
val color = when (crc) {
13 -> R.color.md_red_500
4 -> R.color.md_pink_A400
2 -> R.color.md_purple_A400
9 -> R.color.md_deep_purple_A700
5 -> R.color.md_indigo_500
1 -> R.color.md_indigo_A700
6 -> R.color.md_cyan_A200
14 -> R.color.md_teal_400
15 -> R.color.md_green_500
7 -> R.color.md_yellow_A700
3 -> R.color.md_deep_orange_A400
8 -> R.color.md_deep_orange_A700
10 -> R.color.md_brown_500
12 -> R.color.md_grey_400
11 -> R.color.md_blue_grey_400
else -> R.color.md_light_green_A700
}
return context.getColorFromRes(color)
}
/* This method is not used in the drawer */
/* return null if you do not want an image in the Toolbar */
override fun getImageDrawable(context: Context): Drawable? {
return null
if (!image.isNullOrEmpty()) {
try {
if (image?.endsWith(".gif", true) == true) {
/* if you want to use GIFs as profile drawables, add
implementation "pl.droidsonroids.gif:android-gif-drawable:${versions.gifdrawable}"
*/
return GifDrawable(image ?: "")
}
else {
return RoundedBitmapDrawableFactory.create(context.resources, image ?: "")
//return Drawable.createFromPath(image ?: "") ?: throw Exception()
}
}
catch (e: Exception) {
e.printStackTrace()
}
}
return context.getDrawableFromRes(R.drawable.profile).also {
it.colorFilter = PorterDuffColorFilter(colorFromName(context, name), PorterDuff.Mode.DST_OVER)
}
}
override fun getImageHolder(context: Context): ImageHolder? {
return ImageHolder(image ?: return null)
override fun getImageHolder(context: Context): ImageHolder {
return if (!image.isNullOrEmpty()) {
try {
ImageHolder(image ?: "")
} catch (_: Exception) {
ImageHolder(R.drawable.profile, colorFromName(context, name))
}
}
else {
ImageHolder(R.drawable.profile, colorFromName(context, name))
}
}
override fun applyImageTo(imageView: ImageView) {
ImageHolder(image ?: return).applyTo(imageView)
getImageHolder(imageView.context).applyTo(imageView)
}
}

View File

@ -252,6 +252,8 @@ class MainActivity : AppCompatActivity() {
.withIcon(CommunityMaterial.Icon.cmd_alarm_bell),
DrawerPrimaryItem().withName("Lock screen")
.withDescription("aaand not visible in Mini Drawer")
.withHiddenInMiniDrawer(true)
.withIdentifier(62)
.withBadgeStyle(badgeStyle)
.withIcon(CommunityMaterial.Icon.cmd_fingerprint),
@ -278,15 +280,15 @@ class MainActivity : AppCompatActivity() {
setUnreadCount(2, 20, 30) // phil swift has 30 unreads on "Settings item"
setUnreadCount(4, 40, 1000) // mark has 99+ unreads on "Lock screen item"
setAccountHeaderBackground("/sdcard/ban.gif")
//setAccountHeaderBackground("/sdcard/ban.gif")
appendProfiles(
DrawerProfile(1, "Think Pad", "think with a pad", "/sdcard/thinkpad.gif"),
DrawerProfile(2, "Phil Swift", "I sawed this boat in half!!!", "/sdcard/phil.jpg"),
DrawerProfile(3, "The meme bay", "Visit my amazing website", "/sdcard/loader.gif"),
DrawerProfile(4, "Mark Zuckerberg", "", null),
DrawerProfile(5, "I love GDPR", "spotify:user:popjustice:playlist:5Pe51v0sHLybSEkX0m0JRf", "/sdcard/tenor2.gif"),
DrawerProfile(6, "Gandalf", "http://sax.hol.es/", "/sdcard/facepalm.gif")
DrawerProfile(1, "Think Pad", "think with a pad", null),
DrawerProfile(2, "Phil Swift", "I sawed this boat in half!!!", null),
DrawerProfile(3, "The meme bay", "Visit my amazing website", null),
DrawerProfile(4, "Mark Zuckerberg", null, null),
DrawerProfile(5, "I love GDPR", null, null),
DrawerProfile(6, "Gandalf", "http://sax.hol.es/", null)
)
addProfileSettings(

View File

@ -40,7 +40,7 @@ buildscript {
play_services : "17.0.0",
materialdialogs : "0.9.6.0",
materialdrawer : "e603091449",
materialdrawer : "62b24da031",
iconics : "4.0.1-b02",
font_cmd : "3.5.95.1-kotlin",

View File

@ -44,7 +44,7 @@ dependencies {
implementation "androidx.recyclerview:recyclerview:${versions.recyclerView}"
implementation "com.google.android.material:material:${versions.material}"
api "com.github.kuba2k2.MaterialDrawer:library:${versions.materialdrawer}"
api "com.github.kuba2k2:MaterialDrawer:${versions.materialdrawer}"
api "com.mikepenz:community-material-typeface:${versions.font_cmd}@aar"
api "com.mikepenz:iconics-core:${versions.iconics}"
implementation "com.mikepenz:itemanimators:1.1.0"

View File

@ -85,7 +85,7 @@ class NavDrawer(
.withTranslucentNavigationBar(true)
.withTranslucentNavigationBarProgrammatically(false)
.withToolbar(bottomBar)
.withDisplayBelowStatusBar(true)
.withDisplayBelowStatusBar(false)
.withActionBarDrawerToggleAnimated(true)
.withShowDrawerOnFirstLaunch(true)
.withShowDrawerUntilDraggedOpened(true)

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

View File

@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="135.5"
android:viewportHeight="135.5">
<path
android:pathData="M0,0h135.5L135.5,135.5L0,135.5z"
android:fillColor="#00000000"/>
<path
android:pathData="M41.674,55.89a28.412,26.387 85.529,1 0,52.798 0.379a28.412,26.387 85.529,1 0,-52.798 -0.379z"
android:fillColor="#b0ffffff"/>
<path
android:pathData="M121,133.2a53.3,39 0,0 1,-50.9 38.9A53.3,39 0,0 1,14.8 136.5a53.3,39 0,0 1,46 -42,53.3 39,0 0,1 59.5,32"
android:fillColor="#b0ffffff"/>
</vector>