Skip to content

Commit 0c1da7b

Browse files
authored
Merge branch 'master' into fix/bitkit-not-responding
2 parents 2faf744 + 6be2092 commit 0c1da7b

7 files changed

Lines changed: 56 additions & 17 deletions

File tree

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ suspend fun getData(): Result<Data> = withContext(Dispatchers.IO) {
210210
- PREFER to use one-liners with `run {}` when applicable, e.g. `override fun someCall(value: String) = run { this.value = value }`
211211
- ALWAYS add imports instead of inline fully-qualified names
212212
- PREFER to place `@Suppress()` annotations at the narrowest possible scope
213+
- ALWAYS wrap suspend functions in `withContext(bgDispatcher)` if in domain layer, using ctor injected prop `@BgDispatcher private val bgDispatcher: CoroutineDispatcher`
213214

214215
### Architecture Guidelines
215216

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package to.bitkit.di
22

3+
import android.content.Context
34
import dagger.Module
45
import dagger.Provides
56
import dagger.hilt.InstallIn
7+
import dagger.hilt.android.qualifiers.ApplicationContext
68
import dagger.hilt.components.SingletonComponent
79
import org.lightningdevkit.ldknode.Network
810
import to.bitkit.env.Env
11+
import java.util.Locale
912
import kotlin.time.Clock
1013
import kotlin.time.ExperimentalTime
1114

@@ -14,13 +17,12 @@ import kotlin.time.ExperimentalTime
1417
object EnvModule {
1518

1619
@Provides
17-
fun provideNetwork(): Network {
18-
return Env.network
19-
}
20+
fun provideNetwork(): Network = Env.network
2021

2122
@OptIn(ExperimentalTime::class)
2223
@Provides
23-
fun provideClock(): Clock {
24-
return Clock.System
25-
}
24+
fun provideClock(): Clock = Clock.System
25+
26+
@Provides
27+
fun provideLocale(@ApplicationContext context: Context): Locale = context.resources.configuration.locales[0]
2628
}

app/src/main/java/to/bitkit/models/Currency.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,27 @@ data class ConvertedAmount(
8282
)
8383

8484
fun bitcoinDisplay(unit: BitcoinDisplayUnit): BitcoinDisplayComponents {
85-
val formattedValue = when (unit) {
86-
BitcoinDisplayUnit.MODERN -> sats.formatToModernDisplay(locale)
87-
BitcoinDisplayUnit.CLASSIC -> sats.formatToClassicDisplay(locale)
88-
}
85+
val formattedValue = sats.formatMoney(unit, locale)
8986
return BitcoinDisplayComponents(
9087
symbol = BITCOIN_SYMBOL,
9188
value = formattedValue,
9289
)
9390
}
9491
}
9592

