Skip to content
Snippets Groups Projects
Commit 12d73391 authored by Taras's avatar Taras
Browse files

Create glide data fetcher

parent e0e65171
No related branches found
No related tags found
No related merge requests found
package com.futo.circles.glide
import android.content.Context
import android.util.Log
import com.bumptech.glide.Priority
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.data.DataFetcher
import com.futo.circles.extensions.coroutineScope
import com.futo.circles.model.ImageContent
import com.futo.circles.provider.MatrixSessionProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.matrix.android.sdk.api.Matrix
import java.io.IOException
import java.io.InputStream
class CirclesGlideDataFetcher(context: Context, private val data: ImageContent) :
DataFetcher<InputStream> {
private val localFilesHelper = LocalFileHelper(context)
private val matrixSession = MatrixSessionProvider.currentSession
override fun getDataClass(): Class<InputStream> {
return InputStream::class.java
}
private var stream: InputStream? = null
override fun cleanup() {
cancel()
}
override fun getDataSource(): DataSource {
return DataSource.REMOTE
}
override fun cancel() {
if (stream != null) {
try {
stream?.close()
stream = null
} catch (ignore: Throwable) {
Log.e(this.javaClass.name, "Failed to close stream ${ignore.localizedMessage}")
} finally {
stream = null
}
}
}
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
if (localFilesHelper.isLocalFile(data.fileUrl)) {
localFilesHelper.openInputStream(data.fileUrl)?.use { callback.onDataReady(it) }
return
}
val fileService = matrixSession?.fileService() ?: return Unit.also {
callback.onLoadFailed(IllegalArgumentException("No File service"))
}
matrixSession.coroutineScope.launch {
val result = runCatching {
fileService.downloadFile(
fileName = data.fileName,
mimeType = data.mimeType,
url = data.fileUrl,
elementToDecrypt = data.elementToDecrypt
)
}
withContext(Dispatchers.Main) {
result.fold(
{ callback.onDataReady(it.inputStream()) },
{ callback.onLoadFailed(it as? Exception ?: IOException(it.localizedMessage)) }
)
}
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment