Skip to content

Commit

Permalink
Fix widget setup crash
Browse files Browse the repository at this point in the history
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam committed Dec 25, 2023
1 parent 7375e5c commit 1399e4d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
10 changes: 8 additions & 2 deletions app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand All @@ -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${
Expand All @@ -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(
Expand All @@ -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)
}
Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -55,8 +52,4 @@ class WidgetViewModel @Inject constructor(
withContext(Dispatchers.Main) { goalItem?.let { callback(it) } }
}
}

fun getDefaultCurrencyValue() = preferenceUtil.getString(
PreferenceUtil.DEFAULT_CURRENCY_STR, "$"
)
}

0 comments on commit 1399e4d

Please sign in to comment.