93+
fun Long.formatMoney(
94+
unit: BitcoinDisplayUnit,
95+
locale: Locale = Locale.getDefault(),
96+
): String = when (unit) {
97+
BitcoinDisplayUnit.MODERN -> formatToModernDisplay(locale)
98+
BitcoinDisplayUnit.CLASSIC -> formatToClassicDisplay(locale)
99+
}
100+
101+
fun ULong.formatMoney(
102+
unit: BitcoinDisplayUnit,
103+
locale: Locale = Locale.getDefault(),
104+
): String = toLong().formatMoney(unit, locale)
105+
96106
fun Long.formatToModernDisplay(locale: Locale = Locale.getDefault()): String {
97107
val sats = this
98108
val symbols = DecimalFormatSymbols(locale).apply {

app/src/main/java/to/bitkit/services/CoreService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ class ActivityService(
629629
)
630630
}
631631

632+
@Suppress("CyclomaticComplexMethod")
632633
private suspend fun processOnchainPayment(
633634
kind: PaymentKind.Onchain,
634635
payment: PaymentDetails,

app/src/main/java/to/bitkit/services/MigrationService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ class MigrationService @Inject constructor(
13941394
}
13951395
}
13961396

1397-
@Suppress("CyclomaticComplexMethod")
1397+
@Suppress("CyclomaticComplexMethod", "LongMethod")
13981398
private suspend fun updateOnchainActivityMetadata(
13991399
item: RNActivityItem,
14001400
onchain: OnchainActivity,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package to.bitkit.usecases
2+
3+
import kotlinx.coroutines.CoroutineDispatcher
4+
import kotlinx.coroutines.flow.first
5+
import kotlinx.coroutines.withContext
6+
import to.bitkit.data.SettingsStore
7+
import to.bitkit.di.BgDispatcher
8+
import to.bitkit.models.formatMoney
9+
import java.util.Locale
10+
import javax.inject.Inject
11+
import javax.inject.Singleton
12+
13+
@Singleton
14+
class FormatMoneyValue @Inject constructor(
15+
@BgDispatcher private val bgDispatcher: CoroutineDispatcher,
16+
private val settingsStore: SettingsStore,
17+
private val locale: Locale,
18+
) {
19+
suspend operator fun invoke(sats: ULong): String = withContext(bgDispatcher) {
20+
val unit = settingsStore.data.first().displayUnit
21+
sats.formatMoney(unit, locale)
22+
}
23+
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import to.bitkit.ui.shared.toast.ToastEventBus
108108
import to.bitkit.ui.shared.toast.ToastQueueManager
109109
import to.bitkit.ui.sheets.SendRoute
110110
import to.bitkit.ui.theme.TRANSITION_SCREEN_MS
111+
import to.bitkit.usecases.FormatMoneyValue
111112
import to.bitkit.utils.Bip21Utils
112113
import to.bitkit.utils.Logger
113114
import to.bitkit.utils.NetworkValidationHelper
@@ -153,6 +154,7 @@ class AppViewModel @Inject constructor(
153154
private val notificationsSheet: NotificationsTimedSheet,
154155
private val quickPaySheet: QuickPayTimedSheet,
155156
private val highBalanceSheet: HighBalanceTimedSheet,
157+
private val formatMoneyValue: FormatMoneyValue,
156158
) : ViewModel() {
157159
val healthState = healthRepo.healthState
158160

@@ -748,7 +750,7 @@ class AppViewModel @Inject constructor(
748750
showAddressValidationError(
749751
titleRes = R.string.other__pay_insufficient_spending,
750752
descriptionRes = R.string.other__pay_insufficient_spending_amount_description,
751-
descriptionArgs = mapOf("amount" to shortfall.toString()),
753+
descriptionArgs = mapOf("amount" to formatMoneyValue(shortfall)),
752754
testTag = "InsufficientSpendingToast",
753755
)
754756
return
@@ -758,7 +760,7 @@ class AppViewModel @Inject constructor(
758760
_sendUiState.update { it.copy(isAddressInputValid = true) }
759761
}
760762

761-
private fun validateOnChainAddress(invoice: OnChainInvoice) {
763+
private suspend fun validateOnChainAddress(invoice: OnChainInvoice) {
762764
val validatedAddress = runCatching { validateBitcoinAddress(invoice.address) }
763765
.getOrElse {
764766
showAddressValidationError(
@@ -794,7 +796,7 @@ class AppViewModel @Inject constructor(
794796
showAddressValidationError(
795797
titleRes = R.string.other__pay_insufficient_savings,
796798
descriptionRes = R.string.other__pay_insufficient_savings_amount_description,
797-
descriptionArgs = mapOf("amount" to shortfall.toString()),
799+
descriptionArgs = mapOf("amount" to formatMoneyValue(shortfall)),
798800
testTag = "InsufficientSavingsToast",
799801
)
800802
return
@@ -915,7 +917,7 @@ class AppViewModel @Inject constructor(
915917
type = Toast.ToastType.ERROR,
916918
title = context.getString(R.string.wallet__lnurl_pay__error_min__title),
917919
description = context.getString(R.string.wallet__lnurl_pay__error_min__description)
918-
.replace("{amount}", minSendable.toString()),
920+
.replace("{amount}", formatMoneyValue(minSendable)),
919921
testTag = "LnurlPayAmountTooLowToast",
920922
)
921923
return
@@ -1125,7 +1127,7 @@ class AppViewModel @Inject constructor(
11251127
type = Toast.ToastType.ERROR,
11261128
title = context.getString(R.string.other__pay_insufficient_savings),
11271129
description = context.getString(R.string.other__pay_insufficient_savings_amount_description)
1128-
.replace("{amount}", shortfall.toString()),
1130+
.replace("{amount}", formatMoneyValue(shortfall)),
11291131
testTag = "InsufficientSavingsToast",
11301132
)
11311133
return
@@ -1167,7 +1169,7 @@ class AppViewModel @Inject constructor(
11671169
type = Toast.ToastType.ERROR,
11681170
title = context.getString(R.string.other__pay_insufficient_spending),
11691171
description = context.getString(R.string.other__pay_insufficient_spending_amount_description)
1170-
.replace("{amount}", shortfall.toString()),
1172+
.replace("{amount}", formatMoneyValue(shortfall)),
11711173
testTag = "InsufficientSpendingToast",
11721174
)
11731175
return

0 commit comments

Comments
 (0)