-
Notifications
You must be signed in to change notification settings - Fork 18
/
SellTransactionDetailsPresenter.kt
258 lines (244 loc) · 11.4 KB
/
SellTransactionDetailsPresenter.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
package org.p2p.wallet.moonpay.ui.transaction
import android.content.res.Resources
import org.threeten.bp.ZonedDateTime
import org.threeten.bp.format.DateTimeFormatter
import timber.log.Timber
import java.util.Locale
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.p2p.core.utils.Constants
import org.p2p.core.utils.getHtmlString
import org.p2p.core.utils.removeLinksUnderline
import org.p2p.wallet.R
import org.p2p.wallet.common.date.isSameDayAs
import org.p2p.wallet.common.date.toZonedDateTime
import org.p2p.wallet.common.mvp.BasePresenter
import org.p2p.wallet.history.analytics.HistoryAnalytics
import org.p2p.wallet.history.interactor.HistoryInteractor
import org.p2p.wallet.moonpay.model.SellTransaction
import org.p2p.wallet.moonpay.repository.sell.MoonpaySellCancelResult
import org.p2p.wallet.moonpay.serversideapi.response.SellTransactionStatus
import org.p2p.wallet.sell.interactor.HistoryItemMapper
import org.p2p.wallet.sell.interactor.SellInteractor
import org.p2p.wallet.sell.ui.lock.SellTransactionViewDetails
import org.p2p.wallet.user.interactor.UserTokensInteractor
import org.p2p.wallet.utils.CUT_ADDRESS_SYMBOLS_COUNT
import org.p2p.wallet.utils.cutMiddle
import org.p2p.wallet.utils.emptyString
import org.p2p.wallet.utils.unsafeLazy
private const val DATE_FORMAT = "MMMM dd, yyyy"
private const val TIME_FORMAT = "HH:mm"
class SellTransactionDetailsPresenter(
private val sellInteractor: SellInteractor,
private val userInteractor: UserTokensInteractor,
private val historyInteractor: HistoryInteractor,
private val mapper: HistoryItemMapper,
private val resources: Resources,
private val historyAnalytics: HistoryAnalytics
) : BasePresenter<SellTransactionDetailsContract.View>(),
SellTransactionDetailsContract.Presenter {
private val dateFormat by unsafeLazy { DateTimeFormatter.ofPattern(DATE_FORMAT, Locale.US) }
private val timeFormat by unsafeLazy { DateTimeFormatter.ofPattern(TIME_FORMAT, Locale.US) }
private var currentTransaction: SellTransactionViewDetails? = null
override fun load(transactionId: String) {
launch {
try {
currentTransaction = historyInteractor.findTransactionById(transactionId)
?.takeIf { it is SellTransaction }
?.let { mapper.toSellDetailsModel(it as SellTransaction) }
val transaction = currentTransaction ?: return@launch
val viewState = when (transaction.status) {
SellTransactionStatus.WAITING_FOR_DEPOSIT -> buildWaitingForDepositViewState(transaction)
SellTransactionStatus.PENDING -> buildPendingViewState(transaction)
SellTransactionStatus.COMPLETED -> buildCompletedViewState(transaction)
SellTransactionStatus.FAILED -> buildFailedViewState(transaction)
}
historyAnalytics.logSellTransactionClicked(transaction)
view?.renderViewState(viewState)
} catch (e: Throwable) {
Timber.e(e, "Error on loading moonpay transaction details: $e")
view?.showErrorMessage(e)
}
}
}
private fun buildWaitingForDepositViewState(
transaction: SellTransactionViewDetails
): SellTransactionDetailsViewState {
val titleBlock = SellTransactionDetailsViewState.TitleBlock(
title = resources.getString(
R.string.sell_details_waiting_deposit_title,
transaction.formattedSolAmount
),
updatedAt = transaction.updatedAtTitle(),
boldAmount = resources.getString(
R.string.sell_details_token_amount,
transaction.formattedSolAmount,
Constants.SOL_SYMBOL
),
labelAmount = null,
)
val bodyBlock = SellTransactionDetailsViewState.BodyBlock.rain(
bodyText = resources.getString(R.string.sell_details_waiting_deposit_body),
)
val receiverBlock = SellTransactionDetailsViewState.ReceiverBlock(
receiverTitle = resources.getString(R.string.sell_details_send_to),
receiverValue = transaction.receiverAddress.cutMiddle(cutCount = CUT_ADDRESS_SYMBOLS_COUNT),
isCopyEnabled = true,
copyValueProvider = transaction::receiverAddress
)
val buttonsBlock = SellTransactionDetailsViewState.ButtonsBlock(
mainButtonTitle = resources.getString(R.string.sell_details_button_send),
mainButtonAction = ::onSendClicked,
additionalButtonTitle = resources.getString(R.string.sell_details_button_cancel),
additionalButtonAction = ::onCancelTransactionClicked
)
return SellTransactionDetailsViewState(
titleBlock = titleBlock,
messageBlock = bodyBlock,
receiverBlock = receiverBlock,
buttonsBlock = buttonsBlock
)
}
private fun buildPendingViewState(transaction: SellTransactionViewDetails): SellTransactionDetailsViewState {
val titleBlock = SellTransactionDetailsViewState.TitleBlock(
title = resources.getString(
R.string.sell_details_pending_title
),
updatedAt = transaction.updatedAtTitle(),
boldAmount = resources.getString(
R.string.sell_details_token_amount,
transaction.formattedSolAmount,
Constants.SOL_SYMBOL
),
labelAmount = null,
)
val bodyBlock = SellTransactionDetailsViewState.BodyBlock.silver(
bodyText = resources.getHtmlString(R.string.sell_details_pending_body).removeLinksUnderline(),
)
val receiverBlock = SellTransactionDetailsViewState.ReceiverBlock(
receiverTitle = resources.getString(R.string.sell_details_will_be_send_to),
receiverValue = resources.getString(R.string.sell_details_receiver_moonpay_bank),
isCopyEnabled = false
)
val buttonsBlock = SellTransactionDetailsViewState.ButtonsBlock(
mainButtonTitle = resources.getString(R.string.common_close),
mainButtonAction = { view?.close() }
)
return SellTransactionDetailsViewState(
titleBlock = titleBlock,
messageBlock = bodyBlock,
receiverBlock = receiverBlock,
buttonsBlock = buttonsBlock
)
}
private fun buildCompletedViewState(transaction: SellTransactionViewDetails): SellTransactionDetailsViewState {
val titleBlock = SellTransactionDetailsViewState.TitleBlock(
title = resources.getString(
R.string.sell_details_completed_title
),
updatedAt = transaction.updatedAtTitle(),
boldAmount = resources.getString(
R.string.sell_details_fiat_amount,
transaction.formattedFiatAmount,
transaction.fiatUiName
),
labelAmount = resources.getString(
R.string.sell_details_token_amount,
transaction.formattedSolAmount,
Constants.SOL_SYMBOL
)
)
val bodyBlock = SellTransactionDetailsViewState.BodyBlock.silver(
bodyText = resources.getHtmlString(R.string.sell_details_completed_body).removeLinksUnderline(),
)
val receiverBlock = SellTransactionDetailsViewState.ReceiverBlock(
receiverTitle = resources.getString(R.string.sell_details_sent_to),
receiverValue = resources.getString(R.string.sell_details_receiver_moonpay_bank),
isCopyEnabled = false
)
val buttonsBlock = SellTransactionDetailsViewState.ButtonsBlock(
mainButtonTitle = resources.getString(R.string.common_close),
mainButtonAction = { view?.close() },
additionalButtonTitle = resources.getString(R.string.sell_details_button_remove),
additionalButtonAction = ::onRemoveFromHistoryClicked
)
return SellTransactionDetailsViewState(
titleBlock = titleBlock,
messageBlock = bodyBlock,
receiverBlock = receiverBlock,
buttonsBlock = buttonsBlock
)
}
private fun buildFailedViewState(transaction: SellTransactionViewDetails): SellTransactionDetailsViewState {
val titleBlock = SellTransactionDetailsViewState.TitleBlock(
title = resources.getString(
R.string.sell_details_failed_title,
transaction.formattedSolAmount
),
updatedAt = transaction.updatedAtTitle(),
boldAmount = resources.getString(
R.string.sell_details_token_amount,
transaction.formattedSolAmount,
Constants.SOL_SYMBOL
),
labelAmount = null,
)
val bodyBlock = SellTransactionDetailsViewState.BodyBlock.rose(
bodyText = resources.getString(R.string.sell_details_failed_body)
)
val buttonsBlock = SellTransactionDetailsViewState.ButtonsBlock(
mainButtonTitle = resources.getString(R.string.sell_details_button_delete),
mainButtonAction = ::onRemoveFromHistoryClicked
)
return SellTransactionDetailsViewState(
titleBlock = titleBlock,
messageBlock = bodyBlock,
receiverBlock = null,
buttonsBlock = buttonsBlock
)
}
override fun onCancelTransactionClicked() {
launch {
val transaction = currentTransaction ?: return@launch
when (val result = sellInteractor.cancelTransaction(transaction.transactionId)) {
is MoonpaySellCancelResult.CancelSuccess -> {
view?.showUiKitSnackBar(messageResId = R.string.sell_details_cancel_success)
// I put a 1 second delay here because Snackbar doesn't have any change to appear
// the view closes too fast
delay(1000)
view?.close()
}
is MoonpaySellCancelResult.CancelFailed -> {
Timber.e(result.cause, "Failed to cancel transaction")
view?.showUiKitSnackBar(messageResId = R.string.sell_details_cancel_failed)
}
}
}
}
override fun onSendClicked() {
launch {
val solToken = userInteractor.getUserSolToken() ?: return@launch
val transaction = currentTransaction ?: return@launch
view?.navigateToSendScreen(
tokenToSend = solToken,
sendAmount = transaction.formattedSolAmount.toBigDecimal(),
receiverAddress = transaction.receiverAddress
)
}
}
override fun onRemoveFromHistoryClicked() {
val transaction = currentTransaction ?: return
sellInteractor.hideTransactionFromHistory(transaction.transactionId)
view?.close()
}
private fun SellTransactionViewDetails.updatedAtTitle(): String {
val time = updatedAt?.toZonedDateTime() ?: return emptyString()
val now = ZonedDateTime.now()
val isToday = time.isSameDayAs(now)
// Today / August 21, 2022
val datePart = if (isToday) resources.getString(R.string.common_today) else dateFormat.format(time)
// 15:28
val timePart = timeFormat.format(time)
return resources.getString(R.string.transaction_date_format, datePart, timePart)
}
}