From a5b2c14bdfcc1f90b6119e8bf0ff0df93f67f475 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Thu, 9 Jul 2026 11:40:13 +0300 Subject: [PATCH] Open automatically chat or call history screen when baresip icon with notification dot is touuched --- .../kotlin/com/tutpro/baresip/MainActivity.kt | 21 ++++++++++++++++++- .../kotlin/com/tutpro/baresip/ViewModel.kt | 12 ++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index 608416e0..9952a767 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -102,7 +102,8 @@ class MainActivity : ComponentActivity() { else exitProcess(0) } - } else + } + else handleServiceEvent(this, viewModel, first.event, first.params) } } @@ -254,6 +255,9 @@ class MainActivity : ComponentActivity() { val route = "calls/${command.aor}" navController.navigate(route) } + is NavigationCommand.NavigateToChats -> { + navController.navigate("chats") + } is NavigationCommand.NavigateToHome -> navController.navigate("main") { popUpTo("main") { inclusive = true } @@ -302,6 +306,21 @@ class MainActivity : ComponentActivity() { intent.removeExtra("action") handleIntent(applicationContext, viewModel, intent, action) } + else if (intent.action == Intent.ACTION_MAIN && !atStartup) { + val activeNotifications = nm.activeNotifications + if (activeNotifications.any { it.id == CALL_MISSED_NOTIFICATION_ID }) { + val ua = BaresipService.uas.value.find { it.account.missedCalls } + if (ua != null) + viewModel.navigateToCalls(ua.account.aor) + } + else if (activeNotifications.any { it.id == MESSAGE_NOTIFICATION_ID }) { + val lastUnread = BaresipService.messages.lastOrNull { it.new } + if (lastUnread != null) + viewModel.onNewMessageReceived(lastUnread.aor, lastUnread.peerUri) + else + viewModel.navigateToChats() + } + } } override fun onResume() { diff --git a/app/src/main/kotlin/com/tutpro/baresip/ViewModel.kt b/app/src/main/kotlin/com/tutpro/baresip/ViewModel.kt index 0da2045c..a8d59850 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ViewModel.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ViewModel.kt @@ -16,6 +16,7 @@ import kotlinx.coroutines.launch // Sealed class for type-safe navigation events sealed class NavigationCommand { object NavigateToHome : NavigationCommand() + object NavigateToChats : NavigationCommand() data class NavigateToCalls(val aor: String) : NavigationCommand() data class NavigateToChat(val aor: String, val peerUri: String) : NavigationCommand() } @@ -31,11 +32,10 @@ class ViewModel: ViewModel() { fun updateAorPeerMessage(aor: String, peerUri: String, message: String) { val key = "$aor:$peerUri" - if (message.isEmpty()) { + if (message.isEmpty()) messageDrafts.remove(key) - } else { + else messageDrafts[key] = message - } } data class DialerState( @@ -162,4 +162,10 @@ class ViewModel: ViewModel() { } } + fun navigateToChats() { + viewModelScope.launch { + _navigationCommand.emit(NavigationCommand.NavigateToChats) + } + } + }