Skip to content

Commit

Permalink
[#3914] Track currentBase change in widget
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaozhan committed Sep 14, 2024
1 parent 543d485 commit 94d0c35
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
Modules.Client.Core.apply {
implementation(project(viewModel))
implementation(project(shared))
implementation(project(analytics))
}

Modules.Client.Storage.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<WidgetState, BaseEffect, WidgetEvent, WidgetData>(
initialState = WidgetState(
base = calculationStorage.currentBase,
Expand Down Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ val androidViewModelWidgetModule = module {
calculationStorage = get(),
backendApiService = get(),
currencyDataSource = get(),
appStorage = get()
appStorage = get(),
analyticsManager = get()

Check warning on line 14 in android/viewmodel/widget/src/main/kotlin/com/oztechan/ccc/android/viewmodel/widget/di/AndroidViewModelWidgetModule.kt

View check run for this annotation

Codecov / codecov/patch

android/viewmodel/widget/src/main/kotlin/com/oztechan/ccc/android/viewmodel/widget/di/AndroidViewModelWidgetModule.kt#L13-L14

Added lines #L13 - L14 were not covered by tests
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,7 +47,8 @@ internal class WidgetViewModelTest {
calculationStorage = calculationStorage,
backendApiService = backendApiService,
currencyDataSource = currencyDataSource,
appStorage = appStorage
appStorage = appStorage,
analyticsManager = analyticsManager
)
}

Expand All @@ -55,6 +60,8 @@ internal class WidgetViewModelTest {

private val appStorage = mock<AppStorage>()

private val analyticsManager = mock<AnalyticsManager>(MockMode.autoUnit)

private val base = "EUR"
private val firstBase = "USD"
private val lastBase = "TRY"
Expand Down Expand Up @@ -219,6 +226,9 @@ internal class WidgetViewModelTest {
verifySuspend(VerifyMode.not) { currencyDataSource.getActiveCurrencies() }

verify(VerifyMode.not) { calculationStorage.currentBase = any<String>() }

verify(VerifyMode.not) { analyticsManager.trackEvent(Event.BaseChange(Param.Base(any<String>()))) }
verify(VerifyMode.not) { analyticsManager.setUserProperty(UserProperty.BaseCurrency(any<String>())) }
}

// region Event
Expand All @@ -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)

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

0 comments on commit 94d0c35

Please sign in to comment.