From 95f267f70191452451ec6889b13e517a65346904 Mon Sep 17 00:00:00 2001 From: Amine Date: Tue, 6 Jun 2017 19:46:28 +0200 Subject: [PATCH] Switched primary <=> secodary items for drawer. Added source icon fetching. --- .../bou/readerforselfoss/HomeActivity.kt | 30 +++-- .../apps/amine/bou/readerforselfoss/MyApp.kt | 22 ++++ .../utils/drawer/CustomBaseViewHolder.kt | 17 +++ .../drawer/CustomUrlBasePrimaryDrawerItem.kt | 108 ++++++++++++++++++ .../drawer/CustomUrlPrimaryDrawerItem.kt | 94 +++++++++++++++ app/src/main/res/values/ids.xml | 4 + 6 files changed, 264 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomBaseViewHolder.kt create mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomUrlBasePrimaryDrawerItem.kt create mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomUrlPrimaryDrawerItem.kt create mode 100644 app/src/main/res/values/ids.xml diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index e5f2045..b6975e8 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -5,6 +5,7 @@ import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.graphics.Color +import android.graphics.drawable.Drawable import android.graphics.drawable.GradientDrawable import android.net.Uri import android.os.Bundle @@ -21,6 +22,7 @@ import android.util.DisplayMetrics import android.view.Menu import android.view.MenuItem import android.view.View +import android.widget.ImageView import android.widget.Toast import apps.amine.bou.readerforselfoss.adapters.ItemCardAdapter import apps.amine.bou.readerforselfoss.adapters.ItemListAdapter @@ -30,10 +32,12 @@ import apps.amine.bou.readerforselfoss.utils.Config import apps.amine.bou.readerforselfoss.utils.checkAndDisplayStoreApk import apps.amine.bou.readerforselfoss.utils.checkApkVersion import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper +import apps.amine.bou.readerforselfoss.utils.drawer.CustomUrlPrimaryDrawerItem import apps.amine.bou.readerforselfoss.utils.longHash import com.anupcowkur.reservoir.Reservoir import com.anupcowkur.reservoir.ReservoirGetCallback import com.anupcowkur.reservoir.ReservoirPutCallback +import com.bumptech.glide.Glide import com.crashlytics.android.answers.Answers import com.crashlytics.android.answers.InviteEvent import com.github.stkent.amplify.prompt.DefaultLayoutPromptView @@ -45,11 +49,14 @@ import com.google.firebase.remoteconfig.FirebaseRemoteConfig import com.google.gson.reflect.TypeToken import com.mikepenz.aboutlibraries.Libs import com.mikepenz.aboutlibraries.LibsBuilder +import com.mikepenz.iconics.IconicsDrawable import com.mikepenz.materialdrawer.Drawer import com.mikepenz.materialdrawer.DrawerBuilder import com.mikepenz.materialdrawer.model.DividerDrawerItem -import com.mikepenz.materialdrawer.model.PrimaryDrawerItem import com.mikepenz.materialdrawer.model.SecondaryDrawerItem +import com.mikepenz.materialdrawer.model.PrimaryDrawerItem +import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader +import com.mikepenz.materialdrawer.util.DrawerImageLoader import com.roughike.bottombar.BottomBar import com.roughike.bottombar.BottomBarTab import retrofit2.Call @@ -187,7 +194,7 @@ class HomeActivity : AppCompatActivity() { fun handleTags(maybeTags: List?) { if (maybeTags == null) { if (loadedFromCache) - drawer!!.addItem(PrimaryDrawerItem().withName(getString(R.string.drawer_error_loading_tags)).withSelectable(false)) + drawer!!.addItem(SecondaryDrawerItem().withName(getString(R.string.drawer_error_loading_tags)).withSelectable(false)) } else { for (tag in maybeTags) { @@ -197,7 +204,7 @@ class HomeActivity : AppCompatActivity() { gd.setSize(30, 30) gd.cornerRadius = 30F drawer!!.addItem( - SecondaryDrawerItem() + PrimaryDrawerItem() .withName(tag.tag) .withIdentifier(longHash(tag.tag)) .withIcon(gd) @@ -214,14 +221,15 @@ class HomeActivity : AppCompatActivity() { fun handleSources(maybeSources: List?) { if (maybeSources == null) { if (loadedFromCache) - drawer!!.addItem(PrimaryDrawerItem().withName(getString(R.string.drawer_error_loading_sources)).withSelectable(false)) + drawer!!.addItem(SecondaryDrawerItem().withName(getString(R.string.drawer_error_loading_sources)).withSelectable(false)) } else for (tag in maybeSources) drawer!!.addItem( - SecondaryDrawerItem() + CustomUrlPrimaryDrawerItem() .withName(tag.title) .withIdentifier(tag.id.toLong()) + .withIcon(tag.getIcon(this@HomeActivity)) .withOnDrawerItemClickListener { _, _, _ -> getElementsAccordingToTab(maybeSourceFilter = tag) false @@ -233,7 +241,7 @@ class HomeActivity : AppCompatActivity() { drawer!!.removeAllItems() if (maybeDrawerData != null) { drawer!!.addItem( - PrimaryDrawerItem() + SecondaryDrawerItem() .withName(getString(R.string.drawer_item_filters)) .withSelectable(false) .withIdentifier(DRAWER_ID_FILTERS) @@ -244,10 +252,10 @@ class HomeActivity : AppCompatActivity() { } ) drawer!!.addItem(DividerDrawerItem()) - drawer!!.addItem(PrimaryDrawerItem().withName(getString(R.string.drawer_item_tags)).withIdentifier(DRAWER_ID_TAGS).withSelectable(false)) + drawer!!.addItem(SecondaryDrawerItem().withName(getString(R.string.drawer_item_tags)).withIdentifier(DRAWER_ID_TAGS).withSelectable(false)) handleTags(maybeDrawerData.tags) drawer!!.addItem( - PrimaryDrawerItem() + SecondaryDrawerItem() .withName(getString(R.string.drawer_item_sources)) .withIdentifier(DRAWER_ID_TAGS) .withBadge(getString(R.string.drawer_action_edit)) @@ -271,8 +279,8 @@ class HomeActivity : AppCompatActivity() { }) } else { if (!loadedFromCache) { - drawer!!.addItem(SecondaryDrawerItem().withName(getString(R.string.no_tags_loaded)).withIdentifier(DRAWER_ID_TAGS).withSelectable(false)) - drawer!!.addItem(SecondaryDrawerItem().withName(getString(R.string.no_sources_loaded)).withIdentifier(DRAWER_ID_SOURCES).withSelectable(false)) + drawer!!.addItem(PrimaryDrawerItem().withName(getString(R.string.no_tags_loaded)).withIdentifier(DRAWER_ID_TAGS).withSelectable(false)) + drawer!!.addItem(PrimaryDrawerItem().withName(getString(R.string.no_sources_loaded)).withIdentifier(DRAWER_ID_SOURCES).withSelectable(false)) } } @@ -311,7 +319,7 @@ class HomeActivity : AppCompatActivity() { }) } - drawer!!.addItem(SecondaryDrawerItem().withName(getString(R.string.drawer_loading)).withSelectable(false)) + drawer!!.addItem(PrimaryDrawerItem().withName(getString(R.string.drawer_loading)).withSelectable(false)) val resultType = object : TypeToken() {}.type Reservoir.getAsync("drawerData", resultType, object: ReservoirGetCallback { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt index 62a8e6e..182d904 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt @@ -1,10 +1,18 @@ package apps.amine.bou.readerforselfoss +import android.content.Context +import android.graphics.drawable.Drawable +import android.net.Uri import android.support.multidex.MultiDexApplication +import android.widget.ImageView import com.crashlytics.android.Crashlytics import com.github.stkent.amplify.tracking.Amplify import io.fabric.sdk.android.Fabric import com.anupcowkur.reservoir.Reservoir +import com.bumptech.glide.Glide +import com.mikepenz.iconics.IconicsDrawable +import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader +import com.mikepenz.materialdrawer.util.DrawerImageLoader import java.io.IOException @@ -24,5 +32,19 @@ class MyApp : MultiDexApplication() { } catch (e: IOException) { //failure } + + DrawerImageLoader.init(object : AbstractDrawerImageLoader() { + override fun set(imageView: ImageView?, uri: Uri?, placeholder: Drawable?, tag: String?) { + Glide.with(imageView?.context).load(uri).placeholder(placeholder).into(imageView) + } + + override fun cancel(imageView: ImageView?) { + Glide.clear(imageView) + } + + override fun placeholder(ctx: Context?, tag: String?): Drawable { + return applicationContext.resources.getDrawable(R.mipmap.ic_launcher) + } + }) } } \ No newline at end of file diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomBaseViewHolder.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomBaseViewHolder.kt new file mode 100644 index 0000000..15d6bff --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomBaseViewHolder.kt @@ -0,0 +1,17 @@ +/* From https://github.com/mikepenz/MaterialDrawer/blob/develop/app/src/main/java/com/mikepenz/materialdrawer/app/drawerItems/CustomBaseViewHolder.java */ +package apps.amine.bou.readerforselfoss.utils.drawer + + +import android.support.v7.widget.RecyclerView +import android.view.View +import android.widget.ImageView +import android.widget.TextView + +import apps.amine.bou.readerforselfoss.R + + +open class CustomBaseViewHolder(var view: View) : RecyclerView.ViewHolder(view) { + var icon: ImageView = view.findViewById(R.id.material_drawer_icon) as ImageView + var name: TextView = view.findViewById(R.id.material_drawer_name) as TextView + var description: TextView = view.findViewById(R.id.material_drawer_description) as TextView +} diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomUrlBasePrimaryDrawerItem.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomUrlBasePrimaryDrawerItem.kt new file mode 100644 index 0000000..c10d57b --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomUrlBasePrimaryDrawerItem.kt @@ -0,0 +1,108 @@ +/* From https://github.com/mikepenz/MaterialDrawer/blob/develop/app/src/main/java/com/mikepenz/materialdrawer/app/drawerItems/CustomUrlBasePrimaryDrawerItem.java */ +package apps.amine.bou.readerforselfoss.utils.drawer + + +import android.content.Context +import android.net.Uri +import android.support.annotation.ColorInt +import android.support.annotation.ColorRes +import android.support.annotation.StringRes +import android.support.v7.widget.RecyclerView + +import com.mikepenz.materialdrawer.holder.ColorHolder +import com.mikepenz.materialdrawer.holder.ImageHolder +import com.mikepenz.materialdrawer.holder.StringHolder +import com.mikepenz.materialdrawer.model.BaseDrawerItem +import com.mikepenz.materialdrawer.util.DrawerImageLoader +import com.mikepenz.materialdrawer.util.DrawerUIUtils +import com.mikepenz.materialize.util.UIUtils + + +abstract class CustomUrlBasePrimaryDrawerItem : BaseDrawerItem() { + fun withIcon(url: String): T { + this.icon = ImageHolder(url) + return this as T + } + + fun withIcon(uri: Uri): T { + this.icon = ImageHolder(uri) + return this as T + } + + var description: StringHolder? = null + private set + var descriptionTextColor: ColorHolder? = null + private set + + fun withDescription(description: String): T { + this.description = StringHolder(description) + return this as T + } + + fun withDescription(@StringRes descriptionRes: Int): T { + this.description = StringHolder(descriptionRes) + return this as T + } + + fun withDescriptionTextColor(@ColorInt color: Int): T { + this.descriptionTextColor = ColorHolder.fromColor(color) + return this as T + } + + fun withDescriptionTextColorRes(@ColorRes colorRes: Int): T { + this.descriptionTextColor = ColorHolder.fromColorRes(colorRes) + return this as T + } + + /** + * a helper method to have the logic for all secondaryDrawerItems only once + + * @param viewHolder + */ + protected fun bindViewHelper(viewHolder: CustomBaseViewHolder) { + val ctx = viewHolder.itemView.context + + //set the identifier from the drawerItem here. It can be used to run tests + viewHolder.itemView.id = hashCode() + + //set the item selected if it is + viewHolder.itemView.isSelected = isSelected + + //get the correct color for the background + val selectedColor = getSelectedColor(ctx) + //get the correct color for the text + val color = getColor(ctx) + val selectedTextColor = getSelectedTextColor(ctx) + //get the correct color for the icon + val iconColor = getIconColor(ctx) + val selectedIconColor = getSelectedIconColor(ctx) + + //set the background for the item + UIUtils.setBackground(viewHolder.view, UIUtils.getSelectableBackground(ctx, selectedColor, true)) + //set the text for the name + StringHolder.applyTo(this.getName(), viewHolder.name) + //set the text for the description or hide + StringHolder.applyToOrHide(this.description, viewHolder.description) + + //set the colors for textViews + viewHolder.name.setTextColor(getTextColorStateList(color, selectedTextColor)) + //set the description text color + ColorHolder.applyToOr(descriptionTextColor, + viewHolder.description, getTextColorStateList(color, selectedTextColor)) + + //define the typeface for our textViews + if (getTypeface() != null) { + viewHolder.name.typeface = getTypeface() + viewHolder.description.typeface = getTypeface() + } + + //we make sure we reset the image first before setting the new one in case there is an empty one + DrawerImageLoader.getInstance().cancelImage(viewHolder.icon) + viewHolder.icon.setImageBitmap(null) + //get the drawables for our icon and set it + ImageHolder.applyTo(icon, viewHolder.icon, "customUrlItem") + + //for android API 17 --> Padding not applied via xml + DrawerUIUtils.setDrawerVerticalPadding(viewHolder.view) + } +} diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomUrlPrimaryDrawerItem.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomUrlPrimaryDrawerItem.kt new file mode 100644 index 0000000..9a56d94 --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/drawer/CustomUrlPrimaryDrawerItem.kt @@ -0,0 +1,94 @@ +/* From https://github.com/mikepenz/MaterialDrawer/blob/develop/app/src/main/java/com/mikepenz/materialdrawer/app/drawerItems/CustomUrlPrimaryDrawerItem.java */ +package apps.amine.bou.readerforselfoss.utils.drawer + + +import android.content.Context +import android.support.annotation.LayoutRes +import android.support.annotation.StringRes +import android.view.View +import android.widget.TextView + +import apps.amine.bou.readerforselfoss.R +import com.mikepenz.materialdrawer.holder.BadgeStyle +import com.mikepenz.materialdrawer.holder.StringHolder +import com.mikepenz.materialdrawer.model.interfaces.ColorfulBadgeable + + +class CustomUrlPrimaryDrawerItem : CustomUrlBasePrimaryDrawerItem(), ColorfulBadgeable { + protected var mBadge: StringHolder = StringHolder("") + protected var mBadgeStyle = BadgeStyle() + + override fun withBadge(badge: StringHolder): CustomUrlPrimaryDrawerItem { + this.mBadge = badge + return this + } + + override fun withBadge(badge: String): CustomUrlPrimaryDrawerItem { + this.mBadge = StringHolder(badge) + return this + } + + override fun withBadge(@StringRes badgeRes: Int): CustomUrlPrimaryDrawerItem { + this.mBadge = StringHolder(badgeRes) + return this + } + + override fun withBadgeStyle(badgeStyle: BadgeStyle): CustomUrlPrimaryDrawerItem { + this.mBadgeStyle = badgeStyle + return this + } + + override fun getBadge(): StringHolder { + return mBadge + } + + override fun getBadgeStyle(): BadgeStyle { + return mBadgeStyle + } + + override fun getType(): Int { + return R.id.material_drawer_item_custom_url_item + } + + @LayoutRes + override fun getLayoutRes(): Int { + return R.layout.material_drawer_item_primary + } + + override fun bindView(viewHolder: ViewHolder, payloads: List<*>?) { + super.bindView(viewHolder, payloads) + + val ctx = viewHolder.itemView.context + + //bind the basic view parts + bindViewHelper(viewHolder) + + //set the text for the badge or hide + val badgeVisible = StringHolder.applyToOrHide(mBadge, viewHolder.badge) + //style the badge if it is visible + if (badgeVisible) { + mBadgeStyle.style(viewHolder.badge, getTextColorStateList(getColor(ctx), getSelectedTextColor(ctx))) + viewHolder.badgeContainer.visibility = View.VISIBLE + } else { + viewHolder.badgeContainer.visibility = View.GONE + } + + //define the typeface for our textViews + if (getTypeface() != null) { + viewHolder.badge.typeface = getTypeface() + } + + //call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required) + onPostBindView(this, viewHolder.itemView) + } + + override fun getViewHolder(v: View): ViewHolder { + return ViewHolder(v) + } + + class ViewHolder(view: View) : CustomBaseViewHolder(view) { + val badgeContainer: View = view.findViewById(R.id.material_drawer_badge_container) + val badge: TextView = view.findViewById(R.id.material_drawer_badge) as TextView + + } +} \ No newline at end of file diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml new file mode 100644 index 0000000..201f9a0 --- /dev/null +++ b/app/src/main/res/values/ids.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file