From 7150ebc04cfc249cad0d4d849933d5a0618fca55 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sun, 7 Jun 2026 21:55:53 -0400 Subject: [PATCH] Optimize ViewVideoViewModel.loadVideoLink. --- .../infinityforreddit/VideoLinkFetcherKt.kt | 3 +- .../viewmodels/ViewVideoViewModel.kt | 31 ++++++------------- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/VideoLinkFetcherKt.kt b/app/src/main/java/ml/docilealligator/infinityforreddit/VideoLinkFetcherKt.kt index 18f2016b..e7bfe048 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/VideoLinkFetcherKt.kt +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/VideoLinkFetcherKt.kt @@ -270,9 +270,8 @@ private suspend fun parseRedgifsVideoLinks( if (mp4.contains("-silent")) { mp4 = mp4.substring(0, mp4.indexOf("-silent")) + ".mp4" } - val mp4Name = mp4 - AppResult.Success(Pair(mp4Name, mp4Name)) + AppResult.Success(Pair(mp4, mp4)) } catch (e: JSONException) { e.printStackTrace() AppResult.Error(null) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewVideoViewModel.kt b/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewVideoViewModel.kt index cf677b19..f38f0c9b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewVideoViewModel.kt +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/viewmodels/ViewVideoViewModel.kt @@ -42,8 +42,8 @@ class ViewVideoViewModel( private val vReddItUrl: String?, private var streamableShortCode: String?, var isDataSavingMode: Boolean = false, - var dataSavingModeDefaultResolution: Int = 0, - var nonDataSavingModeDefaultResolution: Int = 0, + val dataSavingModeDefaultResolution: Int = 0, + val nonDataSavingModeDefaultResolution: Int = 0, var playbackSpeed: Int ) : ViewModel() { var wasPlaying: Boolean = false @@ -90,7 +90,6 @@ class ViewVideoViewModel( streamableApiProvider: Provider, currentAccountSharedPreferences: SharedPreferences, ) { - //https://www.redgifs.com/watch/mortifiedunhealthyptarmigan viewModelScope.launch { val result = fetchVideoLink( retrofit, vReddItRetrofit, redgifsRetrofit, streamableApiProvider, @@ -99,12 +98,11 @@ class ViewVideoViewModel( ) when (result) { - is AppResult.Success<*> -> { + is AppResult.Success -> { when (val data = result.data) { is StreamableVideo -> { videoDownloadUrl = data.mp4?.url ?: data.mp4Mobile?.url _videoUri.value = videoDownloadUrl?.toUri() - //title = } is Pair<*, *> -> { @@ -117,37 +115,28 @@ class ViewVideoViewModel( redgifsId = data.newRedgifsId streamableShortCode = data.newStreamableShortCode videoFallbackDirectUrl = data.post.videoFallBackDirectUrl - // post = + post = data.post val optionalResult = data.optionalResult optionalResult?.let { when (it) { - is AppResult.Success<*> -> { + is AppResult.Success -> { when (val optionalData = it.data) { is StreamableVideo -> { videoDownloadUrl = optionalData.mp4?.url ?: optionalData.mp4Mobile?.url _videoUri.value = videoDownloadUrl?.toUri() - //title = } is Pair<*, *> -> { - if (redgifsId == null) { - // Imgur - } else { - // Redgifs - _videoUri.value = (optionalData.first as? String)?.toUri() - videoDownloadUrl = optionalData.first as? String - } + // Redgifs or Imgur + _videoUri.value = (optionalData.first as? String)?.toUri() + videoDownloadUrl = optionalData.second as? String } } } - is AppResult.Error<*> -> { - (it.error as? Int?)?.let { - - } ?: run { - - } + is AppResult.Error -> { + _errorResId.value = LiveDataState.Value(it.error as? Int ?: R.string.error_fetching_video) } } } ?: run {