download thumbnail images

This commit is contained in:
Bnyro 2022-10-15 16:29:12 +02:00
parent 206a24ad7d
commit b8b3159362
7 changed files with 100 additions and 5 deletions

View File

@ -1,10 +1,12 @@
package com.github.libretube.obj
import android.graphics.Bitmap
import com.github.libretube.api.obj.Streams
data class DownloadedFile(
val name: String,
val size: Long,
val type: Int,
var metadata: Streams? = null
var metadata: Streams? = null,
var thumbnail: Bitmap? = null
)

View File

@ -42,6 +42,8 @@ class DownloadsAdapter(
"" + it.uploadDate
}
thumbnailImage.setImageBitmap(file.thumbnail)
root.setOnClickListener {
val intent = Intent(root.context, OfflinePlayerActivity::class.java).also {
it.putExtra(IntentData.fileName, file.name)

View File

@ -17,6 +17,7 @@ import com.github.libretube.databinding.DialogDownloadBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.sanitize
import com.github.libretube.services.DownloadService
import com.github.libretube.util.ImageHelper
import com.github.libretube.util.MetadataHelper
import com.github.libretube.util.ThemeHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -137,8 +138,13 @@ class DownloadDialog(
val metadataHelper = MetadataHelper(requireContext())
metadataHelper.createMetadata(fileName, streams)
Log.e("meta", metadataHelper.getMetadata(fileName).toString())
streams.thumbnailUrl?.let { thumbnailUrl ->
ImageHelper.downloadImage(
requireContext(),
thumbnailUrl,
fileName
)
}
val intent = Intent(context, DownloadService::class.java)

View File

@ -11,6 +11,7 @@ import com.github.libretube.databinding.FragmentDownloadsBinding
import com.github.libretube.ui.adapters.DownloadsAdapter
import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.util.DownloadHelper
import com.github.libretube.util.ImageHelper
import com.github.libretube.util.MetadataHelper
class DownloadsFragment : BaseFragment() {
@ -37,6 +38,9 @@ class DownloadsFragment : BaseFragment() {
metadataHelper.getMetadata(it.name)?.let { streams ->
it.metadata = streams
}
ImageHelper.getDownloadedImage(requireContext(), it.name)?.let { bitmap ->
it.thumbnail = bitmap
}
}
binding.downloadsEmpty.visibility = View.GONE

View File

@ -39,6 +39,13 @@ object DownloadHelper {
).createDir()
}
fun getThumbnailDir(context: Context): File {
return File(
getOfflineStorageDir(context),
"thumbnail"
).createDir()
}
fun getDownloadedFiles(context: Context): MutableList<DownloadedFile> {
val videoFiles = getVideoDir(context).listFiles()
val audioFiles = getAudioDir(context).listFiles()?.toMutableList()

View File

@ -1,12 +1,20 @@
package com.github.libretube.util
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.widget.ImageView
import coil.ImageLoader
import coil.disk.DiskCache
import coil.load
import coil.request.ImageRequest
import com.github.libretube.api.CronetHelper
import com.github.libretube.constants.PreferenceKeys
import okio.use
import java.io.File
import java.io.FileOutputStream
object ImageHelper {
lateinit var imageLoader: ImageLoader
@ -42,4 +50,51 @@ object ImageHelper {
)
if (!dataSaverModeEnabled) target.load(url, imageLoader)
}
fun downloadImage(context: Context, url: String, fileName: String) {
val request = ImageRequest.Builder(context)
.data(url)
.target { result ->
val bitmap = (result as BitmapDrawable).bitmap
saveImage(
context,
bitmap,
Uri.fromFile(
File(
DownloadHelper.getThumbnailDir(context),
fileName
)
)
)
}
.build()
imageLoader.enqueue(request)
}
fun getDownloadedImage(context: Context, fileName: String): Bitmap? {
return getImage(
context,
Uri.fromFile(
File(
DownloadHelper.getThumbnailDir(context),
fileName
)
)
)
}
private fun saveImage(context: Context, bitmapImage: Bitmap, imagePath: Uri) {
context.contentResolver.openFileDescriptor(imagePath, "w")?.use {
FileOutputStream(it.fileDescriptor).use { fos ->
bitmapImage.compress(Bitmap.CompressFormat.PNG, 25, fos)
}
}
}
private fun getImage(context: Context, imagePath: Uri): Bitmap? {
context.contentResolver.openInputStream(imagePath)?.use {
return BitmapFactory.decodeStream(it)
}
return null
}
}

View File

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
@ -7,18 +9,35 @@
android:paddingHorizontal="10dp"
android:paddingVertical="8dp">
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="15dp"
app:cardCornerRadius="8dp">
<ImageView
android:id="@+id/thumbnailImage"
android:layout_width="140dp"
android:layout_height="80dp"
android:scaleType="fitXY"
tools:src="@tools:sample/backgrounds/scenic" />
</com.google.android.material.card.MaterialCardView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginEnd="10dp">
android:orientation="vertical">
<TextView
android:id="@+id/fileName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="2dp"
android:maxLines="2"
android:textSize="16sp" />
<TextView