Skip to content

Commit

Permalink
- add convenient extension properties to all interfaces to allow prop…
Browse files Browse the repository at this point in the history
…er kotlin syntax for specifying the values

- move additional with methods into interfaces
- cleanup
- further use kotlin usage in sample
  • Loading branch information
mikepenz committed Mar 8, 2020
1 parent 91bd285 commit 7d33603
Show file tree
Hide file tree
Showing 25 changed files with 369 additions and 246 deletions.
28 changes: 14 additions & 14 deletions app/src/main/java/com/mikepenz/materialdrawer/app/DrawerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import com.mikepenz.materialdrawer.iconics.iconicsIcon
import com.mikepenz.materialdrawer.iconics.withIcon
import com.mikepenz.materialdrawer.model.*
import com.mikepenz.materialdrawer.model.interfaces.*
import com.mikepenz.materialdrawer.model.utils.descriptionRes
import com.mikepenz.materialdrawer.model.utils.nameRes
import com.mikepenz.materialdrawer.model.utils.nameText
import com.mikepenz.materialdrawer.util.addItems
import com.mikepenz.materialdrawer.util.updateBadge
import com.mikepenz.materialdrawer.widget.AccountHeaderView
Expand All @@ -47,15 +44,18 @@ class DrawerActivity : AppCompatActivity() {

// Create a few sample profile
// NOTE you have to define the loader logic too. See the CustomApplication for more details
val profile = ProfileDrawerItem().withName("Mike Penz").withEmail("mikepenz@gmail.com").withIcon("https://avatars3.githubusercontent.com/u/1476232?v=3&s=460").withIdentifier(100)
val profile2 = ProfileDrawerItem().withName("Demo User").withEmail("demo@github.com").withIcon("https://avatars2.githubusercontent.com/u/3597376?v=3&s=460").withIdentifier(101)
val profile3 = ProfileDrawerItem().withName("Max Muster").withEmail("max.mustermann@gmail.com").withIcon(R.drawable.profile2).withIdentifier(102)
val profile4 = ProfileDrawerItem().withName("Felix House").withEmail("felix.house@gmail.com").withIcon(R.drawable.profile3).withIdentifier(103)
val profile5 = ProfileDrawerItem().withName("Mr. X").withEmail("mister.x.super@gmail.com").withIcon(R.drawable.profile4).withIdentifier(104)
val profile6 = ProfileDrawerItem().withName("Batman").withEmail("batman@gmail.com").withIcon(R.drawable.profile5).withIdentifier(105).withBadge("123").withBadgeStyle(BadgeStyle().apply {
textColor = ColorHolder.fromColor(Color.BLACK)
color = ColorHolder.fromColor(Color.WHITE)
})
val profile = ProfileDrawerItem().apply { nameText = "Mike Penz"; descriptionText = "mikepenz@gmail.com"; iconUrl = "https://avatars3.githubusercontent.com/u/1476232?v=3&s=460"; identifier = 100 }
val profile2 = ProfileDrawerItem().apply { nameText = "Demo User"; descriptionText = "demo@github.com"; iconUrl = "https://avatars2.githubusercontent.com/u/3597376?v=3&s=460"; identifier = 101 }
val profile3 = ProfileDrawerItem().apply { nameText = "Max Muster"; descriptionText = "max.mustermann@gmail.com"; iconRes = R.drawable.profile2; identifier = 102 }
val profile4 = ProfileDrawerItem().apply { nameText = "Felix House"; descriptionText = "felix.house@gmail.com"; iconRes = R.drawable.profile3; identifier = 103 }
val profile5 = ProfileDrawerItem().apply { nameText = "Mr. X"; descriptionText = "mister.x.super@gmail.com"; iconRes = R.drawable.profile4; identifier = 104 }
val profile6 = ProfileDrawerItem().apply {
nameText = "Batman"; descriptionText = "batman@gmail.com"; iconRes = R.drawable.profile5; identifier = 105; badgeText = "123";
badgeStyle = BadgeStyle().apply {
textColor = ColorHolder.fromColor(Color.BLACK)
color = ColorHolder.fromColor(Color.WHITE)
}
}

// Create the AccountHeader
headerView = AccountHeaderView(this).apply {
Expand All @@ -68,8 +68,8 @@ class DrawerActivity : AppCompatActivity() {
profile5,
profile6,
//don't ask but google uses 14dp for the add account icon in gmail but 20dp for the normal icons (like manage account)
ProfileSettingDrawerItem().withName("Add Account").withDescription("Add new GitHub Account").withIcon(IconicsDrawable(context, GoogleMaterial.Icon.gmd_add).apply { actionBar(); paddingDp = 5 }.mutate()).withIconTinted(true).withIdentifier(PROFILE_SETTING.toLong()),
ProfileSettingDrawerItem().withName("Manage Account").withIcon(GoogleMaterial.Icon.gmd_settings).withIdentifier(100001)
ProfileSettingDrawerItem().apply { nameText = "Add Account"; descriptionText = "Add new GitHub Account"; iconDrawable = IconicsDrawable(context, GoogleMaterial.Icon.gmd_add).apply { actionBar(); paddingDp = 5 }.mutate(); isIconTinted = true; identifier = PROFILE_SETTING.toLong() },
ProfileSettingDrawerItem().apply { nameText = "Manage Account"; iconicsIcon = GoogleMaterial.Icon.gmd_settings; identifier = 100001 }
)
onAccountHeaderListener = { view, profile, current ->
//sample usage of the onProfileChanged listener
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mikepenz.materialdrawer.app.drawerItems

import android.content.res.ColorStateList
import android.view.View
import androidx.annotation.LayoutRes
import androidx.core.view.ViewCompat
Expand All @@ -19,17 +20,10 @@ class AccountDividerDrawerItem : AbstractDrawerItem<AccountDividerDrawerItem, Ac
@LayoutRes
get() = com.mikepenz.materialdrawer.R.layout.material_drawer_item_divider

override var name: StringHolder?
get() = null
set(value) {}

override var description: StringHolder?
get() = null
set(value) {}

override var icon: ImageHolder?
get() = null
set(value) {}
override var name: StringHolder? = null
override var description: StringHolder? = null
override var icon: ImageHolder? = null
override var iconColor: ColorStateList? = null

override fun bindView(holder: ViewHolder, payloads: List<Any>) {
super.bindView(holder, payloads)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import androidx.recyclerview.widget.RecyclerView
import com.mikepenz.materialdrawer.app.R
import com.mikepenz.materialdrawer.holder.ImageHolder
import com.mikepenz.materialdrawer.model.AbstractDrawerItem
import com.mikepenz.materialdrawer.model.interfaces.Iconable
import com.mikepenz.materialdrawer.model.interfaces.SelectIconable
import com.mikepenz.materialdrawer.util.createDrawerItemColorStateList

/**
* Created by mikepenz on 03.02.15.
*/
class IconDrawerItem : AbstractDrawerItem<IconDrawerItem, IconDrawerItem.ViewHolder>() {
var icon: ImageHolder? = null
var selectedIcon: ImageHolder? = null
var isIconTinted = false
class IconDrawerItem : AbstractDrawerItem<IconDrawerItem, IconDrawerItem.ViewHolder>(), Iconable, SelectIconable {
override var icon: ImageHolder? = null
override var iconColor: ColorStateList? = null
override var selectedIcon: ImageHolder? = null
override var isIconTinted = false

override val type: Int
get() = R.id.material_drawer_item_icon_only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package com.mikepenz.materialdrawer.iconics
import android.os.Build
import com.mikepenz.iconics.typeface.IIcon
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.Iconable
import com.mikepenz.materialdrawer.model.interfaces.withIconTintingEnabled

var <T : BaseDrawerItem<*, *>> T.iconicsIcon: IIcon
@Deprecated(level = DeprecationLevel.ERROR, message = "Not readable")
Expand Down Expand Up @@ -33,41 +32,15 @@ fun <T : BaseDrawerItem<*, *>> T.withIcon(icon: IIcon): T {
return this
}

var <T : ProfileDrawerItem> T.iconicsIcon: IIcon
var <T : Iconable> T.iconicsIcon: IIcon
@Deprecated(level = DeprecationLevel.ERROR, message = "Not readable")
get() = throw UnsupportedOperationException("Please use the direct property")
set(value) {
this.icon = IconicsImageHolder(value)
}

@Deprecated("Please consider to replace with the actual property setter")
fun ProfileDrawerItem.withIcon(icon: IIcon): ProfileDrawerItem {
this.icon = IconicsImageHolder(icon)
return this
}

var <T : ProfileSettingDrawerItem> T.iconicsIcon: IIcon
@Deprecated(level = DeprecationLevel.ERROR, message = "Not readable")
get() = throw UnsupportedOperationException("Please use the direct property")
set(value) {
this.icon = IconicsImageHolder(value)
}

@Deprecated("Please consider to replace with the actual property setter")
fun ProfileSettingDrawerItem.withIcon(icon: IIcon): ProfileSettingDrawerItem {
this.icon = IconicsImageHolder(icon)
return this
}

var <T : MiniProfileDrawerItem> T.iconicsIcon: IIcon
@Deprecated(level = DeprecationLevel.ERROR, message = "Not readable")
get() = throw UnsupportedOperationException("Please use the direct property")
set(value) {
this.icon = IconicsImageHolder(value)
}

@Deprecated("Please consider to replace with the actual property setter")
fun MiniProfileDrawerItem.withIcon(icon: IIcon): MiniProfileDrawerItem {
fun <T : Iconable> T.withIcon(icon: IIcon): T {
this.icon = IconicsImageHolder(icon)
return this
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.shape.ShapeAppearanceModel
import com.mikepenz.fastadapter.IItemVHFactory
Expand All @@ -17,38 +15,39 @@ import com.mikepenz.fastadapter.ISubItem
import com.mikepenz.materialdrawer.R
import com.mikepenz.materialdrawer.holder.ColorHolder
import com.mikepenz.materialdrawer.interfaces.OnPostBindViewListener
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem
import com.mikepenz.materialdrawer.model.interfaces.Selectable
import com.mikepenz.materialdrawer.model.interfaces.Tagable
import com.mikepenz.materialdrawer.model.interfaces.Typefaceable
import com.mikepenz.materialdrawer.model.interfaces.*
import com.mikepenz.materialdrawer.util.getPrimaryDrawerTextColor
import com.mikepenz.materialdrawer.util.getSelectedColor

/**
* The base abstract [IDrawerItem] implementation describing a drawerItem with the general functionality
*/
abstract class AbstractDrawerItem<T, VH : RecyclerView.ViewHolder> : IDrawerItem<VH>, Selectable, Tagable, Typefaceable {
abstract class AbstractDrawerItem<T, VH : RecyclerView.ViewHolder> : IDrawerItem<VH>, Selectable, SelectableColor, Tagable, Typefaceable {
// the identifier for this item
override var identifier: Long = -1

// the tag for this item
override var tag: Any? = null

// defines if this item is enabled
override var isEnabled = true

/** The factory to use for creating this item, this does not have to be provided if the IItemFactory is implemented by this item too */
override val factory: IItemVHFactory<VH>? = null

// defines if the item is selected
override var isSelected = false

// defines if this item is selectable
override var isSelectable = true

// defines if the item's background' change should be animated when it is (de)selected
var isSelectedBackgroundAnimated = true

// defines the content descripton of items
var contentDescription: String? = null

var selectedColor: ColorHolder? = null
var textColor: ColorStateList? = null
var iconColor: ColorStateList? = null
override var selectedColor: ColorHolder? = null
override var typeface: Typeface? = null

open var onDrawerItemClickListener: ((v: View?, item: IDrawerItem<*>, position: Int) -> Boolean)? = null
Expand All @@ -58,6 +57,7 @@ abstract class AbstractDrawerItem<T, VH : RecyclerView.ViewHolder> : IDrawerItem

// the parent of this item
override var parent: IParentItem<*>? = null

// the subItems to expand for this item
private var _subItems: MutableList<ISubItem<*>> = mutableListOf()
override var subItems: MutableList<ISubItem<*>>
Expand Down Expand Up @@ -89,18 +89,6 @@ abstract class AbstractDrawerItem<T, VH : RecyclerView.ViewHolder> : IDrawerItem
return this as T
}

@Deprecated("Please consider to replace with the actual property setter")
fun withSelectedColor(@ColorInt selectedColor: Int): T {
this.selectedColor = ColorHolder.fromColor(selectedColor)
return this as T
}

@Deprecated("Please consider to replace with the actual property setter")
fun withSelectedColorRes(@ColorRes selectedColorRes: Int): T {
this.selectedColor = ColorHolder.fromColorRes(selectedColorRes)
return this as T
}

/**
* set if this item is selectable
*
Expand Down Expand Up @@ -188,12 +176,6 @@ abstract class AbstractDrawerItem<T, VH : RecyclerView.ViewHolder> : IDrawerItem
return this as T
}

@Deprecated("Please consider to replace with the actual property setter")
fun withIconColor(iconColor: ColorStateList): T {
this.iconColor = iconColor
return this as T
}

/**
* generates a view by the defined LayoutRes
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.mikepenz.materialdrawer.model

import android.content.res.ColorStateList
import androidx.annotation.StringRes
import com.mikepenz.materialdrawer.holder.ImageHolder
import com.mikepenz.materialdrawer.holder.StringHolder
import com.mikepenz.materialdrawer.model.interfaces.Describable
import com.mikepenz.materialdrawer.model.interfaces.DescribableColor
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem
import com.mikepenz.materialdrawer.util.getSecondaryDrawerTextColor
import com.mikepenz.materialdrawer.util.setDrawerVerticalPadding
Expand All @@ -12,21 +13,9 @@ import com.mikepenz.materialdrawer.util.themeDrawerItem
/**
* An abstract [IDrawerItem] implementation describing a drawerItem with support for a description
*/
abstract class BaseDescribeableDrawerItem<T, VH : BaseViewHolder> : BaseDrawerItem<T, VH>() {
var description: StringHolder? = null
var descriptionTextColor: ColorStateList? = null

@Deprecated("Please consider to replace with the actual property setter")
fun withDescription(description: String): T {
this.description = StringHolder(description)
return this as T
}

@Deprecated("Please consider to replace with the actual property setter")
fun withDescription(@StringRes descriptionRes: Int): T {
this.description = StringHolder(descriptionRes)
return this as T
}
abstract class BaseDescribeableDrawerItem<T, VH : BaseViewHolder> : BaseDrawerItem<T, VH>(), Describable, DescribableColor {
override var description: StringHolder? = null
override var descriptionTextColor: ColorStateList? = null

/**
* a helper method to have the logic for all secondaryDrawerItems only once
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,26 @@ package com.mikepenz.materialdrawer.model

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import androidx.annotation.DrawableRes
import androidx.recyclerview.widget.RecyclerView
import com.mikepenz.materialdrawer.holder.ImageHolder
import com.mikepenz.materialdrawer.holder.StringHolder
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem
import com.mikepenz.materialdrawer.model.interfaces.Iconable
import com.mikepenz.materialdrawer.model.interfaces.Nameable
import com.mikepenz.materialdrawer.model.interfaces.Tagable
import com.mikepenz.materialdrawer.model.interfaces.*
import com.mikepenz.materialdrawer.util.getPrimaryDrawerIconColor

/**
* An abstract [IDrawerItem] implementation providing the base properties with their default value
*/
abstract class BaseDrawerItem<T, VH : RecyclerView.ViewHolder> : AbstractDrawerItem<T, VH>(), Nameable, Iconable, Tagable {
abstract class BaseDrawerItem<T, VH : RecyclerView.ViewHolder> : AbstractDrawerItem<T, VH>(), Nameable, NameableColor, Iconable, SelectIconable, Tagable {
override var icon: ImageHolder? = null
var selectedIcon: ImageHolder? = null
override var iconColor: ColorStateList? = null
override var selectedIcon: ImageHolder? = null
override var name: StringHolder? = null
var isIconTinted = false
override var textColor: ColorStateList? = null
override var isIconTinted = false

var level = 1
protected set

@Deprecated("Please consider to replace with the actual property setter")
fun withSelectedIcon(selectedIcon: Drawable): T {
this.selectedIcon = ImageHolder(selectedIcon)
return this as T
}

@Deprecated("Please consider to replace with the actual property setter")
fun withSelectedIcon(@DrawableRes selectedIconRes: Int): T {
this.selectedIcon = ImageHolder(selectedIconRes)
return this as T
}

/**
* will tint the icon with the default (or set) colors
* (default and selected state)
*
* @param iconTintingEnabled
* @return
*/
@Deprecated("Please consider to replace with the actual property setter")
fun withIconTintingEnabled(iconTintingEnabled: Boolean): T {
this.isIconTinted = iconTintingEnabled
return this as T
}

@Deprecated("Please consider to replace with the actual property setter")
fun withLevel(level: Int): T {
this.level = level
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mikepenz.materialdrawer.model

import android.content.res.ColorStateList
import android.view.View
import android.widget.ImageView
import androidx.annotation.DimenRes
Expand All @@ -16,6 +17,7 @@ import com.mikepenz.materialdrawer.model.interfaces.IProfile
*/
open class MiniProfileDrawerItem : AbstractDrawerItem<MiniProfileDrawerItem, MiniProfileDrawerItem.ViewHolder>, IProfile {
override var icon: ImageHolder? = null
override var iconColor: ColorStateList? = null // not supported for this item
override var name: StringHolder? = null
override var description: StringHolder? = null
var customHeight: DimenHolder? = null
Expand Down
Loading

0 comments on commit 7d33603

Please sign in to comment.