Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to coil 2.0 #655

Merged
merged 5 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,10 @@ dependencies {
implementation 'com.wdullaer:materialdatetimepicker:4.2.3'
implementation 'com.github.rubensousa:gravitysnaphelper:2.2.2'

def coil_version = '1.4.0'
def coil_version = '2.0.0-rc01'
implementation "io.coil-kt:coil:$coil_version"
implementation "io.coil-kt:coil-gif:$coil_version"

def glide_version = '4.12.0'
implementation("com.github.bumptech.glide:glide:$glide_version") { exclude group: 'com.android.support' }
implementation "com.github.bumptech.glide:okhttp3-integration:$glide_version"
kapt "com.github.bumptech.glide:compiler:$glide_version"

implementation 'io.realm:android-adapters:4.0.0'

standardImplementation 'com.google.android.play:core:1.10.3'
Expand Down
6 changes: 1 addition & 5 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

## Glider
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep class com.bumptech.glide.GeneratedAppGlideModuleImpl
#-renamesourcefileattribute SourceFile
20 changes: 16 additions & 4 deletions app/src/main/java/com/infomaniak/drive/ApplicationMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.NotificationManagerCompat
import coil.ImageLoader
import coil.ImageLoaderFactory
import coil.annotation.ExperimentalCoilApi
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.util.CoilUtils
import coil.disk.DiskCache
import coil.memory.MemoryCache
import com.facebook.stetho.Stetho
import com.facebook.stetho.okhttp3.StethoInterceptor
import com.infomaniak.drive.BuildConfig.DRIVE_API
Expand Down Expand Up @@ -66,6 +68,7 @@ import okhttp3.OkHttpClient
import org.matomo.sdk.Tracker
import java.util.*

@ExperimentalCoilApi
class ApplicationMain : Application(), ImageLoaderFactory {

val matomoTracker: Tracker by lazy { buildTracker() }
Expand Down Expand Up @@ -122,8 +125,8 @@ class ApplicationMain : Application(), ImageLoaderFactory {
override fun newImageLoader(): ImageLoader {
return ImageLoader.Builder(applicationContext)
.crossfade(true)
.componentRegistry {
add(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) ImageDecoderDecoder(applicationContext) else GifDecoder())
.components {
add(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) ImageDecoderDecoder.Factory() else GifDecoder.Factory())
}
.okHttpClient {
OkHttpClient.Builder().apply {
Expand All @@ -139,9 +142,14 @@ class ApplicationMain : Application(), ImageLoaderFactory {
if (com.infomaniak.lib.core.BuildConfig.DEBUG) {
addNetworkInterceptor(StethoInterceptor())
}
cache(CoilUtils.createDefaultCache(applicationContext))
}.build()
}
.memoryCache {
MemoryCache.Builder(applicationContext).build()
}
.diskCache {
DiskCache.Builder().directory(applicationContext.cacheDir.resolve(COIL_CACHE_DIR)).build()
}
.build()
}

Expand Down Expand Up @@ -172,4 +180,8 @@ class ApplicationMain : Application(), ImageLoaderFactory {
return AccountUtils.currentUser!!.apiToken
}
}

private companion object {
const val COIL_CACHE_DIR = "coil_cache"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import coil.load
import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.FileActivity
import com.infomaniak.drive.utils.loadAvatar
import com.infomaniak.drive.utils.loadGlide
import com.infomaniak.lib.core.views.LoaderAdapter
import com.infomaniak.lib.core.views.ViewHolder
import kotlinx.android.synthetic.main.item_file_activity.view.*
Expand Down Expand Up @@ -58,7 +58,7 @@ class FileActivitiesAdapter(val isFolder: Boolean) : LoaderAdapter<FileActivity>
activityUserAvatar.loadAvatar(driveUser)
} ?: run {
activityUserName.setText(R.string.allUserAnonymous)
activityUserAvatar.loadGlide(R.drawable.ic_account)
activityUserAvatar.load(R.drawable.ic_account)
}

if (position == 0 || !isSameDay(currentFileActivity.createdAt, itemList[position - 1].createdAt)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class FileDetailsFragment : FileDetailsSubFragment() {

private fun setBannerThumbnail(file: File) {
if (file.hasThumbnail) {
collapsingBackground.loadGlideUrl(file.thumbnail())
collapsingBackground.loadAny(file.thumbnail())
} else {
appBar.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background))
val params = subtitleToolbar.layoutParams as CoordinatorLayout.LayoutParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.widget.ArrayAdapter
import android.widget.Filter
import android.widget.Filterable
import androidx.core.view.isGone
import coil.load
import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.DriveUser
import com.infomaniak.drive.data.models.Invitation
Expand All @@ -33,7 +34,6 @@ import com.infomaniak.drive.data.models.Team
import com.infomaniak.drive.utils.AccountUtils
import com.infomaniak.drive.utils.isEmail
import com.infomaniak.drive.utils.loadAvatar
import com.infomaniak.drive.utils.loadGlide
import kotlinx.android.synthetic.main.item_user.view.*

/**
Expand Down Expand Up @@ -104,14 +104,14 @@ class AvailableShareableItemsAdapter(
chevron.isGone = true
}
is Invitation -> {
userAvatar.loadGlide(R.drawable.ic_account)
userAvatar.load(R.drawable.ic_account)
userName.text = item.email
userEmail.text = context.getString(R.string.userInviteByEmail)
chevron.isGone = true
}
is Team -> {
val teamUsersCount = item.usersCount(AccountUtils.getCurrentDrive()!!)
userAvatar.loadGlide(R.drawable.ic_circle_team)
userAvatar.load(R.drawable.ic_circle_team)
userAvatar.setBackgroundColor(item.getParsedColor())
userName.text = item.name
userEmail.text =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.google.android.material.card.MaterialCardView
import com.google.android.material.shape.RelativeCornerSize
import com.google.android.material.shape.ShapeAppearanceModel
Expand All @@ -33,7 +34,6 @@ import com.infomaniak.drive.data.models.Permission
import com.infomaniak.drive.data.models.Shareable.ShareablePermission
import com.infomaniak.drive.utils.AccountUtils
import com.infomaniak.drive.utils.loadAvatar
import com.infomaniak.drive.utils.loadGlide
import com.infomaniak.lib.core.models.User
import com.infomaniak.lib.core.utils.toPx
import com.infomaniak.lib.core.views.ViewHolder
Expand Down Expand Up @@ -134,7 +134,7 @@ class PermissionsAdapter(
}

private fun View.setupOthersPermissionUi(permission: Permission) {
mainIcon.loadGlide(permission.icon)
mainIcon.load(permission.icon)
mainIcon.shapeAppearanceModel = ShapeAppearanceModel()

when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import androidx.core.view.isGone
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.*
import com.infomaniak.drive.utils.loadAny
import com.infomaniak.drive.utils.loadAvatar
import com.infomaniak.drive.utils.loadGlide
import com.infomaniak.lib.core.views.ViewHolder
import kotlinx.android.synthetic.main.item_shareable_item.view.*

Expand Down Expand Up @@ -92,9 +93,7 @@ class SharedItemsAdapter(
infos.isVisible = true
}

avatar.apply {
loadGlide(null, errorRes = R.drawable.ic_circle_send)
}
avatar.loadAny(null, errorRes = R.drawable.ic_circle_send)

rightsValue.setText(invitation.getFilePermission().translation)
externalUserLabel.apply {
Expand All @@ -110,10 +109,10 @@ class SharedItemsAdapter(
private fun View.bindTeam(team: Team) {
if (team.isAllUsers()) {
name.setText(R.string.allAllDriveUsers)
avatar.loadGlide(R.drawable.ic_circle_drive)
avatar.load(R.drawable.ic_circle_drive)
} else {
name.text = team.name
avatar.loadGlide(R.drawable.ic_circle_team)
avatar.load(R.drawable.ic_circle_team)
}
avatar.setBackgroundColor(team.getParsedColor())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ class PreviewPictureFragment : PreviewFragment() {

private fun buildPreviewRequest(): ImageRequest {
val timer = createRefreshTimer(milliseconds = 400) { noThumbnailLayout?.isVisible = true }.start()
val offlineFile = if (file.isOffline) getOfflineFile() else null
// val offlineFile = if (file.isOffline) getOfflineFile() else null // TODO issue https://github.com/coil-kt/coil/issues/1201

return ImageRequest.Builder(requireContext())
.data(offlineFile ?: file.imagePreview())
.data(file.imagePreview()) // load only remote file because load a large image from local issue https://github.com/coil-kt/coil/issues/1201
.listener(
onError = { _, _ ->
fileName?.text = file.name
Expand All @@ -98,6 +98,9 @@ class PreviewPictureFragment : PreviewFragment() {
.build()
}

/**
* TODO waiting for issue https://github.com/coil-kt/coil/issues/1201
*/
private fun getOfflineFile(): File? {
return file.getOfflineFile(requireContext(), previewSliderViewModel.userDrive.userId)?.let {
if (file.isOfflineAndIntact(it)) it else null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isGone
import androidx.core.view.isVisible
import coil.load
import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.ConvertedType
import com.infomaniak.drive.data.models.DriveUser
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.data.models.FileActivity
import com.infomaniak.drive.utils.loadAny
import com.infomaniak.drive.utils.loadAvatar
import com.infomaniak.drive.utils.loadGlide
import com.infomaniak.drive.utils.loadGlideUrl
import com.infomaniak.lib.core.views.LoaderAdapter
import com.infomaniak.lib.core.views.LoaderCardView
import com.infomaniak.lib.core.views.ViewHolder
Expand Down Expand Up @@ -76,7 +76,7 @@ class LastActivitiesAdapter : LoaderAdapter<FileActivity>() {
2 * DAY_IN_MILLIS, FORMAT_ABBREV_ALL
)

fileIcon.loadGlide(getFileTypeIcon(fileActivity.file))
fileIcon.load(getFileTypeIcon(fileActivity.file))
fileName1.text = fileActivityName

if (sizeMergedFile >= 1) {
Expand All @@ -87,7 +87,7 @@ class LastActivitiesAdapter : LoaderAdapter<FileActivity>() {
fileActivity.file.loadPreview(filePreview1, filePreviewIcon1 as ConstraintLayout)
val file2 = fileActivity.mergedFileActivities[0].file
file2.loadPreview(filePreview2, filePreviewIcon2 as ConstraintLayout)
fileIcon2.loadGlide(getFileTypeIcon(file2))
fileIcon2.load(getFileTypeIcon(file2))
fileName2.text = file2?.name ?: fileActivity.mergedFileActivities[0].path.substringAfterLast("/")

if (sizeMergedFile == 1) {
Expand All @@ -107,12 +107,12 @@ class LastActivitiesAdapter : LoaderAdapter<FileActivity>() {
(cardFilePreview2.layoutParams as ConstraintLayout.LayoutParams).dimensionRatio = "1:1"

if (sizeMergedFile > 2) {
fileIcon3.loadGlide(R.drawable.ic_copy)
fileIcon3.load(R.drawable.ic_copy)
fileName3.text = fileName3.context.getString(R.string.fileActivityOtherFiles, sizeMergedFile - 1)
moreFile.text = "+${sizeMergedFile - 1}"
moreFile.isVisible = true
} else {
fileIcon3.loadGlide(getFileTypeIcon(file3))
fileIcon3.load(getFileTypeIcon(file3))
fileName3.text = file3?.name ?: fileActivity.mergedFileActivities[1].path.substringAfterLast("/")
moreFile.isGone = true
}
Expand Down Expand Up @@ -169,11 +169,11 @@ class LastActivitiesAdapter : LoaderAdapter<FileActivity>() {
if (this?.hasThumbnail == true && getFileType() == ConvertedType.IMAGE || this?.getFileType() == ConvertedType.VIDEO) {
iconView.isGone = true
imageView.isVisible = true
imageView.loadGlideUrl(thumbnail(), getFileType().icon)
imageView.loadAny(thumbnail(), getFileType().icon)
} else {
imageView.isGone = true
iconView.isVisible = true
iconView.icon.loadGlide(getFileTypeIcon(this))
iconView.icon.load(getFileTypeIcon(this))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.google.android.material.checkbox.MaterialCheckBox
import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.ui.fileList.multiSelect.MultiSelectManager
import com.infomaniak.drive.utils.loadGlideUrl
import com.infomaniak.drive.utils.loadAny
import com.infomaniak.lib.core.utils.format
import com.infomaniak.lib.core.views.LoaderAdapter
import com.infomaniak.lib.core.views.LoaderCardView
Expand Down Expand Up @@ -78,7 +78,7 @@ class PicturesAdapter(
private fun LoaderCardView.displayThumbnail(file: File) {
stop()
picture.apply {
loadGlideUrl(file.thumbnail())
loadAny(file.thumbnail())
contentDescription = file.name
}
}
Expand Down
39 changes: 6 additions & 33 deletions app/src/main/java/com/infomaniak/drive/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ import android.content.Intent
import android.content.res.Configuration
import android.content.res.Resources
import android.database.Cursor
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.Point
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
Expand Down Expand Up @@ -70,7 +68,6 @@ import androidx.navigation.fragment.findNavController
import coil.ImageLoader
import coil.load
import coil.request.Disposable
import com.bumptech.glide.Glide
import com.google.android.material.card.MaterialCardView
import com.google.android.material.shape.CornerFamily
import com.google.android.material.textfield.MaterialAutoCompleteTextView
Expand Down Expand Up @@ -118,36 +115,12 @@ fun Context.isKeyguardSecure(): Boolean {
return (getSystemService(Context.KEYGUARD_SERVICE) as? KeyguardManager)?.isKeyguardSecure ?: false
}

fun ImageView.loadGlide(@DrawableRes drawable: Int) {
Glide.with(this).load(drawable).into(this)
}

fun ImageView.loadGlide(drawable: Drawable?) {
Glide.with(this).load(drawable).into(this)
}

fun ImageView.loadGlide(bitmap: Bitmap?, @DrawableRes errorRes: Int) {
Glide.with(this)
.load(bitmap)
.transition(Utils.CROSS_FADE_TRANSITION)
.placeholder(R.drawable.placeholder)
.error(errorRes)
.centerCrop()
.into(this)
}

fun ImageView.loadGlideUrl(
url: String?,
@DrawableRes errorRes: Int = R.drawable.fallback_image,
errorDrawable: Drawable? = null,
) {
Glide.with(this)
.load(OkHttpLibraryGlideModule.GlideAuthUrl(url))
.transition(Utils.CROSS_FADE_TRANSITION)
.placeholder(R.drawable.placeholder)
.let { if (errorDrawable == null) it.error(errorRes) else it.error(errorDrawable) }
.centerCrop()
.into(this)
fun ImageView.loadAny(data: Any?, @DrawableRes errorRes: Int = R.drawable.fallback_image): Disposable {
return load(data) {
error(errorRes)
fallback(errorRes)
placeholder(R.drawable.placeholder)
}
}

fun ImageView.loadAvatar(driveUser: DriveUser): Disposable {
Expand Down
Loading