From 98d3520629d93654393d1e7f4b5d9d91abadf435 Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Thu, 8 Feb 2024 14:03:15 +0100 Subject: [PATCH] chore: Keep Connection Alive on Orientation Change --- .../kuksa/companion/CompanionApplication.kt | 5 ++++- .../org/eclipse/kuksa/companion/MainActivity.kt | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/org/eclipse/kuksa/companion/CompanionApplication.kt b/app/src/main/kotlin/org/eclipse/kuksa/companion/CompanionApplication.kt index a603b2f..8551eba 100644 --- a/app/src/main/kotlin/org/eclipse/kuksa/companion/CompanionApplication.kt +++ b/app/src/main/kotlin/org/eclipse/kuksa/companion/CompanionApplication.kt @@ -21,6 +21,7 @@ package org.eclipse.kuksa.companion import android.app.Application import dagger.hilt.android.HiltAndroidApp +import org.eclipse.kuksa.DataBrokerConnection const val PREVIEW_WIDTH_DP = 400 const val PREVIEW_HEIGHT_DP = 900 @@ -29,4 +30,6 @@ const val SHEET_EXPANDED_HEIGHT = 350 const val SHEET_COLLAPSED_HEIGHT = 50 @HiltAndroidApp -class CompanionApplication : Application() +class CompanionApplication : Application() { + var dataBrokerConnection: DataBrokerConnection? = null +} diff --git a/app/src/main/kotlin/org/eclipse/kuksa/companion/MainActivity.kt b/app/src/main/kotlin/org/eclipse/kuksa/companion/MainActivity.kt index 38e6719..74652ed 100644 --- a/app/src/main/kotlin/org/eclipse/kuksa/companion/MainActivity.kt +++ b/app/src/main/kotlin/org/eclipse/kuksa/companion/MainActivity.kt @@ -68,6 +68,9 @@ import javax.inject.Inject @AndroidEntryPoint @VssDefinition("vss_rel_4.0.yaml") class MainActivity : ComponentActivity() { + private val companionApplication + get() = applicationContext as CompanionApplication + @Inject lateinit var connectionInfoRepository: ConnectionInfoRepository @@ -87,7 +90,13 @@ class MainActivity : ComponentActivity() { private val settingsViewModel: SettingsViewModel by viewModels() - private var dataBrokerConnection: DataBrokerConnection? = null + // storing the connection in the Application keeps the Connection alive on orientation changes + private var dataBrokerConnection: DataBrokerConnection? + get() = companionApplication.dataBrokerConnection + set(value) { + companionApplication.dataBrokerConnection = value + } + private val dataBrokerConnectorFactory = DataBrokerConnectorFactory() private val doorVehicleSurface = DoorVehicleSurface() @@ -289,6 +298,12 @@ class MainActivity : ComponentActivity() { } private fun connectToDataBroker(onConnected: () -> Unit = {}) { + // dataBrokerConnection is already established e.g. after an orientation change + if (dataBrokerConnection != null) { + onConnected() + return + } + lifecycleScope.launch { val connectionInfo = connectionInfoRepository.connectionInfoFlow.first()