From d684f323b8a3caf3755c6ee318e42a22309e40b7 Mon Sep 17 00:00:00 2001 From: Amine Date: Sun, 18 Jun 2017 17:21:09 +0200 Subject: [PATCH] Cleaning. --- .../adapters/ItemCardAdapter.kt | 29 +++------------- .../adapters/ItemListAdapter.kt | 33 +++---------------- .../adapters/SourcesListAdapter.kt | 19 ++--------- .../bou/readerforselfoss/utils/GlideUtils.kt | 24 ++++++++++++++ .../bou/readerforselfoss/utils/ItemsUtils.kt | 22 +++++++++++++ 5 files changed, 56 insertions(+), 71 deletions(-) create mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/utils/GlideUtils.kt diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt index afc6263..9e04b62 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt @@ -60,30 +60,16 @@ class ItemCardAdapter(private val app: Activity, holder.saveBtn.isLiked = itm.starred holder.title.text = Html.fromHtml(itm.title) - var sourceAndDate = itm.sourcetitle - val d: Long - try { - d = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(itm.datetime).time - sourceAndDate += " " + DateUtils.getRelativeTimeSpanString( - d, - Date().time, - DateUtils.MINUTE_IN_MILLIS, - DateUtils.FORMAT_ABBREV_RELATIVE - ) - } catch (e: ParseException) { - e.printStackTrace() - } - - holder.sourceTitleAndDate.text = sourceAndDate + holder.sourceTitleAndDate.text = itm.sourceAndDateText() if (itm.getThumbnail(c).isEmpty()) { Glide.clear(holder.itemImage) holder.itemImage.setImageDrawable(null) } else { if (fullHeightCards) { - Glide.with(c).load(itm.getThumbnail(c)).asBitmap().fitCenter().into(holder.itemImage) + c.bitmapFitCenter(itm.getThumbnail(c), holder.itemImage) } else { - Glide.with(c).load(itm.getThumbnail(c)).asBitmap().centerCrop().into(holder.itemImage) + c.bitmapCenterCrop(itm.getThumbnail(c), holder.itemImage) } } @@ -98,14 +84,7 @@ class ItemCardAdapter(private val app: Activity, .build(itm.sourcetitle.toTextDrawableString(), color) holder.sourceImage.setImageDrawable(drawable) } else { - - Glide.with(c).load(itm.getIcon(c)).asBitmap().centerCrop().into(object : BitmapImageViewTarget(holder.sourceImage) { - override fun setResource(resource: Bitmap) { - val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(c.resources, resource) - circularBitmapDrawable.isCircular = true - fHolder.sourceImage.setImageDrawable(circularBitmapDrawable) - } - }) + c.circularBitmapDrawable(itm.getIcon(c), holder.sourceImage) } holder.saveBtn.isLiked = itm.starred diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt index df88509..62a99de 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt @@ -36,11 +36,8 @@ import apps.amine.bou.readerforselfoss.R import apps.amine.bou.readerforselfoss.api.selfoss.Item import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse -import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent +import apps.amine.bou.readerforselfoss.utils.* import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper -import apps.amine.bou.readerforselfoss.utils.openInBrowser -import apps.amine.bou.readerforselfoss.utils.openItemUrl -import apps.amine.bou.readerforselfoss.utils.shareLink class ItemListAdapter(private val app: Activity, @@ -66,21 +63,7 @@ class ItemListAdapter(private val app: Activity, holder.saveBtn.isLiked = itm.starred holder.title.text = Html.fromHtml(itm.title) - var sourceAndDate = itm.sourcetitle - val d: Long - try { - d = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(itm.datetime).time - sourceAndDate += " " + DateUtils.getRelativeTimeSpanString( - d, - Date().time, - DateUtils.MINUTE_IN_MILLIS, - DateUtils.FORMAT_ABBREV_RELATIVE - ) - } catch (e: ParseException) { - e.printStackTrace() - } - - holder.sourceTitleAndDate.text = sourceAndDate + holder.sourceTitleAndDate.text = itm.sourceAndDateText() if (itm.getThumbnail(c).isEmpty()) { val sizeInInt = 46 @@ -111,18 +94,10 @@ class ItemListAdapter(private val app: Activity, val drawable = builder.build(textDrawable.toString(), color) holder.sourceImage.setImageDrawable(drawable) } else { - - val fHolder = holder - Glide.with(c).load(itm.getIcon(c)).asBitmap().centerCrop().into(object : BitmapImageViewTarget(holder.sourceImage) { - override fun setResource(resource: Bitmap) { - val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(c.resources, resource) - circularBitmapDrawable.isCircular = true - fHolder.sourceImage.setImageDrawable(circularBitmapDrawable) - } - }) + c.circularBitmapDrawable(itm.getIcon(c), holder.sourceImage) } } else { - Glide.with(c).load(itm.getThumbnail(c)).asBitmap().centerCrop().into(holder.sourceImage) + c.bitmapCenterCrop(itm.getThumbnail(c), holder.sourceImage) } if (bars[position]) holder.actionBar.visibility = View.VISIBLE else holder.actionBar.visibility = View.GONE diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt index 27f50b6..62f20d5 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/SourcesListAdapter.kt @@ -2,9 +2,7 @@ package apps.amine.bou.readerforselfoss.adapters import android.app.Activity import android.content.Context -import android.graphics.Bitmap import android.support.constraint.ConstraintLayout -import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.ViewGroup @@ -15,8 +13,6 @@ import android.widget.Toast import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator -import com.bumptech.glide.Glide -import com.bumptech.glide.request.target.BitmapImageViewTarget import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -25,6 +21,7 @@ import apps.amine.bou.readerforselfoss.R import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.Sources import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse +import apps.amine.bou.readerforselfoss.utils.circularBitmapDrawable import apps.amine.bou.readerforselfoss.utils.toTextDrawableString @@ -42,7 +39,6 @@ class SourcesListAdapter(private val app: Activity, override fun onBindViewHolder(holder: ViewHolder, position: Int) { val itm = items[position] - val fHolder = holder if (itm.getIcon(c).isEmpty()) { val color = generator.getColor(itm.title) @@ -53,18 +49,7 @@ class SourcesListAdapter(private val app: Activity, .build(itm.title.toTextDrawableString(), color) holder.sourceImage.setImageDrawable(drawable) } else { - Glide - .with(c) - .load(itm.getIcon(c)) - .asBitmap() - .centerCrop() - .into(object : BitmapImageViewTarget(holder.sourceImage) { - override fun setResource(resource: Bitmap) { - val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(c.resources, resource) - circularBitmapDrawable.isCircular = true - fHolder.sourceImage.setImageDrawable(circularBitmapDrawable) - } - }) + c.circularBitmapDrawable(itm.getIcon(c), holder.sourceImage) } holder.sourceTitle.text = itm.title diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/GlideUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/GlideUtils.kt new file mode 100644 index 0000000..28324af --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/GlideUtils.kt @@ -0,0 +1,24 @@ +package apps.amine.bou.readerforselfoss.utils + +import android.content.Context +import android.graphics.Bitmap +import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory +import android.widget.ImageView +import com.bumptech.glide.Glide +import com.bumptech.glide.request.target.BitmapImageViewTarget + + +fun Context.bitmapCenterCrop(url: String, iv: ImageView) = + Glide.with(this).load(url).asBitmap().centerCrop().into(iv) + +fun Context.bitmapFitCenter(url: String, iv: ImageView) = + Glide.with(this).load(url).asBitmap().fitCenter().into(iv) + +fun Context.circularBitmapDrawable(url: String, iv: ImageView) = + Glide.with(this).load(url).asBitmap().centerCrop().into(object : BitmapImageViewTarget(iv) { + override fun setResource(resource: Bitmap) { + val circularBitmapDrawable = RoundedBitmapDrawableFactory.create(resources, resource) + circularBitmapDrawable.isCircular = true + iv.setImageDrawable(circularBitmapDrawable) + } + }) \ No newline at end of file diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt index 520f72e..4a598fa 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt @@ -1,5 +1,11 @@ package apps.amine.bou.readerforselfoss.utils +import android.text.format.DateUtils +import apps.amine.bou.readerforselfoss.api.selfoss.Item +import java.text.ParseException +import java.text.SimpleDateFormat +import java.util.* + fun String.toTextDrawableString(): String { val textDrawable = StringBuilder() @@ -7,4 +13,20 @@ fun String.toTextDrawableString(): String { textDrawable.append(s[0]) } return textDrawable.toString() +} + +fun Item.sourceAndDateText(): String { + var formattedDate: String = try { + " " + DateUtils.getRelativeTimeSpanString( + SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(this.datetime).time, + Date().time, + DateUtils.MINUTE_IN_MILLIS, + DateUtils.FORMAT_ABBREV_RELATIVE + ) + } catch (e: ParseException) { + e.printStackTrace() + "" + } + + return this.sourcetitle + formattedDate } \ No newline at end of file