Skip to content

Commit

Permalink
fix: Fix amount truncatiom logic in widgets & remove useless if-else …
Browse files Browse the repository at this point in the history
…block (#143)

---------
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam authored Jun 8, 2024
1 parent 05406a3 commit b468e31
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/starry/greenstash/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class MainViewModel @Inject constructor(
})
}.build()

val shortCuts = listOf(newGoalShortcut) + topGoals.map { goal ->
val shortcuts = listOf(newGoalShortcut) + topGoals.map { goal ->
val intent = Intent().apply {
action = Intent.ACTION_VIEW
data = Uri.parse("$LAUNCHER_SHORTCUT_SCHEME://goalId")
Expand All @@ -145,7 +145,7 @@ class MainViewModel @Inject constructor(
}.build()
}

withContext(Dispatchers.Main) { onComplete(shortCuts) }
withContext(Dispatchers.Main) { onComplete(shortcuts) }
}

}
Expand Down
23 changes: 6 additions & 17 deletions app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import dagger.hilt.EntryPoints


private const val WIDGET_MANUAL_REFRESH = "widget_manual_refresh"
private const val MAX_AMOUNT_DIGITS = 1000
private const val FULL_WIDGET_MIN_HEIGHT = 110

class GoalWidget : AppWidgetProvider() {
Expand Down Expand Up @@ -117,14 +116,10 @@ class GoalWidget : AppWidgetProvider() {
val datePattern = preferenceUtil.getString(PreferenceUtil.DATE_FORMAT_STR, "")!!

val savedAmount = goalItem.getCurrentlySavedAmount().let {
if (it > MAX_AMOUNT_DIGITS) {
"${NumberUtils.getCurrencySymbol(defCurrency)}${NumberUtils.prettyCount(it)}"
} else NumberUtils.formatCurrency(it, defCurrency)
"${NumberUtils.getCurrencySymbol(defCurrency)}${NumberUtils.prettyCount(it)}"
}
val targetAmount = goalItem.goal.targetAmount.let {
if (it > MAX_AMOUNT_DIGITS) {
"${NumberUtils.getCurrencySymbol(defCurrency)}${NumberUtils.prettyCount(it)}"
} else NumberUtils.formatCurrency(it, defCurrency)
"${NumberUtils.getCurrencySymbol(defCurrency)}${NumberUtils.prettyCount(it)}"
}
val widgetDesc = context.getString(R.string.goal_widget_desc)
.format("$savedAmount / $targetAmount")
Expand Down Expand Up @@ -177,12 +172,9 @@ class GoalWidget : AppWidgetProvider() {
// Calculate amount needed to save per day.
val calcPerDayAmount =
NumberUtils.roundDecimal(remainingAmount / calculatedDays.remainingDays)
// Build amount per day text by checking if the amount is greater than MAX_AMOUNT_DIGITS,
// if yes, then use prettyCount to format the amount.
// Build amount per day text using prettyCount to format the amount.
val amountPerDayText = calcPerDayAmount.let {
if (it > MAX_AMOUNT_DIGITS) {
"${NumberUtils.getCurrencySymbol(defCurrency)}${NumberUtils.prettyCount(it)}"
} else NumberUtils.formatCurrency(it, defCurrency)
"${NumberUtils.getCurrencySymbol(defCurrency)}${NumberUtils.prettyCount(it)}"
} + "/${context.getString(R.string.goal_approx_saving_day)}".let {
if (localeEnglish) it.dropLast(1) else it
}
Expand All @@ -195,12 +187,9 @@ class GoalWidget : AppWidgetProvider() {
// Calculate amount needed to save per week.
val calcPerWeekAmount =
NumberUtils.roundDecimal(remainingAmount / (calculatedDays.remainingDays / 7))
// Build amount per week text by checking if the amount is greater than MAX_AMOUNT_DIGITS,
// if yes, then use prettyCount to format the amount.
// Build amount per week text using prettyCount to format the amount.
val amountPerWeekText = calcPerWeekAmount.let {
if (it > MAX_AMOUNT_DIGITS) {
"${NumberUtils.getCurrencySymbol(defCurrency)}${NumberUtils.prettyCount(it)}"
} else NumberUtils.formatCurrency(it, defCurrency)
"${NumberUtils.getCurrencySymbol(defCurrency)}${NumberUtils.prettyCount(it)}"
} + "/${context.getString(R.string.goal_approx_saving_week)}".let {
if (localeEnglish) it.dropLast(1) else it
}
Expand Down

0 comments on commit b468e31

Please sign in to comment.