|
| 1 | +package com.flipcash.app.directsend.internal |
| 2 | + |
| 3 | +import androidx.compose.foundation.text.input.TextFieldState |
| 4 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 5 | +import androidx.compose.material3.SearchBarState |
| 6 | +import androidx.compose.runtime.snapshotFlow |
| 7 | +import androidx.lifecycle.viewModelScope |
| 8 | +import com.flipcash.app.contacts.ContactCoordinator |
| 9 | +import com.flipcash.app.contacts.ContactCoordinator.ContactState |
| 10 | +import com.flipcash.app.contacts.device.DeviceContact |
| 11 | +import com.flipcash.app.contacts.device.PickedContactData |
| 12 | +import com.flipcash.app.core.send.SendStep |
| 13 | +import com.flipcash.app.featureflags.FeatureFlag |
| 14 | +import com.flipcash.app.featureflags.FeatureFlagController |
| 15 | +import com.flipcash.app.permissions.PickedContact |
| 16 | +import com.flipcash.features.directsend.R |
| 17 | +import com.flipcash.services.user.UserManager |
| 18 | +import com.getcode.util.resources.ResourceHelper |
| 19 | +import com.getcode.view.BaseViewModel |
| 20 | +import com.getcode.view.LoadingSuccessState |
| 21 | +import dagger.hilt.android.lifecycle.HiltViewModel |
| 22 | +import kotlinx.coroutines.delay |
| 23 | +import kotlinx.coroutines.flow.combine |
| 24 | +import kotlinx.coroutines.flow.filter |
| 25 | +import kotlinx.coroutines.flow.filterIsInstance |
| 26 | +import kotlinx.coroutines.flow.flatMapLatest |
| 27 | +import kotlinx.coroutines.flow.launchIn |
| 28 | +import kotlinx.coroutines.flow.map |
| 29 | +import kotlinx.coroutines.flow.onEach |
| 30 | +import javax.inject.Inject |
| 31 | +import kotlin.time.Duration.Companion.seconds |
| 32 | + |
| 33 | +@HiltViewModel |
| 34 | +internal class SendFlowViewModel @Inject constructor( |
| 35 | + userManager: UserManager, |
| 36 | + featureFlags: FeatureFlagController, |
| 37 | + private val contactCoordinator: ContactCoordinator, |
| 38 | + private val resources: ResourceHelper, |
| 39 | +) : BaseViewModel<SendFlowViewModel.State, SendFlowViewModel.Event>( |
| 40 | + initialState = State(), |
| 41 | + updateStateForEvent = updateStateForEvent, |
| 42 | +) { |
| 43 | + |
| 44 | + data class State @OptIn(ExperimentalMaterial3Api::class) constructor( |
| 45 | + val steps: List<SendStep> = listOf(SendStep.ContactList), |
| 46 | + val searchState: TextFieldState = TextFieldState(), |
| 47 | + val isPickerMode: Boolean = false, |
| 48 | + val contactSyncState: LoadingSuccessState = LoadingSuccessState(), |
| 49 | + val listItems: List<ContactListItem> = emptyList(), |
| 50 | + ) |
| 51 | + |
| 52 | + sealed interface Event { |
| 53 | + data class StepsUpdated(val steps: List<SendStep>, val isPickerMode: Boolean) : Event |
| 54 | + |
| 55 | + data object ContactsGranted : Event |
| 56 | + data class ContactsPicked(val contacts: List<PickedContact>) : Event |
| 57 | + data class OnItemsPopulated(val items: List<ContactListItem>) : Event |
| 58 | + data class ContactSyncStateUpdated( |
| 59 | + val loading: Boolean = false, |
| 60 | + val success: Boolean = false, |
| 61 | + val error: Boolean = false, |
| 62 | + ) : Event |
| 63 | + |
| 64 | + data object ContactSyncComplete : Event |
| 65 | + data class OnContactClicked(val contact: ContactListItem.ContactRow) : Event |
| 66 | + data class ContactRemoved(val e164: String) : Event |
| 67 | + data class SendInvite(val contact: DeviceContact) : Event |
| 68 | + data class SendCashToContact(val contact: DeviceContact) : Event |
| 69 | + } |
| 70 | + |
| 71 | + init { |
| 72 | + combine( |
| 73 | + userManager.state, |
| 74 | + featureFlags.observe(FeatureFlag.PhoneNumberSend), |
| 75 | + featureFlags.observe(FeatureFlag.ContactPickerMode), |
| 76 | + contactCoordinator.state, |
| 77 | + ) { userState, phoneNumberSendFlag, contactPickerMode, contactState -> |
| 78 | + val hasLinkedPhone = userState.userProfile?.verifiedPhoneNumber != null |
| 79 | + val phoneNumberSendEnabled = phoneNumberSendFlag || |
| 80 | + userState.flags?.enablePhoneNumberSend == true |
| 81 | + val hasContacts = contactState.contacts.isNotEmpty() |
| 82 | + val needsContacts = phoneNumberSendEnabled && !hasContacts && !contactState.hasEverSynced |
| 83 | + |
| 84 | + val steps = buildList { |
| 85 | + if (!hasLinkedPhone) add(SendStep.PhoneGate) |
| 86 | + if (needsContacts) add(SendStep.ContactsGate) |
| 87 | + add(SendStep.ContactList) |
| 88 | + } |
| 89 | + Event.StepsUpdated(steps = steps, isPickerMode = contactPickerMode) |
| 90 | + }.onEach { event -> |
| 91 | + dispatchEvent(event) |
| 92 | + }.launchIn(viewModelScope) |
| 93 | + |
| 94 | + combine( |
| 95 | + contactCoordinator.state, |
| 96 | + stateFlow.map { it.searchState }.flatMapLatest { snapshotFlow { it.text } } |
| 97 | + ) { contactState, searchText -> |
| 98 | + generateListItems(contactState, searchText.toString()) |
| 99 | + }.onEach { items -> |
| 100 | + dispatchEvent(Event.OnItemsPopulated(items)) |
| 101 | + }.launchIn(viewModelScope) |
| 102 | + |
| 103 | + eventFlow |
| 104 | + .filterIsInstance<Event.ContactsGranted>() |
| 105 | + .onEach { |
| 106 | + dispatchEvent(Event.ContactSyncStateUpdated(loading = true)) |
| 107 | + contactCoordinator.sync() |
| 108 | + .onSuccess { |
| 109 | + dispatchEvent(Event.ContactSyncStateUpdated(success = true)) |
| 110 | + delay(1.seconds) |
| 111 | + dispatchEvent(Event.ContactSyncComplete) |
| 112 | + } |
| 113 | + .onFailure { |
| 114 | + dispatchEvent(Event.ContactSyncStateUpdated(error = true)) |
| 115 | + delay(1.seconds) |
| 116 | + } |
| 117 | + dispatchEvent(Event.ContactSyncStateUpdated()) |
| 118 | + }.launchIn(viewModelScope) |
| 119 | + |
| 120 | + eventFlow |
| 121 | + .filterIsInstance<Event.ContactsPicked>() |
| 122 | + .map { |
| 123 | + it.contacts.map { contact -> |
| 124 | + PickedContactData( |
| 125 | + phoneNumber = contact.phoneNumber, |
| 126 | + displayName = contact.displayName, |
| 127 | + photoUri = contact.photoUri, |
| 128 | + ) |
| 129 | + } |
| 130 | + } |
| 131 | + .onEach { contacts -> |
| 132 | + dispatchEvent(Event.ContactSyncStateUpdated(loading = true)) |
| 133 | + contactCoordinator.addPickedContacts(contacts) |
| 134 | + .onSuccess { |
| 135 | + dispatchEvent(Event.ContactSyncStateUpdated(success = true)) |
| 136 | + delay(1.seconds) |
| 137 | + dispatchEvent(Event.ContactSyncComplete) |
| 138 | + } |
| 139 | + .onFailure { |
| 140 | + dispatchEvent(Event.ContactSyncStateUpdated(error = true)) |
| 141 | + delay(1.seconds) |
| 142 | + } |
| 143 | + dispatchEvent(Event.ContactSyncStateUpdated()) |
| 144 | + }.launchIn(viewModelScope) |
| 145 | + |
| 146 | + eventFlow |
| 147 | + .filterIsInstance<Event.OnContactClicked>() |
| 148 | + .map { it.contact } |
| 149 | + .onEach { (contact, isOnFlipcash) -> |
| 150 | + if (isOnFlipcash) { |
| 151 | + dispatchEvent(Event.SendCashToContact(contact)) |
| 152 | + } else { |
| 153 | + dispatchEvent(Event.SendInvite(contact)) |
| 154 | + } |
| 155 | + }.launchIn(viewModelScope) |
| 156 | + |
| 157 | + eventFlow |
| 158 | + .filterIsInstance<Event.ContactRemoved>() |
| 159 | + .onEach { event -> contactCoordinator.removeContact(event.e164) } |
| 160 | + .launchIn(viewModelScope) |
| 161 | + |
| 162 | + // SendInvite is observed by the UI layer (ContactListScreen) for navigation |
| 163 | + } |
| 164 | + |
| 165 | + private fun generateListItems( |
| 166 | + contactState: ContactState, |
| 167 | + searchString: String, |
| 168 | + ): List<ContactListItem> = buildList { |
| 169 | + val allContacts = contactState.contacts.values.toList() |
| 170 | + val filtered = if (searchString.isBlank()) { |
| 171 | + allContacts |
| 172 | + } else { |
| 173 | + allContacts.filter { |
| 174 | + it.displayName.contains(searchString, ignoreCase = true) || |
| 175 | + it.e164.contains(searchString, ignoreCase = true) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + val flipcash = filtered |
| 180 | + .filter { it.e164 in contactState.flipcashE164s } |
| 181 | + .sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.displayName }) |
| 182 | + val other = filtered |
| 183 | + .filter { it.e164 !in contactState.flipcashE164s } |
| 184 | + .sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.displayName }) |
| 185 | + |
| 186 | + if (flipcash.isNotEmpty()) { |
| 187 | + add(ContactListItem.Header(resources.getString(R.string.title_flipcashContacts))) |
| 188 | + flipcash.forEach { add(ContactListItem.ContactRow(it, isOnFlipcash = true)) } |
| 189 | + } |
| 190 | + if (other.isNotEmpty()) { |
| 191 | + add(ContactListItem.Header(resources.getString(R.string.title_nonFlipcashContacts))) |
| 192 | + other.forEach { add(ContactListItem.ContactRow(it, isOnFlipcash = false)) } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + companion object { |
| 197 | + val updateStateForEvent: (Event) -> ((State) -> State) = { event -> |
| 198 | + when (event) { |
| 199 | + is Event.StepsUpdated -> { state -> |
| 200 | + state.copy(steps = event.steps, isPickerMode = event.isPickerMode) |
| 201 | + } |
| 202 | + |
| 203 | + is Event.ContactsGranted -> { state -> state } |
| 204 | + is Event.ContactsPicked -> { state -> state } |
| 205 | + is Event.ContactSyncStateUpdated -> { state -> |
| 206 | + state.copy( |
| 207 | + contactSyncState = LoadingSuccessState( |
| 208 | + event.loading, |
| 209 | + event.success, |
| 210 | + event.error |
| 211 | + ) |
| 212 | + ) |
| 213 | + } |
| 214 | + |
| 215 | + is Event.ContactRemoved -> { state -> state } |
| 216 | + is Event.ContactSyncComplete -> { state -> state } |
| 217 | + is Event.OnItemsPopulated -> { state -> state.copy(listItems = event.items) } |
| 218 | + is Event.OnContactClicked -> { state -> state } |
| 219 | + is Event.SendInvite -> { state -> state } |
| 220 | + is Event.SendCashToContact -> { state -> state } |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments