This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1013 from mhss1/develop
Added Wallet balance Widget
- Loading branch information
Showing
20 changed files
with
444 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/src/main/java/com/ivy/wallet/ui/widget/WalletBalanceWidget.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.ivy.wallet.ui.widget | ||
|
||
import android.appwidget.AppWidgetManager | ||
import android.content.Context | ||
import androidx.compose.runtime.Composable | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import androidx.glance.appwidget.GlanceAppWidget | ||
import androidx.glance.appwidget.GlanceAppWidgetManager | ||
import androidx.glance.appwidget.GlanceAppWidgetReceiver | ||
import androidx.glance.appwidget.state.updateAppWidgetState | ||
import androidx.glance.currentState | ||
import androidx.glance.state.PreferencesGlanceStateDefinition | ||
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.ui.IvyWalletCtx | ||
import com.ivy.wallet.ui.onboarding.model.toCloseTimeRange | ||
import com.ivy.wallet.utils.shortenAmount | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
class WalletBalanceWidget: GlanceAppWidget() { | ||
|
||
@Composable | ||
override fun Content() { | ||
val prefs = currentState<Preferences>() | ||
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) | ||
} | ||
|
||
} | ||
|
||
@AndroidEntryPoint | ||
class WalletBalanceReceiver : GlanceAppWidgetReceiver() { | ||
|
||
override val glanceAppWidget: GlanceAppWidget = WalletBalanceWidget() | ||
private val coroutineScope = MainScope() | ||
|
||
@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 | ||
|
||
override fun onUpdate( | ||
context: Context, | ||
appWidgetManager: AppWidgetManager, | ||
appWidgetIds: IntArray | ||
) { | ||
super.onUpdate(context, appWidgetManager, appWidgetIds) | ||
updateData(context) | ||
} | ||
|
||
private fun updateData(context: Context) { | ||
coroutineScope.launch { | ||
val settings = settingsAct(Unit) | ||
val currency = settings.baseCurrency | ||
val balance = walletBalanceAct(CalcWalletBalanceAct.Input(baseCurrency = currency)) | ||
val accounts = accountsAct(Unit) | ||
val period = ivyContext.selectedPeriod | ||
val incomeExpense = calcIncomeExpenseAct( | ||
CalcIncomeExpenseAct.Input( | ||
baseCurrency = settings.baseCurrency, | ||
accounts = accounts, | ||
range = period.toRange(ivyContext.startDayOfMonth).toCloseTimeRange() | ||
) | ||
) | ||
|
||
val glanceId = | ||
GlanceAppWidgetManager(context).getGlanceIds(WalletBalanceWidget::class.java).firstOrNull() | ||
glanceId?.let { | ||
updateAppWidgetState(context, PreferencesGlanceStateDefinition, it) { pref -> | ||
pref.toMutablePreferences().apply { | ||
this[stringPreferencesKey("balance")] = shortenAmount(balance.toDouble()) | ||
this[stringPreferencesKey("currency")] = currency | ||
this[stringPreferencesKey("income")] = shortenAmount(incomeExpense.income.toDouble()) | ||
this[stringPreferencesKey("expense")] = shortenAmount(incomeExpense.expense.toDouble()) | ||
} | ||
} | ||
glanceAppWidget.update(context, it) | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
app/src/main/java/com/ivy/wallet/ui/widget/WalletBalanceWidgetActions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.ivy.wallet.ui.widget | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import androidx.glance.GlanceId | ||
import androidx.glance.action.ActionParameters | ||
import androidx.glance.appwidget.action.ActionCallback | ||
import com.ivy.wallet.domain.data.TransactionType | ||
import com.ivy.wallet.ui.RootActivity | ||
|
||
class WalletBalanceButtonsAction : ActionCallback { | ||
override suspend fun onRun(context: Context, glanceId: GlanceId, parameters: ActionParameters) { | ||
when (parameters[walletBtnActParam]) { | ||
AddTransactionWidgetClick.ACTION_ADD_INCOME -> { | ||
context.startActivity( | ||
RootActivity.addTransactionStart( | ||
context = context, | ||
type = TransactionType.INCOME | ||
).apply { | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
} | ||
) | ||
} | ||
AddTransactionWidgetClick.ACTION_ADD_EXPENSE -> { | ||
context.startActivity( | ||
RootActivity.addTransactionStart( | ||
context = context, | ||
type = TransactionType.EXPENSE | ||
).apply { | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
} | ||
) | ||
} | ||
AddTransactionWidgetClick.ACTION_ADD_TRANSFER -> { | ||
context.startActivity( | ||
RootActivity.addTransactionStart( | ||
context = context, | ||
type = TransactionType.TRANSFER | ||
).apply { | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
} | ||
) | ||
} | ||
else -> return | ||
} | ||
} | ||
} | ||
|
||
class WalletBalanceWidgetClickAction: ActionCallback { | ||
override suspend fun onRun(context: Context, glanceId: GlanceId, parameters: ActionParameters) { | ||
context.startActivity( | ||
RootActivity.getIntent( | ||
context = context | ||
).apply { | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | ||
} | ||
) | ||
} | ||
} | ||
|
||
val walletBtnActParam = ActionParameters.Key<String>("wallet_balance_button_action") |
Oops, something went wrong.