From 385ff6dc026296a2df4cffe8dcfdf6e0b9d3fb38 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Thu, 28 Jun 2018 15:53:13 +0300 Subject: [PATCH] Extract logic for setting the user's photo into a separate class --- .../telegram/helpers/TelegramUiHelper.kt | 29 +++++++++++++++++++ .../osmand/telegram/ui/LiveNowTabFragment.kt | 23 ++------------- .../telegram/ui/MyLocationTabFragment.kt | 18 ++---------- 3 files changed, 34 insertions(+), 36 deletions(-) create mode 100644 OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt new file mode 100644 index 0000000000..946934478b --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt @@ -0,0 +1,29 @@ +package net.osmand.telegram.helpers + +import android.graphics.Bitmap +import android.graphics.drawable.Drawable +import android.widget.ImageView +import net.osmand.telegram.R +import net.osmand.telegram.TelegramApplication + +object TelegramUiHelper { + + fun setupPhoto(app: TelegramApplication, iv: ImageView?, photoPath: String?) { + if (iv == null) { + return + } + var drawable: Drawable? = null + var bitmap: Bitmap? = null + if (photoPath != null && photoPath.isNotEmpty()) { + bitmap = app.uiUtils.getCircleBitmap(photoPath) + } + if (bitmap == null) { + drawable = app.uiUtils.getThemedIcon(R.drawable.ic_group) + } + if (bitmap != null) { + iv.setImageBitmap(bitmap) + } else { + iv.setImageDrawable(drawable) + } + } +} diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index f9cf173d5a..29f4dad289 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -1,7 +1,5 @@ package net.osmand.telegram.ui -import android.graphics.Bitmap -import android.graphics.drawable.Drawable import android.os.Bundle import android.support.v4.app.Fragment import android.support.v7.widget.LinearLayoutManager @@ -17,6 +15,7 @@ import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication import net.osmand.telegram.helpers.TelegramHelper.TelegramAuthorizationState import net.osmand.telegram.helpers.TelegramHelper.TelegramListener +import net.osmand.telegram.helpers.TelegramUiHelper import org.drinkless.td.libcore.telegram.TdApi private const val CHAT_VIEW_TYPE = 0 @@ -140,7 +139,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener { val nextItemIsUser = !lastItem && items[position + 1] is TdApi.User val chatTitle = item.title - setupIcon(holder.icon, item.photo?.small?.local?.path) + TelegramUiHelper.setupPhoto(app, holder.icon, item.photo?.small?.local?.path) holder.title?.text = chatTitle holder.description?.text = "Chat description" // FIXME holder.imageButton?.setImageDrawable(app.uiUtils.getThemedIcon(R.drawable.ic_overflow_menu_white)) @@ -180,7 +179,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener { holder.bottomDivider?.visibility = if (nextItemIsUser) View.VISIBLE else View.GONE holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE } else if (item is TdApi.User && holder is ContactViewHolder) { - setupIcon(holder.icon, telegramHelper.getUserPhotoPath(item)) + TelegramUiHelper.setupPhoto(app, holder.icon, telegramHelper.getUserPhotoPath(item)) holder.title?.text = "${item.firstName} ${item.lastName}" holder.description?.text = "User description" // FIXME holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE @@ -189,22 +188,6 @@ class LiveNowTabFragment : Fragment(), TelegramListener { override fun getItemCount() = items.size - private fun setupIcon(iv: ImageView?, photoPath: String?) { - var drawable: Drawable? = null - var bitmap: Bitmap? = null - if (photoPath != null && photoPath.isNotEmpty()) { - bitmap = app.uiUtils.getCircleBitmap(photoPath) - } - if (bitmap == null) { - drawable = app.uiUtils.getThemedIcon(R.drawable.ic_group) - } - if (bitmap != null) { - iv?.setImageBitmap(bitmap) - } else { - iv?.setImageDrawable(drawable) - } - } - inner class ContactViewHolder(val view: View) : RecyclerView.ViewHolder(view) { val icon: ImageView? = view.findViewById(R.id.icon) val title: TextView? = view.findViewById(R.id.title) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt index 7cff6c9ffd..714e7ec779 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt @@ -1,8 +1,6 @@ package net.osmand.telegram.ui import android.animation.* -import android.graphics.Bitmap -import android.graphics.drawable.Drawable import android.graphics.drawable.GradientDrawable import android.os.Build import android.os.Bundle @@ -17,6 +15,7 @@ import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication import net.osmand.telegram.helpers.TelegramHelper import net.osmand.telegram.helpers.TelegramHelper.TelegramListener +import net.osmand.telegram.helpers.TelegramUiHelper import net.osmand.telegram.ui.MyLocationTabFragment.MyLocationListAdapter.ChatViewHolder import org.drinkless.td.libcore.telegram.TdApi @@ -273,20 +272,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener { val chat = chats[position] val lastItem = position == itemCount - 1 - var drawable: Drawable? = null - var bitmap: Bitmap? = null - val chatPhoto = chat.photo?.small - if (chatPhoto != null && chatPhoto.local.path.isNotEmpty()) { - bitmap = app.uiUtils.getCircleBitmap(chatPhoto.local.path) - } - if (bitmap == null) { - drawable = app.uiUtils.getThemedIcon(R.drawable.ic_group) - } - if (bitmap != null) { - holder.icon?.setImageBitmap(bitmap) - } else { - holder.icon?.setImageDrawable(drawable) - } + TelegramUiHelper.setupPhoto(app, holder.icon, chat.photo?.small?.local?.path) holder.title?.text = chat.title holder.description?.text = "Some description" // FIXME holder.checkBox?.apply {