-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTHEOliveEventAdapter.kt
More file actions
100 lines (88 loc) · 4.31 KB
/
Copy pathTHEOliveEventAdapter.kt
File metadata and controls
100 lines (88 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.theoplayer.theolive
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.WritableMap
import com.theoplayer.android.api.event.EventListener
import com.theoplayer.android.api.event.player.theolive.DistributionLoadStartEvent
import com.theoplayer.android.api.event.player.theolive.DistributionLoadedEvent
import com.theoplayer.android.api.event.player.theolive.DistributionOfflineEvent
import com.theoplayer.android.api.event.player.theolive.EndpointLoadedEvent
import com.theoplayer.android.api.event.player.theolive.IntentToFallbackEvent
import com.theoplayer.android.api.event.player.theolive.TheoLiveEventTypes
import com.theoplayer.android.api.player.theolive.TheoLive
import com.theoplayer.util.PayloadBuilder
private const val EVENT_PROP_TYPE = "type"
private const val EVENT_PROP_DISTRIBUTION_ID = "distributionId"
private const val EVENT_PROP_DISTRIBUTION = "distribution"
private const val EVENT_PROP_ENDPOINT = "endpoint"
private const val EVENT_PROP_REASON = "reason"
private const val EVENT_PROP_NAME = "name"
private const val EVENT_PROP_ID = "id"
private const val EVENT_PROP_EXTERNAL_ID = "externalId"
class THEOliveEventAdapter(private val theoLiveApi: TheoLive, private val emitter: Emitter) {
interface Emitter {
fun emit(payload: WritableMap?)
}
private val onDistributionLoadStart =
EventListener<DistributionLoadStartEvent> { onDistributionLoadStart(it) }
private val onDistributionLoaded =
EventListener<DistributionLoadedEvent> { onDistributionLoaded(it) }
private val onDistributionOffline =
EventListener<DistributionOfflineEvent> { onDistributionOffline(it) }
private val onEndPointLoaded =
EventListener<EndpointLoadedEvent> { onEndPointLoaded(it) }
private val onIntentOfFallback =
EventListener<IntentToFallbackEvent> { onIntentOfFallback(it) }
init {
theoLiveApi.addEventListener(TheoLiveEventTypes.DISTRIBUTIONLOADSTART, onDistributionLoadStart)
theoLiveApi.addEventListener(TheoLiveEventTypes.DISTRIBUTIONLOADED, onDistributionLoaded)
theoLiveApi.addEventListener(TheoLiveEventTypes.DISTRIBUTIONOFFLINE, onDistributionOffline)
theoLiveApi.addEventListener(TheoLiveEventTypes.ENDPOINTLOADED, onEndPointLoaded)
theoLiveApi.addEventListener(TheoLiveEventTypes.INTENTTOFALLBACK, onIntentOfFallback)
}
fun destroy() {
theoLiveApi.removeEventListener(
TheoLiveEventTypes.DISTRIBUTIONLOADSTART,
onDistributionLoadStart
)
theoLiveApi.removeEventListener(TheoLiveEventTypes.DISTRIBUTIONLOADED, onDistributionLoaded)
theoLiveApi.removeEventListener(TheoLiveEventTypes.DISTRIBUTIONOFFLINE, onDistributionOffline)
theoLiveApi.removeEventListener(TheoLiveEventTypes.ENDPOINTLOADED, onEndPointLoaded)
theoLiveApi.removeEventListener(TheoLiveEventTypes.INTENTTOFALLBACK, onIntentOfFallback)
}
private fun onDistributionLoadStart(event: DistributionLoadStartEvent) {
emitter.emit(Arguments.createMap().apply {
putString(EVENT_PROP_TYPE, "distributionloadstart")
putString(EVENT_PROP_DISTRIBUTION_ID, event.getDistributionId())
})
}
private fun onDistributionLoaded(event: DistributionLoadedEvent) {
emitter.emit(Arguments.createMap().apply {
putString(EVENT_PROP_TYPE, "distributionloaded")
putMap(EVENT_PROP_DISTRIBUTION, Arguments.createMap().apply {
putString(EVENT_PROP_ID, event.getDistribution().id)
event.getDistribution().externalId?.let { putString(EVENT_PROP_EXTERNAL_ID, it) }
putString(EVENT_PROP_NAME, event.getDistribution().name)
})
})
}
private fun onDistributionOffline(event: DistributionOfflineEvent) {
emitter.emit(Arguments.createMap().apply {
putString(EVENT_PROP_TYPE, "distributionoffline")
putString(EVENT_PROP_DISTRIBUTION_ID, event.getDistributionId())
})
}
private fun onEndPointLoaded(event: EndpointLoadedEvent) {
emitter.emit(Arguments.createMap().apply {
putString(EVENT_PROP_TYPE, "endpointloaded")
putMap(EVENT_PROP_ENDPOINT, EndpointAdapter.fromEndpoint(event.getEndpoint()))
})
}
private fun onIntentOfFallback(event: IntentToFallbackEvent) {
emitter.emit(Arguments.createMap().apply {
putString(EVENT_PROP_TYPE, "intenttofallback")
event.reason?.let {
putMap(EVENT_PROP_REASON, PayloadBuilder().error(it.code.id.toString(), it.message).build())
}
})
}
}