+ return new Currency(cursor.getInt(0), cursor.getString(1),
+ Currency.Position.valueOf(cursor.getInt(2)), cursor.getInt(3) == 1);
+ }
+ return null;
+ }
+ }
+ public Calendar getFirstTransactionDate() {
+ return firstTransactionDate;
+ }
+ public Calendar getLastTransactionDate() {
+ return lastTransactionDate;
+ }
+ private void applyTransactionFilter(List<LedgerTransaction> list) {
+ final String accFilter = Data.accountFilter.getValue();
+ if (TextUtils.isEmpty(accFilter)) {
+ displayedTransactions.postValue(list);
+ }
+ else {
+ ArrayList<LedgerTransaction> newList = new ArrayList<>();
+ for (LedgerTransaction tr : list) {
+ if (tr.hasAccountNamedLike(accFilter))
+ newList.add(tr);
+ }
+ displayedTransactions.postValue(newList);
+ }
+ }
+ synchronized public void storeAccountListAsync(List<LedgerAccount> list,
+ boolean storeUiFields) {
+ if (accountListSaver != null)
+ accountListSaver.interrupt();
+ accountListSaver = new AccountListSaver(this, list, storeUiFields);
+ accountListSaver.start();
+ }
+ public void setAndStoreAccountListFromWeb(ArrayList<LedgerAccount> list) {
+ SQLiteDatabase db = App.getDatabase();
+ db.beginTransactionNonExclusive();
+ try {
+ markAccountsAsNotPresent(db);
+ for (LedgerAccount acc : list) {
+ storeAccount(db, acc, false);
+ for (LedgerAmount amt : acc.getAmounts()) {
+ storeAccountValue(db, acc.getName(), amt.getCurrency(), amt.getAmount());
+ }
+ }
+ deleteNotPresentAccounts(db);
+ setLastUpdateStamp();
+ db.setTransactionSuccessful();
+ }
+ finally {
+ db.endTransaction();
+ }
+
+ mergeAccountList(list);
+ updateDisplayedAccounts();
+ }
+ public synchronized Locker lockAccountsForWriting() {
+ accountsLocker.lockForWriting();
+ return accountsLocker;
+ }
+ public void setAndStoreTransactionList(ArrayList<LedgerTransaction> list) {
+ storeTransactionListAsync(this, list);
+ SQLiteDatabase db = App.getDatabase();
+ db.beginTransactionNonExclusive();
+ try {
+ markTransactionsAsNotPresent(db);
+ for (LedgerTransaction tr : list)
+ storeTransaction(db, tr);
+ deleteNotPresentTransactions(db);
+ setLastUpdateStamp();
+ db.setTransactionSuccessful();
+ }
+ finally {
+ db.endTransaction();
+ }
+
+ allTransactions.postValue(list);
+ }
+ private void storeTransactionListAsync(MobileLedgerProfile mobileLedgerProfile,
+ List<LedgerTransaction> list) {
+ if (transactionListSaver != null)
+ transactionListSaver.interrupt();
+
+ transactionListSaver = new TransactionListSaver(this, list);
+ transactionListSaver.start();
+ }
+ public void setAndStoreAccountAndTransactionListFromWeb(List<LedgerAccount> accounts,
+ List<LedgerTransaction> transactions) {
+ storeAccountAndTransactionListAsync(accounts, transactions, false);
+
+ mergeAccountList(accounts);
+ updateDisplayedAccounts();
+
+ allTransactions.postValue(transactions);
+ }
+ private void storeAccountAndTransactionListAsync(List<LedgerAccount> accounts,
+ List<LedgerTransaction> transactions,
+ boolean storeAccUiFields) {
+ if (accountAndTransactionListSaver != null)
+ accountAndTransactionListSaver.interrupt();
+
+ accountAndTransactionListSaver =
+ new AccountAndTransactionListSaver(this, accounts, transactions, storeAccUiFields);
+ accountAndTransactionListSaver.start();
+ }
+ synchronized public void updateDisplayedAccounts() {
+ if (displayedAccountsUpdater != null) {
+ displayedAccountsUpdater.interrupt();
+ }
+ displayedAccountsUpdater = new AccountListDisplayedFilter(this, allAccounts);
+ displayedAccountsUpdater.start();
+ }
+ public List<LedgerAccount> getAllAccounts() {
+ return allAccounts;
+ }
+ private void updateAccountsMap(List<LedgerAccount> newAccounts) {
+ accountMap.clear();
+ for (LedgerAccount acc : newAccounts) {
+ accountMap.put(acc.getName(), acc);
+ }
+ }
+ @Nullable
+ public LedgerAccount locateAccount(String name) {
+ return accountMap.get(name);
+ }
+
+ public enum FutureDates {
+ None(0), OneWeek(7), TwoWeeks(14), OneMonth(30), TwoMonths(60), ThreeMonths(90),
+ SixMonths(180), OneYear(365), All(-1);
+ private static SparseArray<FutureDates> map = new SparseArray<>();
+
+ static {
+ for (FutureDates item : FutureDates.values()) {
+ map.put(item.value, item);
+ }
+ }
+
+ private int value;
+ FutureDates(int value) {
+ this.value = value;
+ }
+ public static FutureDates valueOf(int i) {
+ return map.get(i, None);
+ }
+ public int toInt() {
+ return this.value;
+ }
+ public String getText(Resources resources) {
+ switch (value) {
+ case 7:
+ return resources.getString(R.string.future_dates_7);
+ case 14:
+ return resources.getString(R.string.future_dates_14);
+ case 30:
+ return resources.getString(R.string.future_dates_30);
+ case 60:
+ return resources.getString(R.string.future_dates_60);
+ case 90:
+ return resources.getString(R.string.future_dates_90);
+ case 180:
+ return resources.getString(R.string.future_dates_180);
+ case 365:
+ return resources.getString(R.string.future_dates_365);
+ case -1:
+ return resources.getString(R.string.future_dates_all);
+ default:
+ return resources.getString(R.string.future_dates_none);
+ }
+ }
+ }
+
+ static class AccountListLoader extends Thread {
+ MobileLedgerProfile profile;
+ AccountListLoader(MobileLedgerProfile profile) {
+ this.profile = profile;
+ }
+ @Override
+ public void run() {
+ Logger.debug("async-acc", "AccountListLoader::run() entered");
+ String profileUUID = profile.getUuid();
+ ArrayList<LedgerAccount> list = new ArrayList<>();
+ HashMap<String, LedgerAccount> map = new HashMap<>();