From f96062a5c35908ec2d0422629e108b910cb3e2e5 Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Tue, 5 Dec 2023 09:00:47 +0100 Subject: [PATCH 1/4] feature: Add Dependency Injection using Hilt Signed-off-by: Andre Weber --- app/build.gradle.kts | 10 +++++++ app/src/main/AndroidManifest.xml | 1 + .../kuksa/companion/CompanionApplication.kt | 26 +++++++++++++++++++ .../eclipse/kuksa/companion/MainActivity.kt | 2 ++ build.gradle.kts | 2 ++ buildSrc/build.gradle.kts | 1 + gradle/libs.versions.toml | 10 ++++++- settings.gradle.kts | 1 - 8 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 app/src/main/kotlin/org/eclipse/kuksa/companion/CompanionApplication.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index c5a1e5f..fe2d109 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -23,6 +23,8 @@ plugins { id("com.android.application") id("com.google.devtools.ksp") alias(libs.plugins.kotlin.serialization) + kotlin("kapt") + id("com.google.dagger.hilt.android") kotlin("android") } @@ -133,4 +135,12 @@ dependencies { debugImplementation(libs.androidx.compose.ui.tooling) debugImplementation(libs.androidx.compose.ui.tooling.test.manifest) + + implementation(libs.hilt.android) + kapt(libs.hilt.android.compiler) +} + +// Allow references to generated code +kapt { + correctErrorTypes = true } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c6f5582..07f0fec 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,6 +23,7 @@ Date: Tue, 5 Dec 2023 09:03:19 +0100 Subject: [PATCH 2/4] fix: Crash when Rotating from Portrait to Landscape Mode The App crashed due to the Android DataStore being re-instantiated during the recreation of the Activity. This has been fixed by using Hilt for Dependency Injection to create the DataStore as a Singleton. Closes: #26 Signed-off-by: Andre Weber --- .../kotlin/org/eclipse/kuksa/companion/MainActivity.kt | 7 ++++--- .../connection/repository/ConnectionInfoRepository.kt | 8 +++++++- 2 files changed, 11 insertions(+), 4 deletions(-) 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 cdfa85f..4bb034b 100644 --- a/app/src/main/kotlin/org/eclipse/kuksa/companion/MainActivity.kt +++ b/app/src/main/kotlin/org/eclipse/kuksa/companion/MainActivity.kt @@ -66,11 +66,14 @@ import org.eclipse.kuksa.vss.VssStation import org.eclipse.kuksa.vss.VssTrunk import org.eclipse.kuksa.vsscore.annotation.VssDefinition import org.eclipse.kuksa.vsscore.model.VssSpecification +import javax.inject.Inject @AndroidEntryPoint @VssDefinition("vss_rel_4.0.yaml") class MainActivity : ComponentActivity() { - private lateinit var connectionInfoRepository: ConnectionInfoRepository + @Inject + lateinit var connectionInfoRepository: ConnectionInfoRepository + private lateinit var doorVehicleScene: DoorVehicleScene private val disconnectListener = DisconnectListener { @@ -174,8 +177,6 @@ class MainActivity : ComponentActivity() { override fun onPostCreate(savedInstanceState: Bundle?) { super.onPostCreate(savedInstanceState) - connectionInfoRepository = ConnectionInfoRepository(this) - connectionStatusViewModel.onClickReconnect = { connectToDataBroker { subscribe() diff --git a/app/src/main/kotlin/org/eclipse/kuksa/companion/feature/connection/repository/ConnectionInfoRepository.kt b/app/src/main/kotlin/org/eclipse/kuksa/companion/feature/connection/repository/ConnectionInfoRepository.kt index 01e8d7f..e185222 100644 --- a/app/src/main/kotlin/org/eclipse/kuksa/companion/feature/connection/repository/ConnectionInfoRepository.kt +++ b/app/src/main/kotlin/org/eclipse/kuksa/companion/feature/connection/repository/ConnectionInfoRepository.kt @@ -22,11 +22,17 @@ package org.eclipse.kuksa.companion.feature.connection.repository import android.content.Context import androidx.datastore.core.DataStore import androidx.datastore.dataStore +import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.flow.Flow import org.eclipse.kuksa.companion.feature.connection.model.ConnectionInfo import org.eclipse.kuksa.companion.feature.connection.model.ConnectionInfoSerializer +import javax.inject.Inject +import javax.inject.Singleton -class ConnectionInfoRepository(context: Context) { +@Singleton +class ConnectionInfoRepository @Inject constructor( + @ApplicationContext context: Context, +) { private val Context.dataStore: DataStore by dataStore(PREFERENCES_NAME, ConnectionInfoSerializer) private val dataStore = context.dataStore From fa49aae736a01e603b89a654ea93d3a4aabc7cf2 Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Tue, 5 Dec 2023 09:18:26 +0100 Subject: [PATCH 3/4] chore: Inject ConnectionInfoRepository into SettingsViewModel --- .../org/eclipse/kuksa/companion/MainActivity.kt | 4 +--- .../settings/viewModel/SettingsViewModel.kt | 15 ++++----------- 2 files changed, 5 insertions(+), 14 deletions(-) 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 4bb034b..f4142c3 100644 --- a/app/src/main/kotlin/org/eclipse/kuksa/companion/MainActivity.kt +++ b/app/src/main/kotlin/org/eclipse/kuksa/companion/MainActivity.kt @@ -87,9 +87,7 @@ class MainActivity : ComponentActivity() { private val lightControlViewModel: LightControlViewModel by viewModels() private val wheelPressureViewModel: WheelPressureViewModel by viewModels() - private val settingsViewModel: SettingsViewModel by viewModels { - SettingsViewModel.Factory(connectionInfoRepository) - } + private val settingsViewModel: SettingsViewModel by viewModels() private var dataBrokerConnection: DataBrokerConnection? = null private val dataBrokerConnectorFactory = DataBrokerConnectorFactory() diff --git a/app/src/main/kotlin/org/eclipse/kuksa/companion/feature/settings/viewModel/SettingsViewModel.kt b/app/src/main/kotlin/org/eclipse/kuksa/companion/feature/settings/viewModel/SettingsViewModel.kt index dfc7762..0d561a5 100644 --- a/app/src/main/kotlin/org/eclipse/kuksa/companion/feature/settings/viewModel/SettingsViewModel.kt +++ b/app/src/main/kotlin/org/eclipse/kuksa/companion/feature/settings/viewModel/SettingsViewModel.kt @@ -20,13 +20,15 @@ package org.eclipse.kuksa.companion.feature.settings.viewModel import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope +import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.launch import org.eclipse.kuksa.companion.feature.connection.model.ConnectionInfo import org.eclipse.kuksa.companion.feature.connection.repository.ConnectionInfoRepository +import javax.inject.Inject -class SettingsViewModel( +@HiltViewModel +class SettingsViewModel @Inject constructor( private val repository: ConnectionInfoRepository, ) : ViewModel() { val connectionInfoFlow = repository.connectionInfoFlow @@ -36,13 +38,4 @@ class SettingsViewModel( repository.updateConnectionInfo(connectionInfo) } } - - class Factory( - private val connectionInfoRepository: ConnectionInfoRepository, - ) : ViewModelProvider.Factory { - @Suppress("UNCHECKED_CAST") - override fun create(modelClass: Class): T { - return SettingsViewModel(connectionInfoRepository) as T - } - } } From 0ea7d950409ac09bf38b10a85d5b709f1c2add0e Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Tue, 5 Dec 2023 13:48:27 +0100 Subject: [PATCH 4/4] chore: Use correct versionCode and versionName --- app/build.gradle.kts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index fe2d109..43320d7 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -24,7 +24,7 @@ plugins { id("com.google.devtools.ksp") alias(libs.plugins.kotlin.serialization) kotlin("kapt") - id("com.google.dagger.hilt.android") + alias(libs.plugins.hilt.android) kotlin("android") } @@ -36,8 +36,8 @@ android { applicationId = "org.eclipse.kuksa.companion" minSdk = 27 targetSdk = 33 - versionCode = 1 - versionName = "1.0" + versionCode = rootProject.extra["projectVersionCode"].toString().toInt() + versionName = rootProject.extra["projectVersion"].toString() testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" vectorDrawables {