Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5555676395 | ||
|
|
ca512f5993 |
@@ -33,8 +33,8 @@ android {
|
|||||||
applicationId = "pl.firmatpp.kierowca"
|
applicationId = "pl.firmatpp.kierowca"
|
||||||
minSdk = 26
|
minSdk = 26
|
||||||
targetSdk = 35
|
targetSdk = 35
|
||||||
versionCode = 33
|
versionCode = 35
|
||||||
versionName = "1.0.32"
|
versionName = "1.0.34"
|
||||||
setProperty("archivesBaseName", "pl.firmatpp.kierowca")
|
setProperty("archivesBaseName", "pl.firmatpp.kierowca")
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
|
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ data class BootstrapResponse(
|
|||||||
val routes: RoutesBucketDto,
|
val routes: RoutesBucketDto,
|
||||||
val driverAppSettings: DriverAppSettingsDto?,
|
val driverAppSettings: DriverAppSettingsDto?,
|
||||||
val notificationPreferences: NotificationPreferencesDto? = null,
|
val notificationPreferences: NotificationPreferencesDto? = null,
|
||||||
|
val realtime: RealtimeConfigDto? = null,
|
||||||
val syncState: SyncStateResponse? = null,
|
val syncState: SyncStateResponse? = null,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -71,6 +72,12 @@ data class NotificationPreferencesDto(
|
|||||||
val notifyNewRoutes: Boolean = false,
|
val notifyNewRoutes: Boolean = false,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
data class RealtimeConfigDto(
|
||||||
|
val reverbEnabled: Boolean = false,
|
||||||
|
val reverbAppKey: String? = null,
|
||||||
|
val reverbWsBaseUrl: String? = null,
|
||||||
|
)
|
||||||
|
|
||||||
data class RoutesBucketDto(
|
data class RoutesBucketDto(
|
||||||
val today: List<DriverRouteDto> = emptyList(),
|
val today: List<DriverRouteDto> = emptyList(),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ import okhttp3.OkHttpClient
|
|||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.WebSocket
|
import okhttp3.WebSocket
|
||||||
import okhttp3.WebSocketListener
|
import okhttp3.WebSocketListener
|
||||||
import pl.firmatpp.kierowca.BuildConfig
|
|
||||||
import pl.firmatpp.kierowca.data.DriverRepository
|
import pl.firmatpp.kierowca.data.DriverRepository
|
||||||
|
import pl.firmatpp.kierowca.data.model.RealtimeConfigDto
|
||||||
|
|
||||||
class DriverLiveSyncClient(
|
class DriverLiveSyncClient(
|
||||||
private val repository: DriverRepository,
|
private val repository: DriverRepository,
|
||||||
@@ -25,20 +25,29 @@ class DriverLiveSyncClient(
|
|||||||
private val client: OkHttpClient = OkHttpClient(),
|
private val client: OkHttpClient = OkHttpClient(),
|
||||||
private val gson: Gson = Gson(),
|
private val gson: Gson = Gson(),
|
||||||
) {
|
) {
|
||||||
|
private companion object {
|
||||||
|
const val HEARTBEAT_INTERVAL_MS = 10_000L
|
||||||
|
}
|
||||||
|
|
||||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||||
private val started = AtomicBoolean(false)
|
private val started = AtomicBoolean(false)
|
||||||
private var webSocket: WebSocket? = null
|
private var webSocket: WebSocket? = null
|
||||||
private var driverId: String? = null
|
private var driverId: String? = null
|
||||||
|
private var realtimeConfig: RealtimeConfigDto? = null
|
||||||
private var socketId: String? = null
|
private var socketId: String? = null
|
||||||
private var heartbeatJob: Job? = null
|
private var heartbeatJob: Job? = null
|
||||||
|
|
||||||
fun start(driverId: String) {
|
fun start(driverId: String, config: RealtimeConfigDto?) {
|
||||||
if (BuildConfig.REVERB_APP_KEY.isBlank()) return
|
val appKey = config?.reverbAppKey?.takeIf { it.isNotBlank() } ?: return
|
||||||
|
val wsBaseUrl = config.reverbWsBaseUrl?.takeIf { it.isNotBlank() } ?: return
|
||||||
|
if (!config.reverbEnabled) return
|
||||||
|
|
||||||
|
realtimeConfig = config
|
||||||
this.driverId = driverId
|
this.driverId = driverId
|
||||||
if (!started.compareAndSet(false, true)) return
|
if (!started.compareAndSet(false, true)) return
|
||||||
|
|
||||||
val wsUrl = BuildConfig.REVERB_WS_BASE_URL.trimEnd('/') +
|
val wsUrl = wsBaseUrl.trimEnd('/') +
|
||||||
"/" + BuildConfig.REVERB_APP_KEY +
|
"/" + appKey +
|
||||||
"?protocol=7&client=android&version=1.0&flash=false"
|
"?protocol=7&client=android&version=1.0&flash=false"
|
||||||
|
|
||||||
webSocket = client.newWebSocket(
|
webSocket = client.newWebSocket(
|
||||||
@@ -72,6 +81,7 @@ class DriverLiveSyncClient(
|
|||||||
webSocket?.close(1000, "logout")
|
webSocket?.close(1000, "logout")
|
||||||
webSocket = null
|
webSocket = null
|
||||||
driverId = null
|
driverId = null
|
||||||
|
realtimeConfig = null
|
||||||
socketId = null
|
socketId = null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +156,7 @@ class DriverLiveSyncClient(
|
|||||||
scope.launch {
|
scope.launch {
|
||||||
delay(5_000)
|
delay(5_000)
|
||||||
if (!started.get() && driverId == id) {
|
if (!started.get() && driverId == id) {
|
||||||
start(id)
|
start(id, realtimeConfig)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,7 +166,7 @@ class DriverLiveSyncClient(
|
|||||||
heartbeatJob = scope.launch {
|
heartbeatJob = scope.launch {
|
||||||
while (started.get()) {
|
while (started.get()) {
|
||||||
reportRealtimeStatus("connected")
|
reportRealtimeStatus("connected")
|
||||||
delay(30_000)
|
delay(HEARTBEAT_INTERVAL_MS)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
response.session.driver.id.let { driverId ->
|
response.session.driver.id.let { driverId ->
|
||||||
liveSyncClient.start(driverId)
|
liveSyncClient.start(driverId, response.realtime)
|
||||||
registerPushTokenIfAvailable(driverId)
|
registerPushTokenIfAvailable(driverId)
|
||||||
}
|
}
|
||||||
}.onFailure { throwable ->
|
}.onFailure { throwable ->
|
||||||
|
|||||||
Reference in New Issue
Block a user