From eb9b309e72e1003980d985bf674bdc1a4fb00302 Mon Sep 17 00:00:00 2001 From: Luna712 <142361265+Luna712@users.noreply.github.com> Date: Wed, 15 Jan 2025 16:26:08 -0700 Subject: [PATCH] Use MainAPI to get folder prefix (#1472) --- .../ui/result/ResultViewModel2.kt | 24 ++-------- .../com/lagradost/cloudstream3/MainAPI.kt | 44 +++++++++++++++++-- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt index 1d864e325b..5ee575b168 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt @@ -635,26 +635,10 @@ class ResultViewModel2 : ViewModel() { } private fun getFolder(currentType: TvType, titleName: String): String { - val sanitizedFileName = VideoDownloadManager.sanitizeFilename(titleName) - return when (currentType) { - TvType.Anime -> "Anime/$sanitizedFileName" - TvType.AnimeMovie -> "Movies" - TvType.AsianDrama -> "AsianDramas/$sanitizedFileName" - TvType.Audio -> "Audio" - TvType.AudioBook -> "AudioBooks" - TvType.Cartoon -> "Cartoons/$sanitizedFileName" - TvType.CustomMedia -> "Media" - TvType.Documentary -> "Documentaries" - TvType.Live -> "LiveStreams" - TvType.Movie -> "Movies" - TvType.Music -> "Music" - TvType.NSFW -> "NSFW" - TvType.OVA -> "OVAs" - TvType.Others -> "Others" - TvType.Podcast -> "Podcasts" - TvType.Torrent -> "Torrents" - TvType.TvSeries -> "TVSeries/$sanitizedFileName" - } + return if (currentType.isEpisodeBased()) { + val sanitizedFileName = VideoDownloadManager.sanitizeFilename(titleName) + "${currentType.getFolderPrefix()}/$sanitizedFileName" + } else currentType.getFolderPrefix() } private fun downloadSubtitle( diff --git a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt index 84113d7c22..efd195bb78 100644 --- a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt +++ b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt @@ -1504,10 +1504,13 @@ fun LoadResponse?.isAnimeBased(): Boolean { return (this.type == TvType.Anime || this.type == TvType.OVA) // && (this is AnimeLoadResponse) } -/** Extension function of [TvType] to check if it's Episode based. - * @return True if the response type is Anime, AsianDrama, Cartoon or TvSeries. - * @see TvType - * */ +/** + * Extension function to determine if the [TvType] is episode-based. + * This function checks if the type corresponds to an episode-based format. Episode-based types will be placed + * in subfolders that include the sanitized title name. This check is used for other logic as well. + * + * @return true if the [TvType] is episode-based, otherwise false. + */ fun TvType?.isEpisodeBased(): Boolean { return when (this) { TvType.Anime, @@ -1519,6 +1522,39 @@ fun TvType?.isEpisodeBased(): Boolean { } } +/** + * Extension function to get the folder prefix based on the [TvType]. + * Non-episode-based types will return a base folder name, while episode-based types will + * have their files placed in subfolders using a sanitized title name. + * + * For the actual folder path, refer to `ResultViewModel2().getFolder()`, which combines + * the folder prefix and, if necessary, the sanitized name to a sub-folder. The folder prefix + * will be used in the root directory of the configured downloads directory. + * + * @return the folder prefix corresponding to the [TvType], which is used as the root directory. + */ +fun TvType.getFolderPrefix(): String { + return when (this) { + TvType.Anime -> "Anime" + TvType.AnimeMovie -> "Movies" + TvType.AsianDrama -> "AsianDramas" + TvType.Audio -> "Audio" + TvType.AudioBook -> "AudioBooks" + TvType.Cartoon -> "Cartoons" + TvType.CustomMedia -> "Media" + TvType.Documentary -> "Documentaries" + TvType.Live -> "LiveStreams" + TvType.Movie -> "Movies" + TvType.Music -> "Music" + TvType.NSFW -> "NSFW" + TvType.OVA -> "OVAs" + TvType.Others -> "Others" + TvType.Podcast -> "Podcasts" + TvType.Torrent -> "Torrents" + TvType.TvSeries -> "TVSeries" + } +} + /** Data class holds next airing Episode info. * @param episode Next airing Episode number. * @param unixTime Next airing Time in Unix time format.