Skip to content

Commit 6830890

Browse files
committed
PlayerUIList: transform to kotlin
And simplify the code a little
1 parent fbafdeb commit 6830890

File tree

2 files changed

+92
-90
lines changed

2 files changed

+92
-90
lines changed

app/src/main/java/org/schabi/newpipe/player/ui/PlayerUiList.java

Lines changed: 0 additions & 90 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.schabi.newpipe.player.ui
2+
3+
import androidx.core.util.Consumer
4+
import java.util.Optional
5+
6+
class PlayerUiList(vararg initialPlayerUis: PlayerUi) {
7+
val playerUis = mutableListOf<PlayerUi>()
8+
9+
/**
10+
* Creates a [PlayerUiList] starting with the provided player uis. The provided player uis
11+
* will not be prepared like those passed to [.addAndPrepare], because when
12+
* the [PlayerUiList] constructor is called, the player is still not running and it
13+
* wouldn't make sense to initialize uis then. Instead the player will initialize them by doing
14+
* proper calls to [.call].
15+
*
16+
* @param initialPlayerUis the player uis this list should start with; the order will be kept
17+
*/
18+
init {
19+
playerUis.addAll(listOf(*initialPlayerUis))
20+
}
21+
22+
/**
23+
* Adds the provided player ui to the list and calls on it the initialization functions that
24+
* apply based on the current player state. The preparation step needs to be done since when UIs
25+
* are removed and re-added, the player will not call e.g. initPlayer again since the exoplayer
26+
* is already initialized, but we need to notify the newly built UI that the player is ready
27+
* nonetheless.
28+
* @param playerUi the player ui to prepare and add to the list; its [PlayerUi.getPlayer]
29+
* will be used to query information about the player state
30+
*/
31+
fun addAndPrepare(playerUi: PlayerUi) {
32+
if (playerUi.getPlayer().fragmentListener.isPresent) {
33+
// make sure UIs know whether a service is connected or not
34+
playerUi.onFragmentListenerSet()
35+
}
36+
37+
if (!playerUi.getPlayer().exoPlayerIsNull()) {
38+
playerUi.initPlayer()
39+
if (playerUi.getPlayer().playQueue != null) {
40+
playerUi.initPlayback()
41+
}
42+
}
43+
44+
playerUis.add(playerUi)
45+
}
46+
47+
/**
48+
* Destroys all matching player UIs and removes them from the list.
49+
* @param playerUiType the class of the player UI to destroy;
50+
* the [Class.isInstance] method will be used, so even subclasses will be
51+
* destroyed and removed
52+
* @param T the class type parameter </T>
53+
* */
54+
fun <T> destroyAll(playerUiType: Class<T?>) {
55+
for (ui in playerUis) {
56+
if (playerUiType.isInstance(ui)) {
57+
ui.destroyPlayer()
58+
ui.destroy()
59+
playerUis.remove(ui)
60+
}
61+
}
62+
}
63+
64+
/**
65+
* @param playerUiType the class of the player UI to return;
66+
* the [Class.isInstance] method will be used, so even subclasses could be returned
67+
* @param T the class type parameter
68+
* @return the first player UI of the required type found in the list, or an empty
69+
* [ ] otherwise
70+
</T> */
71+
fun <T> get(playerUiType: Class<T>): Optional<T & Any> {
72+
for (ui in playerUis) {
73+
if (playerUiType.isInstance(ui)) {
74+
when (val r = playerUiType.cast(ui)) {
75+
null -> continue
76+
else -> return Optional.of(r)
77+
}
78+
}
79+
}
80+
return Optional.empty()
81+
}
82+
83+
/**
84+
* Calls the provided consumer on all player UIs in the list, in order of addition.
85+
* @param consumer the consumer to call with player UIs
86+
*/
87+
fun call(consumer: java.util.function.Consumer<PlayerUi>) {
88+
for (ui in playerUis) {
89+
consumer.accept(ui)
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)