From c12482d00094777d68faa8402c458033068020a5 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 2 Aug 2023 07:34:13 +0530 Subject: [PATCH] refactor: Use Path extensions --- app/src/main/java/com/github/libretube/api/obj/PipedStream.kt | 4 ++-- app/src/main/java/com/github/libretube/api/obj/Streams.kt | 4 ++-- app/src/main/java/com/github/libretube/db/Converters.kt | 4 ++-- .../main/java/com/github/libretube/helpers/DownloadHelper.kt | 3 ++- .../java/com/github/libretube/services/DownloadService.kt | 3 ++- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt b/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt index 65cc05376..ab81a6c63 100644 --- a/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt +++ b/app/src/main/java/com/github/libretube/api/obj/PipedStream.kt @@ -3,7 +3,7 @@ package com.github.libretube.api.obj import com.github.libretube.db.obj.DownloadItem import com.github.libretube.enums.FileType import com.github.libretube.helpers.ProxyHelper -import java.nio.file.Paths +import kotlin.io.path.Path import kotlinx.serialization.Serializable @Serializable @@ -37,7 +37,7 @@ data class PipedStream( type = fileType, videoId = videoId, fileName = getQualityString(fileName), - path = Paths.get(""), + path = Path(""), url = url?.let { ProxyHelper.unwrapUrl(it) }, format = format, quality = quality, diff --git a/app/src/main/java/com/github/libretube/api/obj/Streams.kt b/app/src/main/java/com/github/libretube/api/obj/Streams.kt index 78427db8d..136d14ce3 100644 --- a/app/src/main/java/com/github/libretube/api/obj/Streams.kt +++ b/app/src/main/java/com/github/libretube/api/obj/Streams.kt @@ -4,7 +4,7 @@ import com.github.libretube.db.obj.DownloadItem import com.github.libretube.enums.FileType import com.github.libretube.helpers.ProxyHelper import com.github.libretube.parcelable.DownloadData -import java.nio.file.Paths +import kotlin.io.path.Path import kotlinx.datetime.LocalDate import kotlinx.serialization.Serializable @@ -64,7 +64,7 @@ data class Streams( type = FileType.SUBTITLE, videoId = id, fileName = "${name}_$subCode.srt", - path = Paths.get(""), + path = Path(""), url = subtitles.find { it.code == subCode }?.url?.let { ProxyHelper.unwrapUrl(it) } diff --git a/app/src/main/java/com/github/libretube/db/Converters.kt b/app/src/main/java/com/github/libretube/db/Converters.kt index 0b665ff49..c4e38ee43 100644 --- a/app/src/main/java/com/github/libretube/db/Converters.kt +++ b/app/src/main/java/com/github/libretube/db/Converters.kt @@ -3,7 +3,7 @@ package com.github.libretube.db import androidx.room.TypeConverter import com.github.libretube.api.JsonHelper import java.nio.file.Path -import java.nio.file.Paths +import kotlin.io.path.Path import kotlinx.datetime.LocalDate import kotlinx.datetime.toLocalDate import kotlinx.serialization.encodeToString @@ -19,7 +19,7 @@ object Converters { fun pathToString(path: Path?) = path?.toString() @TypeConverter - fun stringToPath(string: String?) = string?.let { Paths.get(it) } + fun stringToPath(string: String?) = string?.let { Path(it) } @TypeConverter fun stringListToJson(value: List) = JsonHelper.json.encodeToString(value) diff --git a/app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt b/app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt index afa7d0918..b3cac32eb 100644 --- a/app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt @@ -11,6 +11,7 @@ import com.github.libretube.parcelable.DownloadData import com.github.libretube.services.DownloadService import java.nio.file.Path import kotlin.io.path.createDirectories +import kotlin.io.path.div object DownloadHelper { const val VIDEO_DIR = "video" @@ -32,7 +33,7 @@ object DownloadHelper { context.filesDir } } - return storageDir.toPath().resolve(path).createDirectories() + return (storageDir.toPath() / path).createDirectories() } fun getMaxConcurrentDownloads(): Int { 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 8922ceecd..2355e9f3b 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -50,6 +50,7 @@ import java.util.concurrent.Executors import kotlin.io.path.absolute import kotlin.io.path.createFile import kotlin.io.path.deleteIfExists +import kotlin.io.path.div import kotlin.io.path.fileSize import kotlin.math.min import kotlinx.coroutines.CancellationException @@ -501,7 +502,7 @@ class DownloadService : LifecycleService() { * Get a [File] from the corresponding download directory and the file name */ private fun getDownloadPath(directory: String, fileName: String): Path { - return DownloadHelper.getDownloadDir(this, directory).resolve(fileName).absolute() + return DownloadHelper.getDownloadDir(this, directory) / fileName } override fun onDestroy() {