From f8194b173c62daeabd19f96b6d41699925af10a2 Mon Sep 17 00:00:00 2001 From: Igor Makarov Date: Tue, 19 Nov 2019 17:19:01 +0300 Subject: [PATCH 1/2] Get latest available currency rates for new transaction --- .../orangesoftware/financisto/widget/RateNode.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/src/main/java/ru/orangesoftware/financisto/widget/RateNode.java b/app/src/main/java/ru/orangesoftware/financisto/widget/RateNode.java index 6d9f76cc..3c9d39f4 100644 --- a/app/src/main/java/ru/orangesoftware/financisto/widget/RateNode.java +++ b/app/src/main/java/ru/orangesoftware/financisto/widget/RateNode.java @@ -22,9 +22,11 @@ import android.widget.Toast; import java.text.DecimalFormat; +import java.util.List; import ru.orangesoftware.financisto.R; import ru.orangesoftware.financisto.activity.ActivityLayout; +import ru.orangesoftware.financisto.db.DatabaseAdapter; import ru.orangesoftware.financisto.model.Currency; import ru.orangesoftware.financisto.rates.ExchangeRate; import ru.orangesoftware.financisto.rates.ExchangeRateProvider; @@ -48,6 +50,7 @@ public class RateNode { private ImageButton bCalc; private ImageButton bDownload; + private DatabaseAdapter db; public RateNode(RateNodeOwner owner, ActivityLayout x, LinearLayout layout) { this.owner = owner; @@ -57,6 +60,9 @@ public RateNode(RateNodeOwner owner, ActivityLayout x, LinearLayout layout) { } private void createUI() { + db = new DatabaseAdapter(owner.getActivity()); + db.open(); + rateInfoNode = x.addRateNode(layout); rate = rateInfoNode.findViewById(R.id.rate); rate.addTextChangedListener(rateWatcher); @@ -122,6 +128,13 @@ public void updateRateInfo() { Currency currencyFrom = owner.getCurrencyFrom(); Currency currencyTo = owner.getCurrencyTo(); if (currencyFrom != null && currencyTo != null) { + if (r == 0.0) { + List rates = db.findRates(owner.getCurrencyFrom(), owner.getCurrencyTo()); + if (rates != null && !rates.isEmpty()) { + r = rates.get(0).rate; + setRate(r); + } + } sb.append("1").append(currencyFrom.name).append("=").append(nf.format(r)).append(currencyTo.name).append(", "); sb.append("1").append(currencyTo.name).append("=").append(nf.format(1.0 / r)).append(currencyFrom.name); } From 106547b19b528b0f85f303263f085d3acd36fca4 Mon Sep 17 00:00:00 2001 From: Igor Makarov Date: Fri, 17 Sep 2021 13:30:12 +0300 Subject: [PATCH 2/2] Add budgets values on widgets --- .../financisto/activity/AccountWidget.java | 110 +++++++++++++++--- .../financisto/db/BudgetsTotalCalculator.java | 4 + .../financisto/model/AccountType.java | 1 + 3 files changed, 99 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/ru/orangesoftware/financisto/activity/AccountWidget.java b/app/src/main/java/ru/orangesoftware/financisto/activity/AccountWidget.java index 74bc488f..4462b242 100644 --- a/app/src/main/java/ru/orangesoftware/financisto/activity/AccountWidget.java +++ b/app/src/main/java/ru/orangesoftware/financisto/activity/AccountWidget.java @@ -18,13 +18,18 @@ import android.database.Cursor; import android.graphics.Color; import android.net.Uri; +import android.os.Handler; import android.util.Log; import android.widget.RemoteViews; import ru.orangesoftware.financisto.R; +import ru.orangesoftware.financisto.blotter.BlotterFilter; +import ru.orangesoftware.financisto.db.BudgetsTotalCalculator; import ru.orangesoftware.financisto.db.DatabaseAdapter; +import ru.orangesoftware.financisto.filter.WhereFilter; import ru.orangesoftware.financisto.model.Account; import ru.orangesoftware.financisto.model.AccountType; +import ru.orangesoftware.financisto.model.Budget; import ru.orangesoftware.financisto.model.CardIssuer; import ru.orangesoftware.financisto.model.ElectronicPaymentType; import ru.orangesoftware.financisto.utils.MyPreferences; @@ -128,6 +133,12 @@ private static void saveAccountForWidget(Context context, int widgetId, long acc prefs.apply(); } + private static void saveBudgetForWidget(Context context, int widgetId, long budgetId) { + SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit(); + prefs.putLong(PREF_PREFIX_KEY + widgetId, -2 * budgetId); + prefs.apply(); + } + private static RemoteViews updateWidgetFromAccount(Context context, int widgetId, int layoutId, Class providerClass, Account a) { RemoteViews updateViews = new RemoteViews(context.getPackageName(), layoutId); updateViews.setTextViewText(R.id.line1, a.title); @@ -153,6 +164,22 @@ private static RemoteViews updateWidgetFromAccount(Context context, int widgetId return updateViews; } + private static RemoteViews updateWidgetFromBudget(Context context, int widgetId, int layoutId, Class providerClass, Budget b) { + RemoteViews updateViews = new RemoteViews(context.getPackageName(), layoutId); + updateViews.setTextViewText(R.id.line1, b.title); + updateViews.setImageViewResource(R.id.account_icon, AccountType.BUDGET.iconId); + + long amount = b.amount + b.spent; + + updateViews.setTextViewText(R.id.note, Utils.amountToString(b.currency, amount)); + Utils u = new Utils(context); + int amountColor = u.getAmountColor(amount); + updateViews.setTextColor(R.id.note, amountColor); + addScrollOnClick(context, updateViews, widgetId, providerClass); + saveBudgetForWidget(context, widgetId, b.id); + return updateViews; + } + private static void addScrollOnClick(Context context, RemoteViews updateViews, int widgetId, Class providerClass) { Uri widgetUri = ContentUris.withAppendedId(CONTENT_URI, widgetId); Intent intent = new Intent(WIDGET_UPDATE_ACTION, widgetUri, context, providerClass); @@ -180,20 +207,42 @@ private static void addButtonsClick(Context context, RemoteViews updateViews) { private static RemoteViews buildUpdateForCurrentAccount(Context context, int widgetId, int layoutId, Class providerClass, long accountId) { DatabaseAdapter db = new DatabaseAdapter(context); db.open(); - try { - Account a = db.getAccount(accountId); - if (a != null) { - Log.d("Financisto", "buildUpdateForCurrentAccount building update for " + widgetId + " -> " + accountId); - return updateWidgetFromAccount(context, widgetId, layoutId, providerClass, a); - } else { - Log.d("Financisto", "buildUpdateForCurrentAccount not found " + widgetId + " -> " + accountId); - return buildUpdateForNextAccount(context, widgetId, layoutId, providerClass, -1); + + // account + if (accountId > 0) { + try { + Account a = db.getAccount(accountId); + if (a != null) { + Log.d("Financisto", "buildUpdateForCurrentAccount building update for " + widgetId + " -> " + accountId); + return updateWidgetFromAccount(context, widgetId, layoutId, providerClass, a); + } else { + Log.d("Financisto", "buildUpdateForCurrentAccount not found " + widgetId + " -> " + accountId); + return buildUpdateForNextAccount(context, widgetId, layoutId, providerClass, -1); + } + } catch (Exception ex) { + return errorUpdate(context); + } finally { + db.close(); } - } catch (Exception ex) { - return errorUpdate(context); - } finally { - db.close(); } + + // budget + if (accountId < -1) { + accountId /= -2; + try { + ArrayList b = new ArrayList(); + b.add(db.load(Budget.class, accountId)); + BudgetsTotalCalculator bc = new BudgetsTotalCalculator(db, b); + bc.updateBudgets(new Handler()); + for (Budget i : b) + return updateWidgetFromBudget(context, widgetId, layoutId, providerClass, i); + } catch (Exception ex) { + return errorUpdate(context); + } finally { + db.close(); + } + } + return noDataUpdate(context, layoutId); } private static RemoteViews buildUpdateForNextAccount(Context context, int widgetId, int layoutId, Class providerClass, long accountId) { @@ -223,10 +272,39 @@ private static RemoteViews buildUpdateForNextAccount(Context context, int widget } } } - c.moveToFirst(); - Account a = EntityManager.loadFromCursor(c, Account.class); - Log.d("Financisto", "buildUpdateForNextAccount not found, taking the first one -> " + a.id); - return updateWidgetFromAccount(context, widgetId, layoutId, providerClass, a); + + // build budgets list + long millis = System.currentTimeMillis(); + ArrayList b = db.getAllBudgets(WhereFilter.empty() + .btw(BlotterFilter.DATETIME, + Long.toString(millis), + Long.toString(millis))); + BudgetsTotalCalculator bc = new BudgetsTotalCalculator(db, b); + bc.updateBudgets(new Handler()); + + int j = 0; + if (accountId < -1) { + for (Budget i : b) { + j += 1; + if (i.id * -2 == accountId) { + found = true; + break; + } + } + } else { + found = true; + j = 0; + } + + if ((found && b.size() == j) || !found) { + c.moveToFirst(); + Account a = EntityManager.loadFromCursor(c, Account.class); + Log.d("Financisto", "buildUpdateForNextAccount not found, taking the first one -> " + a.id); + return updateWidgetFromAccount(context, widgetId, layoutId, providerClass, a); + } else { + Log.d("Financisto", "buildUpdateForNextBudget for id = " + b.get(j).id); + return updateWidgetFromBudget(context, widgetId, layoutId, providerClass, b.get(j)); + } } } return noDataUpdate(context, layoutId); diff --git a/app/src/main/java/ru/orangesoftware/financisto/db/BudgetsTotalCalculator.java b/app/src/main/java/ru/orangesoftware/financisto/db/BudgetsTotalCalculator.java index de163766..4f0c3b47 100644 --- a/app/src/main/java/ru/orangesoftware/financisto/db/BudgetsTotalCalculator.java +++ b/app/src/main/java/ru/orangesoftware/financisto/db/BudgetsTotalCalculator.java @@ -46,6 +46,10 @@ public void updateBudgets(Handler handler) { final String categoriesText = getChecked(categories, b.categories); final String projectsText = getChecked(projects, b.projects); b.spent = spent; + + long millis = System.currentTimeMillis(); + b.isCurrent = (millis > b.startDate && millis <= b.endDate); + if (handler != null) { handler.post(new Runnable() { @Override diff --git a/app/src/main/java/ru/orangesoftware/financisto/model/AccountType.java b/app/src/main/java/ru/orangesoftware/financisto/model/AccountType.java index 5e4daa73..f43fb5f9 100644 --- a/app/src/main/java/ru/orangesoftware/financisto/model/AccountType.java +++ b/app/src/main/java/ru/orangesoftware/financisto/model/AccountType.java @@ -23,6 +23,7 @@ public enum AccountType implements EntityEnum { ELECTRONIC(R.string.account_type_electronic, R.drawable.account_type_electronic, false, false, false, false, true), ASSET(R.string.account_type_asset, R.drawable.account_type_asset, false, false, false, false, false), LIABILITY(R.string.account_type_liability, R.drawable.account_type_liability, false, false, false, false, false), + BUDGET(R.string.account_type_other, R.drawable.ic_tab_budgets_selected, false, false, false, false, false), OTHER(R.string.account_type_other, R.drawable.account_type_other, false, false, false, false, false); public final int titleId;