From 01ce26003d6128f40fca759d5f5f928d1c67546a Mon Sep 17 00:00:00 2001 From: Luna712 <142361265+Luna712@users.noreply.github.com> Date: Tue, 17 Oct 2023 14:10:33 -0600 Subject: [PATCH] Cleanup --- .../com/lagradost/cloudstream3/MainAPI.kt | 10 +--- .../syncproviders/providers/SimklApi.kt | 2 +- .../ui/home/HomeParentItemAdapterPreview.kt | 24 ++++---- .../cloudstream3/ui/home/HomeViewModel.kt | 14 ++--- .../ui/result/ResultViewModel2.kt | 23 +++---- .../cloudstream3/utils/DataStoreHelper.kt | 60 +++++++++---------- app/src/main/res/values/strings.xml | 14 ++--- 7 files changed, 71 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt b/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt index 7fa65540..55352a12 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt @@ -26,8 +26,6 @@ import com.lagradost.nicehttp.RequestBodyTypes import okhttp3.Interceptor import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody.Companion.toRequestBody -import org.json.JSONException -import org.json.JSONObject import java.text.SimpleDateFormat import java.util.* import kotlin.math.absoluteValue @@ -1249,12 +1247,8 @@ interface LoadResponse { } fun LoadResponse.getImdbId(): String? { - val simklId = this.syncData[simklIdPrefix] ?: return null - return try { - val jsonObject = JSONObject(simklId) - jsonObject.optString("Imdb") - } catch (e: JSONException) { - null + return normalSafeApiCall { + SimklApi.readIdFromString(this.syncData[simklIdPrefix])?.get(SimklApi.Companion.SyncServices.Imdb) } } diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/SimklApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/SimklApi.kt index cd1df562..bd7979f5 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/SimklApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/SimklApi.kt @@ -203,7 +203,7 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI { } /** Read the id string to get all other ids */ - private fun readIdFromString(idString: String?): Map { + fun readIdFromString(idString: String?): Map { return tryParseJson(idString) ?: return emptyMap() } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeParentItemAdapterPreview.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeParentItemAdapterPreview.kt index bb932a4d..f590e528 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeParentItemAdapterPreview.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeParentItemAdapterPreview.kt @@ -361,18 +361,18 @@ class HomeParentItemAdapterPreview( fab.context, item ) { statusChanged: Boolean -> - if (statusChanged) { - homePreviewBookmark.setCompoundDrawablesWithIntrinsicBounds( - null, - ContextCompat.getDrawable( - homePreviewBookmark.context, - newValue.iconRes - ), - null, - null - ) - homePreviewBookmark.setText(newValue.stringRes) - } + if (!statusChanged) return@updateWatchStatus + + homePreviewBookmark.setCompoundDrawablesWithIntrinsicBounds( + null, + ContextCompat.getDrawable( + homePreviewBookmark.context, + newValue.iconRes + ), + null, + null + ) + homePreviewBookmark.setText(newValue.stringRes) } } } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeViewModel.kt index 7109abcc..ad75aa9d 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeViewModel.kt @@ -74,17 +74,17 @@ class HomeViewModel : ViewModel() { val watchPos = getViewPos(resume.episodeId) DataStoreHelper.ResumeWatchingResult( - watchPos, - resume.parentId, - resume.episode, - resume.season, - resume.isFromDownload, - resume.episodeId, data.name, data.url, data.apiName, data.type, - data.poster + data.poster, + watchPos, + resume.episodeId, + resume.parentId, + resume.episode, + resume.season, + resume.isFromDownload ) } } 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 def67787..e90a94e6 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 @@ -54,10 +54,10 @@ import com.lagradost.cloudstream3.utils.Coroutines.ioWork import com.lagradost.cloudstream3.utils.Coroutines.ioWorkSafe import com.lagradost.cloudstream3.utils.Coroutines.main import com.lagradost.cloudstream3.utils.DataStoreHelper.deleteBookmarkedData -import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllBookmarkedDataByWatchType import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllFavorites import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllSubscriptions import com.lagradost.cloudstream3.utils.DataStoreHelper.getBookmarkedData +import com.lagradost.cloudstream3.utils.DataStoreHelper.getBookmarkedDataByWatchType import com.lagradost.cloudstream3.utils.DataStoreHelper.getDub import com.lagradost.cloudstream3.utils.DataStoreHelper.getFavoritesData import com.lagradost.cloudstream3.utils.DataStoreHelper.getLastWatched @@ -832,7 +832,7 @@ class ResultViewModel2 : ViewModel() { response.name, response.year, response.getImdbId(), - getAllBookmarkedDataByWatchType()[status] ?: emptyList() + getBookmarkedDataByWatchType(status) ) { shouldContinue: Boolean, duplicateId: Int? -> if (!shouldContinue) return@checkAndWarnDuplicates @@ -841,20 +841,21 @@ class ResultViewModel2 : ViewModel() { } setResultWatchState(currentId, status.internalId) + val current = getBookmarkedData(currentId) - val currentTime = System.currentTimeMillis() + setBookmarkedData( currentId, DataStoreHelper.BookmarkedData( - current?.bookmarkedTime ?: currentTime, - currentTime, + current?.bookmarkedTime ?: unixTimeMS, currentId, + unixTimeMS, response.name, - response.year, response.url, response.apiName, response.type, response.posterUrl, + response.year, response.getImdbId() ) ) @@ -920,15 +921,15 @@ class ResultViewModel2 : ViewModel() { currentId, DataStoreHelper.SubscribedData( current?.subscribedTime ?: unixTimeMS, - unixTimeMS, response.getLatestEpisodes(), currentId, + unixTimeMS, response.name, - response.year, response.url, response.apiName, response.type, response.posterUrl, + response.year, response.getImdbId() ) ) @@ -983,14 +984,14 @@ class ResultViewModel2 : ViewModel() { currentId, DataStoreHelper.FavoritesData( current?.favoritesTime ?: unixTimeMS, - unixTimeMS, currentId, + unixTimeMS, response.name, - response.year, response.url, response.apiName, response.type, response.posterUrl, + response.year, response.getImdbId() ) ) @@ -1010,7 +1011,7 @@ class ResultViewModel2 : ViewModel() { name: String, year: Int?, imdbId: String?, - data: List, + data: List, checkDuplicatesCallback: (shouldContinue: Boolean, duplicateId: Int?) -> Unit ) { fun normalizeString(input: String): String { diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/DataStoreHelper.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/DataStoreHelper.kt index 07762319..dc332729 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/DataStoreHelper.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/DataStoreHelper.kt @@ -352,34 +352,35 @@ object DataStoreHelper { /** * Used to display notifications on new episodes and posters in library. **/ - abstract class BaseSearchResponse( + abstract class LibrarySearchResponse( @JsonProperty("id") override var id: Int?, + @JsonProperty("latestUpdatedTime") open val latestUpdatedTime: Long, @JsonProperty("name") override val name: String, - @JsonProperty("year") open val year: Int?, @JsonProperty("url") override val url: String, @JsonProperty("apiName") override val apiName: String, - @JsonProperty("type") override var type: TvType? = null, + @JsonProperty("type") override var type: TvType?, @JsonProperty("posterUrl") override var posterUrl: String?, - @JsonProperty("imdbId") open val imdbId: String? = null, - @JsonProperty("quality") override var quality: SearchQuality? = null, - @JsonProperty("posterHeaders") override var posterHeaders: Map? = null + @JsonProperty("year") open val year: Int?, + @JsonProperty("imdbId") open val imdbId: String?, + @JsonProperty("quality") override var quality: SearchQuality?, + @JsonProperty("posterHeaders") override var posterHeaders: Map? ) : SearchResponse data class SubscribedData( @JsonProperty("subscribedTime") val subscribedTime: Long, - @JsonProperty("latestUpdatedTime") val latestUpdatedTime: Long, @JsonProperty("lastSeenEpisodeCount") val lastSeenEpisodeCount: Map, override var id: Int?, + override val latestUpdatedTime: Long, override val name: String, - override val year: Int?, override val url: String, override val apiName: String, override var type: TvType?, override var posterUrl: String?, - override var imdbId: String? = null, + override val year: Int?, + override val imdbId: String?, override var quality: SearchQuality? = null, override var posterHeaders: Map? = null - ) : BaseSearchResponse(id, name, year, url, apiName, type, posterUrl, imdbId, quality, posterHeaders) { + ) : LibrarySearchResponse(id, latestUpdatedTime, name, url, apiName, type, posterUrl, year, imdbId, quality, posterHeaders) { fun toLibraryItem(): SyncAPI.LibraryItem? { return SyncAPI.LibraryItem( name, @@ -396,18 +397,18 @@ object DataStoreHelper { data class BookmarkedData( @JsonProperty("bookmarkedTime") val bookmarkedTime: Long, - @JsonProperty("latestUpdatedTime") val latestUpdatedTime: Long, override var id: Int?, + override val latestUpdatedTime: Long, override val name: String, - override val year: Int?, override val url: String, override val apiName: String, override var type: TvType?, override var posterUrl: String?, - override var imdbId: String? = null, + override val year: Int?, + override val imdbId: String?, override var quality: SearchQuality? = null, override var posterHeaders: Map? = null - ) : BaseSearchResponse(id, name, year, url, apiName, type, posterUrl, imdbId, quality, posterHeaders) { + ) : LibrarySearchResponse(id, latestUpdatedTime, name, url, apiName, type, posterUrl, year, imdbId, quality, posterHeaders) { fun toLibraryItem(id: String): SyncAPI.LibraryItem { return SyncAPI.LibraryItem( name, @@ -424,18 +425,18 @@ object DataStoreHelper { data class FavoritesData( @JsonProperty("favoritesTime") val favoritesTime: Long, - @JsonProperty("latestUpdatedTime") val latestUpdatedTime: Long, override var id: Int?, + override val latestUpdatedTime: Long, override val name: String, - override val year: Int?, override val url: String, override val apiName: String, override var type: TvType?, override var posterUrl: String?, - override var imdbId: String? = null, + override val year: Int?, + override val imdbId: String?, override var quality: SearchQuality? = null, override var posterHeaders: Map? = null - ) : BaseSearchResponse(id, name, year, url, apiName, type, posterUrl, imdbId, quality, posterHeaders) { + ) : LibrarySearchResponse(id, latestUpdatedTime, name, url, apiName, type, posterUrl, year, imdbId, quality, posterHeaders) { fun toLibraryItem(): SyncAPI.LibraryItem? { return SyncAPI.LibraryItem( name, @@ -451,20 +452,20 @@ object DataStoreHelper { } data class ResumeWatchingResult( + @JsonProperty("name") override val name: String, + @JsonProperty("url") override val url: String, + @JsonProperty("apiName") override val apiName: String, + @JsonProperty("type") override var type: TvType? = null, + @JsonProperty("posterUrl") override var posterUrl: String?, @JsonProperty("watchPos") val watchPos: PosDur?, + @JsonProperty("id") override var id: Int?, @JsonProperty("parentId") val parentId: Int?, @JsonProperty("episode") val episode: Int?, @JsonProperty("season") val season: Int?, @JsonProperty("isFromDownload") val isFromDownload: Boolean, - override var id: Int?, - override val name: String, - override val url: String, - override val apiName: String, - override var type: TvType?, - override var posterUrl: String?, - override var quality: SearchQuality? = null, - override var posterHeaders: Map? = null - ) : BaseSearchResponse(id, name, null, url, apiName, type, posterUrl, null, quality, posterHeaders) + @JsonProperty("quality") override var quality: SearchQuality? = null, + @JsonProperty("posterHeaders") override var posterHeaders: Map? = null, + ) : SearchResponse /** * A datastore wide account for future implementations of a multiple account system @@ -588,15 +589,14 @@ object DataStoreHelper { return getKey("$currentAccount/$RESULT_WATCH_STATE_DATA", id.toString()) } - fun getAllBookmarkedDataByWatchType(): Map> { + fun getBookmarkedDataByWatchType(watchType: WatchType): List { val allBookmarkedData: List = getKeys("$currentAccount/$RESULT_WATCH_STATE_DATA")?.mapNotNull { getKey(it) } ?: emptyList() return allBookmarkedData - .groupBy { getResultWatchState(it.id ?: return emptyMap()) } - .mapValues { it.value } + .filter { getResultWatchState(it.id ?: return emptyList()) == watchType } } fun getAllSubscriptions(): List { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6ef7e62e..14418d49 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -694,24 +694,24 @@ Add to favorites Remove from favorites - Possible Duplicate Found + Potential Duplicate Found Add Replace @string/sort_cancel - A possible duplicate with the same title \'%s\' and watch status \'%s\' already exists in your library. + It appears that a potentially duplicate title \'%s\' with the watch status \'%s\' already exists in your library. - \n\nDo you want to still add this to item, replace the existing one, or cancel the action? + \n\nWould you like to add this item anyway, replace the existing one, or cancel the action? - A possible duplicate with the same title \'%s\' already exists in your subscriptions. + You are already subscribed to a title that appears to be a potential duplicate: \'%s.\' - \n\nDo you want to still subscribe to this item, replace the existing subscription, or cancel the action? + \n\nWould you like to add this subscription anyway, replace the existing one, or cancel the action? - A possible duplicate with the same title \'%s\' already exists in your favorites. + It seems that a potentially duplicate title \'%s\' is already in your favorites. - \n\nDo you want to still add this to item, replace the existing one, or cancel the action? + \n\nWould you like to add this item anyway, replace the existing one, or cancel the action?