diff --git a/app/src/main/java/com/github/libretube/extensions/CreateDir.kt b/app/src/main/java/com/github/libretube/extensions/CreateDir.kt deleted file mode 100644 index 5300ff318..000000000 --- a/app/src/main/java/com/github/libretube/extensions/CreateDir.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.github.libretube.extensions - -import java.io.File - -fun File.createDir() = apply { - if (!this.exists()) this.mkdirs() -} diff --git a/app/src/main/java/com/github/libretube/obj/DownloadedFile.kt b/app/src/main/java/com/github/libretube/obj/DownloadedFile.kt index 270c35110..83450823d 100644 --- a/app/src/main/java/com/github/libretube/obj/DownloadedFile.kt +++ b/app/src/main/java/com/github/libretube/obj/DownloadedFile.kt @@ -2,12 +2,10 @@ package com.github.libretube.obj import android.graphics.Bitmap import com.github.libretube.api.obj.Streams -import com.github.libretube.enums.DownloadType data class DownloadedFile( val name: String, val size: Long, - val type: DownloadType, var metadata: Streams? = null, var thumbnail: Bitmap? = null ) diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index 64eb42a57..ede6e3667 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -63,8 +63,8 @@ class DownloadService : Service() { private fun downloadManager() { // initialize and create the directories to download into - val videoDownloadDir = DownloadHelper.getVideoDir(this) - val audioDownloadDir = DownloadHelper.getAudioDir(this) + val videoDownloadDir = DownloadHelper.getDownloadDir(this, DownloadHelper.VIDEO_DIR) + val audioDownloadDir = DownloadHelper.getDownloadDir(this, DownloadHelper.AUDIO_DIR) // start download try { diff --git a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt index 2e717ba73..0606515f8 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt @@ -75,21 +75,20 @@ class OfflinePlayerActivity : BaseActivity() { ) } + private fun File.toUri(): Uri? { + return if (this.exists()) Uri.fromFile(this) else null + } + private fun playVideo() { - val videoDownloadDir = DownloadHelper.getVideoDir(this) - val videoFile = File( - videoDownloadDir, + val videoUri = File( + DownloadHelper.getDownloadDir(this, DownloadHelper.VIDEO_DIR), fileName - ) + ).toUri() - val audioDownloadDir = DownloadHelper.getAudioDir(this) - val audioFile = File( - audioDownloadDir, + val audioUri = File( + DownloadHelper.getDownloadDir(this, DownloadHelper.AUDIO_DIR), fileName - ) - - val videoUri = if (videoFile.exists()) Uri.fromFile(videoFile) else null - val audioUri = if (audioFile.exists()) Uri.fromFile(audioFile) else null + ).toUri() setMediaSource( videoUri, diff --git a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt index 70140dce7..3ecbcb932 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt @@ -61,8 +61,8 @@ class DownloadsAdapter( ) { _, index -> when (index) { 0 -> { - val audioDir = DownloadHelper.getAudioDir(root.context) - val videoDir = DownloadHelper.getVideoDir(root.context) + val audioDir = DownloadHelper.getDownloadDir(root.context, DownloadHelper.AUDIO_DIR) + val videoDir = DownloadHelper.getDownloadDir(root.context, DownloadHelper.VIDEO_DIR) listOf(audioDir, videoDir).forEach { val f = File(it, file.name) diff --git a/app/src/main/java/com/github/libretube/util/DownloadHelper.kt b/app/src/main/java/com/github/libretube/util/DownloadHelper.kt index 6ce1a13a2..f5cb6e7a2 100644 --- a/app/src/main/java/com/github/libretube/util/DownloadHelper.kt +++ b/app/src/main/java/com/github/libretube/util/DownloadHelper.kt @@ -2,12 +2,15 @@ package com.github.libretube.util import android.content.Context import android.os.Build -import com.github.libretube.enums.DownloadType -import com.github.libretube.extensions.createDir import com.github.libretube.obj.DownloadedFile import java.io.File object DownloadHelper { + const val VIDEO_DIR = "video" + const val AUDIO_DIR = "audio" + const val METADATA_DIR = "metadata" + const val THUMBNAIL_DIR = "thumbnail" + private fun getOfflineStorageDir(context: Context): File { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return context.filesDir @@ -18,65 +21,35 @@ object DownloadHelper { } } - fun getVideoDir(context: Context): File { + fun getDownloadDir(context: Context, path: String): File { return File( getOfflineStorageDir(context), - "video" - ).createDir() + path + ).apply { + if (!this.exists()) this.mkdirs() + } } - fun getAudioDir(context: Context): File { - return File( - getOfflineStorageDir(context), - "audio" - ).createDir() - } - - fun getMetadataDir(context: Context): File { - return File( - getOfflineStorageDir(context), - "metadata" - ).createDir() - } - - fun getThumbnailDir(context: Context): File { - return File( - getOfflineStorageDir(context), - "thumbnail" - ).createDir() + private fun File.toDownloadedFile(): DownloadedFile { + return DownloadedFile( + name = this.name, + size = this.length() + ) } fun getDownloadedFiles(context: Context): MutableList { - val videoFiles = getVideoDir(context).listFiles() - val audioFiles = getAudioDir(context).listFiles()?.toMutableList() + val videoFiles = getDownloadDir(context, VIDEO_DIR).listFiles().orEmpty() + val audioFiles = getDownloadDir(context, AUDIO_DIR).listFiles().orEmpty().toMutableList() val files = mutableListOf() - videoFiles?.forEach { - var type = DownloadType.VIDEO - audioFiles?.forEach { audioFile -> - if (audioFile.name == it.name) { - type = DownloadType.AUDIO_VIDEO - audioFiles.remove(audioFile) - } - } - files.add( - DownloadedFile( - name = it.name, - size = it.length(), - type = type - ) - ) + videoFiles.forEach { + audioFiles.removeIf { audioFile -> audioFile.name == it.name } + files.add(it.toDownloadedFile()) } - audioFiles?.forEach { - files.add( - DownloadedFile( - name = it.name, - size = it.length(), - type = DownloadType.AUDIO - ) - ) + audioFiles.forEach { + files.add(it.toDownloadedFile()) } return files diff --git a/app/src/main/java/com/github/libretube/util/ImageHelper.kt b/app/src/main/java/com/github/libretube/util/ImageHelper.kt index ffad865bc..d72c61fc9 100644 --- a/app/src/main/java/com/github/libretube/util/ImageHelper.kt +++ b/app/src/main/java/com/github/libretube/util/ImageHelper.kt @@ -56,16 +56,11 @@ object ImageHelper { .data(url) .target { result -> val bitmap = (result as BitmapDrawable).bitmap - saveImage( - context, - bitmap, - Uri.fromFile( - File( - DownloadHelper.getThumbnailDir(context), - fileName - ) - ) + val file = File( + DownloadHelper.getDownloadDir(context, DownloadHelper.THUMBNAIL_DIR), + fileName ) + saveImage(context, bitmap, Uri.fromFile(file)) } .build() @@ -73,15 +68,11 @@ object ImageHelper { } fun getDownloadedImage(context: Context, fileName: String): Bitmap? { - return getImage( - context, - Uri.fromFile( - File( - DownloadHelper.getThumbnailDir(context), - fileName - ) - ) + val file = File( + DownloadHelper.getDownloadDir(context, DownloadHelper.THUMBNAIL_DIR), + fileName ) + return getImage(context, Uri.fromFile(file)) } private fun saveImage(context: Context, bitmapImage: Bitmap, imagePath: Uri) { diff --git a/app/src/main/java/com/github/libretube/util/MetadataHelper.kt b/app/src/main/java/com/github/libretube/util/MetadataHelper.kt index 0cb6b7172..a2b46d13d 100644 --- a/app/src/main/java/com/github/libretube/util/MetadataHelper.kt +++ b/app/src/main/java/com/github/libretube/util/MetadataHelper.kt @@ -11,7 +11,7 @@ class MetadataHelper( private val context: Context ) { private val mapper = ObjectMapper() - private val metadataDir = DownloadHelper.getMetadataDir(context) + private val metadataDir = DownloadHelper.getDownloadDir(context, DownloadHelper.METADATA_DIR) fun createMetadata(fileName: String, streams: Streams) { val targetFile = File(metadataDir, fileName)