-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMyCryptoPresenter.kt
300 lines (264 loc) · 11.1 KB
/
MyCryptoPresenter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package org.p2p.wallet.home.ui.crypto
import android.content.SharedPreferences
import timber.log.Timber
import java.math.BigDecimal
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.p2p.core.network.ConnectionManager
import org.p2p.core.token.Token
import org.p2p.core.token.TokenVisibility
import org.p2p.core.token.filterTokensForWalletScreen
import org.p2p.core.utils.isMoreThan
import org.p2p.core.utils.scaleTwo
import org.p2p.uikit.model.AnyCellItem
import org.p2p.wallet.R
import org.p2p.wallet.auth.interactor.UsernameInteractor
import org.p2p.wallet.auth.model.Username
import org.p2p.wallet.common.mvp.BasePresenter
import org.p2p.wallet.common.ui.widget.actionbuttons.ActionButton
import org.p2p.wallet.home.interactor.MyCryptoInteractor
import org.p2p.wallet.home.model.VisibilityState
import org.p2p.wallet.home.ui.crypto.analytics.CryptoScreenAnalytics
import org.p2p.wallet.home.ui.crypto.handlers.BridgeClaimBundleClickHandler
import org.p2p.wallet.home.ui.crypto.mapper.MyCryptoMapper
import org.p2p.wallet.home.ui.wallet.analytics.MainScreenAnalytics
import org.p2p.wallet.infrastructure.network.provider.TokenKeyProvider
import org.p2p.wallet.pnl.interactor.PnlDataObserver
import org.p2p.wallet.pnl.interactor.PnlDataState
import org.p2p.wallet.pnl.ui.PnlUiMapper
import org.p2p.wallet.settings.interactor.SettingsInteractor
import org.p2p.wallet.tokenservice.TokenServiceCoordinator
import org.p2p.wallet.tokenservice.UserTokensState
import org.p2p.wallet.utils.ellipsizeAddress
import org.p2p.wallet.utils.unsafeLazy
private val MINIMAL_DUST_FOR_BALANCE = BigDecimal(0.01)
class MyCryptoPresenter(
private val cryptoInteractor: MyCryptoInteractor,
private val cryptoMapper: MyCryptoMapper,
private val connectionManager: ConnectionManager,
private val tokenServiceCoordinator: TokenServiceCoordinator,
private val cryptoScreenAnalytics: CryptoScreenAnalytics,
private val claimHandler: BridgeClaimBundleClickHandler,
private val sharedPreferences: SharedPreferences,
private val usernameInteractor: UsernameInteractor,
private val tokenKeyProvider: TokenKeyProvider,
private val mainScreenAnalytics: MainScreenAnalytics,
private val pnlDataObserver: PnlDataObserver,
private val pnlUiMapper: PnlUiMapper,
) : BasePresenter<MyCryptoContract.View>(), MyCryptoContract.Presenter {
private var currentVisibilityState: VisibilityState = if (cryptoInteractor.getHiddenTokensVisibility()) {
VisibilityState.Visible
} else {
VisibilityState.Hidden
}
private val sharedPreferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key == SettingsInteractor.KEY_HIDDEN_ZERO_BALANCE) {
observeCryptoTokens()
}
}
private val userPublicKey: String by unsafeLazy { tokenKeyProvider.publicKey }
private var username: Username? = null
private var cryptoTokensSubscription: Job? = null
private var pnlDataSubscription: Job? = null
override fun attach(view: MyCryptoContract.View) {
super.attach(view)
showUserAddressAndUsername()
prepareAndShowActionButtons()
observeCryptoTokens()
observePnlData()
cryptoScreenAnalytics.logCryptoScreenOpened()
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
}
override fun refreshTokens() {
launchInternetAware(connectionManager) {
try {
tokenServiceCoordinator.refresh()
// pnl must restart it's 5 minutes timer after force refresh
// and we should restart it only if it was initially started
// other cases might indicate that we didn't start observer due to empty state
if (pnlDataObserver.isStarted()) {
pnlDataObserver.restartAndRefresh()
}
} catch (cancelled: CancellationException) {
Timber.i("Loading tokens job cancelled")
} catch (error: Throwable) {
Timber.e(error, "Error refreshing user tokens")
view?.showErrorMessage(error)
}
}
}
override fun onTokenClicked(token: Token.Active) {
cryptoScreenAnalytics.logCryptoTokenClick(tokenName = token.tokenName, tokenSymbol = token.tokenSymbol)
view?.showTokenHistory(token)
}
override fun onAmountClicked() {
cryptoScreenAnalytics.logCryptoAmountClick()
}
private fun observeCryptoTokens() {
cryptoTokensSubscription?.cancel()
cryptoTokensSubscription = tokenServiceCoordinator.observeUserTokens()
.onEach { handleTokenState(it) }
.launchIn(this)
}
private fun observePnlData() {
pnlDataSubscription?.cancel()
pnlDataSubscription = pnlDataObserver.pnlState
.onEach { handleTokenState(tokenServiceCoordinator.observeLastState().value) }
.launchIn(this)
}
private fun handleTokenState(newState: UserTokensState) {
view?.showRefreshing(isRefreshing = newState.isLoading())
when (newState) {
is UserTokensState.Idle -> Unit
is UserTokensState.Loading -> Unit
is UserTokensState.Refreshing -> Unit
is UserTokensState.Error -> Unit
is UserTokensState.Empty -> {
view?.showEmptyState(isEmpty = true)
handleEmptyAccount()
}
is UserTokensState.Loaded -> {
view?.showEmptyState(isEmpty = false)
if (!pnlDataObserver.isStarted()) {
pnlDataObserver.start()
}
showTokensAndBalance(
pnlDataState = pnlDataObserver.pnlState.value,
// separated screens logic: solTokens = filterCryptoTokens(newState.solTokens),
solTokens = newState.solTokens,
ethTokens = newState.ethTokens
)
}
}
}
private fun filterCryptoTokens(solTokens: List<Token.Active>): List<Token.Active> {
val excludedTokens = solTokens.filterTokensForWalletScreen()
return solTokens.minus(excludedTokens.toSet())
}
private fun showTokensAndBalance(
pnlDataState: PnlDataState,
solTokens: List<Token.Active>,
ethTokens: List<Token.Eth>
) {
val balance = getUserBalance(solTokens)
view?.showBalance(cryptoMapper.mapBalance(balance))
view?.showBalancePnl(pnlUiMapper.mapBalancePnl(pnlDataState))
cryptoScreenAnalytics.logUserAggregateBalanceBase(balance)
val areZerosHidden = cryptoInteractor.areZerosHidden()
if (ethTokens.isNotEmpty()) {
cryptoScreenAnalytics.logCryptoClaimTransferedViewed(ethTokens.size)
}
val mappedItems: List<AnyCellItem> = cryptoMapper.mapToCellItems(
pnlDataState = pnlDataState,
tokens = solTokens,
ethereumTokens = ethTokens,
visibilityState = currentVisibilityState,
isZerosHidden = areZerosHidden,
)
view?.showItems(mappedItems)
}
override fun onBalancePnlClicked() {
if (pnlDataObserver.pnlState.value.isLoaded()) {
view?.showPnlDetails(pnlDataObserver.pnlState.value.toLoadedOrNull()!!.total.percent)
}
}
private fun showUserAddressAndUsername() {
this.username = usernameInteractor.getUsername()
val userAddress = username?.fullUsername ?: userPublicKey.ellipsizeAddress(6)
view?.showUserAddress(userAddress)
}
private fun getUserBalance(tokens: List<Token.Active>): BigDecimal {
if (tokens.none { it.totalInUsd != null }) return BigDecimal.ZERO
return tokens
.mapNotNull(Token.Active::totalInUsd)
.filter { it.isMoreThan(MINIMAL_DUST_FOR_BALANCE) }
.fold(BigDecimal.ZERO, BigDecimal::add)
.scaleTwo()
}
private fun handleEmptyAccount() {
cryptoScreenAnalytics.logUserAggregateBalanceBase(BigDecimal.ZERO)
view?.showBalancePnl(null)
view?.showBalance(cryptoMapper.mapBalance(BigDecimal.ZERO))
view?.showItems(cryptoMapper.mapEmptyStateCellItems())
}
override fun toggleTokenVisibility(token: Token.Active) {
launch {
val handleDefaultVisibility = { token: Token.Active ->
if (cryptoInteractor.areZerosHidden() && token.isZero) {
TokenVisibility.SHOWN
} else {
TokenVisibility.HIDDEN
}
}
val newVisibility = when (token.visibility) {
TokenVisibility.SHOWN -> TokenVisibility.HIDDEN
TokenVisibility.HIDDEN -> TokenVisibility.SHOWN
TokenVisibility.DEFAULT -> handleDefaultVisibility(token)
}
cryptoInteractor.setTokenHidden(
mintAddress = token.mintAddress,
visibility = newVisibility.stringValue
)
}
}
override fun toggleTokenVisibilityState() {
currentVisibilityState = currentVisibilityState.toggle()
cryptoInteractor.setHiddenTokensVisibility(currentVisibilityState.isVisible)
observeCryptoTokens()
}
private fun prepareAndShowActionButtons() {
val buttons = listOf(
ActionButton.SELL_BUTTON,
ActionButton.BUY_BUTTON,
ActionButton.SWAP_BUTTON,
ActionButton.RECEIVE_BUTTON,
ActionButton.SEND_BUTTON
)
view?.showActionButtons(buttons)
}
override fun onBuyClicked() {
mainScreenAnalytics.logMainScreenAddMoneyClick()
view?.showAddMoneyDialog()
}
override fun onReceiveClicked() {
cryptoScreenAnalytics.logCryptoReceiveClick()
view?.navigateToReceive()
}
override fun onSwapClicked() {
cryptoScreenAnalytics.logCryptoSwapClick()
view?.navigateToSwap()
}
override fun onSellClicked() {
view?.navigateToSell()
}
override fun onClaimClicked(canBeClaimed: Boolean, token: Token.Eth) {
cryptoScreenAnalytics.logCryptoClaimTransferedClicked()
launch {
claimHandler.handle(view = view, canBeClaimed = canBeClaimed, token = token)
}
}
override fun detach() {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
cryptoTokensSubscription?.cancel()
cryptoTokensSubscription = null
pnlDataSubscription?.cancel()
pnlDataSubscription = null
super.detach()
}
override fun onAddressClicked() {
mainScreenAnalytics.logMainScreenAddressClick()
val fullUsername = username?.fullUsername
val hasUserName = !fullUsername.isNullOrEmpty()
view?.showAddressCopied(
addressOrUsername = fullUsername ?: userPublicKey,
stringResId = if (hasUserName) {
R.string.wallet_username_copy_snackbar_text
} else {
R.string.wallet_address_copy_snackbar_text
}
)
}
}