Skip to content

Commit 597050f

Browse files
Cache parsed THEOplayer version
1 parent 124e19f commit 597050f

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

ui/src/main/java/com/theoplayer/android/ui/Helper.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,24 @@ fun rememberTrackLabel(
7070
resources: Resources = LocalResources.current,
7171
): String = remember(key1 = track.id, key2 = track.uid) {
7272
constructLabel(track) ?: resources.getString(R.string.theoplayer_ui_track_unknown)
73+
}
74+
75+
/**
76+
* Memoize the most recent call.
77+
*/
78+
internal inline fun <P, R> memoizeLast(crossinline transform: (P) -> R): (P) -> R {
79+
return object : (P) -> R {
80+
private var lastCall: Pair<P, R>? = null
81+
82+
override fun invoke(input: P): R {
83+
val lastCall = this.lastCall
84+
return if (lastCall != null && lastCall.first == input) {
85+
lastCall.second
86+
} else {
87+
transform(input).also { output ->
88+
this.lastCall = input to output
89+
}
90+
}
91+
}
92+
}
7393
}

ui/src/main/java/com/theoplayer/android/ui/util/TrackExts.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.theoplayer.android.ui.util
22

33
import androidx.annotation.CheckResult
4-
import com.theoplayer.android.api.THEOplayerGlobal
54
import com.theoplayer.android.api.player.track.Track
65
import com.theoplayer.android.api.player.track.texttrack.TextTrack
76
import com.theoplayer.android.api.player.track.texttrack.TextTrackType
@@ -52,11 +51,9 @@ internal val Track.localizedLanguageName: String?
5251
internal fun constructLabel(
5352
track: Track,
5453
): String? {
55-
val playerVersion = Version.parse(THEOplayerGlobal.getVersion()) ?: Version.ZERO
56-
5754
val label: String? = if (
5855
(track is TextTrack) &&
59-
playerVersion.major < 11 &&
56+
theoplayerVersion.major < 11 &&
6057
(isLabelCeaFormatted(track.label) || (track.label != null && track.language == track.label))
6158
) {
6259
// If we are below 11th major release

ui/src/main/java/com/theoplayer/android/ui/util/VersionUtil.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.theoplayer.android.ui.util
22

3+
import com.theoplayer.android.api.THEOplayerGlobal
4+
import com.theoplayer.android.ui.memoizeLast
5+
36
private const val VERSION_DELIMITER = '.'
47

58
/**
@@ -42,3 +45,11 @@ internal data class Version(
4245
}
4346
}
4447
}
48+
49+
private val getCachedTheoplayerVersion = memoizeLast(Version::parse)
50+
51+
/**
52+
* Returns the major version of THEOplayer.
53+
*/
54+
internal val theoplayerVersion: Version
55+
get() = getCachedTheoplayerVersion(THEOplayerGlobal.getVersion()) ?: Version.ZERO

0 commit comments

Comments
 (0)