@@ -37,6 +37,7 @@ import kotlinx.coroutines.flow.onEach
37
37
import kotlinx.coroutines.flow.stateIn
38
38
import kotlinx.coroutines.launch
39
39
import timber.log.Timber
40
+ import java.util.Locale
40
41
import java.util.concurrent.TimeUnit
41
42
import javax.inject.Inject
42
43
@@ -45,7 +46,7 @@ data class BalanceDisplay(
45
46
val formattedValue : String = " " ,
46
47
val currency : Currency ? = null ,
47
48
48
- )
49
+ )
49
50
50
51
open class BalanceController @Inject constructor(
51
52
exchange : Exchange ,
@@ -73,10 +74,11 @@ open class BalanceController @Inject constructor(
73
74
prefs.observeOrDefault(
74
75
PrefsString .KEY_BALANCE_CURRENCY_SELECTED ,
75
76
getDefaultCurrency()?.name.orEmpty()
76
- )
77
- .mapNotNull { CurrencyCode .tryValueOf(it) }
78
- .map { getCurrencyFromCode(it) }
79
- .stateIn(scope, SharingStarted .Eagerly , getCurrencyFromCode(getDefaultCurrency()))
77
+ ).mapNotNull { CurrencyCode .tryValueOf(it) }
78
+ .map { getCurrencyFromCode(it) }
79
+ .distinctUntilChanged()
80
+ .onEach { Timber .d(" currency for balance is now ${it?.code} " ) }
81
+ .stateIn(scope, SharingStarted .Eagerly , getCurrencyFromCode(getDefaultCurrency()))
80
82
81
83
private val _balanceDisplay = MutableStateFlow <BalanceDisplay ?>(null )
82
84
@@ -115,7 +117,8 @@ open class BalanceController @Inject constructor(
115
117
refreshBalance(balance, rate.fx)
116
118
}.distinctUntilChanged().onEach { (marketValue, amountText) ->
117
119
val display = _balanceDisplay .value ? : BalanceDisplay ()
118
- _balanceDisplay .value = display.copy(marketValue = marketValue, formattedValue = amountText)
120
+ _balanceDisplay .value =
121
+ display.copy(marketValue = marketValue, formattedValue = amountText)
119
122
}.launchIn(scope)
120
123
}
121
124
@@ -130,13 +133,14 @@ open class BalanceController @Inject constructor(
130
133
Timber .d(" FetchBalance - Not authenticated" )
131
134
return Completable .complete()
132
135
}
133
- val owner = SessionManager .getKeyPair() ? : return Completable .error(IllegalStateException (" Missing Owner" ))
136
+ val owner = SessionManager .getKeyPair()
137
+ ? : return Completable .error(IllegalStateException (" Missing Owner" ))
134
138
135
139
fun getTokenAccountInfos (): Completable {
136
140
return accountRepository.getTokenAccountInfos(owner)
137
141
.flatMapCompletable { infos ->
138
- val organizer = SessionManager .getOrganizer() ? :
139
- return @flatMapCompletable Completable .error(IllegalStateException (" Missing Organizer" ))
142
+ val organizer = SessionManager .getOrganizer()
143
+ ? : return @flatMapCompletable Completable .error(IllegalStateException (" Missing Organizer" ))
140
144
141
145
scope.launch { organizer.setAccountInfo(infos) }
142
146
balanceRepository.setBalance(organizer.availableBalance.toKinValueDouble())
@@ -151,7 +155,10 @@ open class BalanceController @Inject constructor(
151
155
}
152
156
.onErrorResumeNext {
153
157
Timber .i(" Error: ${it.javaClass.simpleName} ${it.cause} " )
154
- val organizer = SessionManager .getOrganizer() ? : return @onErrorResumeNext Completable .error(IllegalStateException (" Missing Organizer" ))
158
+ val organizer =
159
+ SessionManager .getOrganizer() ? : return @onErrorResumeNext Completable .error(
160
+ IllegalStateException (" Missing Organizer" )
161
+ )
155
162
156
163
when (it) {
157
164
is AccountRepository .FetchAccountInfosException .MigrationRequiredException -> {
@@ -163,13 +170,15 @@ open class BalanceController @Inject constructor(
163
170
.ignoreElement()
164
171
.concatWith(getTokenAccountInfos())
165
172
}
173
+
166
174
is AccountRepository .FetchAccountInfosException .NotFoundException -> {
167
175
transactionRepository.createAccounts(
168
176
organizer = organizer
169
177
)
170
178
.ignoreElement()
171
179
.concatWith(getTokenAccountInfos())
172
180
}
181
+
173
182
else -> {
174
183
Completable .error(it)
175
184
}
@@ -178,7 +187,6 @@ open class BalanceController @Inject constructor(
178
187
}
179
188
180
189
181
-
182
190
suspend fun fetchBalanceSuspend () {
183
191
if (SessionManager .isAuthenticated() != true ) {
184
192
Timber .d(" FetchBalance - Not authenticated" )
@@ -188,15 +196,17 @@ open class BalanceController @Inject constructor(
188
196
189
197
try {
190
198
val accountInfo = accountRepository.getTokenAccountInfos(owner).blockingGet()
191
- val organizer = SessionManager .getOrganizer() ? : throw IllegalStateException (" Missing Organizer" )
199
+ val organizer =
200
+ SessionManager .getOrganizer() ? : throw IllegalStateException (" Missing Organizer" )
192
201
193
202
organizer.setAccountInfo(accountInfo)
194
203
balanceRepository.setBalance(organizer.availableBalance.toKinValueDouble())
195
204
transactionReceiver.receiveFromIncoming(organizer)
196
205
transactionRepository.swapIfNeeded(organizer)
197
206
} catch (ex: Exception ) {
198
207
Timber .i(" Error: ${ex.javaClass.simpleName} ${ex.cause} " )
199
- val organizer = SessionManager .getOrganizer() ? : throw IllegalStateException (" Missing Organizer" )
208
+ val organizer =
209
+ SessionManager .getOrganizer() ? : throw IllegalStateException (" Missing Organizer" )
200
210
201
211
when (ex) {
202
212
is AccountRepository .FetchAccountInfosException .MigrationRequiredException -> {
@@ -206,6 +216,7 @@ open class BalanceController @Inject constructor(
206
216
organizer = organizer
207
217
)
208
218
}
219
+
209
220
is AccountRepository .FetchAccountInfosException .NotFoundException -> {
210
221
transactionRepository.createAccounts(
211
222
organizer = organizer
@@ -219,7 +230,9 @@ open class BalanceController @Inject constructor(
219
230
val preferredCurrency = preferredCurrency.value
220
231
val fiatValue = FormatUtils .getFiatValue(balance, rate)
221
232
222
- val prefix = formatPrefix(preferredCurrency).takeIf { it != preferredCurrency?.code }.orEmpty()
233
+ val prefix =
234
+ formatPrefix(preferredCurrency).takeIf { it != preferredCurrency?.code }.orEmpty()
235
+
223
236
val amountText = StringBuilder ().apply {
224
237
append(prefix)
225
238
append(formatAmount(fiatValue, preferredCurrency))
@@ -230,6 +243,8 @@ open class BalanceController @Inject constructor(
230
243
}
231
244
}.toString()
232
245
246
+ Timber .d(" formatted balance is now $prefix $amountText in ${preferredCurrency?.code} " )
247
+
233
248
return fiatValue to amountText
234
249
}
235
250
@@ -238,13 +253,14 @@ open class BalanceController @Inject constructor(
238
253
return if (! isKin(selectedCurrency)) selectedCurrency.symbol else " "
239
254
}
240
255
241
- private fun isKin (selectedCurrency : Currency ): Boolean = selectedCurrency.code == CurrencyCode .KIN .name
256
+ private fun isKin (selectedCurrency : Currency ): Boolean =
257
+ selectedCurrency.code == CurrencyCode .KIN .name
242
258
243
259
private fun formatAmount (amount : Double , currency : Currency ? ): String {
244
260
return if (amount % 1 == 0.0 || currency?.code == CurrencyCode .KIN .name) {
245
- String .format(" %,.0f" , amount)
261
+ String .format(Locale .getDefault(), " %,.0f" , amount)
246
262
} else {
247
- String .format(" %,.2f" , amount)
263
+ String .format(Locale .getDefault(), " %,.2f" , amount)
248
264
}
249
265
}
250
266
}
0 commit comments