From 1399e4d9faec5d44b41f6f74ac9bd7bd1757d350 Mon Sep 17 00:00:00 2001 From: starry-shivam Date: Mon, 25 Dec 2023 13:50:09 +0530 Subject: [PATCH] Fix widget setup crash Signed-off-by: starry-shivam --- .../java/com/starry/greenstash/widget/GoalWidget.kt | 10 ++++++++-- .../com/starry/greenstash/widget/WidgetViewModel.kt | 7 ------- 2 files changed, 8 insertions(+), 9 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 9bc66427..728f24f9 100644 --- a/app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt +++ b/app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt @@ -38,6 +38,8 @@ import android.view.View import android.widget.RemoteViews import com.starry.greenstash.R import com.starry.greenstash.database.core.GoalWithTransactions +import com.starry.greenstash.utils.GoalTextUtils +import com.starry.greenstash.utils.PreferenceUtil import com.starry.greenstash.utils.Utils import dagger.hilt.EntryPoints @@ -87,6 +89,8 @@ class GoalWidget : AppWidgetProvider() { 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) @@ -101,7 +105,7 @@ class GoalWidget : AppWidgetProvider() { views.setCharSequence(R.id.widgetTitle, "setText", goalItem.goal.title) // Set Widget description. - val defCurrency = viewModel.getDefaultCurrencyValue() + val defCurrency = preferenceUtil.getString(PreferenceUtil.DEFAULT_CURRENCY_STR, "$") val widgetDesc = context.getString(R.string.goal_widget_desc) .format( "$defCurrency${Utils.formatCurrency(goalItem.getCurrentlySavedAmount())} / $defCurrency${ @@ -116,7 +120,7 @@ class GoalWidget : AppWidgetProvider() { val remainingAmount = (goalItem.goal.targetAmount - goalItem.getCurrentlySavedAmount()) if (remainingAmount > 0f) { if (goalItem.goal.deadline.isNotEmpty() && goalItem.goal.deadline.isNotBlank()) { - val calculatedDays = viewModel.goalTextUtils.calcRemainingDays(goalItem.goal) + val calculatedDays = goalTextUtils.calcRemainingDays(goalItem.goal) if (calculatedDays.remainingDays > 2) { val amountDays = "$defCurrency${ Utils.formatCurrency( @@ -143,6 +147,7 @@ class GoalWidget : AppWidgetProvider() { 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) } @@ -187,6 +192,7 @@ class GoalWidget : AppWidgetProvider() { private fun initialiseVm(context: Context) { if (!this::viewModel.isInitialized) { + println("viewmodel not initialised") viewModel = EntryPoints .get(context.applicationContext, WidgetEntryPoint::class.java).getViewModel() } diff --git a/app/src/main/java/com/starry/greenstash/widget/WidgetViewModel.kt b/app/src/main/java/com/starry/greenstash/widget/WidgetViewModel.kt index ad43b499..5270d525 100644 --- a/app/src/main/java/com/starry/greenstash/widget/WidgetViewModel.kt +++ b/app/src/main/java/com/starry/greenstash/widget/WidgetViewModel.kt @@ -30,7 +30,6 @@ import androidx.lifecycle.viewModelScope import com.starry.greenstash.database.core.GoalWithTransactions import com.starry.greenstash.database.goal.GoalDao import com.starry.greenstash.database.widget.WidgetDao -import com.starry.greenstash.utils.GoalTextUtils import com.starry.greenstash.utils.PreferenceUtil import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -43,8 +42,6 @@ class WidgetViewModel @Inject constructor( private val preferenceUtil: PreferenceUtil ) : ViewModel() { - val goalTextUtils = GoalTextUtils(preferenceUtil) - fun getGoalFromWidgetId( appWidgetId: Int, callback: (goalItem: GoalWithTransactions) -> Unit @@ -55,8 +52,4 @@ class WidgetViewModel @Inject constructor( withContext(Dispatchers.Main) { goalItem?.let { callback(it) } } } } - - fun getDefaultCurrencyValue() = preferenceUtil.getString( - PreferenceUtil.DEFAULT_CURRENCY_STR, "$" - ) } \ No newline at end of file