Skip to content

Commit 57c3d33

Browse files
authored
Fix: Race conditon fix, and code cleanup
1 parent fbc588b commit 57c3d33

File tree

4 files changed

+104
-117
lines changed

4 files changed

+104
-117
lines changed

app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeParentItemAdapterPreview.kt

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import com.lagradost.cloudstream3.ui.account.AccountHelper.showAccountEditDialog
3939
import com.lagradost.cloudstream3.ui.account.AccountHelper.showAccountSelectLinear
4040
import com.lagradost.cloudstream3.ui.account.AccountViewModel
4141
import com.lagradost.cloudstream3.ui.result.FOCUS_SELF
42+
import com.lagradost.cloudstream3.ui.result.ResultFragment.bindLogo
4243
import com.lagradost.cloudstream3.ui.result.ResultViewModel2
4344
import com.lagradost.cloudstream3.ui.result.START_ACTION_RESUME_LATEST
4445
import com.lagradost.cloudstream3.ui.result.getId
@@ -58,7 +59,6 @@ import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showOptionSelectSt
5859
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbarMargin
5960
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbarView
6061
import com.lagradost.cloudstream3.utils.UIHelper.populateChips
61-
import com.lagradost.cloudstream3.utils.UiImage
6262

6363
class HomeParentItemAdapterPreview(
6464
val fragment: LifecycleOwner,
@@ -352,40 +352,12 @@ class HomeParentItemAdapterPreview(
352352
)
353353

354354

355-
val logoUrl = item.logoUrl?.takeIf { it.isNotBlank() }
356-
357-
homeBackgroundPosterWatermarkBadgeHolder.isVisible = false
358-
homeBackgroundPosterWatermarkBadgeHolder.alpha = 0f
359-
homePreviewText.isVisible = false
360-
361-
if (logoUrl != null) {
362-
homeBackgroundPosterWatermarkBadgeHolder.loadImage(
363-
imageData = UiImage.Image(
364-
logoUrl,
365-
headers = item.posterHeaders
366-
),
367-
builder = {
368-
listener(
369-
onSuccess = { _, _ ->
370-
// logo loaded → show logo only
371-
homeBackgroundPosterWatermarkBadgeHolder.isVisible = true
372-
homeBackgroundPosterWatermarkBadgeHolder.alpha = 1f
373-
homePreviewText.isVisible = false
374-
},
375-
onError = { _, _ ->
376-
// logo failed → show title
377-
homeBackgroundPosterWatermarkBadgeHolder.isVisible = false
378-
homePreviewText.isVisible = true
379-
}
380-
)
381-
}
382-
)
383-
} else {
384-
// no logo → show title immediately
385-
homeBackgroundPosterWatermarkBadgeHolder.isVisible = false
386-
homePreviewText.isVisible = true
387-
}
388-
355+
bindLogo(
356+
url = item.logoUrl,
357+
headers = item.posterHeaders,
358+
titleView = homePreviewText,
359+
logoView = homeBackgroundPosterWatermarkBadgeHolder
360+
)
389361

390362
homePreviewTags.isGone =
391363
item.tags.isNullOrEmpty()

app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.lagradost.cloudstream3.ui.result
22

33
import android.os.Bundle
4+
import android.widget.ImageView
5+
import android.widget.TextView
6+
import androidx.core.view.isVisible
47
import androidx.fragment.app.Fragment
58
import androidx.preference.PreferenceManager
9+
import coil3.dispose
610
import com.lagradost.cloudstream3.DubStatus
711
import com.lagradost.cloudstream3.R
812
import com.lagradost.cloudstream3.Score
@@ -15,6 +19,8 @@ import com.lagradost.cloudstream3.utils.DataStoreHelper
1519
import com.lagradost.cloudstream3.utils.DataStoreHelper.getVideoWatchState
1620
import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos
1721
import com.lagradost.cloudstream3.utils.Event
22+
import com.lagradost.cloudstream3.utils.ImageLoader.loadImage
23+
import com.lagradost.cloudstream3.utils.UiImage
1824

1925
const val START_ACTION_RESUME_LATEST = 1
2026
const val START_ACTION_LOAD_EP = 2
@@ -233,6 +239,44 @@ object ResultFragment {
233239
val restart: Boolean,
234240
)
235241

242+
fun bindLogo(
243+
url: String?,
244+
headers: Map<String, String>?,
245+
logoView: ImageView,
246+
titleView: TextView
247+
) {
248+
// Cancel it, as we want to remove the listener onSuccess race condition
249+
logoView.dispose()
250+
251+
if (url.isNullOrBlank()) {
252+
logoView.isVisible = false
253+
titleView.isVisible = true
254+
return
255+
}
256+
257+
logoView.isVisible = true
258+
titleView.isVisible = false
259+
260+
logoView.loadImage(
261+
imageData = UiImage.Image(url, headers = headers),
262+
builder = {
263+
listener(
264+
onSuccess = { _, _ ->
265+
logoView.isVisible = true
266+
titleView.isVisible = false
267+
},
268+
onError = { _, _ ->
269+
logoView.isVisible = false
270+
titleView.isVisible = true
271+
},
272+
onCancel = {
273+
// If we manually cancel, then it should not do anything
274+
}
275+
)
276+
}
277+
)
278+
}
279+
236280
fun Fragment.getStoredData(): StoredData? {
237281
val context = this.context ?: this.activity ?: return null
238282
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)

app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragmentPhone.kt

Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import com.lagradost.cloudstream3.ui.download.DownloadButtonSetup
5959
import com.lagradost.cloudstream3.ui.player.CSPlayerEvent
6060
import com.lagradost.cloudstream3.ui.player.FullScreenPlayer
6161
import com.lagradost.cloudstream3.ui.quicksearch.QuickSearchFragment
62+
import com.lagradost.cloudstream3.ui.result.ResultFragment.bindLogo
6263
import com.lagradost.cloudstream3.ui.result.ResultFragment.getStoredData
6364
import com.lagradost.cloudstream3.ui.result.ResultFragment.updateUIEvent
6465
import com.lagradost.cloudstream3.ui.search.SearchAdapter
@@ -86,14 +87,11 @@ import com.lagradost.cloudstream3.utils.UIHelper.populateChips
8687
import com.lagradost.cloudstream3.utils.UIHelper.popupMenuNoIconsAndNoStringRes
8788
import com.lagradost.cloudstream3.utils.UIHelper.setListViewHeightBasedOnItems
8889
import com.lagradost.cloudstream3.utils.UIHelper.setNavigationBarColorCompat
89-
import com.lagradost.cloudstream3.utils.UiImage
9090
import com.lagradost.cloudstream3.utils.VideoDownloadHelper
9191
import com.lagradost.cloudstream3.utils.getImageFromDrawable
9292
import com.lagradost.cloudstream3.utils.setText
9393
import com.lagradost.cloudstream3.utils.setTextHtml
9494
import java.net.URLEncoder
95-
import java.nio.charset.Charset
96-
import kotlin.io.encoding.Base64
9795
import kotlin.math.roundToInt
9896

9997
open class ResultFragmentPhone : FullScreenPlayer() {
@@ -141,7 +139,7 @@ open class ResultFragmentPhone : FullScreenPlayer() {
141139
}
142140
}
143141

144-
var currentTrailers: List<Pair<ExtractorLink,String>> = emptyList()
142+
var currentTrailers: List<Pair<ExtractorLink, String>> = emptyList()
145143
var currentTrailerIndex = 0
146144

147145
override fun nextMirror() {
@@ -164,25 +162,26 @@ open class ResultFragmentPhone : FullScreenPlayer() {
164162
private fun loadTrailer(index: Int? = null) {
165163

166164
val isSuccess =
167-
currentTrailers.getOrNull(index ?: currentTrailerIndex)?.let { (extractedTrailerLink,_) ->
168-
context?.let { ctx ->
169-
player.onPause()
170-
player.loadPlayer(
171-
ctx,
172-
false,
173-
extractedTrailerLink,
174-
null,
175-
startPosition = 0L,
176-
subtitles = emptySet(),
177-
subtitle = null,
178-
autoPlay = false,
179-
preview = false
180-
)
181-
true
165+
currentTrailers.getOrNull(index ?: currentTrailerIndex)
166+
?.let { (extractedTrailerLink, _) ->
167+
context?.let { ctx ->
168+
player.onPause()
169+
player.loadPlayer(
170+
ctx,
171+
false,
172+
extractedTrailerLink,
173+
null,
174+
startPosition = 0L,
175+
subtitles = emptySet(),
176+
subtitle = null,
177+
autoPlay = false,
178+
preview = false
179+
)
180+
true
181+
} ?: run {
182+
false
183+
}
182184
} ?: run {
183-
false
184-
}
185-
} ?: run {
186185
false
187186
}
188187
//result_trailer_thumbnail?.setImageBitmap(result_poster_background?.drawable?.toBitmap())
@@ -191,7 +190,17 @@ open class ResultFragmentPhone : FullScreenPlayer() {
191190
// result_trailer_loading?.isVisible = isSuccess
192191
val turnVis = !isSuccess && !isFullScreenPlayer
193192
resultBinding?.apply {
194-
resultTitle.isVisible = isSuccess
193+
// If we load a trailer, then cancel the big logo and only show the small title
194+
if (isSuccess) {
195+
// This is still a bit of a race condition, but it should work if we have the
196+
// trailers observe after the page observe!
197+
bindLogo(
198+
url = null,
199+
headers = null,
200+
logoView = backgroundPosterWatermarkBadge,
201+
titleView = resultTitle
202+
)
203+
}
195204
resultSmallscreenHolder.isVisible = turnVis
196205
resultPosterBackgroundHolder.apply {
197206
val fadeIn: Animation = AlphaAnimation(alpha, if (turnVis) 1.0f else 0.0f).apply {
@@ -227,7 +236,7 @@ open class ResultFragmentPhone : FullScreenPlayer() {
227236
//}
228237
}
229238

230-
private fun setTrailers(trailers: List<Pair<ExtractorLink,String>>?) {
239+
private fun setTrailers(trailers: List<Pair<ExtractorLink, String>>?) {
231240
context?.updateHasTrailers()
232241
if (!LoadResponse.isTrailersEnabled) return
233242
currentTrailers = trailers?.sortedBy { -it.first.quality } ?: emptyList()
@@ -571,7 +580,7 @@ open class ResultFragmentPhone : FullScreenPlayer() {
571580

572581
playerBinding?.apply {
573582
playerOpenSource.setOnClickListener {
574-
currentTrailers.getOrNull(currentTrailerIndex)?.let {(_,ogTrailerLink)->
583+
currentTrailers.getOrNull(currentTrailerIndex)?.let { (_, ogTrailerLink) ->
575584
context?.openBrowser(ogTrailerLink)
576585
}
577586
}
@@ -683,10 +692,6 @@ open class ResultFragmentPhone : FullScreenPlayer() {
683692
binding?.resultFavorite?.setImageResource(drawable)
684693
}
685694

686-
observe(viewModel.trailers) { trailers ->
687-
setTrailers(trailers.flatMap { it.mirros }) // I dont care about subtitles yet!
688-
}
689-
690695
observeNullable(viewModel.episodes) { episodes ->
691696
resultBinding?.apply {
692697
// no failure?
@@ -803,31 +808,12 @@ open class ResultFragmentPhone : FullScreenPlayer() {
803808
}
804809
}
805810

806-
if (!d.logoUrl.isNullOrBlank()) {
807-
backgroundPosterWatermarkBadge.isVisible = true
808-
resultTitle.isVisible = false
809-
810-
backgroundPosterWatermarkBadge.loadImage(
811-
imageData = UiImage.Image(d.logoUrl,headers = d.posterHeaders),
812-
builder = {
813-
listener(
814-
onSuccess = { _, _ ->
815-
backgroundPosterWatermarkBadge.isVisible = true
816-
resultTitle.isVisible = false
817-
},
818-
onError = { _, _ ->
819-
backgroundPosterWatermarkBadge.isVisible = false
820-
resultTitle.isVisible = true
821-
}
822-
)
823-
}
824-
)
825-
826-
} else {
827-
backgroundPosterWatermarkBadge.isVisible = false
828-
resultTitle.isVisible = true
829-
}
830-
811+
bindLogo(
812+
url = d.logoUrl,
813+
headers = d.posterHeaders,
814+
titleView = resultTitle,
815+
logoView = backgroundPosterWatermarkBadge
816+
)
831817

832818
var isExpanded = false
833819
resultDescription.apply {
@@ -945,6 +931,10 @@ open class ResultFragmentPhone : FullScreenPlayer() {
945931
}
946932
}
947933

934+
observe(viewModel.trailers) { trailers ->
935+
setTrailers(trailers.flatMap { it.mirros }) // I dont care about subtitles yet!
936+
}
937+
948938
observe(syncModel.synced) { list ->
949939
syncBinding?.resultSyncNames?.text =
950940
list.filter { it.isSynced && it.hasAccount }.joinToString { it.name }

app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragmentTv.kt

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import com.lagradost.cloudstream3.ui.player.ExtractorLinkGenerator
3636
import com.lagradost.cloudstream3.ui.player.GeneratorPlayer
3737
import com.lagradost.cloudstream3.ui.player.NEXT_WATCH_EPISODE_PERCENTAGE
3838
import com.lagradost.cloudstream3.ui.quicksearch.QuickSearchFragment
39+
import com.lagradost.cloudstream3.ui.result.ResultFragment.bindLogo
3940
import com.lagradost.cloudstream3.ui.result.ResultFragment.getStoredData
4041
import com.lagradost.cloudstream3.ui.result.ResultFragment.updateUIEvent
4142
import com.lagradost.cloudstream3.ui.search.SEARCH_ACTION_FOCUSED
@@ -547,7 +548,8 @@ class ResultFragmentTv : BaseFragment<FragmentResultTvBinding>(
547548
observe(viewModel.trailers) { trailersLinks ->
548549
context?.updateHasTrailers()
549550
if (!LoadResponse.isTrailersEnabled) return@observe
550-
val extractedTrailerLinks = trailersLinks.flatMap{ it.mirros }.map{ (extractedTrailerLink,_) -> extractedTrailerLink }
551+
val extractedTrailerLinks = trailersLinks.flatMap { it.mirros }
552+
.map { (extractedTrailerLink, _) -> extractedTrailerLink }
551553
binding.apply {
552554
resultPlayTrailer.isGone = extractedTrailerLinks.isEmpty()
553555
resultPlayTrailerButton.setOnClickListener {
@@ -912,33 +914,12 @@ class ResultFragmentTv : BaseFragment<FragmentResultTvBinding>(
912914
error { getImageFromDrawable(context ?: return@error null, error) }
913915
}
914916

915-
if (!d.logoUrl.isNullOrBlank()) {
916-
917-
backgroundPosterWatermarkBadgeHolder.isVisible = true
918-
resultTitle.isVisible = false
919-
920-
backgroundPosterWatermarkBadgeHolder.loadImage(
921-
imageData = UiImage.Image(d.logoUrl,headers = d.posterHeaders),
922-
builder = {
923-
listener(
924-
onSuccess = { _, _ ->
925-
backgroundPosterWatermarkBadgeHolder.isVisible = true
926-
resultTitle.isVisible = false
927-
},
928-
onError = { _, _ ->
929-
backgroundPosterWatermarkBadgeHolder.isVisible = false
930-
resultTitle.isVisible = true
931-
}
932-
)
933-
}
934-
)
935-
936-
} else {
937-
backgroundPosterWatermarkBadgeHolder.isVisible = false
938-
resultTitle.isVisible = true
939-
}
940-
941-
917+
bindLogo(
918+
url = d.logoUrl,
919+
headers = d.posterHeaders,
920+
titleView = resultTitle,
921+
logoView = backgroundPosterWatermarkBadgeHolder
922+
)
942923

943924
comingSoon = d.comingSoon
944925
resultTvComingSoon.isVisible = d.comingSoon

0 commit comments

Comments
 (0)