From 9b948531991862ded5eb6b1b307fbf0a4291df33 Mon Sep 17 00:00:00 2001 From: LagradOst <11805592+LagradOst@users.noreply.github.com> Date: Fri, 27 May 2022 00:34:09 +0200 Subject: [PATCH] fixed small crash issue --- .../cloudstream3/ui/player/GeneratorPlayer.kt | 49 +++++++++-------- .../cloudstream3/ui/search/SearchHelper.kt | 41 +++++++++------ .../lagradost/cloudstream3/utils/DataStore.kt | 52 ++++++++++++------- .../cloudstream3/utils/DataStoreHelper.kt | 2 +- app/src/main/res/values/strings.xml | 1 + 5 files changed, 84 insertions(+), 61 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt index 64367eb0..5a3d3f7e 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt @@ -560,32 +560,35 @@ class GeneratorPlayer : FullScreenPlayer() { } //Generate video title - var playerVideoTitle = if (headerName != null) { - (headerName + - if (tvType.isEpisodeBased() && episode != null) - if (season == null) - " - ${getString(R.string.episode)} $episode" - else - " \"${getString(R.string.season_short)}${season}:${getString(R.string.episode_short)}${episode}\"" - else "") + if (subName.isNullOrBlank() || subName == headerName) "" else " - $subName" - } else { - "" - } - - //Hide title, if set in setting - if (limitTitle < 0) { - player_video_title?.visibility = View.GONE - } else { - //Truncate video title if it exceeds limit - val differenceInLength = playerVideoTitle.length - limitTitle - val margin = 3 //If the difference is smaller than or equal to this value, ignore it - if (limitTitle > 0 && differenceInLength > margin) { - playerVideoTitle = playerVideoTitle.substring(0, limitTitle - 1) + "..." + context?.let { ctx -> + var playerVideoTitle = if (headerName != null) { + (headerName + + if (tvType.isEpisodeBased() && episode != null) + if (season == null) + " - ${ctx.getString(R.string.episode)} $episode" + else + " \"${ctx.getString(R.string.season_short)}${season}:${ctx.getString(R.string.episode_short)}${episode}\"" + else "") + if (subName.isNullOrBlank() || subName == headerName) "" else " - $subName" + } else { + "" } + + //Hide title, if set in setting + if (limitTitle < 0) { + player_video_title?.visibility = View.GONE + } else { + //Truncate video title if it exceeds limit + val differenceInLength = playerVideoTitle.length - limitTitle + val margin = 3 //If the difference is smaller than or equal to this value, ignore it + if (limitTitle > 0 && differenceInLength > margin) { + playerVideoTitle = playerVideoTitle.substring(0, limitTitle - 1) + "..." + } + } + + player_episode_filler_holder?.isVisible = isFiller ?: false + player_video_title?.text = playerVideoTitle } - player_episode_filler_holder?.isVisible = isFiller ?: false - player_video_title?.text = playerVideoTitle } @SuppressLint("SetTextI18n") diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchHelper.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchHelper.kt index 5f71c0a8..cbaed031 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchHelper.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchHelper.kt @@ -3,6 +3,7 @@ package com.lagradost.cloudstream3.ui.search import android.app.Activity import android.widget.Toast import com.lagradost.cloudstream3.CommonActivity.showToast +import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.ui.download.DOWNLOAD_ACTION_PLAY_FILE import com.lagradost.cloudstream3.ui.download.DownloadButtonSetup.handleDownloadClick import com.lagradost.cloudstream3.ui.download.DownloadClickEvent @@ -20,26 +21,32 @@ object SearchHelper { } SEARCH_ACTION_PLAY_FILE -> { if (card is DataStoreHelper.ResumeWatchingResult) { - if (card.isFromDownload) { - handleDownloadClick( - activity, card.name, DownloadClickEvent( - DOWNLOAD_ACTION_PLAY_FILE, - VideoDownloadHelper.DownloadEpisodeCached( - card.name, - card.posterUrl, - card.episode ?: 0, - card.season, - card.id!!, - card.parentId ?: return, - null, - null, - System.currentTimeMillis() + val id = card.id + if(id == null) { + showToast(activity, R.string.error_invalid_id, Toast.LENGTH_SHORT) + } else { + if (card.isFromDownload) { + handleDownloadClick( + activity, card.name, DownloadClickEvent( + DOWNLOAD_ACTION_PLAY_FILE, + VideoDownloadHelper.DownloadEpisodeCached( + card.name, + card.posterUrl, + card.episode ?: 0, + card.season, + id, + card.parentId ?: return, + null, + null, + System.currentTimeMillis() + ) ) ) - ) - } else { - activity.loadSearchResult(card, START_ACTION_LOAD_EP, card.id) + } else { + activity.loadSearchResult(card, START_ACTION_LOAD_EP, id) + } } + } else { handleSearchClickCallback( activity, diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/DataStore.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/DataStore.kt index cd7b2248..3ad95730 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/DataStore.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/DataStore.kt @@ -6,6 +6,7 @@ import androidx.preference.PreferenceManager import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.json.JsonMapper import com.fasterxml.jackson.module.kotlin.KotlinModule +import com.lagradost.cloudstream3.mvvm.logError const val DOWNLOAD_HEADER_CACHE = "download_header_cache" @@ -13,9 +14,8 @@ const val DOWNLOAD_HEADER_CACHE = "download_header_cache" const val DOWNLOAD_EPISODE_CACHE = "download_episode_cache" const val VIDEO_PLAYER_BRIGHTNESS = "video_player_alpha_key" const val HOMEPAGE_API = "home_api_used" -const val SEARCH_PROVIDER_TOGGLE = "settings_providers_toggle" -const val PREFERENCES_NAME: String = "rebuild_preference" +const val PREFERENCES_NAME = "rebuild_preference" object DataStore { val mapper: JsonMapper = JsonMapper.builder().addModule(KotlinModule()) @@ -34,17 +34,21 @@ object DataStore { } fun Context.setKeyRaw(path: String, value: T, isEditingAppSettings: Boolean = false) { - val editor: SharedPreferences.Editor = - if (isEditingAppSettings) getDefaultSharedPrefs().edit() else getSharedPrefs().edit() - when (value) { - is Boolean -> editor.putBoolean(path, value) - is Int -> editor.putInt(path, value) - is String -> editor.putString(path, value) - is Float -> editor.putFloat(path, value) - is Long -> editor.putLong(path, value) - (value as? Set != null) -> editor.putStringSet(path, value as Set) + try { + val editor: SharedPreferences.Editor = + if (isEditingAppSettings) getDefaultSharedPrefs().edit() else getSharedPrefs().edit() + when (value) { + is Boolean -> editor.putBoolean(path, value) + is Int -> editor.putInt(path, value) + is String -> editor.putString(path, value) + is Float -> editor.putFloat(path, value) + is Long -> editor.putLong(path, value) + (value as? Set != null) -> editor.putStringSet(path, value as Set) + } + editor.apply() + } catch (e: Exception) { + logError(e) } - editor.apply() } fun Context.getDefaultSharedPrefs(): SharedPreferences { @@ -69,11 +73,15 @@ object DataStore { } fun Context.removeKey(path: String) { - val prefs = getSharedPrefs() - if (prefs.contains(path)) { - val editor: SharedPreferences.Editor = prefs.edit() - editor.remove(path) - editor.apply() + try { + val prefs = getSharedPrefs() + if (prefs.contains(path)) { + val editor: SharedPreferences.Editor = prefs.edit() + editor.remove(path) + editor.apply() + } + } catch (e: Exception) { + logError(e) } } @@ -86,9 +94,13 @@ object DataStore { } fun Context.setKey(path: String, value: T) { - val editor: SharedPreferences.Editor = getSharedPrefs().edit() - editor.putString(path, mapper.writeValueAsString(value)) - editor.apply() + try { + val editor: SharedPreferences.Editor = getSharedPrefs().edit() + editor.putString(path, mapper.writeValueAsString(value)) + editor.apply() + } catch (e: Exception) { + logError(e) + } } fun Context.setKey(folder: String, path: String, value: T) { 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 f1744f6e..3fef6756 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/DataStoreHelper.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/DataStoreHelper.kt @@ -132,7 +132,7 @@ object DataStoreHelper { ) } - fun removeLastWatchedOld(parentId: Int?) { + private fun removeLastWatchedOld(parentId: Int?) { if (parentId == null) return removeKey("$currentAccount/$RESULT_RESUME_WATCHING_OLD", parentId.toString()) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2ca74e99..4569994a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -520,4 +520,5 @@ Resolution and title Title Resolution + Invalid id