From 1c7e6973f9a059a37df70bc9a14e1b16f4f34ac4 Mon Sep 17 00:00:00 2001 From: Javinator9889 Date: Wed, 1 Jul 2020 20:04:13 +0200 Subject: [PATCH] Removed Glide support in favor of Coil --- ads/build.gradle | 10 +- app/build.gradle | 15 ++- .../views/fragments/news/adapter/News.kt | 54 ++++---- .../fragments/washinghands/SliderView.kt | 11 +- .../graphics/CustomGraphicsModule.java | 49 -------- .../graphics/RecyclingImageView.java | 116 ------------------ .../graphics/RecyclingImageView.kt | 103 ++++++++++++++++ app/src/main/res/layout/news_card_view.xml | 3 + .../appintro/timeconfig/TimeConfigActivity.kt | 7 +- .../appintro/timeconfig/TimeConfigItem.kt | 7 +- bundledemoji/build.gradle | 7 ++ 11 files changed, 164 insertions(+), 218 deletions(-) delete mode 100644 app/src/main/java/com/javinator9889/handwashingreminder/graphics/CustomGraphicsModule.java delete mode 100644 app/src/main/java/com/javinator9889/handwashingreminder/graphics/RecyclingImageView.java create mode 100644 app/src/main/java/com/javinator9889/handwashingreminder/graphics/RecyclingImageView.kt diff --git a/ads/build.gradle b/ads/build.gradle index 1a212ef..e279d01 100644 --- a/ads/build.gradle +++ b/ads/build.gradle @@ -22,6 +22,14 @@ android { proguardFiles 'proguard-rules.pro' } } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + + kotlinOptions { + jvmTarget = JavaVersion.VERSION_1_8.toString() + } } dependencies { @@ -29,7 +37,7 @@ dependencies { implementation project(':app') // https://firebase.google.com/docs/admob/android/quick-start#import_the_mobile_ads_sdk - implementation 'com.google.firebase:firebase-ads:19.1.0' + implementation 'com.google.firebase:firebase-ads:19.2.0' } repositories { mavenCentral() diff --git a/app/build.gradle b/app/build.gradle index 9f45cfb..dba7b5f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -99,7 +99,12 @@ android { kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() } +} +tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { + kotlinOptions { + jvmTarget = "1.8" + } } dependencies { @@ -176,10 +181,6 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.2.0' // https://developer.android.com/reference/kotlin/androidx/preference/package-summary api 'androidx.preference:preference-ktx:1.1.1' - // https://github.com/bumptech/glide - api 'com.github.bumptech.glide:glide:4.11.0' - api 'com.github.bumptech.glide:okhttp3-integration:4.11.0' - kapt 'com.github.bumptech.glide:compiler:4.11.0' // https://github.com/afollestad/material-dialogs/ implementation 'com.afollestad.material-dialogs:core:3.3.0' // https://developer.android.com/google/play/billing/billing_library_overview @@ -190,9 +191,9 @@ dependencies { // https://github.com/SufficientlySecure/html-textview implementation 'org.sufficientlysecure:html-textview:3.9' // https://github.com/square/okhttp/tree/okhttp_3.12.x - implementation 'com.squareup.okhttp3:okhttp:3.12.1' + implementation 'com.squareup.okhttp3:okhttp:3.12.11' // https://square.github.io/okio/#releases - implementation 'com.squareup.okio:okio:2.5.0' + implementation 'com.squareup.okio:okio:2.6.0' // https://github.com/deano2390/MaterialShowcaseView implementation 'com.github.deano2390:MaterialShowcaseView:1.3.4' // https://github.com/PhilJay/MPAndroidChart @@ -200,5 +201,7 @@ dependencies { // https://developer.android.com/training/data-storage/room implementation "androidx.room:room-ktx:$room_version" kapt "androidx.room:room-compiler:$room_version" + // https://coil-kt.github.io/coil/ + api "io.coil-kt:coil:0.11.0" } apply plugin: 'com.google.gms.google-services' diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/news/adapter/News.kt b/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/news/adapter/News.kt index b5bea1e..446f3b8 100644 --- a/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/news/adapter/News.kt +++ b/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/news/adapter/News.kt @@ -23,13 +23,16 @@ import android.view.View import android.widget.ImageView import android.widget.TextView import androidx.lifecycle.LifecycleCoroutineScope +import coil.Coil +import coil.api.load +import coil.request.GetRequest import com.airbnb.lottie.LottieAnimationView import com.google.android.material.card.MaterialCardView import com.javinator9889.handwashingreminder.R -import com.javinator9889.handwashingreminder.graphics.GlideApp import com.mikepenz.fastadapter.FastAdapter import com.mikepenz.fastadapter.items.AbstractItem -import kotlinx.coroutines.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import timber.log.Timber import java.text.DateFormat import java.util.* @@ -60,10 +63,6 @@ data class News( val cardContainer: MaterialCardView = view.findViewById(R.id.root) val shareImage: ImageView = view.findViewById(R.id.share) - /*init { - setIsRecyclable(!isHighPerformingDevice()) - }*/ - @SuppressLint("SetTextI18n") override fun bindView(item: News, payloads: List) { val formatter = DateFormat.getDateTimeInstance() @@ -72,33 +71,32 @@ data class News( item.lifecycleScope.launch(context = Dispatchers.Main) { title.text = item.title description.text = item.short - val deferreds = mutableSetOf>() - if (item.imageUrl != null) - deferreds.add( - async { - GlideApp.with(context) - .load(item.imageUrl) - .optionalCenterCrop() - .into(imageHeader) - } - ) - else imageHeader.visibility = View.GONE - if (item.websiteImageUrl != null) - deferreds.add( - async { - GlideApp.with(context) - .load(item.websiteImageUrl) - .optionalCenterCrop() - .into(websiteLogo) - } - ) - else websiteLogo.visibility = View.GONE + val imageLoader = Coil.imageLoader(view.context) + if (item.imageUrl != null) { + val request = GetRequest.Builder(view.context) + .data(item.imageUrl) + .size(imageHeader.width, imageHeader.height) + .allowHardware(true) + .build() + launch(Dispatchers.IO) { + imageHeader.load(imageLoader.execute(request).drawable) + } + } else imageHeader.visibility = View.GONE + if (item.websiteImageUrl != null) { + val request = GetRequest.Builder(view.context) + .data(item.websiteImageUrl) + .size(websiteLogo.width, websiteLogo.height) + .allowHardware(true) + .build() + launch(Dispatchers.IO) { + websiteLogo.load(imageLoader.execute(request).drawable) + } + } else websiteLogo.visibility = View.GONE websiteName.text = item.website ?: context.getString(R.string.no_website) publishDate.text = item.discoverDate?.let { formatter.format(it) } ?: context.getString(R.string.no_date) - deferreds.awaitAll() } } diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/washinghands/SliderView.kt b/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/washinghands/SliderView.kt index a48fca7..27dc1a3 100644 --- a/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/washinghands/SliderView.kt +++ b/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/washinghands/SliderView.kt @@ -27,10 +27,10 @@ import androidx.fragment.app.viewModels import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope import androidx.lifecycle.whenStarted +import coil.api.load import com.javinator9889.handwashingreminder.R import com.javinator9889.handwashingreminder.activities.base.BaseFragmentView import com.javinator9889.handwashingreminder.activities.views.viewmodels.* -import com.javinator9889.handwashingreminder.graphics.GlideApp import kotlinx.android.synthetic.main.wash_your_hands_demo.* import kotlinx.coroutines.launch import timber.log.Timber @@ -69,9 +69,7 @@ class SliderView : BaseFragmentView() { }) washingHandsModel.image.observe(viewLifecycleOwner, Observer { try { - GlideApp.with(this@SliderView) - .load(it) - .into(image) + image.load(it) } catch (e: Exception) { Timber.e(e, "Error while loading Glide view") image.setImageResource(it) @@ -124,10 +122,7 @@ class SliderView : BaseFragmentView() { video.requestFocus() video.start() try { - GlideApp.with(this) - .load(drawableId) - .centerInside() - .into(image) + image.load(drawableId) } catch (e: Exception) { Timber.e(e, "Error while loading Glide view") image.setImageResource(drawableId) diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/graphics/CustomGraphicsModule.java b/app/src/main/java/com/javinator9889/handwashingreminder/graphics/CustomGraphicsModule.java deleted file mode 100644 index c1a1739..0000000 --- a/app/src/main/java/com/javinator9889/handwashingreminder/graphics/CustomGraphicsModule.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright © 2020 - present | Handwashing reminder by Javinator9889 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see https://www.gnu.org/licenses/. - * - * Created by Javinator9889 on 15/04/20 - Handwashing reminder. - */ -package com.javinator9889.handwashingreminder.graphics; - -import android.content.Context; - -import androidx.annotation.NonNull; - -import com.bumptech.glide.GlideBuilder; -import com.bumptech.glide.annotation.GlideModule; -import com.bumptech.glide.load.DecodeFormat; -import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory; -import com.bumptech.glide.module.AppGlideModule; -import com.bumptech.glide.request.RequestOptions; - -import static com.javinator9889.handwashingreminder.utils.AndroidKt.isHighPerformingDevice; - -@GlideModule -public final class CustomGraphicsModule extends AppGlideModule { - @Override - public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) { - builder.setDefaultRequestOptions( - new RequestOptions() - .format(DecodeFormat.PREFER_ARGB_8888) - ); - int diskCacheSizeBytes = isHighPerformingDevice() - ? (2 << 20) * 200 - : (2 << 20) * 20; // 200 MB or 20 MB - builder.setDiskCache( - new InternalCacheDiskCacheFactory(context, diskCacheSizeBytes) - ); - } -} diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/graphics/RecyclingImageView.java b/app/src/main/java/com/javinator9889/handwashingreminder/graphics/RecyclingImageView.java deleted file mode 100644 index 194d5c9..0000000 --- a/app/src/main/java/com/javinator9889/handwashingreminder/graphics/RecyclingImageView.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.javinator9889.handwashingreminder.graphics; - -import android.content.Context; -import android.graphics.drawable.Drawable; -import android.graphics.drawable.LayerDrawable; -import android.util.AttributeSet; - -import androidx.annotation.DrawableRes; -import androidx.annotation.Nullable; -import androidx.appcompat.widget.AppCompatImageView; - -/** - * Sub-class of ImageView which automatically notifies the drawable when it is - * being displayed. - */ -public class RecyclingImageView extends AppCompatImageView { - @DrawableRes - private Integer mSavedDrawableRes = null; - - public RecyclingImageView(Context context) { - super(context); - } - - public RecyclingImageView(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public void setSavedDrawableRes(@Nullable @DrawableRes Integer mSavedDrawableRes) { - this.mSavedDrawableRes = mSavedDrawableRes; - } - - @Nullable - @DrawableRes - public Integer getSavedDrawableRes() { - return mSavedDrawableRes; - } - - @Override - protected void onWindowVisibilityChanged(int visibility) { - super.onWindowVisibilityChanged(visibility); - if (mSavedDrawableRes != null && - visibility == VISIBLE && - getDrawable() == null) { - try { - GlideApp.with(this) - .load(mSavedDrawableRes) - .centerInside() - .into(this); - } catch (Exception ignored) { - setImageResource(mSavedDrawableRes); - } - } else if (visibility == INVISIBLE || visibility == GONE) - onDetachedFromWindow(); - } - - /** - * @see android.widget.ImageView#onDetachedFromWindow() - */ - @Override - public void onDetachedFromWindow() { - // This has been detached from Window, so clear the drawable - setImageDrawable(null); - - super.onDetachedFromWindow(); - } - - /** - * @see android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable) - */ - @Override - public void setImageDrawable(Drawable drawable) { - // Keep hold of previous Drawable - final Drawable previousDrawable = getDrawable(); - - // Call super to set new Drawable - super.setImageDrawable(drawable); - - // Notify new Drawable that it is being displayed - notifyDrawable(drawable, true); - - // Notify old Drawable so it is no longer being displayed - notifyDrawable(previousDrawable, false); - } - - /** - * Notifies the drawable that it's displayed state has changed. - */ - private static void notifyDrawable(Drawable drawable, final boolean isDisplayed) { - if (drawable instanceof RecyclingBitmapDrawable) { - // The drawable is a CountingBitmapDrawable, so notify it - ((RecyclingBitmapDrawable) drawable).setIsDisplayed(isDisplayed); - } else if (drawable instanceof LayerDrawable) { - // The drawable is a LayerDrawable, so recurse on each layer - LayerDrawable layerDrawable = (LayerDrawable) drawable; - for (int i = 0, z = layerDrawable.getNumberOfLayers(); i < z; i++) { - notifyDrawable(layerDrawable.getDrawable(i), isDisplayed); - } - } - } - -} diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/graphics/RecyclingImageView.kt b/app/src/main/java/com/javinator9889/handwashingreminder/graphics/RecyclingImageView.kt new file mode 100644 index 0000000..64a3818 --- /dev/null +++ b/app/src/main/java/com/javinator9889/handwashingreminder/graphics/RecyclingImageView.kt @@ -0,0 +1,103 @@ +/* + * Copyright © 2020 - present | Handwashing reminder by Javinator9889 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + * + * Created by Javinator9889 on 1/07/20 - Handwashing reminder. + */ +package com.javinator9889.handwashingreminder.graphics + +import android.content.Context +import android.graphics.drawable.Drawable +import android.graphics.drawable.LayerDrawable +import android.util.AttributeSet +import android.view.View +import androidx.annotation.DrawableRes +import androidx.appcompat.widget.AppCompatImageView +import coil.api.load + +class RecyclingImageView : AppCompatImageView { + constructor(context: Context) : super(context) + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) + constructor( + context: Context, + attrs: AttributeSet, + defStyleAttr: Int + ) : super(context, attrs, defStyleAttr) + + @DrawableRes + var savedDrawableRes: Int? = null + + override fun onWindowVisibilityChanged(visibility: Int) { + super.onWindowVisibilityChanged(visibility) + if (savedDrawableRes != null + && visibility == View.VISIBLE + && drawable != null + ) + try { + load(savedDrawableRes!!) + } catch (ignored: Throwable) { + setImageResource(savedDrawableRes!!) + } + else if (visibility == View.INVISIBLE || visibility == View.GONE) + onDetachedFromWindow() + } + + /** + * @see android.widget.ImageView.onDetachedFromWindow + */ + public override fun onDetachedFromWindow() { + // This has been detached from Window, so clear the drawable + setImageDrawable(null) + super.onDetachedFromWindow() + } + + /** + * @see android.widget.ImageView.setImageDrawable + */ + override fun setImageDrawable(drawable: Drawable?) { + // Keep hold of previous Drawable + val previousDrawable = getDrawable() + + // Call super to set new Drawable + super.setImageDrawable(drawable) + + // Notify new Drawable that it is being displayed + notifyDrawable(drawable, true) + + // Notify old Drawable so it is no longer being displayed + notifyDrawable(previousDrawable, false) + } + + /** + * Notifies the drawable that it's displayed state has changed. + */ + private fun notifyDrawable( + drawable: Drawable?, + isDisplayed: Boolean + ) { + if (drawable is RecyclingBitmapDrawable) { + // The drawable is a CountingBitmapDrawable, so notify it + drawable.setIsDisplayed(isDisplayed) + } else if (drawable is LayerDrawable) { + // The drawable is a LayerDrawable, so recurse on each layer + var i = 0 + val z = drawable.numberOfLayers + while (i < z) { + notifyDrawable(drawable.getDrawable(i), isDisplayed) + i++ + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/news_card_view.xml b/app/src/main/res/layout/news_card_view.xml index d652344..6ebb48f 100644 --- a/app/src/main/res/layout/news_card_view.xml +++ b/app/src/main/res/layout/news_card_view.xml @@ -98,6 +98,7 @@ android:layout_width="20dp" android:layout_height="20dp" app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toStartOf="@+id/ws_name" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> @@ -106,8 +107,10 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" + android:maxWidth="120dp" android:textSize="12sp" app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toStartOf="@+id/bullet" app:layout_constraintStart_toEndOf="@+id/ws_logo" app:layout_constraintTop_toTopOf="parent" tools:text="Website name" /> diff --git a/appintro/src/main/java/com/javinator9889/handwashingreminder/appintro/timeconfig/TimeConfigActivity.kt b/appintro/src/main/java/com/javinator9889/handwashingreminder/appintro/timeconfig/TimeConfigActivity.kt index 03e903c..865c61f 100644 --- a/appintro/src/main/java/com/javinator9889/handwashingreminder/appintro/timeconfig/TimeConfigActivity.kt +++ b/appintro/src/main/java/com/javinator9889/handwashingreminder/appintro/timeconfig/TimeConfigActivity.kt @@ -30,10 +30,10 @@ import android.widget.TextView import android.widget.TimePicker import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.view.ViewCompat +import coil.api.load import com.google.android.play.core.splitcompat.SplitCompat import com.javinator9889.handwashingreminder.activities.support.ActionBarBase import com.javinator9889.handwashingreminder.appintro.R -import com.javinator9889.handwashingreminder.graphics.GlideApp import com.javinator9889.handwashingreminder.utils.AndroidVersion import com.javinator9889.handwashingreminder.utils.TimeConfig import com.javinator9889.handwashingreminder.utils.formatTime @@ -129,10 +129,7 @@ class TimeConfigActivity : } if (imageRes != null) try { - GlideApp.with(this) - .load(imageRes) - .centerInside() - .into(image) + image.load(imageRes) } catch (e: Exception) { Timber.e(e, "Error while loading Glide view") image.setImageResource(imageRes) diff --git a/appintro/src/main/java/com/javinator9889/handwashingreminder/appintro/timeconfig/TimeConfigItem.kt b/appintro/src/main/java/com/javinator9889/handwashingreminder/appintro/timeconfig/TimeConfigItem.kt index 1720ca2..4ed510e 100644 --- a/appintro/src/main/java/com/javinator9889/handwashingreminder/appintro/timeconfig/TimeConfigItem.kt +++ b/appintro/src/main/java/com/javinator9889/handwashingreminder/appintro/timeconfig/TimeConfigItem.kt @@ -22,8 +22,8 @@ import android.view.View import android.widget.TextView import androidx.annotation.LayoutRes import androidx.cardview.widget.CardView +import coil.api.load import com.javinator9889.handwashingreminder.appintro.R -import com.javinator9889.handwashingreminder.graphics.GlideApp import com.javinator9889.handwashingreminder.graphics.RecyclingImageView import com.javinator9889.handwashingreminder.utils.TimeConfig import com.javinator9889.handwashingreminder.utils.notNull @@ -70,10 +70,7 @@ class TimeConfigItem( TimeConfig.DINNER_ID -> R.drawable.ic_dinner else -> null }.notNull { - GlideApp.with(view) - .load(it) - .centerInside() - .into(image) + image.load(it) image.savedDrawableRes = it } } diff --git a/bundledemoji/build.gradle b/bundledemoji/build.gradle index 6898fb8..51644f3 100644 --- a/bundledemoji/build.gradle +++ b/bundledemoji/build.gradle @@ -13,7 +13,14 @@ android { versionName "1.0" } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = JavaVersion.VERSION_1_8.toString() + } }