Compare commits

..
6 changed files with 66 additions and 9 deletions
+2 -2
View File
@@ -34,8 +34,8 @@ android {
applicationId = "pl.firmatpp.kierowca" applicationId = "pl.firmatpp.kierowca"
minSdk = 26 minSdk = 26
targetSdk = 35 targetSdk = 35
versionCode = 107 versionCode = 109
versionName = "1.0.54" versionName = "1.0.56"
setProperty("archivesBaseName", "pl.firmatpp.kierowca") setProperty("archivesBaseName", "pl.firmatpp.kierowca")
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
@@ -117,6 +117,7 @@ data class RealtimeConfigDto(
val reverbEnabled: Boolean = false, val reverbEnabled: Boolean = false,
val reverbAppKey: String? = null, val reverbAppKey: String? = null,
val reverbWsBaseUrl: String? = null, val reverbWsBaseUrl: String? = null,
val reverbOrigin: String? = null,
) )
data class RoutesBucketDto( data class RoutesBucketDto(
@@ -43,17 +43,17 @@ class DriverRepositoryLiveSyncGateway(
} }
interface LiveWebSocketFactory { interface LiveWebSocketFactory {
fun newWebSocket(url: String, listener: WebSocketListener): WebSocket fun newWebSocket(url: String, origin: String?, listener: WebSocketListener): WebSocket
} }
class OkHttpLiveWebSocketFactory( class OkHttpLiveWebSocketFactory(
private val client: OkHttpClient, private val client: OkHttpClient,
) : LiveWebSocketFactory { ) : LiveWebSocketFactory {
override fun newWebSocket(url: String, listener: WebSocketListener): WebSocket = override fun newWebSocket(url: String, origin: String?, listener: WebSocketListener): WebSocket =
client.newWebSocket( client.newWebSocket(
Request.Builder() Request.Builder()
.url(url) .url(url)
.header("Origin", websocketOrigin(url)) .header("Origin", origin?.takeIf { it.isNotBlank() } ?: websocketOrigin(url))
.build(), .build(),
listener, listener,
) )
@@ -248,7 +248,7 @@ class DriverLiveSyncClient(
} }
private fun connectNow(resetAttempt: Boolean = false) { private fun connectNow(resetAttempt: Boolean = false) {
val wsUrl = synchronized(lock) { val (wsUrl, origin) = synchronized(lock) {
val config = realtimeConfig ?: return val config = realtimeConfig ?: return
val id = driverId ?: return val id = driverId ?: return
if (!desiredActive || !foregroundActive || !networkAvailable || !isConfigUsable(config)) return if (!desiredActive || !foregroundActive || !networkAvailable || !isConfigUsable(config)) return
@@ -262,11 +262,15 @@ class DriverLiveSyncClient(
val appKey = config.reverbAppKey.orEmpty() val appKey = config.reverbAppKey.orEmpty()
val wsBaseUrl = config.reverbWsBaseUrl.orEmpty() val wsBaseUrl = config.reverbWsBaseUrl.orEmpty()
wsBaseUrl.trimEnd('/') + "/" + appKey + "?protocol=7&client=android&version=1.0&flash=false" Pair(
wsBaseUrl.trimEnd('/') + "/" + appKey + "?protocol=7&client=android&version=1.0&flash=false",
config.reverbOrigin,
)
} }
val socket = webSocketFactory.newWebSocket( val socket = webSocketFactory.newWebSocket(
wsUrl, wsUrl,
origin,
object : WebSocketListener() { object : WebSocketListener() {
override fun onMessage(webSocket: WebSocket, text: String) { override fun onMessage(webSocket: WebSocket, text: String) {
handleMessage(webSocket, text) handleMessage(webSocket, text)
@@ -228,6 +228,22 @@ fun DriverApp(
viewModel.openLeaveRequest(initialLeaveRequestId) viewModel.openLeaveRequest(initialLeaveRequestId)
} }
} }
LaunchedEffect(state.screen, state.notifyNewRoutes, state.notificationPermissionDenied, appInForeground) {
if (!appInForeground || state.screen != DriverScreen.Profile || !state.notifyNewRoutes) return@LaunchedEffect
if (canPostNotifications(context)) {
if (state.notificationPermissionDenied) {
viewModel.clearNotificationPermissionWarning()
}
return@LaunchedEffect
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && !hasPostNotificationsRuntimePermission(context)) {
notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
} else {
viewModel.markNotificationPermissionBlocked()
}
}
Box(Modifier.fillMaxSize().background(TppTheme.colors.surface)) { Box(Modifier.fillMaxSize().background(TppTheme.colors.surface)) {
when (state.screen) { when (state.screen) {
@@ -2238,13 +2254,18 @@ private fun ProfileScreen(
Text("Zmiana powiadomień wymaga połączenia z internetem.", color = TppTheme.colors.muted, style = MaterialTheme.typography.bodySmall) Text("Zmiana powiadomień wymaga połączenia z internetem.", color = TppTheme.colors.muted, style = MaterialTheme.typography.bodySmall)
} }
if (state.notificationPermissionDenied) { if (state.notificationPermissionDenied) {
Text(
"Android blokuje powiadomienia dla tej aplikacji. Token FCM może być zarejestrowany, ale powiadomienie nie pojawi się na telefonie, dopóki nie włączysz zgody systemowej.",
color = TppTheme.colors.error,
style = MaterialTheme.typography.bodySmall,
)
Button( Button(
onClick = onOpenNotificationSettings, onClick = onOpenNotificationSettings,
modifier = Modifier.fillMaxWidth().height(48.dp), modifier = Modifier.fillMaxWidth().height(48.dp),
colors = ButtonDefaults.buttonColors(containerColor = TppTheme.colors.navy), colors = ButtonDefaults.buttonColors(containerColor = TppTheme.colors.navy),
shape = MaterialTheme.shapes.small, shape = MaterialTheme.shapes.small,
) { ) {
Text("Przejdź do ustawień", fontWeight = FontWeight.Bold) Text("Otwórz ustawienia powiadomień", fontWeight = FontWeight.Bold)
} }
} }
HorizontalDivider(color = TppTheme.colors.outline.copy(alpha = 0.55f)) HorizontalDivider(color = TppTheme.colors.outline.copy(alpha = 0.55f))
@@ -639,6 +639,24 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
} }
} }
fun markNotificationPermissionBlocked() {
_state.update {
it.copy(
notificationPermissionDenied = true,
error = "Powiadomienia są włączone w aplikacji, ale Android blokuje ich wyświetlanie. Włącz powiadomienia w ustawieniach aplikacji.",
)
}
}
fun clearNotificationPermissionWarning() {
_state.update {
it.copy(
notificationPermissionDenied = false,
error = if (it.error?.contains("powiadom", ignoreCase = true) == true) null else it.error,
)
}
}
fun setThemeMode(themeMode: AppThemeMode) { fun setThemeMode(themeMode: AppThemeMode) {
_state.update { it.copy(themeMode = themeMode) } _state.update { it.copy(themeMode = themeMode) }
viewModelScope.launch { viewModelScope.launch {
@@ -23,6 +23,7 @@ class DriverLiveSyncClientTest {
reverbEnabled = true, reverbEnabled = true,
reverbAppKey = "app-key", reverbAppKey = "app-key",
reverbWsBaseUrl = "wss://example.test/app", reverbWsBaseUrl = "wss://example.test/app",
reverbOrigin = "https://bootstrap-origin.test",
) )
@Test @Test
@@ -32,6 +33,16 @@ class DriverLiveSyncClientTest {
assertEquals("http://localhost:8080", websocketOrigin("ws://localhost:8080/app/app-key")) assertEquals("http://localhost:8080", websocketOrigin("ws://localhost:8080/app/app-key"))
} }
@Test
fun passesBootstrapOriginToWebSocketFactory() = runTest {
val factory = FakeWebSocketFactory()
val client = liveClient(factory = factory, scope = backgroundScope)
client.start("driver-1", config)
assertEquals("https://bootstrap-origin.test", factory.origins.single())
}
@Test @Test
fun reconnectsWithBackoffAfterSocketFailure() = runTest { fun reconnectsWithBackoffAfterSocketFailure() = runTest {
val factory = FakeWebSocketFactory() val factory = FakeWebSocketFactory()
@@ -211,10 +222,12 @@ class DriverLiveSyncClientTest {
private class FakeWebSocketFactory : LiveWebSocketFactory { private class FakeWebSocketFactory : LiveWebSocketFactory {
val sockets = mutableListOf<FakeWebSocket>() val sockets = mutableListOf<FakeWebSocket>()
val origins = mutableListOf<String?>()
override fun newWebSocket(url: String, listener: WebSocketListener): WebSocket { override fun newWebSocket(url: String, origin: String?, listener: WebSocketListener): WebSocket {
val socket = FakeWebSocket(listener) val socket = FakeWebSocket(listener)
sockets += socket sockets += socket
origins += origin
return socket return socket
} }
} }