Skip to content

Commit

Permalink
Introduced Glide integration with RecyclerView
Browse files Browse the repository at this point in the history
  • Loading branch information
JuancaG05 committed Aug 11, 2022
1 parent d0791b8 commit d202546
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions owncloudApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
// Image loading
implementation 'com.github.bumptech.glide:glide:4.13.2'
implementation 'com.github.corouteam:GlideToVectorYou:v2.0.0'
implementation 'com.github.bumptech.glide:recyclerview-integration:4.11.0'

// CustomTabs required for OAuth2 and OIDC.
implementation "androidx.browser:browser:1.4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class TransfersAdapter(
uploadStatus.isVisible = transferItem.transfer.status != TransferStatus.TRANSFER_SUCCEEDED
uploadStatus.text = "" + holder.itemView.context.getString(transferItem.transfer.statusToStringRes())

Glide.with(holder.itemView)
Glide.with(holder.itemView.context)
.load(transferItem.transfer.localPath)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(MimetypeIconUtil.getFileTypeIconId(MimetypeIconUtil.getBestMimeTypeByFilename(transferItem.transfer.localPath), fileName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader
import com.bumptech.glide.util.ViewPreloadSizeProvider
import com.google.android.material.snackbar.Snackbar
import com.owncloud.android.R
import com.owncloud.android.authentication.AccountUtils
Expand Down Expand Up @@ -104,6 +107,10 @@ class TransferListFragment : Fragment() {
}

transfersViewModel.transfersListLiveData.observe(viewLifecycleOwner) { transfers ->
val sizeProvider = ViewPreloadSizeProvider<Any>()
val modelProvider = TransferPreloadModelProvider(transfers, requireContext())
val preloader = RecyclerViewPreloader(Glide.with(this), modelProvider, sizeProvider, 30)
binding.transfersRecyclerView.addOnScrollListener(preloader)
val recyclerViewState = binding.transfersRecyclerView.layoutManager?.onSaveInstanceState()
setData(transfers)
binding.transfersRecyclerView.layoutManager?.onRestoreInstanceState(recyclerViewState)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.owncloud.android.presentation.ui.transfers

import android.content.Context
import com.bumptech.glide.Glide
import com.bumptech.glide.ListPreloader
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.owncloud.android.domain.transfers.model.OCTransfer
import com.owncloud.android.utils.MimetypeIconUtil
import java.io.File
import java.util.Collections

class TransferPreloadModelProvider(
val transfers: List<OCTransfer>,
val context: Context
) : ListPreloader.PreloadModelProvider<Any> {

override fun getPreloadItems(position: Int): MutableList<Any> {
val localPath = transfers[position].localPath
return if (localPath.isEmpty()) {
Collections.emptyList()
} else {
Collections.singletonList(localPath)
}
}

override fun getPreloadRequestBuilder(transfer: Any): RequestBuilder<*> {
val ocTransfer = transfer as OCTransfer
val remoteFile = File(ocTransfer.remotePath)
var fileName = remoteFile.name
if (fileName.isEmpty()) {
fileName = File.separator
}
return Glide.with(context)
.load(ocTransfer.localPath)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(MimetypeIconUtil.getFileTypeIconId(MimetypeIconUtil.getBestMimeTypeByFilename(ocTransfer.localPath), fileName))
}
}

0 comments on commit d202546

Please sign in to comment.