From 5d0e8fadc5644f28b34c419d94a6a3195f196eff Mon Sep 17 00:00:00 2001 From: starry-shivam Date: Sun, 4 Feb 2024 21:45:38 +0530 Subject: [PATCH] Simplify widget updater & remove 'no deadline set' text Signed-off-by: starry-shivam --- .../starry/greenstash/widget/GoalWidget.kt | 150 ++++++++++-------- app/src/main/res/layout/goal_widget.xml | 11 -- 2 files changed, 85 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt b/app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt index 9d98600c..49782ebe 100644 --- a/app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt +++ b/app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt @@ -86,19 +86,13 @@ class GoalWidget : AppWidgetProvider() { } } - fun updateWidgetContents( - context: Context, appWidgetId: Int, goalItem: GoalWithTransactions - ) { + fun updateWidgetContents(context: Context, appWidgetId: Int, goalItem: GoalWithTransactions) { val preferenceUtil = PreferenceUtil(context) - val goalTextUtils = GoalTextUtils(preferenceUtil) val appWidgetManager = AppWidgetManager.getInstance(context) val views = RemoteViews(context.packageName, R.layout.goal_widget) - // Check if widget was manually refreshed. if (isManualRefresh) { - views.setViewVisibility(R.id.widgetUpdateButton, View.INVISIBLE) - views.setViewVisibility(R.id.widgetUpdateProgress, View.VISIBLE) - appWidgetManager.updateAppWidget(appWidgetId, views) + handleManualRefresh(views, appWidgetManager, appWidgetId) } // Set widget title. @@ -117,59 +111,23 @@ class GoalWidget : AppWidgetProvider() { ) views.setCharSequence(R.id.widgetDesc, "setText", widgetDesc) - // Calculate how much need to save per day and week. - val remainingAmount = (goalItem.goal.targetAmount - goalItem.getCurrentlySavedAmount()) - if (remainingAmount > 0f) { - if (goalItem.goal.deadline.isNotEmpty() && goalItem.goal.deadline.isNotBlank()) { - val calculatedDays = goalTextUtils.calcRemainingDays(goalItem.goal) - if (calculatedDays.remainingDays > 2) { - val amountDays = "${ - Utils.formatCurrency( - Utils.roundDecimal( - remainingAmount / calculatedDays.remainingDays - ), defCurrency - ) - }/${context.getString(R.string.goal_approx_saving_day)}" - views.setCharSequence(R.id.widgetAmountDay, "setText", amountDays) - views.setViewVisibility(R.id.widgetAmountDay, View.VISIBLE) - } - if (calculatedDays.remainingDays > 7) { - val amountWeeks = "${ - Utils.formatCurrency( - Utils.roundDecimal( - remainingAmount / (calculatedDays.remainingDays / 7) - ), defCurrency - ) - }/${ - context.getString( - R.string.goal_approx_saving_week - ) - }" - views.setCharSequence(R.id.widgetAmountWeek, "setText", amountWeeks) - views.setViewVisibility(R.id.widgetAmountWeek, View.VISIBLE) - } - views.setViewVisibility(R.id.widgetNoDeadlineSet, View.GONE) - } else { - views.setViewVisibility(R.id.widgetNoDeadlineSet, View.VISIBLE) - } - } else { - views.setViewVisibility(R.id.amountDurationGroup, View.GONE) - views.setViewVisibility(R.id.widgetNoDeadlineSet, View.GONE) - views.setViewVisibility(R.id.widgetGoalAchieved, View.VISIBLE) - } + // Calculate and display savings per day and week if applicable. + handleSavingsPerDuration(context, views, goalItem, defCurrency, preferenceUtil) + + // Display appropriate views when the goal is achieved. + handleGoalAchieved(views, goalItem) // Calculate current progress percentage. - val progressPercent = - ((goalItem.getCurrentlySavedAmount() / goalItem.goal.targetAmount) * 100).toInt() - views.setProgressBar(R.id.widgetGoalProgress, 100, progressPercent, false) + handleProgress(views, goalItem) // Set refresh button click action. - val intent = Intent(context, GoalWidget::class.java) - intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE - intent.type = WIDGET_MANUAL_REFRESH - val ids = AppWidgetManager.getInstance(context) - .getAppWidgetIds(ComponentName(context, GoalWidget::class.java)) - intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids) + val intent = Intent(context, GoalWidget::class.java).apply { + action = AppWidgetManager.ACTION_APPWIDGET_UPDATE + type = WIDGET_MANUAL_REFRESH + val ids = AppWidgetManager.getInstance(context) + .getAppWidgetIds(ComponentName(context, GoalWidget::class.java)) + putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids) + } val pendingIntent = PendingIntent.getBroadcast( context, @@ -179,18 +137,80 @@ class GoalWidget : AppWidgetProvider() { ) views.setOnClickPendingIntent(R.id.widgetUpdateButton, pendingIntent) - // update widget contents. - if (isManualRefresh) { - Handler(Looper.getMainLooper()).postDelayed({ - views.setViewVisibility(R.id.widgetUpdateButton, View.VISIBLE) - views.setViewVisibility(R.id.widgetUpdateProgress, View.GONE) - appWidgetManager.updateAppWidget(appWidgetId, views) - }, 750) - } else { + // Update widget contents. + appWidgetManager.updateAppWidget(appWidgetId, views) + } + + private fun handleManualRefresh( + views: RemoteViews, + appWidgetManager: AppWidgetManager, + appWidgetId: Int + ) { + views.setViewVisibility(R.id.widgetUpdateButton, View.INVISIBLE) + views.setViewVisibility(R.id.widgetUpdateProgress, View.VISIBLE) + appWidgetManager.updateAppWidget(appWidgetId, views) + + // Delayed update to show the button again. + Handler(Looper.getMainLooper()).postDelayed({ + views.setViewVisibility(R.id.widgetUpdateButton, View.VISIBLE) + views.setViewVisibility(R.id.widgetUpdateProgress, View.GONE) appWidgetManager.updateAppWidget(appWidgetId, views) + }, 750) + } + + private fun handleSavingsPerDuration( + context: Context, + views: RemoteViews, + goalItem: GoalWithTransactions, + defCurrency: String, + preferenceUtil: PreferenceUtil + ) { + val remainingAmount = (goalItem.goal.targetAmount - goalItem.getCurrentlySavedAmount()) + + if (remainingAmount > 0f && goalItem.goal.deadline.isNotEmpty()) { + val calculatedDays = GoalTextUtils(preferenceUtil).calcRemainingDays(goalItem.goal) + + if (calculatedDays.remainingDays > 2) { + val amountDays = "${ + Utils.formatCurrency( + Utils.roundDecimal(remainingAmount / calculatedDays.remainingDays), + defCurrency + ) + }/${context.getString(R.string.goal_approx_saving_day)}" + views.setCharSequence(R.id.widgetAmountDay, "setText", amountDays) + views.setViewVisibility(R.id.widgetAmountDay, View.VISIBLE) + } + + if (calculatedDays.remainingDays > 7) { + val amountWeeks = "${ + Utils.formatCurrency( + Utils.roundDecimal(remainingAmount / (calculatedDays.remainingDays / 7)), + defCurrency + ) + }/${context.getString(R.string.goal_approx_saving_week)}" + views.setCharSequence(R.id.widgetAmountWeek, "setText", amountWeeks) + views.setViewVisibility(R.id.widgetAmountWeek, View.VISIBLE) + } + + views.setViewVisibility(R.id.amountDurationGroup, View.VISIBLE) + views.setViewVisibility(R.id.widgetGoalAchieved, View.GONE) } } + private fun handleGoalAchieved(views: RemoteViews, goalItem: GoalWithTransactions) { + if (goalItem.getCurrentlySavedAmount() >= goalItem.goal.targetAmount) { + views.setViewVisibility(R.id.amountDurationGroup, View.GONE) + views.setViewVisibility(R.id.widgetGoalAchieved, View.VISIBLE) + } + } + + private fun handleProgress(views: RemoteViews, goalItem: GoalWithTransactions) { + val progressPercent = + ((goalItem.getCurrentlySavedAmount() / goalItem.goal.targetAmount) * 100).toInt() + views.setProgressBar(R.id.widgetGoalProgress, 100, progressPercent, false) + } + + private fun initialiseVm(context: Context) { if (!this::viewModel.isInitialized) { println("viewmodel not initialised") diff --git a/app/src/main/res/layout/goal_widget.xml b/app/src/main/res/layout/goal_widget.xml index cf17e03e..a33764d4 100644 --- a/app/src/main/res/layout/goal_widget.xml +++ b/app/src/main/res/layout/goal_widget.xml @@ -113,7 +113,6 @@ tools:text="$153.45/day" tools:visibility="visible" /> - - -