+ if (lastUpdate == 0) {
+ Data.lastUpdateDate.postValue(null);
+ }
+ else {
+ Data.lastUpdateDate.postValue(new Date(lastUpdate));
+ }
+
+ scheduleDataRetrievalIfStale(lastUpdate);
+ });
+ }
+ private void refreshLastUpdateInfo() {
+ final int formatFlags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR |
+ DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NUMERIC_DATE;
+ String templateForTransactions =
+ getResources().getString(R.string.transaction_count_summary);
+ String templateForAccounts = getResources().getString(R.string.account_count_summary);
+ Integer accountCount = Data.lastUpdateAccountCount.getValue();
+ Integer transactionCount = Data.lastUpdateTransactionCount.getValue();
+ Date lastUpdate = Data.lastUpdateDate.getValue();
+ if (lastUpdate == null) {
+ Data.lastTransactionsUpdateText.setValue("----");
+ Data.lastAccountsUpdateText.setValue("----");
+ }
+ else {
+ Data.lastTransactionsUpdateText.setValue(
+ String.format(Objects.requireNonNull(Data.locale.getValue()),
+ templateForTransactions,
+ transactionCount == null ? 0 : transactionCount,
+ DateUtils.formatDateTime(this, lastUpdate.getTime(), formatFlags)));
+ Data.lastAccountsUpdateText.setValue(
+ String.format(Objects.requireNonNull(Data.locale.getValue()),
+ templateForAccounts, accountCount == null ? 0 : accountCount,
+ DateUtils.formatDateTime(this, lastUpdate.getTime(), formatFlags)));
+ }
+ }
+ public void onStopTransactionRefreshClick(View view) {
+ Logger.debug(TAG, "Cancelling transactions refresh");
+ mainModel.stopTransactionsRetrieval();
+ b.transactionListCancelDownload.setEnabled(false);
+ }
+ public void onRetrieveRunningChanged(Boolean running) {
+ if (running) {
+ b.transactionListCancelDownload.setEnabled(true);
+ ColorStateList csl = Colors.getColorStateList();
+ b.transactionListProgressBar.setIndeterminateTintList(csl);
+ b.transactionListProgressBar.setProgressTintList(csl);
+ b.transactionListProgressBar.setIndeterminate(true);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ b.transactionListProgressBar.setProgress(0, false);
+ }
+ else {
+ b.transactionListProgressBar.setProgress(0);
+ }
+ b.transactionProgressLayout.setVisibility(View.VISIBLE);
+ }
+ else {
+ b.transactionProgressLayout.setVisibility(View.GONE);
+ }
+ }
+ public void onRetrieveProgress(@Nullable RetrieveTransactionsTask.Progress progress) {
+ if (progress == null ||
+ progress.getState() == RetrieveTransactionsTask.ProgressState.FINISHED)
+ {
+ Logger.debug(TAG, "progress: Done");
+ b.transactionProgressLayout.setVisibility(View.GONE);
+
+ mainModel.transactionRetrievalDone();
+
+ String error = (progress == null) ? null : progress.getError();
+ if (error != null) {
+ if (error.equals(RetrieveTransactionsTask.Result.ERR_JSON_PARSER_ERROR))
+ error = getResources().getString(R.string.err_json_parser_error);
+
+ AlertDialog.Builder builder = new AlertDialog.Builder(this);
+ builder.setMessage(error);
+ builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
+ Logger.debug(TAG, "will start profile editor");
+ ProfileDetailActivity.start(this, profile);
+ });
+ builder.create()
+ .show();
+ return;
+ }
+
+ return;
+ }
+
+
+ b.transactionListCancelDownload.setEnabled(true);
+// ColorStateList csl = Colors.getColorStateList();
+// progressBar.setIndeterminateTintList(csl);
+// progressBar.setProgressTintList(csl);
+// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
+// progressBar.setProgress(0, false);
+// else
+// progressBar.setProgress(0);
+ b.transactionProgressLayout.setVisibility(View.VISIBLE);
+
+ if (progress.isIndeterminate() || (progress.getTotal() <= 0)) {
+ b.transactionListProgressBar.setIndeterminate(true);
+ Logger.debug(TAG, "progress: indeterminate");
+ }
+ else {
+ if (b.transactionListProgressBar.isIndeterminate()) {
+ b.transactionListProgressBar.setIndeterminate(false);
+ }
+// Logger.debug(TAG,
+// String.format(Locale.US, "progress: %d/%d", progress.getProgress(),
+// progress.getTotal
+// ()));
+ b.transactionListProgressBar.setMax(progress.getTotal());
+ // for some reason animation doesn't work - no progress is shown (stick at 0)
+ // on lineageOS 14.1 (Nougat, 7.1.2)
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
+ b.transactionListProgressBar.setProgress(progress.getProgress(), false);
+ else
+ b.transactionListProgressBar.setProgress(progress.getProgress());
+ }
+ }
+ public void fabShouldShow() {
+ if ((profile != null) && profile.permitPosting() && !b.drawerLayout.isOpen())
+ fabManager.showFab();
+ }
+ @Override
+ public Context getContext() {
+ return this;
+ }
+ @Override
+ public void showManagedFab() {
+ fabShouldShow();
+ }
+ @Override
+ public void hideManagedFab() {
+ fabManager.hideFab();
+ }
+ public static class SectionsPagerAdapter extends FragmentStateAdapter {
+
+ public SectionsPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
+ super(fragmentActivity);
+ }
+ @NotNull
+ @Override
+ public Fragment createFragment(int position) {
+ Logger.debug(TAG, String.format(Locale.ENGLISH, "Switching to fragment %d", position));
+ switch (position) {
+ case 0:
+// debug(TAG, "Creating account summary fragment");
+ return new AccountSummaryFragment();
+ case 1:
+ return new TransactionListFragment();
+ default:
+ throw new IllegalStateException(
+ String.format("Unexpected fragment index: " + "%d", position));
+ }
+ }
+
+ @Override
+ public int getItemCount() {
+ return 2;
+ }
+ }
+
+ static private class ConverterThread extends Thread {
+ private final List<TransactionWithAccounts> list;
+ private final MainModel model;
+ private final String accFilter;
+ public ConverterThread(@NonNull MainModel model,
+ @NonNull List<TransactionWithAccounts> list, String accFilter) {
+ this.model = model;
+ this.list = list;
+ this.accFilter = accFilter;
+ }
+ @Override
+ public void run() {
+ TransactionAccumulator accumulator = new TransactionAccumulator(accFilter);
+
+ for (TransactionWithAccounts tr : list) {
+ if (isInterrupted()) {
+ Logger.debug(TAG, "ConverterThread bailing out on interrupt");
+ return;
+ }
+ accumulator.put(new LedgerTransaction(tr));
+ }
+
+ if (isInterrupted()) {
+ Logger.debug(TAG, "ConverterThread bailing out on interrupt");
+ return;
+ }
+
+ Logger.debug(TAG, "ConverterThread publishing results");
+
+ Misc.onMainThread(() -> accumulator.publishResults(model));
+ }
+ }