Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Disable balance widget when the app is locked
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Jun 10, 2022
1 parent 6e1f434 commit 0fb6cb2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -303,6 +305,7 @@ class SettingsViewModel @Inject constructor(

sharedPrefs.putBoolean(SharedPrefs.APP_LOCK_ENABLED, lockApp)
_lockApp.value = lockApp
refreshWidget(WalletBalanceReceiver::class.java)

TestIdlingResource.decrement()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -28,12 +31,13 @@ class WalletBalanceWidget: GlanceAppWidget() {
@Composable
override fun Content() {
val prefs = currentState<Preferences>()
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)
}

}
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ 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
import com.ivy.wallet.stringRes

@Composable
fun WalletBalanceWidgetContent(
appLocked: Boolean,
balance: String,
currency: String,
income: String,
Expand All @@ -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()
}
}
}
}
Expand All @@ -51,18 +65,18 @@ fun RowScope.WidgetClickableItem(
Column(
GlanceModifier
.defaultWeight()
.clickable( actionRunCallback<WalletBalanceButtonsAction>(
.clickable(
actionRunCallback<WalletBalanceButtonsAction>(
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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 0fb6cb2

Please sign in to comment.