From 94d0c3512c96d14fd94b6fb9beb0a2bd24c8f316 Mon Sep 17 00:00:00 2001 From: Mustafa Ozhan Date: Sat, 14 Sep 2024 10:31:31 +0200 Subject: [PATCH] [Oztechan/CCC#3914] Track currentBase change in widget --- .../android-viewmodel-widget.gradle.kts | 1 + .../viewmodel/widget/WidgetViewModel.kt | 12 +++++++++- .../widget/di/AndroidViewModelWidgetModule.kt | 3 ++- .../viewmodel/widget/WidgetViewModelTest.kt | 22 ++++++++++++++++++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/android/viewmodel/widget/android-viewmodel-widget.gradle.kts b/android/viewmodel/widget/android-viewmodel-widget.gradle.kts index f3131eec22..5dacd47d53 100644 --- a/android/viewmodel/widget/android-viewmodel-widget.gradle.kts +++ b/android/viewmodel/widget/android-viewmodel-widget.gradle.kts @@ -41,6 +41,7 @@ dependencies { Modules.Client.Core.apply { implementation(project(viewModel)) implementation(project(shared)) + implementation(project(analytics)) } Modules.Client.Storage.apply { diff --git a/android/viewmodel/widget/src/main/kotlin/com/oztechan/ccc/android/viewmodel/widget/WidgetViewModel.kt b/android/viewmodel/widget/src/main/kotlin/com/oztechan/ccc/android/viewmodel/widget/WidgetViewModel.kt index dfa1c4206c..d7803edecb 100644 --- a/android/viewmodel/widget/src/main/kotlin/com/oztechan/ccc/android/viewmodel/widget/WidgetViewModel.kt +++ b/android/viewmodel/widget/src/main/kotlin/com/oztechan/ccc/android/viewmodel/widget/WidgetViewModel.kt @@ -2,6 +2,10 @@ package com.oztechan.ccc.android.viewmodel.widget import co.touchlab.kermit.Logger import com.oztechan.ccc.android.viewmodel.widget.WidgetData.Companion.MAXIMUM_NUMBER_OF_CURRENCY +import com.oztechan.ccc.client.core.analytics.AnalyticsManager +import com.oztechan.ccc.client.core.analytics.model.Event +import com.oztechan.ccc.client.core.analytics.model.Param +import com.oztechan.ccc.client.core.analytics.model.UserProperty import com.oztechan.ccc.client.core.shared.util.getFormatted import com.oztechan.ccc.client.core.shared.util.getRateFromCode import com.oztechan.ccc.client.core.shared.util.isNotPassed @@ -18,7 +22,8 @@ class WidgetViewModel( private val calculationStorage: CalculationStorage, private val backendApiService: BackendApiService, private val currencyDataSource: CurrencyDataSource, - private val appStorage: AppStorage + private val appStorage: AppStorage, + private val analyticsManager: AnalyticsManager ) : SEEDViewModel( initialState = WidgetState( base = calculationStorage.currentBase, @@ -82,8 +87,13 @@ class WidgetViewModel( (it + activeCurrencies.size) % activeCurrencies.size // it handles index -1 and index size +1 } + val newBase = activeCurrencies[newBaseIndex].code + calculationStorage.currentBase = activeCurrencies[newBaseIndex].code + analyticsManager.trackEvent(Event.BaseChange(Param.Base(newBase))) + analyticsManager.setUserProperty(UserProperty.BaseCurrency(newBase)) + refreshWidgetData() } } diff --git a/android/viewmodel/widget/src/main/kotlin/com/oztechan/ccc/android/viewmodel/widget/di/AndroidViewModelWidgetModule.kt b/android/viewmodel/widget/src/main/kotlin/com/oztechan/ccc/android/viewmodel/widget/di/AndroidViewModelWidgetModule.kt index 92461f7597..422419b88d 100644 --- a/android/viewmodel/widget/src/main/kotlin/com/oztechan/ccc/android/viewmodel/widget/di/AndroidViewModelWidgetModule.kt +++ b/android/viewmodel/widget/src/main/kotlin/com/oztechan/ccc/android/viewmodel/widget/di/AndroidViewModelWidgetModule.kt @@ -10,7 +10,8 @@ val androidViewModelWidgetModule = module { calculationStorage = get(), backendApiService = get(), currencyDataSource = get(), - appStorage = get() + appStorage = get(), + analyticsManager = get() ) } } diff --git a/android/viewmodel/widget/src/test/kotlin/com/oztechan/ccc/android/viewmodel/widget/WidgetViewModelTest.kt b/android/viewmodel/widget/src/test/kotlin/com/oztechan/ccc/android/viewmodel/widget/WidgetViewModelTest.kt index ef7b4e699f..052a5127a1 100644 --- a/android/viewmodel/widget/src/test/kotlin/com/oztechan/ccc/android/viewmodel/widget/WidgetViewModelTest.kt +++ b/android/viewmodel/widget/src/test/kotlin/com/oztechan/ccc/android/viewmodel/widget/WidgetViewModelTest.kt @@ -2,6 +2,10 @@ package com.oztechan.ccc.android.viewmodel.widget import co.touchlab.kermit.CommonWriter import co.touchlab.kermit.Logger +import com.oztechan.ccc.client.core.analytics.AnalyticsManager +import com.oztechan.ccc.client.core.analytics.model.Event +import com.oztechan.ccc.client.core.analytics.model.Param +import com.oztechan.ccc.client.core.analytics.model.UserProperty import com.oztechan.ccc.client.core.shared.util.getFormatted import com.oztechan.ccc.client.core.shared.util.getRateFromCode import com.oztechan.ccc.client.core.shared.util.isNotPassed @@ -43,7 +47,8 @@ internal class WidgetViewModelTest { calculationStorage = calculationStorage, backendApiService = backendApiService, currencyDataSource = currencyDataSource, - appStorage = appStorage + appStorage = appStorage, + analyticsManager = analyticsManager ) } @@ -55,6 +60,8 @@ internal class WidgetViewModelTest { private val appStorage = mock() + private val analyticsManager = mock(MockMode.autoUnit) + private val base = "EUR" private val firstBase = "USD" private val lastBase = "TRY" @@ -219,6 +226,9 @@ internal class WidgetViewModelTest { verifySuspend(VerifyMode.not) { currencyDataSource.getActiveCurrencies() } verify(VerifyMode.not) { calculationStorage.currentBase = any() } + + verify(VerifyMode.not) { analyticsManager.trackEvent(Event.BaseChange(Param.Base(any()))) } + verify(VerifyMode.not) { analyticsManager.setUserProperty(UserProperty.BaseCurrency(any())) } } // region Event @@ -234,6 +244,9 @@ internal class WidgetViewModelTest { verify { calculationStorage.currentBase = lastBase } + verify { analyticsManager.trackEvent(Event.BaseChange(Param.Base(lastBase))) } + verify { analyticsManager.setUserProperty(UserProperty.BaseCurrency(lastBase)) } + every { calculationStorage.currentBase } .returns(lastBase) @@ -242,6 +255,9 @@ internal class WidgetViewModelTest { verifySuspend { currencyDataSource.getActiveCurrencies() } verify { calculationStorage.currentBase = firstBase } + + verify { analyticsManager.trackEvent(Event.BaseChange(Param.Base(firstBase))) } + verify { analyticsManager.setUserProperty(UserProperty.BaseCurrency(firstBase)) } } @Test @@ -255,6 +271,8 @@ internal class WidgetViewModelTest { verifySuspend { currencyDataSource.getActiveCurrencies() } verify { calculationStorage.currentBase = firstBase } + verify { analyticsManager.trackEvent(Event.BaseChange(Param.Base(firstBase))) } + verify { analyticsManager.setUserProperty(UserProperty.BaseCurrency(firstBase)) } every { calculationStorage.currentBase } .returns(firstBase) @@ -264,6 +282,8 @@ internal class WidgetViewModelTest { verifySuspend { currencyDataSource.getActiveCurrencies() } verify { calculationStorage.currentBase = lastBase } + verify { analyticsManager.trackEvent(Event.BaseChange(Param.Base(lastBase))) } + verify { analyticsManager.setUserProperty(UserProperty.BaseCurrency(lastBase)) } } @Test