From 3667e7d425d58cbc36889a70b73feca2c00a7f80 Mon Sep 17 00:00:00 2001
From: Isaac <57831153+isaacnguyen0809@users.noreply.github.com>
Date: Tue, 16 Apr 2024 23:17:36 +0700
Subject: [PATCH] Fix issue 3116 (#3139)
* Fix error and improve UI widget
* Fix spelling errors in source code, fix storing balance in app widget
* Fix comment
* Fix comment
---
.../src/main/java/com/ivy/home/HomeHeader.kt | 4 +-
...ml => expense_shape_widget_background.xml} | 0
...xml => income_shape_widget_background.xml} | 0
.../com/ivy/legacy/utils/AmountFormatting.kt | 9 ----
.../res/xml/wallet_balance_widget_info.xml | 11 ++--
.../ivy/widget/balance/WalletBalanceWidget.kt | 50 +++++++++++--------
.../balance/WalletBalanceWidgetContent.kt | 48 +++++++-----------
.../res/xml/wallet_balance_widget_info.xml | 10 ----
8 files changed, 54 insertions(+), 78 deletions(-)
rename shared/ui/core/src/main/res/drawable/{expense_shape_widget_backgroun.xml => expense_shape_widget_background.xml} (100%)
rename shared/ui/core/src/main/res/drawable/{income_shape_widget_backgroud.xml => income_shape_widget_background.xml} (100%)
delete mode 100644 widget/balance/src/main/res/xml/wallet_balance_widget_info.xml
diff --git a/screen/home/src/main/java/com/ivy/home/HomeHeader.kt b/screen/home/src/main/java/com/ivy/home/HomeHeader.kt
index 91c8b8d569..36458054dc 100644
--- a/screen/home/src/main/java/com/ivy/home/HomeHeader.kt
+++ b/screen/home/src/main/java/com/ivy/home/HomeHeader.kt
@@ -63,7 +63,7 @@ import com.ivy.wallet.ui.theme.wallet.AmountCurrencyB1
import kotlin.math.absoluteValue
private const val OverflowLengthOfBalance = 7
-private const val OverflowLengthOfMontthRange = 12
+private const val OverflowLengthOfMonthRange = 12
@ExperimentalAnimationApi
@Composable
@@ -169,7 +169,7 @@ private fun HeaderStickyRow(
val overflow by remember(lengthOfCurrencyAndBalance, lengthOfMonthRange) {
derivedStateOf {
lengthOfCurrencyAndBalance >= OverflowLengthOfBalance &&
- lengthOfMonthRange >= OverflowLengthOfMontthRange
+ lengthOfMonthRange >= OverflowLengthOfMonthRange
}
}
diff --git a/shared/ui/core/src/main/res/drawable/expense_shape_widget_backgroun.xml b/shared/ui/core/src/main/res/drawable/expense_shape_widget_background.xml
similarity index 100%
rename from shared/ui/core/src/main/res/drawable/expense_shape_widget_backgroun.xml
rename to shared/ui/core/src/main/res/drawable/expense_shape_widget_background.xml
diff --git a/shared/ui/core/src/main/res/drawable/income_shape_widget_backgroud.xml b/shared/ui/core/src/main/res/drawable/income_shape_widget_background.xml
similarity index 100%
rename from shared/ui/core/src/main/res/drawable/income_shape_widget_backgroud.xml
rename to shared/ui/core/src/main/res/drawable/income_shape_widget_background.xml
diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/utils/AmountFormatting.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/utils/AmountFormatting.kt
index da7a343e35..1270a765aa 100644
--- a/temp/legacy-code/src/main/java/com/ivy/legacy/utils/AmountFormatting.kt
+++ b/temp/legacy-code/src/main/java/com/ivy/legacy/utils/AmountFormatting.kt
@@ -120,15 +120,6 @@ private fun formatShortenedNumber(
}
}
-fun String.toCalcBalanceAmount(): Double {
- val amountString = this.lowercase()
- return when {
- amountString.contains("k") -> amountString.replace("k", "").toDouble() * THOUSAND
- amountString.contains("m") -> amountString.replace("m", "").toDouble() * MILLION
- else -> this.toDouble()
- }
-}
-
fun hasSignificantDecimalPart(number: Double): Boolean {
// TODO: Review, might cause trouble when integrating crypto
val intPart = number.toInt()
diff --git a/widget/add-transaction/src/main/res/xml/wallet_balance_widget_info.xml b/widget/add-transaction/src/main/res/xml/wallet_balance_widget_info.xml
index 53322d8dfe..719c747293 100644
--- a/widget/add-transaction/src/main/res/xml/wallet_balance_widget_info.xml
+++ b/widget/add-transaction/src/main/res/xml/wallet_balance_widget_info.xml
@@ -2,9 +2,10 @@
\ No newline at end of file
+ android:updatePeriodMillis="0"
+ />
\ No newline at end of file
diff --git a/widget/balance/src/main/java/com/ivy/widget/balance/WalletBalanceWidget.kt b/widget/balance/src/main/java/com/ivy/widget/balance/WalletBalanceWidget.kt
index f4c6264095..058ccee121 100644
--- a/widget/balance/src/main/java/com/ivy/widget/balance/WalletBalanceWidget.kt
+++ b/widget/balance/src/main/java/com/ivy/widget/balance/WalletBalanceWidget.kt
@@ -8,6 +8,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
+import androidx.datastore.preferences.core.doublePreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.glance.GlanceId
import androidx.glance.appwidget.GlanceAppWidget
@@ -22,7 +23,6 @@ import com.ivy.base.model.TransactionType
import com.ivy.domain.AppStarter
import com.ivy.legacy.data.model.toCloseTimeRange
import com.ivy.legacy.utils.shortenAmount
-import com.ivy.legacy.utils.toCalcBalanceAmount
import com.ivy.wallet.domain.action.account.AccountsAct
import com.ivy.wallet.domain.action.settings.SettingsAct
import com.ivy.wallet.domain.action.wallet.CalcIncomeExpenseAct
@@ -32,20 +32,28 @@ import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import javax.inject.Inject
+import kotlin.math.abs
const val THOUSAND = 1000
+object PrefsKey {
+ const val APP_LOCKED = "appLocked"
+ const val BALANCE = "balance_v2"
+ const val CURRENCY = "currency"
+ const val INCOME = "income_v2"
+ const val EXPENSE = "expense_v2"
+}
+
class WalletBalanceWidget(
private val getAppStarter: () -> AppStarter,
) : GlanceAppWidget() {
@Composable
- fun formatBalance(balance: String): String {
+ fun formatBalance(balance: Double): String {
val formattedBalance = remember(balance) {
- val balanceDouble = balance.toCalcBalanceAmount()
- if (Math.abs(balanceDouble) < THOUSAND) {
- DecimalFormat("###,###.##").format(balanceDouble)
+ if (abs(balance) < THOUSAND) {
+ DecimalFormat("###,###.##").format(balance)
} else {
- shortenAmount(balanceDouble)
+ shortenAmount(balance)
}
}
return formattedBalance
@@ -54,18 +62,18 @@ class WalletBalanceWidget(
override suspend fun provideGlance(context: Context, id: GlanceId) {
provideContent {
val prefs = currentState()
- val appLocked = prefs[booleanPreferencesKey("appLocked")] ?: false
- val balance = prefs[stringPreferencesKey("balance")] ?: "0.00"
- val currency = prefs[stringPreferencesKey("currency")] ?: "USD"
- val income = prefs[stringPreferencesKey("income")] ?: "0.00"
- val expense = prefs[stringPreferencesKey("expense")] ?: "0.00"
+ val appLocked = prefs[booleanPreferencesKey(PrefsKey.APP_LOCKED)] ?: false
+ val balance = prefs[doublePreferencesKey(PrefsKey.BALANCE)] ?: 0.00
+ val currency = prefs[stringPreferencesKey(PrefsKey.CURRENCY)] ?: "USD"
+ val income = prefs[doublePreferencesKey(PrefsKey.INCOME)] ?: 0.00
+ val expense = prefs[doublePreferencesKey(PrefsKey.EXPENSE)] ?: 0.00
WalletBalanceWidgetContent(
appLocked = appLocked,
balance = formatBalance(balance),
currency = currency,
- income = income,
- expense = expense,
+ income = shortenAmount(income),
+ expense = shortenAmount(expense),
onIncomeClick = {
getAppStarter().addTransactionStart(TransactionType.INCOME)
},
@@ -156,14 +164,14 @@ class WalletBalanceWidgetReceiver : GlanceAppWidgetReceiver() {
it
) { pref ->
pref.toMutablePreferences().apply {
- this[booleanPreferencesKey("appLocked")] = appLocked
- this[stringPreferencesKey("balance")] =
- shortenAmount(balance.toDouble())
- this[stringPreferencesKey("currency")] = currency
- this[stringPreferencesKey("income")] =
- shortenAmount(incomeExpense.income.toDouble())
- this[stringPreferencesKey("expense")] =
- shortenAmount(incomeExpense.expense.toDouble())
+ this[booleanPreferencesKey(PrefsKey.APP_LOCKED)] = appLocked
+ this[doublePreferencesKey(PrefsKey.BALANCE)] =
+ balance.toDouble()
+ this[stringPreferencesKey(PrefsKey.CURRENCY)] = currency
+ this[doublePreferencesKey(PrefsKey.INCOME)] =
+ incomeExpense.income.toDouble()
+ this[doublePreferencesKey(PrefsKey.EXPENSE)] =
+ incomeExpense.expense.toDouble()
}
}
glanceAppWidget.update(context, it)
diff --git a/widget/balance/src/main/java/com/ivy/widget/balance/WalletBalanceWidgetContent.kt b/widget/balance/src/main/java/com/ivy/widget/balance/WalletBalanceWidgetContent.kt
index 1412a18c44..b6dcfba664 100644
--- a/widget/balance/src/main/java/com/ivy/widget/balance/WalletBalanceWidgetContent.kt
+++ b/widget/balance/src/main/java/com/ivy/widget/balance/WalletBalanceWidgetContent.kt
@@ -41,20 +41,24 @@ fun WalletBalanceWidgetContent(
onTransferClick: () -> Unit,
onWidgetClick: () -> Unit,
) {
+ val resources = LocalContext.current.resources
Box(
GlanceModifier
.background(ImageProvider(R.drawable.shape_widget_background))
- .clickable(onWidgetClick)
+ .clickable(onWidgetClick),
+ contentAlignment = Alignment.Center
) {
Column(
modifier = GlanceModifier.fillMaxSize(),
+ verticalAlignment = Alignment.CenterVertically,
+ horizontalAlignment = if (appLocked) Alignment.CenterHorizontally else Alignment.Start
) {
if (appLocked) {
Text(
- modifier = GlanceModifier.fillMaxSize(),
- text = "App locked",
+ modifier = GlanceModifier.padding(8.dp),
+ text = resources.getString(R.string.app_locked),
style = TextStyle(
- fontSize = 30.sp,
+ fontSize = 25.sp,
color = ColorProvider(Color.White),
textAlign = TextAlign.Center
)
@@ -90,11 +94,11 @@ fun RowScope.WidgetClickableItem(
@Composable
fun BalanceSection(
balance: String,
- currency: String
+ currency: String,
) {
Row(
verticalAlignment = Alignment.CenterVertically,
- modifier = GlanceModifier.padding(start = 14.dp, top = 14.dp),
+ modifier = GlanceModifier.padding(start = 12.dp, end = 12.dp, top = 12.dp),
) {
Text(
text = currency,
@@ -119,11 +123,11 @@ fun BalanceSection(
fun IncomeExpenseSection(
income: String,
expense: String,
- currency: String
+ currency: String,
) {
Row(
GlanceModifier.fillMaxWidth()
- .padding(start = 14.dp, end = 14.dp, top = 12.dp, bottom = 12.dp),
+ .padding(horizontal = 12.dp, vertical = 12.dp),
verticalAlignment = Alignment.CenterVertically
) {
val resources = LocalContext.current.resources
@@ -131,57 +135,39 @@ fun IncomeExpenseSection(
GlanceModifier
.padding(10.dp)
.defaultWeight()
- .background(ImageProvider(R.drawable.income_shape_widget_backgroud)),
+ .background(ImageProvider(R.drawable.income_shape_widget_background)),
verticalAlignment = Alignment.CenterVertically,
) {
Image(ImageProvider(R.drawable.ic_income_white), resources.getString((R.string.income)))
- Spacer(GlanceModifier.width(8.dp))
Text(
- text = income,
+ text = "$income $currency",
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Bold,
color = ColorProvider(Color.White)
)
)
- Spacer(GlanceModifier.width(4.dp))
- Text(
- text = currency,
- style = TextStyle(
- fontSize = 16.sp,
- color = ColorProvider(Color.White),
- )
- )
}
Spacer(GlanceModifier.width(8.dp))
Row(
GlanceModifier
.padding(10.dp)
.defaultWeight()
- .background(ImageProvider(R.drawable.expense_shape_widget_backgroun)),
+ .background(ImageProvider(R.drawable.expense_shape_widget_background)),
verticalAlignment = Alignment.CenterVertically,
) {
Image(
ImageProvider(R.drawable.ic_expense),
resources.getString(R.string.expense)
)
- Spacer(GlanceModifier.width(8.dp))
Text(
- text = expense,
+ text = "$expense $currency",
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight.Bold,
color = ColorProvider(Color.Black)
)
)
- Spacer(GlanceModifier.width(4.dp))
- Text(
- text = currency,
- style = TextStyle(
- fontSize = 16.sp,
- color = ColorProvider(Color.Black)
- )
- )
}
}
}
@@ -198,7 +184,7 @@ fun ButtonsSection(
R.drawable.ic_widget_transfer to R.string.transfer
)
Row(
- GlanceModifier.fillMaxWidth().padding(10.dp),
+ GlanceModifier.fillMaxWidth().padding(12.dp),
verticalAlignment = Alignment.CenterVertically
) {
buttons.forEach { (image, text) ->
diff --git a/widget/balance/src/main/res/xml/wallet_balance_widget_info.xml b/widget/balance/src/main/res/xml/wallet_balance_widget_info.xml
deleted file mode 100644
index 53322d8dfe..0000000000
--- a/widget/balance/src/main/res/xml/wallet_balance_widget_info.xml
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
\ No newline at end of file