File tree Expand file tree Collapse file tree
ui/src/main/java/com/theoplayer/android/ui Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11package com.theoplayer.android.ui.util
22
33import androidx.annotation.CheckResult
4- import com.theoplayer.android.api.THEOplayerGlobal
54import com.theoplayer.android.api.player.track.Track
65import com.theoplayer.android.api.player.track.texttrack.TextTrack
76import com.theoplayer.android.api.player.track.texttrack.TextTrackType
@@ -52,11 +51,9 @@ internal val Track.localizedLanguageName: String?
5251internal 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
Original file line number Diff line number Diff line change 11package com.theoplayer.android.ui.util
22
3+ import com.theoplayer.android.api.THEOplayerGlobal
4+ import com.theoplayer.android.ui.memoizeLast
5+
36private 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
You can’t perform that action at this time.
0 commit comments