2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageInfo;
23 import android.content.pm.ShortcutInfo;
24 import android.content.pm.ShortcutManager;
25 import android.content.res.ColorStateList;
26 import android.graphics.Color;
27 import android.graphics.drawable.Icon;
28 import android.os.Build;
29 import android.os.Bundle;
30 import android.text.format.DateUtils;
31 import android.util.Log;
32 import android.view.View;
33 import android.view.animation.AnimationUtils;
34 import android.widget.TextView;
36 import androidx.annotation.NonNull;
37 import androidx.annotation.Nullable;
38 import androidx.appcompat.app.ActionBarDrawerToggle;
39 import androidx.appcompat.app.AlertDialog;
40 import androidx.core.view.GravityCompat;
41 import androidx.drawerlayout.widget.DrawerLayout;
42 import androidx.fragment.app.Fragment;
43 import androidx.fragment.app.FragmentActivity;
44 import androidx.lifecycle.LiveData;
45 import androidx.lifecycle.MutableLiveData;
46 import androidx.lifecycle.ViewModelProvider;
47 import androidx.recyclerview.widget.LinearLayoutManager;
48 import androidx.recyclerview.widget.RecyclerView;
49 import androidx.viewpager2.adapter.FragmentStateAdapter;
50 import androidx.viewpager2.widget.ViewPager2;
52 import com.google.android.material.snackbar.Snackbar;
54 import net.ktnx.mobileledger.BackupsActivity;
55 import net.ktnx.mobileledger.R;
56 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
57 import net.ktnx.mobileledger.async.TransactionAccumulator;
58 import net.ktnx.mobileledger.databinding.ActivityMainBinding;
59 import net.ktnx.mobileledger.db.DB;
60 import net.ktnx.mobileledger.db.Option;
61 import net.ktnx.mobileledger.db.Profile;
62 import net.ktnx.mobileledger.db.TransactionWithAccounts;
63 import net.ktnx.mobileledger.model.Data;
64 import net.ktnx.mobileledger.model.LedgerTransaction;
65 import net.ktnx.mobileledger.ui.FabManager;
66 import net.ktnx.mobileledger.ui.MainModel;
67 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
68 import net.ktnx.mobileledger.ui.new_transaction.NewTransactionActivity;
69 import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
70 import net.ktnx.mobileledger.ui.profiles.ProfilesRecyclerViewAdapter;
71 import net.ktnx.mobileledger.ui.templates.TemplatesActivity;
72 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
73 import net.ktnx.mobileledger.utils.Colors;
74 import net.ktnx.mobileledger.utils.Logger;
75 import net.ktnx.mobileledger.utils.Misc;
77 import org.jetbrains.annotations.NotNull;
79 import java.util.ArrayList;
80 import java.util.Date;
81 import java.util.List;
82 import java.util.Locale;
83 import java.util.Objects;
89 public class MainActivity extends ProfileThemedActivity implements FabManager.FabHandler {
90 public static final String TAG = "main-act";
91 public static final String STATE_CURRENT_PAGE = "current_page";
92 public static final String BUNDLE_SAVED_STATE = "bundle_savedState";
93 public static final String STATE_ACC_FILTER = "account_filter";
94 private static final boolean FAB_HIDDEN = false;
95 private static final boolean FAB_SHOWN = true;
96 private ConverterThread converterThread = null;
97 private SectionsPagerAdapter mSectionsPagerAdapter;
98 private ProfilesRecyclerViewAdapter mProfileListAdapter;
99 private int mCurrentPage;
100 private boolean mBackMeansToAccountList = false;
101 private DrawerLayout.SimpleDrawerListener drawerListener;
102 private ActionBarDrawerToggle barDrawerToggle;
103 private ViewPager2.OnPageChangeCallback pageChangeCallback;
104 private Profile profile;
105 private MainModel mainModel;
106 private ActivityMainBinding b;
107 private int fabVerticalOffset;
108 private FabManager fabManager;
110 protected void onStart() {
113 Logger.debug(TAG, "onStart()");
115 b.mainPager.setCurrentItem(mCurrentPage, false);
118 protected void onSaveInstanceState(@NotNull Bundle outState) {
119 super.onSaveInstanceState(outState);
120 outState.putInt(STATE_CURRENT_PAGE, b.mainPager.getCurrentItem());
121 if (mainModel.getAccountFilter()
123 outState.putString(STATE_ACC_FILTER, mainModel.getAccountFilter()
127 protected void onDestroy() {
128 mSectionsPagerAdapter = null;
129 b.navProfileList.setAdapter(null);
130 b.drawerLayout.removeDrawerListener(drawerListener);
131 drawerListener = null;
132 b.drawerLayout.removeDrawerListener(barDrawerToggle);
133 barDrawerToggle = null;
134 b.mainPager.unregisterOnPageChangeCallback(pageChangeCallback);
135 pageChangeCallback = null;
139 protected void onResume() {
144 protected void onCreate(Bundle savedInstanceState) {
145 Logger.debug(TAG, "onCreate()/entry");
146 super.onCreate(savedInstanceState);
147 Logger.debug(TAG, "onCreate()/after super");
148 b = ActivityMainBinding.inflate(getLayoutInflater());
149 setContentView(b.getRoot());
151 mainModel = new ViewModelProvider(this).get(MainModel.class);
153 mSectionsPagerAdapter = new SectionsPagerAdapter(this);
155 Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
156 if (extra != null && savedInstanceState == null)
157 savedInstanceState = extra;
160 setSupportActionBar(b.toolbar);
162 Data.observeProfile(this, this::onProfileChanged);
164 Data.profiles.observe(this, this::onProfileListChanged);
166 Data.backgroundTaskProgress.observe(this, this::onRetrieveProgress);
167 Data.backgroundTasksRunning.observe(this, this::onRetrieveRunningChanged);
169 if (barDrawerToggle == null) {
170 barDrawerToggle = new ActionBarDrawerToggle(this, b.drawerLayout, b.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
171 b.drawerLayout.addDrawerListener(barDrawerToggle);
173 barDrawerToggle.syncState();
176 PackageInfo pi = getApplicationContext().getPackageManager()
177 .getPackageInfo(getPackageName(), 0);
178 ((TextView) b.navUpper.findViewById(R.id.drawer_version_text)).setText(pi.versionName);
179 ((TextView) b.noProfilesLayout.findViewById(R.id.drawer_version_text)).setText(pi.versionName);
181 catch (Exception e) {
185 markDrawerItemCurrent(R.id.nav_account_summary);
187 b.mainPager.setAdapter(mSectionsPagerAdapter);
188 b.mainPager.setOffscreenPageLimit(1);
190 if (pageChangeCallback == null) {
191 pageChangeCallback = new ViewPager2.OnPageChangeCallback() {
193 public void onPageSelected(int position) {
194 mCurrentPage = position;
197 markDrawerItemCurrent(R.id.nav_account_summary);
200 markDrawerItemCurrent(R.id.nav_latest_transactions);
203 Log.e(TAG, String.format("Unexpected page index %d", position));
206 super.onPageSelected(position);
209 b.mainPager.registerOnPageChangeCallback(pageChangeCallback);
213 if (savedInstanceState != null) {
214 int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
215 if (currentPage != -1) {
216 mCurrentPage = currentPage;
218 mainModel.getAccountFilter()
219 .setValue(savedInstanceState.getString(STATE_ACC_FILTER, null));
222 b.btnNoProfilesAdd.setOnClickListener(v -> ProfileDetailActivity.start(this, null));
223 b.btnRestore.setOnClickListener(v -> BackupsActivity.start(this));
225 b.btnAddTransaction.setOnClickListener(this::fabNewTransactionClicked);
227 b.navNewProfileButton.setOnClickListener(v -> ProfileDetailActivity.start(this, null));
229 b.transactionListCancelDownload.setOnClickListener(this::onStopTransactionRefreshClick);
231 if (mProfileListAdapter == null)
232 mProfileListAdapter = new ProfilesRecyclerViewAdapter();
233 b.navProfileList.setAdapter(mProfileListAdapter);
235 mProfileListAdapter.editingProfiles.observe(this, newValue -> {
237 b.navProfilesStartEdit.setVisibility(View.GONE);
238 b.navProfilesCancelEdit.setVisibility(View.VISIBLE);
239 b.navNewProfileButton.setVisibility(View.VISIBLE);
240 if (b.drawerLayout.isDrawerOpen(GravityCompat.START)) {
241 b.navProfilesStartEdit.startAnimation(
242 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
243 b.navProfilesCancelEdit.startAnimation(
244 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
245 b.navNewProfileButton.startAnimation(
246 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
250 b.navProfilesCancelEdit.setVisibility(View.GONE);
251 b.navProfilesStartEdit.setVisibility(View.VISIBLE);
252 b.navNewProfileButton.setVisibility(View.GONE);
253 if (b.drawerLayout.isDrawerOpen(GravityCompat.START)) {
254 b.navProfilesCancelEdit.startAnimation(
255 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
256 b.navProfilesStartEdit.startAnimation(
257 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
258 b.navNewProfileButton.startAnimation(
259 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
263 mProfileListAdapter.notifyDataSetChanged();
266 fabManager = new FabManager(b.btnAddTransaction);
268 LinearLayoutManager llm = new LinearLayoutManager(this);
270 llm.setOrientation(RecyclerView.VERTICAL);
271 b.navProfileList.setLayoutManager(llm);
273 b.navProfilesStartEdit.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
274 b.navProfilesCancelEdit.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
275 b.navProfileListHeadButtons.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
276 if (drawerListener == null) {
277 drawerListener = new DrawerLayout.SimpleDrawerListener() {
279 public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
280 if (slideOffset > 0.2)
281 fabManager.hideFab();
284 public void onDrawerClosed(View drawerView) {
285 super.onDrawerClosed(drawerView);
286 mProfileListAdapter.setAnimationsEnabled(false);
287 mProfileListAdapter.editingProfiles.setValue(false);
288 Data.drawerOpen.setValue(false);
292 public void onDrawerOpened(View drawerView) {
293 super.onDrawerOpened(drawerView);
294 mProfileListAdapter.setAnimationsEnabled(true);
295 Data.drawerOpen.setValue(true);
296 fabManager.hideFab();
299 b.drawerLayout.addDrawerListener(drawerListener);
302 Data.drawerOpen.observe(this, open -> {
304 b.drawerLayout.open();
306 b.drawerLayout.close();
309 mainModel.getUpdateError()
310 .observe(this, (error) -> {
314 Snackbar.make(b.mainPager, error, Snackbar.LENGTH_INDEFINITE)
316 mainModel.clearUpdateError();
318 Data.locale.observe(this, l -> refreshLastUpdateInfo());
319 Data.lastUpdateDate.observe(this, date -> refreshLastUpdateInfo());
320 Data.lastUpdateTransactionCount.observe(this, date -> refreshLastUpdateInfo());
321 Data.lastUpdateAccountCount.observe(this, date -> refreshLastUpdateInfo());
322 b.navAccountSummary.setOnClickListener(this::onAccountSummaryClicked);
323 b.navLatestTransactions.setOnClickListener(this::onLatestTransactionsClicked);
324 b.navPatterns.setOnClickListener(this::onPatternsClick);
325 b.navBackupRestore.setOnClickListener(this::onBackupRestoreClick);
327 private void onBackupRestoreClick(View view) {
328 Intent intent = new Intent(this, BackupsActivity.class);
329 startActivity(intent);
331 private void onPatternsClick(View view) {
332 Intent intent = new Intent(this, TemplatesActivity.class);
333 startActivity(intent);
335 private void scheduleDataRetrievalIfStale(long lastUpdate) {
336 long now = new Date().getTime();
337 if ((lastUpdate == 0) || (now > (lastUpdate + (24 * 3600 * 1000)))) {
339 Logger.debug("db::", "WEB data never fetched. scheduling a fetch");
341 Logger.debug("db", String.format(Locale.ENGLISH,
342 "WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
343 lastUpdate / 1000f, now / 1000f));
345 mainModel.scheduleTransactionListRetrieval();
348 private void createShortcuts(@NotNull List<Profile> list) {
349 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
352 ShortcutManager sm = getSystemService(ShortcutManager.class);
353 List<ShortcutInfo> shortcuts = new ArrayList<>();
355 for (Profile p : list) {
356 if (shortcuts.size() >= sm.getMaxShortcutCountPerActivity())
359 if (!p.permitPosting())
362 final ShortcutInfo.Builder builder =
363 new ShortcutInfo.Builder(this, "new_transaction_" + p.getId());
364 ShortcutInfo si = builder.setShortLabel(p.getName())
365 .setIcon(Icon.createWithResource(this,
366 R.drawable.thick_plus_icon))
367 .setIntent(new Intent(Intent.ACTION_VIEW, null, this,
368 NewTransactionActivity.class).putExtra(
369 ProfileThemedActivity.PARAM_PROFILE_ID, p.getId())
371 ProfileThemedActivity.PARAM_THEME,
378 sm.setDynamicShortcuts(shortcuts);
380 private void onProfileListChanged(@NotNull List<Profile> newList) {
381 createShortcuts(newList);
383 if (newList.isEmpty()) {
384 b.noProfilesLayout.setVisibility(View.VISIBLE);
385 b.mainAppLayout.setVisibility(View.GONE);
389 b.mainAppLayout.setVisibility(View.VISIBLE);
390 b.noProfilesLayout.setVisibility(View.GONE);
392 b.navProfileList.setMinimumHeight(
393 (int) (getResources().getDimension(R.dimen.thumb_row_height) * newList.size()));
395 Logger.debug("profiles", "profile list changed");
396 mProfileListAdapter.setProfileList(newList);
398 final Profile currentProfile = Data.getProfile();
399 Profile replacementProfile = null;
400 if (currentProfile != null) {
401 for (Profile p : newList) {
402 if (p.getId() == currentProfile.getId()) {
403 replacementProfile = p;
409 if (replacementProfile == null) {
410 Logger.debug(TAG, "Switching profile because the current is no longer available");
411 Data.setCurrentProfile(newList.get(0));
414 Data.setCurrentProfile(replacementProfile);
418 * called when the current profile has changed
420 private void onProfileChanged(@Nullable Profile newProfile) {
421 if (this.profile != null) {
422 if (this.profile.equals(newProfile))
426 boolean haveProfile = newProfile != null;
429 setTitle(newProfile.getName());
431 setTitle(R.string.app_name);
433 int newProfileTheme = haveProfile ? newProfile.getTheme() : Colors.DEFAULT_HUE_DEG;
434 if (newProfileTheme != Colors.profileThemeId) {
435 Logger.debug("profiles",
436 String.format(Locale.ENGLISH, "profile theme %d → %d", Colors.profileThemeId,
438 Colors.profileThemeId = newProfileTheme;
439 profileThemeChanged();
440 // profileThemeChanged would restart the activity, so no need to reload the
445 final boolean sameProfileId = (newProfile != null) && (this.profile != null) &&
446 this.profile.getId() == newProfile.getId();
448 this.profile = newProfile;
450 b.noProfilesLayout.setVisibility(haveProfile ? View.GONE : View.VISIBLE);
451 b.pagerLayout.setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
453 mProfileListAdapter.notifyDataSetChanged();
456 if (newProfile.permitPosting()) {
457 b.toolbar.setSubtitle(null);
458 b.btnAddTransaction.show();
461 b.toolbar.setSubtitle(R.string.profile_subtitle_read_only);
462 b.btnAddTransaction.hide();
466 b.toolbar.setSubtitle(null);
467 b.btnAddTransaction.hide();
470 updateLastUpdateTextFromDB();
473 Logger.debug(TAG, String.format(Locale.ROOT, "Short-cut profile 'changed' to %d",
474 newProfile.getId()));
478 mainModel.getAccountFilter()
479 .observe(this, this::onAccountFilterChanged);
481 mainModel.stopTransactionsRetrieval();
482 mainModel.clearTransactions();
484 private void onAccountFilterChanged(String accFilter) {
485 Logger.debug(TAG, "account filter changed, reloading transactions");
486 // mainModel.scheduleTransactionListReload();
487 LiveData<List<TransactionWithAccounts>> transactions =
488 new MutableLiveData<>(new ArrayList<>());
489 if (profile != null) {
490 if (accFilter == null || accFilter.isEmpty()) {
491 transactions = DB.get()
493 .getAllWithAccounts(profile.getId());
496 transactions = DB.get()
498 .getAllWithAccountsFiltered(profile.getId(), accFilter);
502 transactions.observe(this, list -> {
504 String.format(Locale.ROOT, "got transaction list from DB (%d transactions)",
507 if (converterThread != null)
508 converterThread.interrupt();
509 converterThread = new ConverterThread(mainModel, list, accFilter);
510 converterThread.start();
513 private void profileThemeChanged() {
514 // un-hook all observed LiveData
515 Data.removeProfileObservers(this);
516 Data.profiles.removeObservers(this);
517 Data.lastUpdateTransactionCount.removeObservers(this);
518 Data.lastUpdateAccountCount.removeObservers(this);
519 Data.lastUpdateDate.removeObservers(this);
521 Logger.debug(TAG, "profileThemeChanged(): recreating activity");
524 public void fabNewTransactionClicked(View view) {
525 Intent intent = new Intent(this, NewTransactionActivity.class);
526 intent.putExtra(ProfileThemedActivity.PARAM_PROFILE_ID, profile.getId());
527 intent.putExtra(ProfileThemedActivity.PARAM_THEME, profile.getTheme());
528 startActivity(intent);
529 overridePendingTransition(R.anim.slide_in_up, R.anim.dummy);
531 public void markDrawerItemCurrent(int id) {
532 TextView item = b.drawerLayout.findViewById(id);
533 item.setBackgroundColor(Colors.tableRowDarkBG);
535 for (int i = 0; i < b.navActions.getChildCount(); i++) {
536 View view = b.navActions.getChildAt(i);
537 if (view.getId() != id) {
538 view.setBackgroundColor(Color.TRANSPARENT);
542 public void onAccountSummaryClicked(View view) {
543 b.drawerLayout.closeDrawers();
545 showAccountSummaryFragment();
547 private void showAccountSummaryFragment() {
548 b.mainPager.setCurrentItem(0, true);
549 mainModel.getAccountFilter()
552 public void onLatestTransactionsClicked(View view) {
553 b.drawerLayout.closeDrawers();
555 showTransactionsFragment(null);
557 public void showTransactionsFragment(String accName) {
558 mainModel.getAccountFilter()
560 b.mainPager.setCurrentItem(1, true);
562 public void showAccountTransactions(String accountName) {
563 mBackMeansToAccountList = true;
564 showTransactionsFragment(accountName);
567 public void onBackPressed() {
568 if (b.drawerLayout.isDrawerOpen(GravityCompat.START)) {
569 b.drawerLayout.closeDrawer(GravityCompat.START);
572 if (mBackMeansToAccountList && (b.mainPager.getCurrentItem() == 1)) {
573 mainModel.getAccountFilter()
575 showAccountSummaryFragment();
576 mBackMeansToAccountList = false;
579 Logger.debug(TAG, String.format(Locale.ENGLISH, "manager stack: %d",
580 getSupportFragmentManager().getBackStackEntryCount()));
582 super.onBackPressed();
586 public void updateLastUpdateTextFromDB() {
592 .load(profile.getId(), Option.OPT_LAST_SCRAPE)
593 .observe(this, opt -> {
597 lastUpdate = Long.parseLong(opt.getValue());
599 catch (NumberFormatException ex) {
600 Logger.debug(TAG, String.format("Error parsing '%s' as long", opt.getValue()),
605 if (lastUpdate == 0) {
606 Data.lastUpdateDate.postValue(null);
609 Data.lastUpdateDate.postValue(new Date(lastUpdate));
612 scheduleDataRetrievalIfStale(lastUpdate);
615 private void refreshLastUpdateInfo() {
616 final int formatFlags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR |
617 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NUMERIC_DATE;
618 String templateForTransactions =
619 getResources().getString(R.string.transaction_count_summary);
620 String templateForAccounts = getResources().getString(R.string.account_count_summary);
621 Integer accountCount = Data.lastUpdateAccountCount.getValue();
622 Integer transactionCount = Data.lastUpdateTransactionCount.getValue();
623 Date lastUpdate = Data.lastUpdateDate.getValue();
624 if (lastUpdate == null) {
625 Data.lastTransactionsUpdateText.setValue("----");
626 Data.lastAccountsUpdateText.setValue("----");
629 Data.lastTransactionsUpdateText.setValue(
630 String.format(Objects.requireNonNull(Data.locale.getValue()),
631 templateForTransactions,
632 transactionCount == null ? 0 : transactionCount,
633 DateUtils.formatDateTime(this, lastUpdate.getTime(), formatFlags)));
634 Data.lastAccountsUpdateText.setValue(
635 String.format(Objects.requireNonNull(Data.locale.getValue()),
636 templateForAccounts, accountCount == null ? 0 : accountCount,
637 DateUtils.formatDateTime(this, lastUpdate.getTime(), formatFlags)));
640 public void onStopTransactionRefreshClick(View view) {
641 Logger.debug(TAG, "Cancelling transactions refresh");
642 mainModel.stopTransactionsRetrieval();
643 b.transactionListCancelDownload.setEnabled(false);
645 public void onRetrieveRunningChanged(Boolean running) {
647 b.transactionListCancelDownload.setEnabled(true);
648 ColorStateList csl = Colors.getColorStateList();
649 b.transactionListProgressBar.setIndeterminateTintList(csl);
650 b.transactionListProgressBar.setProgressTintList(csl);
651 b.transactionListProgressBar.setIndeterminate(true);
652 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
653 b.transactionListProgressBar.setProgress(0, false);
656 b.transactionListProgressBar.setProgress(0);
658 b.transactionProgressLayout.setVisibility(View.VISIBLE);
661 b.transactionProgressLayout.setVisibility(View.GONE);
664 public void onRetrieveProgress(@Nullable RetrieveTransactionsTask.Progress progress) {
665 if (progress == null ||
666 progress.getState() == RetrieveTransactionsTask.ProgressState.FINISHED)
668 Logger.debug(TAG, "progress: Done");
669 b.transactionProgressLayout.setVisibility(View.GONE);
671 mainModel.transactionRetrievalDone();
673 String error = (progress == null) ? null : progress.getError();
675 if (error.equals(RetrieveTransactionsTask.Result.ERR_JSON_PARSER_ERROR))
676 error = getResources().getString(R.string.err_json_parser_error);
678 AlertDialog.Builder builder = new AlertDialog.Builder(this);
679 builder.setMessage(error);
680 builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
681 Logger.debug(TAG, "will start profile editor");
682 ProfileDetailActivity.start(this, profile);
693 b.transactionListCancelDownload.setEnabled(true);
694 // ColorStateList csl = Colors.getColorStateList();
695 // progressBar.setIndeterminateTintList(csl);
696 // progressBar.setProgressTintList(csl);
697 // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
698 // progressBar.setProgress(0, false);
700 // progressBar.setProgress(0);
701 b.transactionProgressLayout.setVisibility(View.VISIBLE);
703 if (progress.isIndeterminate() || (progress.getTotal() <= 0)) {
704 b.transactionListProgressBar.setIndeterminate(true);
705 Logger.debug(TAG, "progress: indeterminate");
708 if (b.transactionListProgressBar.isIndeterminate()) {
709 b.transactionListProgressBar.setIndeterminate(false);
712 // String.format(Locale.US, "progress: %d/%d", progress.getProgress(),
715 b.transactionListProgressBar.setMax(progress.getTotal());
716 // for some reason animation doesn't work - no progress is shown (stick at 0)
717 // on lineageOS 14.1 (Nougat, 7.1.2)
718 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
719 b.transactionListProgressBar.setProgress(progress.getProgress(), false);
721 b.transactionListProgressBar.setProgress(progress.getProgress());
724 public void fabShouldShow() {
725 if ((profile != null) && profile.permitPosting() && !b.drawerLayout.isOpen())
726 fabManager.showFab();
729 public Context getContext() {
733 public void showManagedFab() {
737 public void hideManagedFab() {
738 fabManager.hideFab();
740 public static class SectionsPagerAdapter extends FragmentStateAdapter {
742 public SectionsPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
743 super(fragmentActivity);
747 public Fragment createFragment(int position) {
748 Logger.debug(TAG, String.format(Locale.ENGLISH, "Switching to fragment %d", position));
751 // debug(TAG, "Creating account summary fragment");
752 return new AccountSummaryFragment();
754 return new TransactionListFragment();
756 throw new IllegalStateException(
757 String.format("Unexpected fragment index: " + "%d", position));
762 public int getItemCount() {
767 static private class ConverterThread extends Thread {
768 private final List<TransactionWithAccounts> list;
769 private final MainModel model;
770 private final String accFilter;
771 public ConverterThread(@NonNull MainModel model,
772 @NonNull List<TransactionWithAccounts> list, String accFilter) {
775 this.accFilter = accFilter;
779 TransactionAccumulator accumulator = new TransactionAccumulator(accFilter, accFilter);
781 for (TransactionWithAccounts tr : list) {
782 if (isInterrupted()) {
783 Logger.debug(TAG, "ConverterThread bailing out on interrupt");
786 accumulator.put(new LedgerTransaction(tr));
789 if (isInterrupted()) {
790 Logger.debug(TAG, "ConverterThread bailing out on interrupt");
794 Logger.debug(TAG, "ConverterThread publishing results");
796 Misc.onMainThread(() -> accumulator.publishResults(model));