Skip to content

Commit ea745d9

Browse files
committed
chore: log context in appVM
1 parent ca96757 commit ea745d9

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ import to.bitkit.utils.Logger
8787
import java.math.BigDecimal
8888
import javax.inject.Inject
8989

90-
private const val SEND_AMOUNT_WARNING_THRESHOLD = 100.0
91-
9290
@HiltViewModel
9391
class AppViewModel @Inject constructor(
9492
@ApplicationContext private val context: Context,
@@ -251,7 +249,7 @@ class AppViewModel @Inject constructor(
251249
is Event.PaymentForwarded -> Unit
252250
}
253251
} catch (e: Exception) {
254-
Logger.error("LDK event handler error", e)
252+
Logger.error("LDK event handler error", e, context = TAG)
255253
}
256254
}
257255
}
@@ -272,7 +270,7 @@ class AppViewModel @Inject constructor(
272270
try {
273271
isGeoBlocked = coreService.checkGeoStatus()
274272
} catch (e: Throwable) {
275-
Logger.error("Failed to check geo status: ${e.message}", context = "GeoCheck")
273+
Logger.error("Failed to check geo status: ${e.message}", e, context = TAG)
276274
}
277275
}
278276
}
@@ -501,8 +499,8 @@ class AppViewModel @Inject constructor(
501499
resetQuickPayData()
502500

503501
val scan = runCatching { decode(result) }
504-
.onFailure { Logger.error("Failed to decode scan result: '$result'", it) }
505-
.onSuccess { Logger.info("Handling scan data: $it") }
502+
.onFailure { Logger.error("Failed to decode scan result: '$result'", it, context = TAG) }
503+
.onSuccess { Logger.info("Handling scan data: $it", context = TAG) }
506504
.getOrNull()
507505

508506
when (scan) {
@@ -514,7 +512,7 @@ class AppViewModel @Inject constructor(
514512
is Scanner.LnurlChannel -> onScanLnurlChannel(scan.data)
515513
is Scanner.NodeId -> onScanNodeId(scan)
516514
else -> {
517-
Logger.warn("Unhandled scan data: $scan")
515+
Logger.warn("Unhandled scan data: $scan", context = TAG)
518516
toast(
519517
type = Toast.ToastType.WARNING,
520518
title = context.getString(R.string.other__scan_err_decoding),
@@ -540,7 +538,7 @@ class AppViewModel @Inject constructor(
540538
}
541539
val isLnInvoiceWithAmount = lnInvoice?.amountSatoshis?.takeIf { it > 0uL } != null
542540
if (isLnInvoiceWithAmount) {
543-
Logger.info("Found amount in unified invoice, checking QuickPay conditions")
541+
Logger.info("Found amount in unified invoice, checking QuickPay conditions", context = TAG)
544542

545543
val quickPayHandled = handleQuickPayIfApplicable(
546544
amountSats = lnInvoice.amountSatoshis,
@@ -556,7 +554,7 @@ class AppViewModel @Inject constructor(
556554
}
557555
return
558556
}
559-
Logger.info("No amount found in invoice, proceeding to enter amount manually")
557+
Logger.info("No amount found in invoice, proceeding to enter amount manually", context = TAG)
560558
resetAmountInput()
561559

562560
if (isMainScanner) {
@@ -597,7 +595,7 @@ class AppViewModel @Inject constructor(
597595
}
598596

599597
if (invoice.amountSatoshis > 0uL) {
600-
Logger.info("Found amount in invoice, proceeding with payment")
598+
Logger.info("Found amount in invoice, proceeding with payment", context = TAG)
601599

602600
if (isMainScanner) {
603601
showSheet(Sheet.Send(SendRoute.Confirm))
@@ -606,7 +604,7 @@ class AppViewModel @Inject constructor(
606604
}
607605
return
608606
}
609-
Logger.info("No amount found in invoice, proceeding to enter amount manually")
607+
Logger.info("No amount found in invoice, proceeding to enter amount manually", context = TAG)
610608
resetAmountInput()
611609

612610
if (isMainScanner) {
@@ -617,7 +615,7 @@ class AppViewModel @Inject constructor(
617615
}
618616

619617
private suspend fun onScanLnurlPay(data: LnurlPayData) {
620-
Logger.debug("LNURL: $data")
618+
Logger.debug("LNURL: $data", context = TAG)
621619

622620
val minSendable = data.minSendableSat()
623621
val maxSendable = data.maxSendableSat()
@@ -641,7 +639,7 @@ class AppViewModel @Inject constructor(
641639

642640
val hasAmount = minSendable == maxSendable && minSendable > 0u
643641
if (hasAmount) {
644-
Logger.info("Found amount $$minSendable in lnurlPay, proceeding with payment")
642+
Logger.info("Found amount $$minSendable in lnurlPay, proceeding with payment", context = TAG)
645643

646644
val quickPayHandled = handleQuickPayIfApplicable(amountSats = minSendable, lnurlPay = data)
647645
if (quickPayHandled) return
@@ -654,7 +652,7 @@ class AppViewModel @Inject constructor(
654652
return
655653
}
656654

657-
Logger.info("No amount found in lnurlPay, proceeding to enter amount manually")
655+
Logger.info("No amount found in lnurlPay, proceeding to enter amount manually", context = TAG)
658656
if (isMainScanner) {
659657
showSheet(Sheet.Send(SendRoute.Amount))
660658
} else {
@@ -663,7 +661,7 @@ class AppViewModel @Inject constructor(
663661
}
664662

665663
private fun onScanLnurlWithdraw(data: LnurlWithdrawData) {
666-
Logger.debug("LNURL: $data")
664+
Logger.debug("LNURL: $data", context = TAG)
667665

668666
val minWithdrawable = data.minWithdrawableSat()
669667
val maxWithdrawable = data.maxWithdrawableSat()
@@ -698,7 +696,7 @@ class AppViewModel @Inject constructor(
698696
}
699697

700698
private fun onScanLnurlAuth(data: LnurlAuthData, lnurl: String) {
701-
Logger.debug("LNURL: $data")
699+
Logger.debug("LNURL: $data", context = TAG)
702700

703701
val domain = runCatching { data.uri.toUri().host }.getOrDefault(data.uri).orEmpty().trim()
704702

@@ -736,7 +734,7 @@ class AppViewModel @Inject constructor(
736734
}
737735

738736
private fun onScanLnurlChannel(data: LnurlChannelData) {
739-
Logger.debug("LNURL: $data")
737+
Logger.debug("LNURL: $data", context = TAG)
740738
hideSheet() // hide scan sheet if opened
741739
mainScreenEffect(
742740
MainScreenEffect.Navigate(
@@ -777,7 +775,7 @@ class AppViewModel @Inject constructor(
777775
?: return false
778776

779777
if (amountSats <= quickPayAmountSats) {
780-
Logger.info("Using QuickPay: $amountSats sats <= $quickPayAmountSats sats threshold")
778+
Logger.info("Using QuickPay: $amountSats sats <= $quickPayAmountSats sats threshold", context = TAG)
781779

782780
val quickPayData: QuickPayData = when {
783781
lnurlPay != null -> {
@@ -792,7 +790,7 @@ class AppViewModel @Inject constructor(
792790

793791
_quickPayData.update { quickPayData }
794792

795-
Logger.debug("QuickPayData: $quickPayData")
793+
Logger.debug("QuickPayData: $quickPayData", context = TAG)
796794

797795
if (isMainScanner) {
798796
showSheet(Sheet.Send(SendRoute.QuickPay))
@@ -815,7 +813,7 @@ class AppViewModel @Inject constructor(
815813
}
816814

817815
private fun onSwipeToPay() {
818-
Logger.debug("Swipe to pay event, checking send confirmation conditions")
816+
Logger.debug("Swipe to pay event, checking send confirmation conditions", context = TAG)
819817
viewModelScope.launch {
820818
val amount = _sendUiState.value.amount
821819

@@ -908,7 +906,7 @@ class AppViewModel @Inject constructor(
908906
// TODO validate early, validate network & address types, showing detailed errors
909907
val validatedAddress = runCatching { validateBitcoinAddress(address) }
910908
.getOrElse { e ->
911-
Logger.error("Invalid bitcoin send address: '$address'", e)
909+
Logger.error("Invalid bitcoin send address: '$address'", e, context = TAG)
912910
toast(Exception("Invalid bitcoin send address"))
913911
hideSheet()
914912
return
@@ -923,7 +921,7 @@ class AppViewModel @Inject constructor(
923921
txType = PaymentType.SENT,
924922
tags = tags
925923
)
926-
Logger.info("Onchain send result txid: $txId")
924+
Logger.info("Onchain send result txid: $txId", context = TAG)
927925
setSendEffect(
928926
SendEffect.PaymentSuccess(
929927
NewTransactionSheetDetails(
@@ -934,7 +932,7 @@ class AppViewModel @Inject constructor(
934932
)
935933
)
936934
}.onFailure { e ->
937-
Logger.error(msg = "Error sending onchain payment", e = e)
935+
Logger.error(msg = "Error sending onchain payment", e = e, context = TAG)
938936
toast(
939937
type = Toast.ToastType.ERROR,
940938
title = "Error Sending",
@@ -952,7 +950,7 @@ class AppViewModel @Inject constructor(
952950
val paymentAmount = decodedInvoice.amountSatoshis.takeIf { it > 0uL } ?: amount
953951

954952
sendLightning(bolt11, paymentAmount).onSuccess { paymentHash ->
955-
Logger.info("Lightning send result payment hash: $paymentHash")
953+
Logger.info("Lightning send result payment hash: $paymentHash", context = TAG)
956954
val tags = _sendUiState.value.selectedTags
957955
activityRepo.addTagsToTransaction(
958956
paymentHashOrTxId = paymentHash,
@@ -962,7 +960,7 @@ class AppViewModel @Inject constructor(
962960
)
963961
setSendEffect(SendEffect.PaymentSuccess())
964962
}.onFailure { e ->
965-
Logger.error("Error sending lightning payment", e)
963+
Logger.error("Error sending lightning payment", e, context = TAG)
966964
toast(e)
967965
hideSheet()
968966
}
@@ -1027,7 +1025,7 @@ class AppViewModel @Inject constructor(
10271025
val activity = coreService.activity.get(filter = filter, txType = paymentType, limit = 1u).firstOrNull()
10281026

10291027
if (activity == null) {
1030-
Logger.error(msg = "Activity not found")
1028+
Logger.error(msg = "Activity not found", context = TAG)
10311029
return@launch
10321030
}
10331031

@@ -1205,7 +1203,7 @@ class AppViewModel @Inject constructor(
12051203

12061204
fun showNewTransactionSheet(details: NewTransactionSheetDetails) {
12071205
if (!isNewTransactionSheetEnabled) {
1208-
Logger.debug("NewTransactionSheet display blocked by isNewTransactionSheetEnabled=false")
1206+
Logger.debug("NewTransactionSheet display blocked by isNewTransactionSheetEnabled=false", context = TAG)
12091207
return
12101208
}
12111209

@@ -1356,7 +1354,7 @@ class AppViewModel @Inject constructor(
13561354
}
13571355

13581356
private fun onConfirmPay() {
1359-
Logger.debug("Payment checks confirmed, proceeding…")
1357+
Logger.debug("Payment checks confirmed, proceeding…", context = TAG)
13601358
viewModelScope.launch {
13611359
_sendUiState.update { it.copy(shouldConfirmPay = false) }
13621360
proceedWithPayment()
@@ -1365,10 +1363,10 @@ class AppViewModel @Inject constructor(
13651363

13661364
companion object {
13671365
private const val TAG = "AppViewModel"
1366+
private const val SEND_AMOUNT_WARNING_THRESHOLD = 100.0
13681367
}
13691368
}
13701369

1371-
13721370
// region send contract
13731371
data class SendUiState(
13741372
val address: String = "",

0 commit comments

Comments
 (0)