Skip to content

Commit

Permalink
Extract UplynkEventDispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegRyz committed Aug 12, 2024
1 parent 04d874d commit 92c0560
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.theoplayer.android.api.ads.ServerSideAdIntegrationHandler
import com.theoplayer.android.connector.uplynk.internal.InternalUplynkAdIntegration
import com.theoplayer.android.connector.uplynk.internal.UplynkSsaiDescriptionConverter
import com.theoplayer.android.connector.uplynk.common.EventDispatcherImpl
import com.theoplayer.android.connector.uplynk.internal.UplynkEventDispatcher
import com.theoplayer.android.connector.uplynk.network.UplynkApi

class UplynkAdIntegration(
private val theoplayerView: THEOplayerView,
Expand All @@ -15,5 +17,7 @@ class UplynkAdIntegration(
by InternalUplynkAdIntegration(
theoplayerView,
controller,
eventDispatcher,
UplynkSsaiDescriptionConverter())
UplynkEventDispatcher(eventDispatcher),
UplynkSsaiDescriptionConverter(),
UplynkApi()
)
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
package com.theoplayer.android.connector.uplynk.internal

import android.os.Handler
import android.os.Looper
import com.theoplayer.android.api.THEOplayerView
import com.theoplayer.android.api.ads.ServerSideAdIntegrationController
import com.theoplayer.android.api.ads.ServerSideAdIntegrationHandler
import com.theoplayer.android.api.player.Player
import com.theoplayer.android.api.source.SourceDescription
import com.theoplayer.android.connector.uplynk.common.EventDispatcherImpl
import com.theoplayer.android.connector.uplynk.UplynkSsaiDescription
import com.theoplayer.android.connector.uplynk.internal.events.UplynkAssetInfoErrorResponseEventImpl
import com.theoplayer.android.connector.uplynk.internal.events.UplynkAssetInfoResponseEventImpl
import com.theoplayer.android.connector.uplynk.internal.events.UplynkPreplayErrorResponseEventImpl
import com.theoplayer.android.connector.uplynk.internal.events.UplynkPreplayResponseEventImpl
import com.theoplayer.android.connector.uplynk.internal.network.AssetInfoInternalResponse
import com.theoplayer.android.connector.uplynk.internal.network.PreplayInternalResponse
import com.theoplayer.android.connector.uplynk.network.UplynkApi
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import java.util.Date

internal class InternalUplynkAdIntegration(
val theoplayerView: THEOplayerView,
val controller: ServerSideAdIntegrationController,
val eventDispatcher: EventDispatcherImpl,
val uplynkDescriptionConverter: UplynkSsaiDescriptionConverter
val eventDispatcher: UplynkEventDispatcher,
val uplynkDescriptionConverter: UplynkSsaiDescriptionConverter,
private val uplynkApi: UplynkApi
) : ServerSideAdIntegrationHandler {

private val player: Player
get() = theoplayerView.player

private val uplynkApi = UplynkApi()

override suspend fun setSource(source: SourceDescription): SourceDescription {

val uplynkSource = source.sources.singleOrNull { it.ssai is UplynkSsaiDescription }
Expand All @@ -41,7 +27,7 @@ internal class InternalUplynkAdIntegration(
val response = uplynkDescriptionConverter
.buildPreplayUrl(ssaiDescription)
.let { uplynkApi.preplay(it) }
.also { dispatchPreplayEvents(it) }
.also { eventDispatcher.dispatchPreplayEvents(it) }


val newSource = source.replaceSources(source.sources.toMutableList().apply {
Expand All @@ -52,49 +38,9 @@ internal class InternalUplynkAdIntegration(
uplynkDescriptionConverter
.buildAssetInfoUrls(ssaiDescription, response.internalResponse.sid)
.map { uplynkApi.assetInfo(it) }
.forEach { dispatchAssetInfoEvents(it) }
.forEach { eventDispatcher.dispatchAssetInfoEvents(it) }
}

return newSource
}

private fun dispatchPreplayEvents(response: PreplayInternalResponse) {
if (response.externalResponse != null) {
eventDispatcher.dispatchEvent(
UplynkPreplayResponseEventImpl(
Date(),
response.externalResponse!!
)
)
} else {
eventDispatcher.dispatchEvent(
UplynkPreplayErrorResponseEventImpl(
Date(),
response.body,
response.error
)
)
}
}

private fun dispatchAssetInfoEvents(assetInfo: AssetInfoInternalResponse) =
CoroutineScope(Dispatchers.IO).async {
if (assetInfo.externalResponse != null) {
eventDispatcher.dispatchEvent(
UplynkAssetInfoResponseEventImpl(
Date(),
assetInfo.externalResponse!!
)
)
} else {
eventDispatcher.dispatchEvent(
UplynkAssetInfoErrorResponseEventImpl(
Date(),
assetInfo.body,
assetInfo.error
)
)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.theoplayer.android.connector.uplynk.internal

import com.theoplayer.android.connector.uplynk.common.EventDispatcherImpl
import com.theoplayer.android.connector.uplynk.internal.events.UplynkAssetInfoErrorResponseEventImpl
import com.theoplayer.android.connector.uplynk.internal.events.UplynkAssetInfoResponseEventImpl
import com.theoplayer.android.connector.uplynk.internal.events.UplynkPreplayErrorResponseEventImpl
import com.theoplayer.android.connector.uplynk.internal.events.UplynkPreplayResponseEventImpl
import com.theoplayer.android.connector.uplynk.internal.network.AssetInfoInternalResponse
import com.theoplayer.android.connector.uplynk.internal.network.PreplayInternalResponse
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import java.util.Date

internal class UplynkEventDispatcher(val eventDispatcher: EventDispatcherImpl) {

fun dispatchPreplayEvents(response: PreplayInternalResponse) {
if (response.externalResponse != null) {
eventDispatcher.dispatchEvent(
UplynkPreplayResponseEventImpl(
Date(),
response.externalResponse!!
)
)
} else {
eventDispatcher.dispatchEvent(
UplynkPreplayErrorResponseEventImpl(
Date(),
response.body,
response.error
)
)
}
}

fun dispatchAssetInfoEvents(assetInfo: AssetInfoInternalResponse) =
CoroutineScope(Dispatchers.IO).async {
if (assetInfo.externalResponse != null) {
eventDispatcher.dispatchEvent(
UplynkAssetInfoResponseEventImpl(
Date(),
assetInfo.externalResponse!!
)
)
} else {
eventDispatcher.dispatchEvent(
UplynkAssetInfoErrorResponseEventImpl(
Date(),
assetInfo.body,
assetInfo.error
)
)
}
}
}

0 comments on commit 92c0560

Please sign in to comment.