Skip to content

Commit 0764bb0

Browse files
committed
refactor: update the current activity while the new one is not created
1 parent 809ce2f commit 0764bb0

File tree

1 file changed

+60
-51
lines changed

1 file changed

+60
-51
lines changed

app/src/main/java/to/bitkit/ui/screens/wallets/activity/BoostTransactionViewModel.kt

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -256,32 +256,53 @@ class BoostTransactionViewModel @Inject constructor(
256256
}
257257
}
258258

259+
/**RBF: Update the current activity with boost data -> when the new transaction is created, se it as boosting and
260+
* delete the old one
261+
* CPFP: Just update the current activity*/
259262
private suspend fun updateActivity(newTxId: Txid, isRBF: Boolean): Result<Unit> {
260263
Logger.debug("Updating activity for txId: $newTxId. isRBF:$isRBF", context = TAG)
261264

262-
return activityRepo.findActivityByPaymentId(
263-
paymentHashOrTxId = newTxId,
264-
type = ActivityFilter.ONCHAIN,
265-
txType = PaymentType.SENT
266-
).fold(
267-
onSuccess = { newActivity ->
268-
Logger.debug("Activity found: $newActivity", context = TAG)
269-
270-
val newOnChainActivity = newActivity as? Activity.Onchain
271-
?: return Result.failure(Exception("Activity is not onchain type"))
272-
273-
val updatedActivity = Activity.Onchain(
274-
v1 = newOnChainActivity.v1.copy(
275-
isBoosted = true,
276-
txId = newTxId,
277-
feeRate = _uiState.value.feeRate,
278-
fee = _uiState.value.totalFeeSats,
279-
updatedAt = nowTimestamp().toEpochMilli().toULong()
265+
val currentActivity = activity?.v1 ?: return Result.failure(Exception("Activity required"))
266+
267+
// For CPFP, just update the activity
268+
val updatedActivity = Activity.Onchain(
269+
v1 = currentActivity.copy(
270+
isBoosted = true,
271+
feeRate = _uiState.value.feeRate,
272+
fee = _uiState.value.totalFeeSats,
273+
updatedAt = nowTimestamp().toEpochMilli().toULong()
274+
)
275+
)
276+
277+
val updateResult = activityRepo.updateActivity(
278+
id = updatedActivity.v1.id,
279+
activity = updatedActivity
280+
)
281+
282+
return if (!isRBF) {
283+
updateResult
284+
} else {
285+
// For RBF, update new activity and delete old one
286+
activityRepo.findActivityByPaymentId(
287+
paymentHashOrTxId = newTxId,
288+
type = ActivityFilter.ONCHAIN,
289+
txType = PaymentType.SENT
290+
).fold(
291+
onSuccess = { newActivity ->
292+
Logger.debug("Activity found: $newActivity", context = TAG)
293+
294+
val newOnChainActivity = newActivity as? Activity.Onchain
295+
?: return Result.failure(Exception("Activity is not onchain type"))
296+
297+
val updatedActivity = Activity.Onchain(
298+
v1 = newOnChainActivity.v1.copy(
299+
isBoosted = true,
300+
feeRate = _uiState.value.feeRate,
301+
fee = _uiState.value.totalFeeSats,
302+
updatedAt = nowTimestamp().toEpochMilli().toULong()
303+
)
280304
)
281-
)
282305

283-
if (isRBF) {
284-
// For RBF, update new activity and delete old one
285306
activityRepo.replaceActivity(
286307
id = updatedActivity.v1.id,
287308
activityIdToDelete = activity?.v1?.id.orEmpty(),
@@ -297,38 +318,26 @@ class BoostTransactionViewModel @Inject constructor(
297318
)
298319
)
299320
}
300-
} else {
301-
// For CPFP, just update the activity
302-
activityRepo.updateActivity(
303-
id = updatedActivity.v1.id,
304-
activity = updatedActivity
305-
).onFailure {
306-
activityRepo.addActivityToPendingBoost(
307-
PendingBoostActivity(
308-
txId = newTxId,
309-
feeRate = _uiState.value.feeRate,
310-
fee = _uiState.value.totalFeeSats,
311-
updatedAt = nowTimestamp().toEpochMilli().toULong(),
312-
activityToDelete = null
313-
)
321+
},
322+
onFailure = { error ->
323+
Logger.error(
324+
"Activity $newTxId not found. Caching data to try again on next sync",
325+
e = error,
326+
context = TAG
327+
)
328+
activityRepo.addActivityToPendingBoost(
329+
PendingBoostActivity(
330+
txId = newTxId,
331+
feeRate = _uiState.value.feeRate,
332+
fee = _uiState.value.totalFeeSats,
333+
updatedAt = nowTimestamp().toEpochMilli().toULong(),
334+
activityToDelete = activity?.v1?.id.takeIf { isRBF }
314335
)
315-
}
316-
}
317-
},
318-
onFailure = { error ->
319-
Logger.error("Activity $newTxId not found. Caching data to try again on next sync", e = error, context = TAG)
320-
activityRepo.addActivityToPendingBoost(
321-
PendingBoostActivity(
322-
txId = newTxId,
323-
feeRate = _uiState.value.feeRate,
324-
fee = _uiState.value.totalFeeSats,
325-
updatedAt = nowTimestamp().toEpochMilli().toULong(),
326-
activityToDelete = activity?.v1?.id.takeIf { isRBF }
327336
)
328-
)
329-
Result.failure(error)
330-
}
331-
)
337+
Result.failure(error)
338+
}
339+
)
340+
}
332341
}
333342

334343
private fun handleError(message: String, error: Throwable? = null) {

0 commit comments

Comments
 (0)