diff --git a/app/src/main/java/io/agora/flat/data/RoomServiceFetcher.kt b/app/src/main/java/io/agora/flat/data/ServiceFetcher.kt similarity index 62% rename from app/src/main/java/io/agora/flat/data/RoomServiceFetcher.kt rename to app/src/main/java/io/agora/flat/data/ServiceFetcher.kt index bdbcd709..a1322918 100644 --- a/app/src/main/java/io/agora/flat/data/RoomServiceFetcher.kt +++ b/app/src/main/java/io/agora/flat/data/ServiceFetcher.kt @@ -1,6 +1,7 @@ package io.agora.flat.data import io.agora.flat.di.NetworkModule +import io.agora.flat.http.api.CloudRecordService import io.agora.flat.http.api.RoomService import okhttp3.OkHttpClient import retrofit2.Retrofit @@ -8,23 +9,24 @@ import retrofit2.converter.gson.GsonConverterFactory import javax.inject.Inject import javax.inject.Singleton + /** * Fetch room service by uuid * * For joining rooms between different regions */ @Singleton -class RoomServiceFetcher @Inject constructor( +class ServiceFetcher @Inject constructor( @NetworkModule.NormalOkHttpClient private val client: OkHttpClient, private val appEnv: AppEnv ) { companion object { - val regions = listOf( + private val regions = listOf( "CN", "SG", ) - val codeMap = mapOf( + private val codeMap = mapOf( "1" to "CN", "2" to "SG", ) @@ -48,23 +50,31 @@ class RoomServiceFetcher @Inject constructor( } } - private val cache = mutableMapOf() + private val allCache = mutableMapOf, Any>() - fun fetch(uuid: String): RoomService { - val env = fetchEnv(uuid, appEnv.getEnv()) - val envServiceUrl = appEnv.getEnvServiceUrl(env) + fun fetchRoomService(uuid: String): RoomService { + return getApiService(uuid) + } - return cache[env] ?: createRoomService(envServiceUrl).also { - cache[env] = it - } + fun fetchCloudRecordService(uuid: String): CloudRecordService { + return getApiService(uuid) + } + + private inline fun getApiService(uuid: String): T { + val env = fetchEnv(uuid, appEnv.getEnv()) + val name = T::class.java.simpleName + return allCache.getOrPut(env to name) { + val serviceUrl = appEnv.getEnvServiceUrl(env) + createService(serviceUrl)!! + } as T } - private fun createRoomService(serviceUrl: String): RoomService { + private inline fun createService(serviceUrl: String): T { return Retrofit.Builder() .baseUrl(serviceUrl) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build() - .create(RoomService::class.java) + .create(T::class.java) } } \ No newline at end of file diff --git a/app/src/main/java/io/agora/flat/data/model/RecordInfo.kt b/app/src/main/java/io/agora/flat/data/model/RecordInfo.kt index 877d0069..f43b8e80 100644 --- a/app/src/main/java/io/agora/flat/data/model/RecordInfo.kt +++ b/app/src/main/java/io/agora/flat/data/model/RecordInfo.kt @@ -4,6 +4,7 @@ data class RecordInfo constructor( val title: String, val ownerUUID: String, val roomType: RoomType, + val region: String, val whiteboardRoomToken: String, val whiteboardRoomUUID: String, val rtmToken: String, diff --git a/app/src/main/java/io/agora/flat/data/repository/CloudRecordRepository.kt b/app/src/main/java/io/agora/flat/data/repository/CloudRecordRepository.kt index 5f659d5d..5e9646c9 100644 --- a/app/src/main/java/io/agora/flat/data/repository/CloudRecordRepository.kt +++ b/app/src/main/java/io/agora/flat/data/repository/CloudRecordRepository.kt @@ -1,6 +1,7 @@ package io.agora.flat.data.repository import io.agora.flat.data.Result +import io.agora.flat.data.ServiceFetcher import io.agora.flat.data.model.* import io.agora.flat.data.toResult import io.agora.flat.http.api.CloudRecordService @@ -12,6 +13,7 @@ import javax.inject.Singleton @Singleton class CloudRecordRepository @Inject constructor( private val cloudRecordService: CloudRecordService, + private val serviceFetcher: ServiceFetcher, ) { suspend fun acquireRecord(roomUUID: String, expiredHour: Int = 24): Result { return withContext(Dispatchers.IO) { @@ -100,7 +102,11 @@ class CloudRecordRepository @Inject constructor( suspend fun getRecordInfo(roomUUID: String): Result { return withContext(Dispatchers.IO) { - cloudRecordService.getRecordInfo(PureRoomReq(roomUUID)).toResult() + fetchService(roomUUID).getRecordInfo(PureRoomReq(roomUUID)).toResult() } } + + private fun fetchService(roomUUID: String): CloudRecordService { + return serviceFetcher.fetchCloudRecordService(roomUUID) + } } \ No newline at end of file diff --git a/app/src/main/java/io/agora/flat/data/repository/RoomRepository.kt b/app/src/main/java/io/agora/flat/data/repository/RoomRepository.kt index 9ad253d0..97c64e57 100644 --- a/app/src/main/java/io/agora/flat/data/repository/RoomRepository.kt +++ b/app/src/main/java/io/agora/flat/data/repository/RoomRepository.kt @@ -1,10 +1,9 @@ package io.agora.flat.data.repository import io.agora.flat.common.android.I18NFetcher -import io.agora.flat.data.AppEnv import io.agora.flat.data.AppKVCenter import io.agora.flat.data.Result -import io.agora.flat.data.RoomServiceFetcher +import io.agora.flat.data.ServiceFetcher import io.agora.flat.data.manager.JoinRoomRecordManager import io.agora.flat.data.model.CancelRoomReq import io.agora.flat.data.model.JoinRoomRecord @@ -32,13 +31,13 @@ import javax.inject.Singleton @Singleton class RoomRepository @Inject constructor( private val roomService: RoomService, - private val roomServiceFetcher: RoomServiceFetcher, + private val serviceFetcher: ServiceFetcher, private val joinRoomRecordManager: JoinRoomRecordManager, private val appKVCenter: AppKVCenter, private val i18NFetcher: I18NFetcher, ) { private fun fetchService(uuid: String): RoomService { - return roomServiceFetcher.fetch(uuid) + return serviceFetcher.fetchRoomService(uuid) } suspend fun getRoomListAll(page: Int): Result> { diff --git a/app/src/main/java/io/agora/flat/ui/activity/playback/ReplayPlayerComponent.kt b/app/src/main/java/io/agora/flat/ui/activity/playback/ReplayPlayerComponent.kt index c08c368c..c5af7dde 100644 --- a/app/src/main/java/io/agora/flat/ui/activity/playback/ReplayPlayerComponent.kt +++ b/app/src/main/java/io/agora/flat/ui/activity/playback/ReplayPlayerComponent.kt @@ -21,10 +21,12 @@ import com.herewhite.sdk.WhiteSdk import com.herewhite.sdk.WhiteSdkConfiguration import com.herewhite.sdk.domain.PlayerConfiguration import com.herewhite.sdk.domain.Promise +import com.herewhite.sdk.domain.Region import com.herewhite.sdk.domain.SDKError import com.herewhite.sdk.domain.WindowParams import com.herewhite.sdk.domain.WindowPrefersColorScheme.Dark import com.herewhite.sdk.domain.WindowPrefersColorScheme.Light +import io.agora.board.fast.model.FastRegion import io.agora.flat.BuildConfig import io.agora.flat.R import io.agora.flat.data.AppEnv @@ -182,6 +184,7 @@ class ReplayPlayerComponent( createWhitePlayer( it.recordInfo.whiteboardRoomUUID, it.recordInfo.whiteboardRoomToken, + it.recordInfo.region, it.beginTime, it.duration ) @@ -208,7 +211,18 @@ class ReplayPlayerComponent( } } - private fun createWhitePlayer(roomUUID: String, roomToken: String, beginTime: Long, duration: Long) { + private fun String.toRegion(): Region { + val region = Region.values().find { it.name.lowercase().replace('_', '-') == this } + return region ?: Region.cn + } + + private fun createWhitePlayer( + roomUUID: String, + roomToken: String, + region: String, + beginTime: Long, + duration: Long + ) { val conf = PlayerConfiguration(roomUUID, roomToken).apply { val styleMap = hashMapOf( "bottom" to "30px", @@ -224,6 +238,7 @@ class ReplayPlayerComponent( windowParams.scrollVerticalOnly = true windowParams.prefersColorScheme = if (activity.isDarkMode()) Dark else Light } + conf.region = region.toRegion() conf.beginTimestamp = beginTime conf.duration = duration diff --git a/app/src/test/java/io/agora/flat/data/RoomServiceFetcherTest.kt b/app/src/test/java/io/agora/flat/data/RoomServiceFetcherTest.kt index f8c262ff..ec171ecf 100644 --- a/app/src/test/java/io/agora/flat/data/RoomServiceFetcherTest.kt +++ b/app/src/test/java/io/agora/flat/data/RoomServiceFetcherTest.kt @@ -41,7 +41,7 @@ class RoomServiceFetcherTest { ) cases.forEach { it -> - assertEquals(it.expected, RoomServiceFetcher.fetchEnv(it.uuid, it.env)) + assertEquals(it.expected, ServiceFetcher.fetchEnv(it.uuid, it.env)) } } } \ No newline at end of file