@@ -18,21 +18,18 @@ import kotlinx.coroutines.flow.mapLatest
1818import kotlinx.coroutines.flow.update
1919import kotlinx.coroutines.launch
2020import to.bitkit.R
21- import to.bitkit.data.SettingsData
2221import to.bitkit.data.SettingsStore
23- import to.bitkit.data.entities.TransferEntity
2422import to.bitkit.models.ActivityBannerType
2523import to.bitkit.models.BannerItem
2624import to.bitkit.models.Suggestion
27- import to.bitkit.models.TransferType
2825import to.bitkit.models.WidgetType
29- import to.bitkit.models.toSuggestionOrNull
3026import to.bitkit.models.widget.ArticleModel
3127import to.bitkit.models.widget.toArticleModel
3228import to.bitkit.models.widget.toBlockModel
3329import to.bitkit.repositories.ActivityRepo
3430import to.bitkit.repositories.CurrencyRepo
3531import to.bitkit.repositories.PubkyRepo
32+ import to.bitkit.repositories.SuggestionsRepo
3633import to.bitkit.repositories.TransferRepo
3734import to.bitkit.repositories.WalletRepo
3835import to.bitkit.repositories.WidgetsRepo
@@ -51,12 +48,9 @@ class HomeViewModel @Inject constructor(
5148 private val transferRepo : TransferRepo ,
5249 private val pubkyRepo : PubkyRepo ,
5350 private val activityRepo : ActivityRepo ,
51+ private val suggestionsRepo : SuggestionsRepo ,
5452) : ViewModel() {
5553
56- companion object {
57- private const val MAX_SUGGESTIONS = 4
58- }
59-
6054 private val _uiState = MutableStateFlow (HomeUiState ())
6155 val uiState: StateFlow <HomeUiState > = _uiState .asStateFlow()
6256
@@ -115,7 +109,7 @@ class HomeViewModel @Inject constructor(
115109 }
116110
117111 viewModelScope.launch {
118- createSuggestionsFlow() .collect { suggestions ->
112+ suggestionsRepo.suggestionsFlow .collect { suggestions ->
119113 _uiState .update { it.copy(suggestions = suggestions.toImmutableList()) }
120114 }
121115 }
@@ -306,68 +300,4 @@ class HomeViewModel @Inject constructor(
306300 _uiState .update { it.copy(banners = banners.toImmutableList()) }
307301 }
308302 }
309-
310- private fun createSuggestionsFlow () = combine(
311- walletRepo.balanceState,
312- settingsStore.data,
313- transferRepo.activeTransfers,
314- pubkyRepo.isAuthenticated,
315- ) { balanceState, settings, transfers, profileAuthenticated ->
316- val baseSuggestions = when {
317- balanceState.totalLightningSats > 0uL ->
318- spendingSuggestions(settings, profileAuthenticated)
319- balanceState.totalOnchainSats > 0uL ->
320- savingsOnlySuggestions(settings, transfers, profileAuthenticated)
321- else -> emptyWalletSuggestions(settings, transfers, profileAuthenticated)
322- }
323- val dismissedList = settings.dismissedSuggestions.mapNotNull { it.toSuggestionOrNull() }
324- baseSuggestions
325- .filterNot { it in dismissedList }
326- .take(MAX_SUGGESTIONS )
327- }
328-
329- private fun spendingSuggestions (
330- settings : SettingsData ,
331- profileAuthenticated : Boolean ,
332- ) = listOfNotNull(
333- Suggestion .QUICK_PAY .takeIf { ! settings.isQuickPayEnabled },
334- Suggestion .NOTIFICATIONS .takeIf { ! settings.notificationsGranted },
335- Suggestion .SHOP ,
336- Suggestion .PROFILE .takeIf { ! profileAuthenticated },
337- Suggestion .SUPPORT ,
338- Suggestion .INVITE ,
339- Suggestion .BUY ,
340- )
341-
342- private fun savingsOnlySuggestions (
343- settings : SettingsData ,
344- transfers : List <TransferEntity >,
345- profileAuthenticated : Boolean ,
346- ) = listOfNotNull(
347- Suggestion .BACK_UP .takeIf { ! settings.backupVerified },
348- Suggestion .SECURE .takeIf { ! settings.isPinEnabled },
349- Suggestion .LIGHTNING .takeIf {
350- transfers.all { it.type != TransferType .TO_SPENDING }
351- },
352- Suggestion .SUPPORT ,
353- Suggestion .PROFILE .takeIf { ! profileAuthenticated },
354- Suggestion .INVITE ,
355- Suggestion .BUY ,
356- )
357-
358- private fun emptyWalletSuggestions (
359- settings : SettingsData ,
360- transfers : List <TransferEntity >,
361- profileAuthenticated : Boolean ,
362- ) = listOfNotNull(
363- Suggestion .BUY ,
364- Suggestion .LIGHTNING .takeIf {
365- transfers.all { it.type != TransferType .TO_SPENDING }
366- },
367- Suggestion .SUPPORT ,
368- Suggestion .BACK_UP .takeIf { ! settings.backupVerified },
369- Suggestion .SECURE .takeIf { ! settings.isPinEnabled },
370- Suggestion .PROFILE .takeIf { ! profileAuthenticated },
371- Suggestion .INVITE ,
372- )
373303}
0 commit comments