Skip to content

Commit e944a26

Browse files
authored
Merge pull request #547 from code-payments/chore/make-home-vm-more-extensible
chore: HomeViewModel => Session
2 parents 4800b86 + 4959988 commit e944a26

43 files changed

Lines changed: 284 additions & 292 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/src/main/java/com/getcode/manager/SessionManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import javax.inject.Inject
1818
import javax.inject.Singleton
1919

2020

21+
// TODO: figure out a better naming paradigm b/w this and Session
2122
@Singleton
2223
class SessionManager @Inject constructor(
2324
private val client: Client,

app/src/main/java/com/getcode/CodeApp.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ import com.getcode.ui.components.bars.TopBarContainer
4747
import com.getcode.ui.utils.getActivity
4848
import com.getcode.ui.utils.getActivityScopedViewModel
4949
import com.getcode.ui.utils.measured
50+
import com.getcode.ui.utils.rememberBiometricsState
5051
import com.getcode.util.BiometricsError
51-
import com.getcode.view.main.home.components.BiometricsBlockingView
52-
import com.getcode.view.main.home.components.rememberBiometricsState
52+
import com.getcode.view.main.scanner.views.BiometricsBlockingView
5353
import dev.bmcreations.tipkit.TipScaffold
5454
import dev.bmcreations.tipkit.engines.TipsEngine
5555

app/src/main/java/com/getcode/Locals.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import com.getcode.analytics.AnalyticsServiceNull
99
import com.getcode.network.exchange.Exchange
1010
import com.getcode.network.exchange.ExchangeNull
1111
import com.getcode.network.repository.BetaOptions
12+
import com.getcode.ui.utils.BiometricsState
1213
import com.getcode.util.CurrencyUtils
1314
import com.getcode.util.DeeplinkHandler
1415
import com.getcode.util.PhoneUtils
1516
import com.getcode.utils.network.NetworkConnectivityListener
1617
import com.getcode.utils.network.NetworkObserverStub
17-
import com.getcode.view.main.home.components.BiometricsState
1818

19+
val LocalSession: ProvidableCompositionLocal<Session?> = staticCompositionLocalOf { null }
1920
val LocalAnalytics: ProvidableCompositionLocal<AnalyticsService> = staticCompositionLocalOf { AnalyticsServiceNull() }
2021
val LocalNetworkObserver: ProvidableCompositionLocal<NetworkConnectivityListener> = staticCompositionLocalOf { NetworkObserverStub() }
2122
val LocalPhoneFormatter: ProvidableCompositionLocal<PhoneUtils?> = staticCompositionLocalOf { null }

app/src/main/java/com/getcode/view/main/home/HomeViewModel.kt renamed to app/src/main/java/com/getcode/Session.kt

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.getcode.view.main.home
1+
package com.getcode
22

33
import android.Manifest
44
import android.annotation.SuppressLint
@@ -12,8 +12,6 @@ import androidx.core.app.NotificationManagerCompat
1212
import androidx.core.net.toUri
1313
import androidx.lifecycle.viewModelScope
1414
import cafe.adriel.voyager.core.model.ScreenModel
15-
import com.getcode.BuildConfig
16-
import com.getcode.R
1715
import com.getcode.analytics.AnalyticsManager
1816
import com.getcode.analytics.AnalyticsService
1917
import com.getcode.domain.CashLinkManager
@@ -99,9 +97,8 @@ import com.getcode.utils.nonce
9997
import com.getcode.utils.trace
10098
import com.getcode.vendor.Base58
10199
import com.getcode.view.BaseViewModel
102-
import com.kik.kikx.kikcodes.KikCodeScanner
100+
import com.getcode.view.main.scanner.UiElement
103101
import com.kik.kikx.kikcodes.implementation.KikCodeAnalyzer
104-
import com.kik.kikx.kikcodes.implementation.KikCodeScannerImpl
105102
import com.kik.kikx.models.ScannableKikCode
106103
import dagger.hilt.android.lifecycle.HiltViewModel
107104
import io.reactivex.rxjava3.core.Completable
@@ -147,7 +144,7 @@ sealed interface PresentationStyle {
147144
data object Slide : PresentationStyle, Visible
148145
}
149146

150-
data class HomeUiModel(
147+
data class SessionState(
151148
val isCameraPermissionGranted: Boolean? = null,
152149
val vibrateOnScan: Boolean = false,
153150
val balance: KinAmount? = null,
@@ -167,14 +164,18 @@ data class HomeUiModel(
167164
val invertedDragZoom: Feature = InvertedDragZoomFeature(),
168165
val gallery: Feature = GalleryFeature(),
169166
val flippableTipCard: Feature = FlippableTipCardFeature(),
170-
val actions: List<HomeAction> = listOf(HomeAction.GIVE_KIN, HomeAction.TIP_CARD, HomeAction.BALANCE),
167+
val scannerElements: List<UiElement> = listOf(
168+
UiElement.GIVE_KIN,
169+
UiElement.TIP_CARD,
170+
UiElement.BALANCE
171+
),
171172
val tipCardConnected: Boolean = false,
172173
)
173174

174-
sealed interface HomeEvent {
175-
data object PresentTipEntry : HomeEvent
176-
data object RequestNotificationPermissions: HomeEvent
177-
data class SendIntent(val intent: Intent): HomeEvent
175+
sealed interface SessionEvent {
176+
data object PresentTipEntry : SessionEvent
177+
data object RequestNotificationPermissions: SessionEvent
178+
data class SendIntent(val intent: Intent): SessionEvent
178179
}
179180

180181
enum class RestrictionType {
@@ -185,7 +186,7 @@ enum class RestrictionType {
185186

186187
@SuppressLint("CheckResult")
187188
@HiltViewModel
188-
class HomeViewModel @Inject constructor(
189+
class Session @Inject constructor(
189190
private val client: Client,
190191
private val receiveTransactionRepository: ReceiveTransactionRepository,
191192
private val paymentRepository: PaymentRepository,
@@ -210,10 +211,10 @@ class HomeViewModel @Inject constructor(
210211
betaFlagsRepository: BetaFlagsRepository,
211212
features: FeatureRepository,
212213
) : BaseViewModel(resources), ScreenModel {
213-
val uiFlow = MutableStateFlow(HomeUiModel())
214+
val uiFlow = MutableStateFlow(SessionState())
214215

215-
private val _eventFlow: MutableSharedFlow<HomeEvent> = MutableSharedFlow()
216-
val eventFlow: SharedFlow<HomeEvent> = _eventFlow.asSharedFlow()
216+
private val _eventFlow: MutableSharedFlow<SessionEvent> = MutableSharedFlow()
217+
val eventFlow: SharedFlow<SessionEvent> = _eventFlow.asSharedFlow()
217218

218219
private var sheetDismissTimer: TimerTask? = null
219220

@@ -280,7 +281,7 @@ class HomeViewModel @Inject constructor(
280281
betaFlagsRepository.observe()
281282
.distinctUntilChanged()
282283
.onEach { betaFlags ->
283-
uiFlow.update { it.copy(actions = buildActions(betaFlags)) }
284+
uiFlow.update { it.copy(scannerElements = buildScannerElements(betaFlags)) }
284285
}.launchIn(viewModelScope)
285286

286287
tipController.showTwitterSplat
@@ -441,21 +442,21 @@ class HomeViewModel @Inject constructor(
441442
}
442443
}
443444

444-
private fun buildActions(
445+
private fun buildScannerElements(
445446
betaOptions: BetaOptions,
446-
): List<HomeAction> {
447-
val actions = mutableListOf(HomeAction.GIVE_KIN)
447+
): List<UiElement> {
448+
val actions = mutableListOf(UiElement.GIVE_KIN)
448449
actions += if (betaOptions.tipCardOnHomeScreen) {
449-
HomeAction.TIP_CARD
450+
UiElement.TIP_CARD
450451
} else {
451-
HomeAction.GET_KIN
452+
UiElement.GET_KIN
452453
}
453454

454455
if (betaOptions.conversationsEnabled) {
455-
actions += HomeAction.CHAT
456+
actions += UiElement.CHAT
456457
}
457458

458-
actions += HomeAction.BALANCE
459+
actions += UiElement.BALANCE
459460

460461
return actions
461462
}
@@ -863,7 +864,7 @@ class HomeViewModel @Inject constructor(
863864
when {
864865
isDenied -> {
865866
viewModelScope.launch {
866-
_eventFlow.emit(HomeEvent.RequestNotificationPermissions)
867+
_eventFlow.emit(SessionEvent.RequestNotificationPermissions)
867868
}
868869
}
869870
else -> {
@@ -918,7 +919,7 @@ class HomeViewModel @Inject constructor(
918919
)
919920
)
920921
viewModelScope.launch {
921-
_eventFlow.emit(HomeEvent.SendIntent(intent))
922+
_eventFlow.emit(SessionEvent.SendIntent(intent))
922923
}
923924
cancelTip()
924925
},
@@ -928,7 +929,7 @@ class HomeViewModel @Inject constructor(
928929
)
929930
}.onSuccess {
930931
delay(300.milliseconds)
931-
_eventFlow.emit(HomeEvent.PresentTipEntry)
932+
_eventFlow.emit(SessionEvent.PresentTipEntry)
932933
analytics.tipCardShown(username)
933934
}
934935
}
@@ -1194,7 +1195,7 @@ class HomeViewModel @Inject constructor(
11941195
).apply {
11951196
flags = Intent.FLAG_ACTIVITY_NEW_TASK
11961197
}
1197-
_eventFlow.emit(HomeEvent.SendIntent(intent))
1198+
_eventFlow.emit(SessionEvent.SendIntent(intent))
11981199
}
11991200
}
12001201
} else {
@@ -1208,7 +1209,7 @@ class HomeViewModel @Inject constructor(
12081209
).apply {
12091210
flags = Intent.FLAG_ACTIVITY_NEW_TASK
12101211
}
1211-
_eventFlow.emit(HomeEvent.SendIntent(intent))
1212+
_eventFlow.emit(SessionEvent.SendIntent(intent))
12121213
}
12131214
}
12141215
}
@@ -1361,7 +1362,7 @@ class HomeViewModel @Inject constructor(
13611362
).apply {
13621363
flags = Intent.FLAG_ACTIVITY_NEW_TASK
13631364
}
1364-
_eventFlow.emit(HomeEvent.SendIntent(intent))
1365+
_eventFlow.emit(SessionEvent.SendIntent(intent))
13651366
}
13661367
} else {
13671368
request?.successUrl?.let { url ->
@@ -1371,7 +1372,7 @@ class HomeViewModel @Inject constructor(
13711372
).apply {
13721373
flags = Intent.FLAG_ACTIVITY_NEW_TASK
13731374
}
1374-
_eventFlow.emit(HomeEvent.SendIntent(intent))
1375+
_eventFlow.emit(SessionEvent.SendIntent(intent))
13751376
}
13761377
}
13771378
}
@@ -1550,7 +1551,7 @@ class HomeViewModel @Inject constructor(
15501551
withContext(Dispatchers.Main) {
15511552
val shareIntent = IntentUtils.tipCard(connectedAccount.username, connectedAccount.platform)
15521553

1553-
_eventFlow.emit(HomeEvent.SendIntent(shareIntent))
1554+
_eventFlow.emit(SessionEvent.SendIntent(shareIntent))
15541555
}
15551556
}
15561557

@@ -1581,7 +1582,7 @@ class HomeViewModel @Inject constructor(
15811582

15821583
CoroutineScope(Dispatchers.IO).launch {
15831584
withContext(Dispatchers.Main) {
1584-
_eventFlow.emit(HomeEvent.SendIntent(shareIntent))
1585+
_eventFlow.emit(SessionEvent.SendIntent(shareIntent))
15851586
}
15861587
delay(2500)
15871588

app/src/main/java/com/getcode/navigation/screens/MainScreens.kt

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ import cafe.adriel.voyager.core.lifecycle.LifecycleEffect
1515
import cafe.adriel.voyager.core.screen.ScreenKey
1616
import cafe.adriel.voyager.core.screen.uniqueScreenKey
1717
import cafe.adriel.voyager.hilt.getViewModel
18+
import cafe.adriel.voyager.navigator.currentOrThrow
19+
import com.getcode.LocalSession
1820
import com.getcode.R
19-
import com.getcode.model.KinAmount
2021
import com.getcode.models.DeepLinkRequest
2122
import com.getcode.navigation.core.LocalCodeNavigator
2223
import com.getcode.ui.components.SheetTitleDefaults
@@ -29,27 +30,17 @@ import com.getcode.view.main.account.AccountSheetViewModel
2930
import com.getcode.view.main.balance.BalanceScreen
3031
import com.getcode.view.main.balance.BalanceSheetViewModel
3132
import com.getcode.view.main.giveKin.GiveKinScreen
32-
import com.getcode.view.main.home.HomeScreen
33-
import com.getcode.view.main.home.HomeViewModel
33+
import com.getcode.view.main.scanner.ScanScreen
3434
import com.getcode.view.main.requestKin.RequestKinScreen
35-
import com.google.firebase.encoders.annotations.Encodable.Ignore
3635
import kotlinx.coroutines.flow.filterNotNull
3736
import kotlinx.coroutines.flow.launchIn
3837
import kotlinx.coroutines.flow.mapNotNull
3938
import kotlinx.coroutines.flow.onEach
4039
import kotlinx.parcelize.IgnoredOnParcel
4140
import kotlinx.parcelize.Parcelize
4241

43-
sealed interface HomeResult {
44-
data class Bill(val bill: com.getcode.models.Bill) : HomeResult
45-
data class Request(val amount: KinAmount) : HomeResult
46-
data class ConfirmTip(val amount: KinAmount) : HomeResult
47-
data object ShowTipCard : HomeResult
48-
data object CancelTipEntry: HomeResult
49-
}
50-
5142
@Parcelize
52-
data class HomeScreen(
43+
data class ScanScreen(
5344
val seed: String? = null,
5445
val cashLink: String? = null,
5546
@IgnoredOnParcel
@@ -60,34 +51,10 @@ data class HomeScreen(
6051

6152
@Composable
6253
override fun Content() {
63-
val vm = getViewModel<HomeViewModel>()
64-
6554
trace("home rendered")
66-
HomeScreen(vm, cashLink, request)
67-
68-
OnScreenResult<HomeResult> { result ->
69-
when (result) {
70-
is HomeResult.Bill -> {
71-
vm.showBill(result.bill)
72-
}
73-
74-
is HomeResult.Request -> {
75-
vm.presentRequest(amount = result.amount, payload = null, request = null)
76-
}
55+
val session = LocalSession.currentOrThrow
7756

78-
is HomeResult.ConfirmTip -> {
79-
vm.presentTipConfirmation(result.amount)
80-
}
81-
82-
is HomeResult.ShowTipCard -> {
83-
vm.presentShareableTipCard()
84-
}
85-
86-
is HomeResult.CancelTipEntry -> {
87-
vm.cancelTipEntry()
88-
}
89-
}
90-
}
57+
ScanScreen(session, cashLink, request)
9158
}
9259
}
9360

app/src/main/java/com/getcode/navigation/screens/ModalScreens.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import androidx.compose.ui.res.stringResource
1010
import cafe.adriel.voyager.core.screen.ScreenKey
1111
import cafe.adriel.voyager.core.screen.uniqueScreenKey
1212
import cafe.adriel.voyager.hilt.getViewModel
13+
import cafe.adriel.voyager.navigator.currentOrThrow
14+
import com.getcode.LocalSession
1315
import com.getcode.R
1416
import com.getcode.navigation.core.LocalCodeNavigator
1517
import com.getcode.theme.CodeTheme
@@ -407,6 +409,8 @@ data class EnterTipModal(val isInChat: Boolean = false) : MainGraph, ModalRoot {
407409
@Composable
408410
override fun Content() {
409411
val navigator = LocalCodeNavigator.current
412+
val session = LocalSession.currentOrThrow
413+
410414
if (isInChat) {
411415
ModalContainer(
412416
backButtonEnabled = {
@@ -417,8 +421,8 @@ data class EnterTipModal(val isInChat: Boolean = false) : MainGraph, ModalRoot {
417421
}
418422
}
419423
) {
420-
EnterTipScreen(getViewModel()) { result ->
421-
navigator.popWithResult(result)
424+
EnterTipScreen(getViewModel()) {
425+
navigator.pop()
422426
}
423427
}
424428
} else {
@@ -431,17 +435,19 @@ data class EnterTipModal(val isInChat: Boolean = false) : MainGraph, ModalRoot {
431435
}
432436
},
433437
onCloseClicked = {
434-
navigator.hideWithResult(HomeResult.CancelTipEntry)
438+
session.cancelTipEntry()
439+
navigator.hide()
435440
}
436441
) {
437-
EnterTipScreen(getViewModel()) { result ->
438-
navigator.hideWithResult(result)
442+
EnterTipScreen(getViewModel()) {
443+
navigator.hide()
439444
}
440445
}
441446
}
442447

443448
BackHandler {
444-
navigator.hideWithResult(HomeResult.CancelTipEntry)
449+
session.cancelTipEntry()
450+
navigator.hide()
445451
}
446452
}
447453

0 commit comments

Comments
 (0)