From 58b0574cf96ab4a810f8a2cf555b481cdea45e6f Mon Sep 17 00:00:00 2001 From: davidoskky Date: Fri, 22 Oct 2021 20:04:28 +0200 Subject: [PATCH] Use the drawer image loader to load images (#364) --- app/build.gradle | 2 +- .../bou/readerforselfoss/HomeActivity.kt | 34 ++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b446d13..7b8fb5f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -135,7 +135,7 @@ dependencies { implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' // glide - implementation 'com.github.bumptech.glide:glide:4.1.1' + kapt 'com.github.bumptech.glide:compiler:4.11.0' implementation 'com.github.bumptech.glide:okhttp3-integration:4.1.1' // Drawer 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 77f1632..f0cc549 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -4,7 +4,7 @@ import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.graphics.Color -import android.graphics.drawable.BitmapDrawable +import android.graphics.drawable.Drawable import android.graphics.drawable.GradientDrawable import android.net.Uri import android.os.Build @@ -13,6 +13,7 @@ import android.preference.PreferenceManager import android.view.Menu import android.view.MenuItem import android.view.View +import android.widget.ImageView import android.widget.Toast import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AlertDialog @@ -54,6 +55,7 @@ import com.ashokvarma.bottomnavigation.BottomNavigationBar import com.ashokvarma.bottomnavigation.BottomNavigationItem import com.ashokvarma.bottomnavigation.TextBadgeItem import com.bumptech.glide.Glide +import com.bumptech.glide.request.RequestOptions import com.ftinc.scoop.Scoop import com.mikepenz.aboutlibraries.LibsBuilder import com.mikepenz.materialdrawer.holder.BadgeStyle @@ -457,6 +459,23 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { } private fun handleDrawer() { + DrawerImageLoader.init(object : AbstractDrawerImageLoader() { + override fun set(imageView: ImageView, uri: Uri, placeholder: Drawable, tag: String?) { + Glide.with(this@HomeActivity) + .asBitmap() + .load(uri) + .apply(RequestOptions() + .placeholder(R.mipmap.ic_launcher) + .fallback(R.mipmap.ic_launcher) + .fitCenter()) + .into(imageView) + } + + override fun cancel(imageView: ImageView) { + Glide.with(this@HomeActivity).clear(imageView) + } + }) + val drawerListener = object : DrawerLayout.DrawerListener { override fun onDrawerSlide(drawerView: View, slideOffset: Float) { } @@ -515,7 +534,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { addProfiles( ProfileDrawerItem().apply { nameText = settings.getString("url", "").toString() - background = resources.getDrawable(R.drawable.bg) + setBackgroundResource(R.drawable.bg) iconRes = R.mipmap.ic_launcher selectionListEnabledForSingleProfile = false } @@ -646,6 +665,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { val item = PrimaryDrawerItem().apply { nameText = source.getTitleDecoded() identifier = source.id.toLong() + iconUrl = source.getIcon(this@HomeActivity) onDrawerItemClickListener = { _,_,_ -> allItems = ArrayList() maybeSourceFilter = source @@ -656,16 +676,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { false } } - if (source.getIcon(this@HomeActivity).isNotBlank()) { - thread { - try { - item.iconDrawable = BitmapDrawable(resources, Glide.with(this@HomeActivity).asBitmap().load(source.getIcon(this@HomeActivity)).submit(100, 100).get()) - } catch (e: Exception) { - } - } - } else { - item.iconRes = R.mipmap.ic_launcher - } binding.mainDrawer.itemAdapter.add(item) } }