Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Fix issue 3116 (#3139)
Browse files Browse the repository at this point in the history
* Fix error and improve UI widget

* Fix spelling errors in source code, fix storing balance in app widget

* Fix comment

* Fix comment
  • Loading branch information
isaacnguyen0809 authored Apr 16, 2024
1 parent a57144e commit 3667e7d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 78 deletions.
4 changes: 2 additions & 2 deletions screen/home/src/main/java/com/ivy/home/HomeHeader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -169,7 +169,7 @@ private fun HeaderStickyRow(
val overflow by remember(lengthOfCurrencyAndBalance, lengthOfMonthRange) {
derivedStateOf {
lengthOfCurrencyAndBalance >= OverflowLengthOfBalance &&
lengthOfMonthRange >= OverflowLengthOfMontthRange
lengthOfMonthRange >= OverflowLengthOfMonthRange
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:resizeMode="horizontal|vertical"
android:previewImage="@drawable/preview_widget_wallet_balance"
android:minResizeWidth="250dp"
android:minWidth="250dp"
android:minHeight="110dp"
android:minResizeHeight="110dp"
android:minResizeWidth="240dp"
android:minWidth="240dp"
android:minHeight="120dp"
android:minResizeHeight="120dp"
android:widgetCategory="home_screen"
/>
android:updatePeriodMillis="0"
/>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -54,18 +62,18 @@ class WalletBalanceWidget(
override suspend fun provideGlance(context: Context, id: GlanceId) {
provideContent {
val prefs = currentState<Preferences>()
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)
},
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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,
Expand All @@ -119,69 +123,51 @@ 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
Row(
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)
)
)
}
}
}
Expand All @@ -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) ->
Expand Down
10 changes: 0 additions & 10 deletions widget/balance/src/main/res/xml/wallet_balance_widget_info.xml

This file was deleted.

0 comments on commit 3667e7d

Please sign in to comment.