Skip to content

Commit

Permalink
chore: Keep Connection Alive on Orientation Change
Browse files Browse the repository at this point in the history
  • Loading branch information
wba2hi committed Feb 8, 2024
1 parent 3db8d9d commit 98d3520
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
17 changes: 16 additions & 1 deletion app/src/main/kotlin/org/eclipse/kuksa/companion/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 98d3520

Please sign in to comment.