From 8df9a2f8e2134adf3b58ad215031ff38c82eb539 Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 22 Jan 2024 13:40:55 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../ui/appsettings/AppSettingsFragment.kt | 44 ++------ .../ui/appsettings/AppSettingsItemsFactory.kt | 21 +++- .../ui/appsettings/AppSettingsViewModel.kt | 106 +++++++++++++----- .../AppSettingsItemsAnalyticsSender.kt | 32 ++++++ .../utils/SelectedWalletAnalyticsSender.kt | 29 +++++ .../utils/WalletWarningsAnalyticsSender.kt | 2 +- .../loaders/WalletContentLoaderFactory.kt | 4 +- .../wallet/viewmodels/WalletViewModelV2.kt | 4 + 8 files changed, 170 insertions(+), 72 deletions(-) create mode 100644 app/src/main/java/com/tangem/tap/features/details/ui/appsettings/analytics/AppSettingsItemsAnalyticsSender.kt create mode 100644 features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/SelectedWalletAnalyticsSender.kt diff --git a/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsFragment.kt b/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsFragment.kt index 8afcd15101..61e02daeda 100644 --- a/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsFragment.kt +++ b/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsFragment.kt @@ -1,21 +1,22 @@ package com.tangem.tap.features.details.ui.appsettings import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier -import androidx.lifecycle.lifecycleScope +import androidx.compose.ui.platform.LocalLifecycleOwner +import androidx.hilt.navigation.compose.hiltViewModel +import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.tangem.core.navigation.NavigationAction import com.tangem.core.ui.screen.ComposeFragment import com.tangem.core.ui.theme.AppThemeModeHolder import com.tangem.domain.appcurrency.repository.AppCurrencyRepository import com.tangem.tap.features.details.redux.DetailsAction -import com.tangem.tap.features.details.redux.DetailsState import com.tangem.tap.store import dagger.hilt.android.AndroidEntryPoint -import org.rekotlin.StoreSubscriber import javax.inject.Inject @AndroidEntryPoint -internal class AppSettingsFragment : ComposeFragment(), StoreSubscriber { +internal class AppSettingsFragment : ComposeFragment() { @Inject override lateinit var appThemeModeHolder: AppThemeModeHolder @@ -23,43 +24,20 @@ internal class AppSettingsFragment : ComposeFragment(), StoreSubscriber().apply { + LocalLifecycleOwner.current.lifecycle.addObserver(observer = this) + } + val state by viewModel.uiState.collectAsStateWithLifecycle() + AppSettingsScreen( modifier = modifier, - state = viewModel.uiState, + state = state, onBackClick = { store.dispatch(DetailsAction.ResetCardSettingsData) store.dispatch(NavigationAction.PopBackTo()) }, ) } - - override fun onResume() { - super.onResume() - viewModel.checkBiometricsStatus(lifecycleScope) - } - - override fun onStart() { - super.onStart() - store.subscribe(this) { state -> - state.skipRepeats { oldState, newState -> - oldState.detailsState == newState.detailsState - }.select { it.detailsState } - } - } - - override fun onStop() { - super.onStop() - store.unsubscribe(this) - } - - override fun newState(state: DetailsState) { - if (activity == null || view == null) return - viewModel.updateState(state) - } } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsItemsFactory.kt b/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsItemsFactory.kt index 0d1f66f8bb..47bbb8376f 100644 --- a/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsItemsFactory.kt +++ b/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsItemsFactory.kt @@ -10,7 +10,7 @@ internal class AppSettingsItemsFactory { fun createEnrollBiometricsCard(onClick: () -> Unit): Item.Card { return Item.Card( - id = "enroll_biometrics_card", + id = ID_ENROLL_BIOMETRICS_CARD, title = resourceReference(R.string.app_settings_enable_biometrics_title), description = resourceReference(R.string.app_settings_enable_biometrics_description), iconResId = R.drawable.ic_alert_circle_24, @@ -24,7 +24,7 @@ internal class AppSettingsItemsFactory { onCheckedChange: (Boolean) -> Unit, ): Item.Switch { return Item.Switch( - id = "save_wallets_switch", + id = ID_SAVE_WALLETS_SWITCH, title = resourceReference(R.string.app_settings_saved_wallet), description = resourceReference(R.string.app_settings_saved_wallet_footer), isEnabled = isEnabled, @@ -39,7 +39,7 @@ internal class AppSettingsItemsFactory { onCheckedChange: (Boolean) -> Unit, ): Item.Switch { return Item.Switch( - id = "save_access_codes_switch", + id = ID_SAVE_ACCESS_CODES_SWITCH, title = resourceReference(R.string.app_settings_saved_access_codes), description = resourceReference(R.string.app_settings_saved_access_codes_footer), isEnabled = isEnabled, @@ -54,7 +54,7 @@ internal class AppSettingsItemsFactory { onCheckedChange: (Boolean) -> Unit, ): Item.Switch { return Item.Switch( - id = "flip_to_hide_balance_switch", + id = ID_FLIP_TO_HIDE_BALANCE_SWITCH, title = resourceReference(R.string.details_row_title_flip_to_hide), description = resourceReference(R.string.details_row_description_flip_to_hide), isEnabled = isEnabled, @@ -65,7 +65,7 @@ internal class AppSettingsItemsFactory { fun createSelectAppCurrencyButton(currentAppCurrencyName: String, onClick: () -> Unit): Item.Button { return Item.Button( - id = "select_app_currency_button", + id = ID_SELECT_APP_CURRENCY_BUTTON, title = resourceReference(R.string.details_row_title_currency), description = stringReference(currentAppCurrencyName), isEnabled = true, @@ -75,7 +75,7 @@ internal class AppSettingsItemsFactory { fun createSelectThemeModeButton(currentThemeMode: AppThemeMode, onClick: () -> Unit): Item.Button { return Item.Button( - id = "select_theme_mode_button", + id = ID_SELECT_THEME_MODE_BUTTON, title = resourceReference(R.string.app_settings_theme_selector_title), description = resourceReference( id = when (currentThemeMode) { @@ -88,4 +88,13 @@ internal class AppSettingsItemsFactory { onClick = onClick, ) } + + companion object { + const val ID_ENROLL_BIOMETRICS_CARD = "enroll_biometrics_card" + const val ID_SAVE_WALLETS_SWITCH = "save_wallets_switch" + const val ID_SAVE_ACCESS_CODES_SWITCH = "save_access_codes_switch" + const val ID_FLIP_TO_HIDE_BALANCE_SWITCH = "flip_to_hide_balance_switch" + const val ID_SELECT_APP_CURRENCY_BUTTON = "select_app_currency_button" + const val ID_SELECT_THEME_MODE_BUTTON = "select_theme_mode_button" + } } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsViewModel.kt b/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsViewModel.kt index f2dae0ca71..1a473c49ec 100644 --- a/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsViewModel.kt +++ b/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsViewModel.kt @@ -1,10 +1,10 @@ package com.tangem.tap.features.details.ui.appsettings -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.setValue -import androidx.lifecycle.LifecycleCoroutineScope -import com.tangem.core.analytics.Analytics +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.ViewModel +import androidx.lifecycle.lifecycleScope +import com.tangem.core.analytics.api.AnalyticsEventHandler import com.tangem.core.navigation.AppScreen import com.tangem.core.navigation.NavigationAction import com.tangem.domain.appcurrency.repository.AppCurrencyRepository @@ -13,53 +13,78 @@ import com.tangem.tap.common.analytics.events.AnalyticsParam import com.tangem.tap.common.analytics.events.Settings import com.tangem.tap.common.extensions.dispatchOnMain import com.tangem.tap.common.extensions.dispatchWithMain -import com.tangem.tap.common.redux.AppState import com.tangem.tap.features.details.redux.AppSetting import com.tangem.tap.features.details.redux.AppSettingsState import com.tangem.tap.features.details.redux.DetailsAction import com.tangem.tap.features.details.redux.DetailsState +import com.tangem.tap.features.details.ui.appsettings.analytics.AppSettingsItemsAnalyticsSender import com.tangem.tap.scope +import com.tangem.tap.store import com.tangem.utils.coroutines.JobHolder import com.tangem.utils.coroutines.saveIn +import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList -import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.onEach -import org.rekotlin.Store +import kotlinx.coroutines.flow.* +import org.rekotlin.StoreSubscriber +import javax.inject.Inject -internal class AppSettingsViewModel( - private val store: Store, +@HiltViewModel +internal class AppSettingsViewModel @Inject constructor( private val appCurrencyRepository: AppCurrencyRepository, -) { + private val analyticsEventHandler: AnalyticsEventHandler, + private val appSettingsItemsAnalyticsSender: AppSettingsItemsAnalyticsSender, +) : ViewModel(), + StoreSubscriber, + DefaultLifecycleObserver { private val itemsFactory = AppSettingsItemsFactory() private val dialogsFactory = AppSettingsDialogsFactory() private val appCurrencyUpdatesJobHolder = JobHolder() - var uiState: AppSettingsScreenState by mutableStateOf(AppSettingsScreenState.Loading) - private set + private val _uiState: MutableStateFlow = MutableStateFlow( + value = AppSettingsScreenState.Loading, + ) + val uiState: StateFlow = _uiState init { bootstrapAppCurrencyUpdates() + + subscribeToStoreChanges() + sendItemsAnalytics() } - fun updateState(state: DetailsState) { - uiState = AppSettingsScreenState.Content( - items = buildItems(state.appSettingsState), - dialog = (uiState as? AppSettingsScreenState.Content)?.dialog, - ) + override fun newState(state: DetailsState) { + val items = buildItems(state.appSettingsState) + + _uiState.update { prevState -> + when (prevState) { + is AppSettingsScreenState.Content -> prevState.copy( + items = items, + ) + is AppSettingsScreenState.Loading -> AppSettingsScreenState.Content( + items = items, + dialog = null, + ) + } + } } - fun checkBiometricsStatus(lifecycleScope: LifecycleCoroutineScope) { - store.dispatch(DetailsAction.AppSettings.CheckBiometricsStatus(lifecycleScope)) + override fun onResume(owner: LifecycleOwner) { + store.dispatch(DetailsAction.AppSettings.CheckBiometricsStatus(owner.lifecycleScope)) + } + + override fun onCleared() { + store.unsubscribe(subscriber = this) } private fun buildItems(state: AppSettingsState): ImmutableList { val items = buildList { if (state.needEnrollBiometrics) { - Analytics.send(Settings.AppSettings.EnableBiometrics) - itemsFactory.createEnrollBiometricsCard(onClick = ::enrollBiometrics).let(::add) + itemsFactory.createEnrollBiometricsCard( + onClick = ::enrollBiometrics, + ).let(::add) } itemsFactory.createSelectAppCurrencyButton( @@ -89,9 +114,10 @@ internal class AppSettingsViewModel( onCheckedChange = ::onFlipToHideBalanceToggled, ).let(::add) - itemsFactory.createSelectThemeModeButton(state.selectedThemeMode) { - showThemeModeSelector(state.selectedThemeMode) - }.let(::add) + itemsFactory.createSelectThemeModeButton( + currentThemeMode = state.selectedThemeMode, + onClick = { showThemeModeSelector(state.selectedThemeMode) }, + ).let(::add) } return items.toImmutableList() @@ -111,7 +137,7 @@ internal class AppSettingsViewModel( dialog = dialogsFactory.createThemeModeSelectorDialog( selectedModeIndex = selectedMode.ordinal, onSelect = { mode -> - Analytics.send( + analyticsEventHandler.send( event = Settings.AppSettings.ThemeSwitched( theme = AnalyticsParam.AppTheme.fromAppThemeMode(mode), ), @@ -167,7 +193,7 @@ internal class AppSettingsViewModel( private fun onFlipToHideBalanceToggled(enable: Boolean) { val param = AnalyticsParam.OnOffState(enable) - Analytics.send(Settings.AppSettings.HideBalanceChanged(param)) + analyticsEventHandler.send(Settings.AppSettings.HideBalanceChanged(param)) store.dispatch(DetailsAction.AppSettings.ChangeBalanceHiding(hideBalance = enable)) } @@ -188,10 +214,28 @@ internal class AppSettingsViewModel( .saveIn(appCurrencyUpdatesJobHolder) } + private fun subscribeToStoreChanges() { + store.subscribe(subscriber = this) { state -> + state.skipRepeats { oldState, newState -> + oldState.detailsState == newState.detailsState + }.select { it.detailsState } + } + } + + private fun sendItemsAnalytics() { + uiState + .filterIsInstance() + .distinctUntilChangedBy(AppSettingsScreenState.Content::items) + .onEach { appSettingsItemsAnalyticsSender.send(it.items) } + .launchIn(scope) + } + private fun updateContentState(block: AppSettingsScreenState.Content.() -> AppSettingsScreenState.Content) { - uiState = when (val state = uiState) { - is AppSettingsScreenState.Content -> block(state) - is AppSettingsScreenState.Loading -> state + _uiState.update { prevState -> + when (prevState) { + is AppSettingsScreenState.Content -> block(prevState) + is AppSettingsScreenState.Loading -> prevState + } } } } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/analytics/AppSettingsItemsAnalyticsSender.kt b/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/analytics/AppSettingsItemsAnalyticsSender.kt new file mode 100644 index 0000000000..d61f054cb5 --- /dev/null +++ b/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/analytics/AppSettingsItemsAnalyticsSender.kt @@ -0,0 +1,32 @@ +package com.tangem.tap.features.details.ui.appsettings.analytics + +import com.tangem.core.analytics.api.AnalyticsEventHandler +import com.tangem.core.analytics.models.AnalyticsEvent +import com.tangem.tap.common.analytics.events.Settings +import com.tangem.tap.features.details.ui.appsettings.AppSettingsItemsFactory +import com.tangem.tap.features.details.ui.appsettings.AppSettingsScreenState +import dagger.hilt.android.scopes.ViewModelScoped +import javax.inject.Inject + +@ViewModelScoped +internal class AppSettingsItemsAnalyticsSender @Inject constructor( + private val analyticsHandler: AnalyticsEventHandler, +) { + + fun send(items: List) { + val events = getEvents(items) + + events.forEach { analyticsHandler.send(it) } + } + + private fun getEvents(items: List): Set { + return items.mapNotNullTo(mutableSetOf(), ::getEvent) + } + + private fun getEvent(item: AppSettingsScreenState.Item): AnalyticsEvent? { + return when (item.id) { + AppSettingsItemsFactory.ID_ENROLL_BIOMETRICS_CARD -> Settings.AppSettings.EnableBiometrics + else -> null + } + } +} \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/SelectedWalletAnalyticsSender.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/SelectedWalletAnalyticsSender.kt new file mode 100644 index 0000000000..ca5a6989b8 --- /dev/null +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/SelectedWalletAnalyticsSender.kt @@ -0,0 +1,29 @@ +package com.tangem.feature.wallet.presentation.wallet.analytics.utils + +import com.tangem.core.analytics.api.AnalyticsEventHandler +import com.tangem.core.analytics.models.AnalyticsEvent +import com.tangem.domain.wallets.models.UserWallet +import com.tangem.feature.wallet.presentation.wallet.analytics.WalletScreenAnalyticsEvent +import javax.inject.Inject + +internal class SelectedWalletAnalyticsSender @Inject constructor( + private val analyticsEventHandler: AnalyticsEventHandler, +) { + + fun send(userWallet: UserWallet) { + val event = getEvent(userWallet) + + if (event != null) { + analyticsEventHandler.send(event) + } + } + + /** + + * that cannot be processed in [WalletWarningsAnalyticsSender]. + * */ + private fun getEvent(userWallet: UserWallet): AnalyticsEvent? = when { + userWallet.isLocked -> WalletScreenAnalyticsEvent.MainScreen.WalletUnlock + else -> null + } +} \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/WalletWarningsAnalyticsSender.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/WalletWarningsAnalyticsSender.kt index d28b7fdc06..d8f8fa7457 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/WalletWarningsAnalyticsSender.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/analytics/utils/WalletWarningsAnalyticsSender.kt @@ -43,7 +43,7 @@ internal class WalletWarningsAnalyticsSender @Inject constructor( is WalletNotification.Informational.DemoCard -> MainScreen.DemoCard is WalletNotification.Informational.MissingAddresses -> MainScreen.MissingAddresses is WalletNotification.RateApp -> MainScreen.HowDoYouLikeTangem - is WalletNotification.UnlockWallets -> MainScreen.WalletUnlock + is WalletNotification.UnlockWallets -> null // See [SelectedWalletAnalyticsSender] is WalletNotification.Informational.NoAccount, is WalletNotification.Warning.LowSignatures, is WalletNotification.Warning.SomeNetworksUnreachable, diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/WalletContentLoaderFactory.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/WalletContentLoaderFactory.kt index 224cd27c24..f897350e0e 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/WalletContentLoaderFactory.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/loaders/WalletContentLoaderFactory.kt @@ -21,7 +21,9 @@ internal class WalletContentLoaderFactory @Inject constructor( isRefresh: Boolean = false, ): WalletContentLoader? { return when { - userWallet.isMultiCurrency -> multiWalletContentLoaderFactory.create(userWallet, clickIntents) + userWallet.isMultiCurrency -> { + multiWalletContentLoaderFactory.create(userWallet, clickIntents) + } userWallet.scanResponse.cardTypesResolver.isSingleWalletWithToken() -> { singleWalletWithTokenContentLoaderFactory.create(userWallet, clickIntents) } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModelV2.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModelV2.kt index fed5c1dc6e..a7bd5b9f0b 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModelV2.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModelV2.kt @@ -15,6 +15,7 @@ import com.tangem.domain.wallets.usecase.GetWalletsUseCase import com.tangem.domain.wallets.usecase.ShouldSaveUserWalletsUseCase import com.tangem.feature.wallet.presentation.router.InnerWalletRouter import com.tangem.feature.wallet.presentation.wallet.analytics.WalletScreenAnalyticsEvent +import com.tangem.feature.wallet.presentation.wallet.analytics.utils.SelectedWalletAnalyticsSender import com.tangem.feature.wallet.presentation.wallet.loaders.WalletScreenContentLoader import com.tangem.feature.wallet.presentation.wallet.state.WalletEvent import com.tangem.feature.wallet.presentation.wallet.state.WalletEvent.DemonstrateWalletsScrollPreview.Direction @@ -55,6 +56,7 @@ internal class WalletViewModelV2 @Inject constructor( private val dispatchers: CoroutineDispatcherProvider, private val reduxStateHolder: ReduxStateHolder, private val screenLifecycleProvider: ScreenLifecycleProvider, + private val selectedWalletAnalyticsSender: SelectedWalletAnalyticsSender, ) : ViewModel() { val uiState: StateFlow = stateHolder.uiState @@ -152,6 +154,8 @@ internal class WalletViewModelV2 @Inject constructor( reduxStateHolder.dispatch( action = WalletConnectActions.New.SetupUserChains(userWallet = selectedWallet), ) + + selectedWalletAnalyticsSender.send(selectedWallet) } } .flowOn(dispatchers.main)