diff --git a/app/src/main/java/com/ivy/wallet/ui/settings/SettingsViewModel.kt b/app/src/main/java/com/ivy/wallet/ui/settings/SettingsViewModel.kt index e7e76e0920..826e5eb66b 100644 --- a/app/src/main/java/com/ivy/wallet/ui/settings/SettingsViewModel.kt +++ b/app/src/main/java/com/ivy/wallet/ui/settings/SettingsViewModel.kt @@ -24,8 +24,10 @@ import com.ivy.wallet.io.network.request.github.OpenIssueRequest import com.ivy.wallet.io.persistence.SharedPrefs import com.ivy.wallet.io.persistence.dao.SettingsDao import com.ivy.wallet.io.persistence.dao.UserDao +import com.ivy.wallet.refreshWidget import com.ivy.wallet.ui.IvyWalletCtx import com.ivy.wallet.ui.RootActivity +import com.ivy.wallet.ui.widget.WalletBalanceReceiver import com.ivy.wallet.utils.* import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers @@ -303,6 +305,7 @@ class SettingsViewModel @Inject constructor( sharedPrefs.putBoolean(SharedPrefs.APP_LOCK_ENABLED, lockApp) _lockApp.value = lockApp + refreshWidget(WalletBalanceReceiver::class.java) TestIdlingResource.decrement() } diff --git a/app/src/main/java/com/ivy/wallet/ui/widget/WalletBalanceWidget.kt b/app/src/main/java/com/ivy/wallet/ui/widget/WalletBalanceWidget.kt index 23edca9af8..978ea4705e 100644 --- a/app/src/main/java/com/ivy/wallet/ui/widget/WalletBalanceWidget.kt +++ b/app/src/main/java/com/ivy/wallet/ui/widget/WalletBalanceWidget.kt @@ -4,6 +4,7 @@ import android.appwidget.AppWidgetManager import android.content.Context import androidx.compose.runtime.Composable import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.booleanPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey import androidx.glance.appwidget.GlanceAppWidget import androidx.glance.appwidget.GlanceAppWidgetManager @@ -15,8 +16,10 @@ import com.ivy.wallet.domain.action.account.AccountsAct import com.ivy.wallet.domain.action.settings.SettingsAct import com.ivy.wallet.domain.action.wallet.CalcIncomeExpenseAct import com.ivy.wallet.domain.action.wallet.CalcWalletBalanceAct +import com.ivy.wallet.io.persistence.SharedPrefs import com.ivy.wallet.ui.IvyWalletCtx import com.ivy.wallet.ui.onboarding.model.toCloseTimeRange +import com.ivy.wallet.utils.ioThread import com.ivy.wallet.utils.shortenAmount import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.MainScope @@ -28,12 +31,13 @@ class WalletBalanceWidget: GlanceAppWidget() { @Composable override fun Content() { val prefs = currentState() + 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" - WalletBalanceWidgetContent(balance, currency, income, expense) + WalletBalanceWidgetContent(appLocked, balance, currency, income, expense) } } @@ -46,15 +50,22 @@ class WalletBalanceReceiver : GlanceAppWidgetReceiver() { @Inject lateinit var walletBalanceAct: CalcWalletBalanceAct + @Inject lateinit var settingsAct: SettingsAct + @Inject lateinit var accountsAct: AccountsAct + @Inject lateinit var calcIncomeExpenseAct: CalcIncomeExpenseAct + @Inject lateinit var ivyContext: IvyWalletCtx + @Inject + lateinit var sharedPrefs: SharedPrefs + override fun onUpdate( context: Context, appWidgetManager: AppWidgetManager, @@ -67,6 +78,7 @@ class WalletBalanceReceiver : GlanceAppWidgetReceiver() { private fun updateData(context: Context) { coroutineScope.launch { val settings = settingsAct(Unit) + val appLocked = ioThread { sharedPrefs.getBoolean(SharedPrefs.APP_LOCK_ENABLED, false) } val currency = settings.baseCurrency val balance = walletBalanceAct(CalcWalletBalanceAct.Input(baseCurrency = currency)) val accounts = accountsAct(Unit) @@ -84,6 +96,7 @@ class WalletBalanceReceiver : GlanceAppWidgetReceiver() { glanceId?.let { updateAppWidgetState(context, PreferencesGlanceStateDefinition, 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()) diff --git a/app/src/main/java/com/ivy/wallet/ui/widget/WalletBalanceWidgetContent.kt b/app/src/main/java/com/ivy/wallet/ui/widget/WalletBalanceWidgetContent.kt index 0c61160d1a..0b8c7fb107 100644 --- a/app/src/main/java/com/ivy/wallet/ui/widget/WalletBalanceWidgetContent.kt +++ b/app/src/main/java/com/ivy/wallet/ui/widget/WalletBalanceWidgetContent.kt @@ -16,6 +16,7 @@ import androidx.glance.background import androidx.glance.layout.* import androidx.glance.text.FontWeight import androidx.glance.text.Text +import androidx.glance.text.TextAlign import androidx.glance.text.TextStyle import androidx.glance.unit.ColorProvider import com.ivy.wallet.R @@ -23,6 +24,7 @@ import com.ivy.wallet.stringRes @Composable fun WalletBalanceWidgetContent( + appLocked: Boolean, balance: String, currency: String, income: String, @@ -36,9 +38,21 @@ fun WalletBalanceWidgetContent( Column( modifier = GlanceModifier.fillMaxSize(), ) { - BalanceSection(balance, currency) - IncomeExpenseSection(income, expense, currency) - ButtonsSection() + if (appLocked) { + Text( + modifier = GlanceModifier.fillMaxSize(), + text = "App locked", + style = TextStyle( + fontSize = 30.sp, + color = ColorProvider(Color.White), + textAlign = TextAlign.Center + ) + ) + } else { + BalanceSection(balance, currency) + IncomeExpenseSection(income, expense, currency) + ButtonsSection() + } } } } @@ -51,18 +65,18 @@ fun RowScope.WidgetClickableItem( Column( GlanceModifier .defaultWeight() - .clickable( actionRunCallback( + .clickable( + actionRunCallback( parameters = actionParametersOf( - walletBtnActParam to when (text) { - R.string.income -> AddTransactionWidgetClick.ACTION_ADD_INCOME - R.string.expense -> AddTransactionWidgetClick.ACTION_ADD_EXPENSE - R.string.transfer -> AddTransactionWidgetClick.ACTION_ADD_TRANSFER - else -> return - } + walletBtnActParam to when (text) { + R.string.income -> AddTransactionWidgetClick.ACTION_ADD_INCOME + R.string.expense -> AddTransactionWidgetClick.ACTION_ADD_EXPENSE + R.string.transfer -> AddTransactionWidgetClick.ACTION_ADD_TRANSFER + else -> return + } ) ) - ) - , + ), horizontalAlignment = Alignment.CenterHorizontally ) { Image( @@ -117,7 +131,8 @@ fun IncomeExpenseSection( currency: String ) { Row( - GlanceModifier.fillMaxWidth().padding(start = 14.dp, end = 14.dp, top = 12.dp, bottom = 12.dp), + GlanceModifier.fillMaxWidth() + .padding(start = 14.dp, end = 14.dp, top = 12.dp, bottom = 12.dp), verticalAlignment = Alignment.CenterVertically ) { Row(