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;