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));
224 b.btnAddTransaction.setOnClickListener(this::fabNewTransactionClicked);
226 b.navNewProfileButton.setOnClickListener(v -> ProfileDetailActivity.start(this, null));
228 b.transactionListCancelDownload.setOnClickListener(this::onStopTransactionRefreshClick);
230 if (mProfileListAdapter == null)
231 mProfileListAdapter = new ProfilesRecyclerViewAdapter();
232 b.navProfileList.setAdapter(mProfileListAdapter);
234 mProfileListAdapter.editingProfiles.observe(this, newValue -> {
236 b.navProfilesStartEdit.setVisibility(View.GONE);
237 b.navProfilesCancelEdit.setVisibility(View.VISIBLE);
238 b.navNewProfileButton.setVisibility(View.VISIBLE);
239 if (b.drawerLayout.isDrawerOpen(GravityCompat.START)) {
240 b.navProfilesStartEdit.startAnimation(
241 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
242 b.navProfilesCancelEdit.startAnimation(
243 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
244 b.navNewProfileButton.startAnimation(
245 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
249 b.navProfilesCancelEdit.setVisibility(View.GONE);
250 b.navProfilesStartEdit.setVisibility(View.VISIBLE);
251 b.navNewProfileButton.setVisibility(View.GONE);
252 if (b.drawerLayout.isDrawerOpen(GravityCompat.START)) {
253 b.navProfilesCancelEdit.startAnimation(
254 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
255 b.navProfilesStartEdit.startAnimation(
256 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
257 b.navNewProfileButton.startAnimation(
258 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
262 mProfileListAdapter.notifyDataSetChanged();
265 fabManager = new FabManager(b.btnAddTransaction);
267 LinearLayoutManager llm = new LinearLayoutManager(this);
269 llm.setOrientation(RecyclerView.VERTICAL);
270 b.navProfileList.setLayoutManager(llm);
272 b.navProfilesStartEdit.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
273 b.navProfilesCancelEdit.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
274 b.navProfileListHeadButtons.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
275 if (drawerListener == null) {
276 drawerListener = new DrawerLayout.SimpleDrawerListener() {
278 public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
279 if (slideOffset > 0.2)
280 fabManager.hideFab();
283 public void onDrawerClosed(View drawerView) {
284 super.onDrawerClosed(drawerView);
285 mProfileListAdapter.setAnimationsEnabled(false);
286 mProfileListAdapter.editingProfiles.setValue(false);
287 Data.drawerOpen.setValue(false);
291 public void onDrawerOpened(View drawerView) {
292 super.onDrawerOpened(drawerView);
293 mProfileListAdapter.setAnimationsEnabled(true);
294 Data.drawerOpen.setValue(true);
295 fabManager.hideFab();
298 b.drawerLayout.addDrawerListener(drawerListener);
301 Data.drawerOpen.observe(this, open -> {
303 b.drawerLayout.open();
305 b.drawerLayout.close();
308 mainModel.getUpdateError()
309 .observe(this, (error) -> {
313 Snackbar.make(b.mainPager, error, Snackbar.LENGTH_INDEFINITE)
315 mainModel.clearUpdateError();
317 Data.locale.observe(this, l -> refreshLastUpdateInfo());
318 Data.lastUpdateDate.observe(this, date -> refreshLastUpdateInfo());
319 Data.lastUpdateTransactionCount.observe(this, date -> refreshLastUpdateInfo());
320 Data.lastUpdateAccountCount.observe(this, date -> refreshLastUpdateInfo());
321 b.navAccountSummary.setOnClickListener(this::onAccountSummaryClicked);
322 b.navLatestTransactions.setOnClickListener(this::onLatestTransactionsClicked);
323 b.navPatterns.setOnClickListener(this::onPatternsClick);
324 b.navBackupRestore.setOnClickListener(this::onBackupRestoreClick);
326 private void onBackupRestoreClick(View view) {
327 Intent intent = new Intent(this, BackupsActivity.class);
328 startActivity(intent);
330 private void onPatternsClick(View view) {
331 Intent intent = new Intent(this, TemplatesActivity.class);
332 startActivity(intent);
334 private void scheduleDataRetrievalIfStale(long lastUpdate) {
335 long now = new Date().getTime();
336 if ((lastUpdate == 0) || (now > (lastUpdate + (24 * 3600 * 1000)))) {
338 Logger.debug("db::", "WEB data never fetched. scheduling a fetch");
340 Logger.debug("db", String.format(Locale.ENGLISH,
341 "WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
342 lastUpdate / 1000f, now / 1000f));
344 mainModel.scheduleTransactionListRetrieval();
347 private void createShortcuts(List<Profile> list) {
348 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
351 ShortcutManager sm = getSystemService(ShortcutManager.class);
352 List<ShortcutInfo> shortcuts = new ArrayList<>();
354 for (Profile p : list) {
355 if (shortcuts.size() >= sm.getMaxShortcutCountPerActivity())
358 if (!p.permitPosting())
361 final ShortcutInfo.Builder builder =
362 new ShortcutInfo.Builder(this, "new_transaction_" + p.getId());
363 ShortcutInfo si = builder.setShortLabel(p.getName())
364 .setIcon(Icon.createWithResource(this,
365 R.drawable.thick_plus_icon))
366 .setIntent(new Intent(Intent.ACTION_VIEW, null, this,
367 NewTransactionActivity.class).putExtra(
368 ProfileThemedActivity.PARAM_PROFILE_ID, p.getId())
370 ProfileThemedActivity.PARAM_THEME,
377 sm.setDynamicShortcuts(shortcuts);
379 private void onProfileListChanged(List<Profile> newList) {
380 if ((newList == null) || newList.isEmpty()) {
381 b.noProfilesLayout.setVisibility(View.VISIBLE);
382 b.mainAppLayout.setVisibility(View.GONE);
386 b.mainAppLayout.setVisibility(View.VISIBLE);
387 b.noProfilesLayout.setVisibility(View.GONE);
389 b.navProfileList.setMinimumHeight(
390 (int) (getResources().getDimension(R.dimen.thumb_row_height) * newList.size()));
392 Logger.debug("profiles", "profile list changed");
393 mProfileListAdapter.setProfileList(newList);
395 createShortcuts(newList);
397 final Profile currentProfile = Data.getProfile();
398 Profile replacementProfile = null;
399 if (currentProfile != null) {
400 for (Profile p : newList) {
401 if (p.getId() == currentProfile.getId()) {
402 replacementProfile = p;
408 if (replacementProfile == null) {
409 Logger.debug(TAG, "Switching profile because the current is no longer available");
410 Data.setCurrentProfile(newList.get(0));
413 Data.setCurrentProfile(replacementProfile);
417 * called when the current profile has changed
419 private void onProfileChanged(@Nullable Profile newProfile) {
420 if (this.profile != null) {
421 if (this.profile.equals(newProfile))
425 boolean haveProfile = newProfile != null;
428 setTitle(newProfile.getName());
430 setTitle(R.string.app_name);
432 int newProfileTheme = haveProfile ? newProfile.getTheme() : Colors.DEFAULT_HUE_DEG;
433 if (newProfileTheme != Colors.profileThemeId) {
434 Logger.debug("profiles",
435 String.format(Locale.ENGLISH, "profile theme %d → %d", Colors.profileThemeId,
437 Colors.profileThemeId = newProfileTheme;
438 profileThemeChanged();
439 // profileThemeChanged would restart the activity, so no need to reload the
444 final boolean sameProfileId = (newProfile != null) && (this.profile != null) &&
445 this.profile.getId() == newProfile.getId();
447 this.profile = newProfile;
449 b.noProfilesLayout.setVisibility(haveProfile ? View.GONE : View.VISIBLE);
450 b.pagerLayout.setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
452 mProfileListAdapter.notifyDataSetChanged();
455 if (newProfile.permitPosting()) {
456 b.toolbar.setSubtitle(null);
457 b.btnAddTransaction.show();
460 b.toolbar.setSubtitle(R.string.profile_subtitle_read_only);
461 b.btnAddTransaction.hide();
465 b.toolbar.setSubtitle(null);
466 b.btnAddTransaction.hide();
469 updateLastUpdateTextFromDB();
472 Logger.debug(TAG, String.format(Locale.ROOT, "Short-cut profile 'changed' to %d",
473 newProfile.getId()));
477 mainModel.getAccountFilter()
478 .observe(this, this::onAccountFilterChanged);
480 mainModel.stopTransactionsRetrieval();
481 mainModel.clearTransactions();
483 private void onAccountFilterChanged(String accFilter) {
484 Logger.debug(TAG, "account filter changed, reloading transactions");
485 // mainModel.scheduleTransactionListReload();
486 LiveData<List<TransactionWithAccounts>> transactions =
487 new MutableLiveData<>(new ArrayList<>());
488 if (profile != null) {
489 if (accFilter == null || accFilter.isEmpty()) {
490 transactions = DB.get()
492 .getAllWithAccounts(profile.getId());
495 transactions = DB.get()
497 .getAllWithAccountsFiltered(profile.getId(), accFilter);
501 transactions.observe(this, list -> {
503 String.format(Locale.ROOT, "got transaction list from DB (%d transactions)",
506 if (converterThread != null)
507 converterThread.interrupt();
508 converterThread = new ConverterThread(mainModel, list, accFilter);
509 converterThread.start();
512 private void profileThemeChanged() {
513 // un-hook all observed LiveData
514 Data.removeProfileObservers(this);
515 Data.profiles.removeObservers(this);
516 Data.lastUpdateTransactionCount.removeObservers(this);
517 Data.lastUpdateAccountCount.removeObservers(this);
518 Data.lastUpdateDate.removeObservers(this);
520 Logger.debug(TAG, "profileThemeChanged(): recreating activity");
523 public void fabNewTransactionClicked(View view) {
524 Intent intent = new Intent(this, NewTransactionActivity.class);
525 intent.putExtra(ProfileThemedActivity.PARAM_PROFILE_ID, profile.getId());
526 intent.putExtra(ProfileThemedActivity.PARAM_THEME, profile.getTheme());
527 startActivity(intent);
528 overridePendingTransition(R.anim.slide_in_up, R.anim.dummy);
530 public void markDrawerItemCurrent(int id) {
531 TextView item = b.drawerLayout.findViewById(id);
532 item.setBackgroundColor(Colors.tableRowDarkBG);
534 for (int i = 0; i < b.navActions.getChildCount(); i++) {
535 View view = b.navActions.getChildAt(i);
536 if (view.getId() != id) {
537 view.setBackgroundColor(Color.TRANSPARENT);
541 public void onAccountSummaryClicked(View view) {
542 b.drawerLayout.closeDrawers();
544 showAccountSummaryFragment();
546 private void showAccountSummaryFragment() {
547 b.mainPager.setCurrentItem(0, true);
548 mainModel.getAccountFilter()
551 public void onLatestTransactionsClicked(View view) {
552 b.drawerLayout.closeDrawers();
554 showTransactionsFragment(null);
556 public void showTransactionsFragment(String accName) {
557 mainModel.getAccountFilter()
559 b.mainPager.setCurrentItem(1, true);
561 public void showAccountTransactions(String accountName) {
562 mBackMeansToAccountList = true;
563 showTransactionsFragment(accountName);
566 public void onBackPressed() {
567 if (b.drawerLayout.isDrawerOpen(GravityCompat.START)) {
568 b.drawerLayout.closeDrawer(GravityCompat.START);
571 if (mBackMeansToAccountList && (b.mainPager.getCurrentItem() == 1)) {
572 mainModel.getAccountFilter()
574 showAccountSummaryFragment();
575 mBackMeansToAccountList = false;
578 Logger.debug(TAG, String.format(Locale.ENGLISH, "manager stack: %d",
579 getSupportFragmentManager().getBackStackEntryCount()));
581 super.onBackPressed();
585 public void updateLastUpdateTextFromDB() {
591 .load(profile.getId(), Option.OPT_LAST_SCRAPE)
592 .observe(this, opt -> {
596 lastUpdate = Long.parseLong(opt.getValue());
598 catch (NumberFormatException ex) {
599 Logger.debug(TAG, String.format("Error parsing '%s' as long", opt.getValue()),
604 if (lastUpdate == 0) {
605 Data.lastUpdateDate.postValue(null);
608 Data.lastUpdateDate.postValue(new Date(lastUpdate));
611 scheduleDataRetrievalIfStale(lastUpdate);
614 private void refreshLastUpdateInfo() {
615 final int formatFlags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR |
616 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NUMERIC_DATE;
617 String templateForTransactions =
618 getResources().getString(R.string.transaction_count_summary);
619 String templateForAccounts = getResources().getString(R.string.account_count_summary);
620 Integer accountCount = Data.lastUpdateAccountCount.getValue();
621 Integer transactionCount = Data.lastUpdateTransactionCount.getValue();
622 Date lastUpdate = Data.lastUpdateDate.getValue();
623 if (lastUpdate == null) {
624 Data.lastTransactionsUpdateText.setValue("----");
625 Data.lastAccountsUpdateText.setValue("----");
628 Data.lastTransactionsUpdateText.setValue(
629 String.format(Objects.requireNonNull(Data.locale.getValue()),
630 templateForTransactions,
631 transactionCount == null ? 0 : transactionCount,
632 DateUtils.formatDateTime(this, lastUpdate.getTime(), formatFlags)));
633 Data.lastAccountsUpdateText.setValue(
634 String.format(Objects.requireNonNull(Data.locale.getValue()),
635 templateForAccounts, accountCount == null ? 0 : accountCount,
636 DateUtils.formatDateTime(this, lastUpdate.getTime(), formatFlags)));
639 public void onStopTransactionRefreshClick(View view) {
640 Logger.debug(TAG, "Cancelling transactions refresh");
641 mainModel.stopTransactionsRetrieval();
642 b.transactionListCancelDownload.setEnabled(false);
644 public void onRetrieveRunningChanged(Boolean running) {
646 b.transactionListCancelDownload.setEnabled(true);
647 ColorStateList csl = Colors.getColorStateList();
648 b.transactionListProgressBar.setIndeterminateTintList(csl);
649 b.transactionListProgressBar.setProgressTintList(csl);
650 b.transactionListProgressBar.setIndeterminate(true);
651 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
652 b.transactionListProgressBar.setProgress(0, false);
655 b.transactionListProgressBar.setProgress(0);
657 b.transactionProgressLayout.setVisibility(View.VISIBLE);
660 b.transactionProgressLayout.setVisibility(View.GONE);
663 public void onRetrieveProgress(@Nullable RetrieveTransactionsTask.Progress progress) {
664 if (progress == null ||
665 progress.getState() == RetrieveTransactionsTask.ProgressState.FINISHED)
667 Logger.debug(TAG, "progress: Done");
668 b.transactionProgressLayout.setVisibility(View.GONE);
670 mainModel.transactionRetrievalDone();
672 String error = (progress == null) ? null : progress.getError();
674 if (error.equals(RetrieveTransactionsTask.Result.ERR_JSON_PARSER_ERROR))
675 error = getResources().getString(R.string.err_json_parser_error);
677 AlertDialog.Builder builder = new AlertDialog.Builder(this);
678 builder.setMessage(error);
679 builder.setPositiveButton(R.string.btn_profile_options, (dialog, which) -> {
680 Logger.debug(TAG, "will start profile editor");
681 ProfileDetailActivity.start(this, profile);
692 b.transactionListCancelDownload.setEnabled(true);
693 // ColorStateList csl = Colors.getColorStateList();
694 // progressBar.setIndeterminateTintList(csl);
695 // progressBar.setProgressTintList(csl);
696 // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
697 // progressBar.setProgress(0, false);
699 // progressBar.setProgress(0);
700 b.transactionProgressLayout.setVisibility(View.VISIBLE);
702 if (progress.isIndeterminate() || (progress.getTotal() <= 0)) {
703 b.transactionListProgressBar.setIndeterminate(true);
704 Logger.debug(TAG, "progress: indeterminate");
707 if (b.transactionListProgressBar.isIndeterminate()) {
708 b.transactionListProgressBar.setIndeterminate(false);
711 // String.format(Locale.US, "progress: %d/%d", progress.getProgress(),
714 b.transactionListProgressBar.setMax(progress.getTotal());
715 // for some reason animation doesn't work - no progress is shown (stick at 0)
716 // on lineageOS 14.1 (Nougat, 7.1.2)
717 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
718 b.transactionListProgressBar.setProgress(progress.getProgress(), false);
720 b.transactionListProgressBar.setProgress(progress.getProgress());
723 public void fabShouldShow() {
724 if ((profile != null) && profile.permitPosting() && !b.drawerLayout.isOpen())
725 fabManager.showFab();
728 public Context getContext() {
732 public void showManagedFab() {
736 public void hideManagedFab() {
737 fabManager.hideFab();
739 public static class SectionsPagerAdapter extends FragmentStateAdapter {
741 public SectionsPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
742 super(fragmentActivity);
746 public Fragment createFragment(int position) {
747 Logger.debug(TAG, String.format(Locale.ENGLISH, "Switching to fragment %d", position));
750 // debug(TAG, "Creating account summary fragment");
751 return new AccountSummaryFragment();
753 return new TransactionListFragment();
755 throw new IllegalStateException(
756 String.format("Unexpected fragment index: " + "%d", position));
761 public int getItemCount() {
766 static private class ConverterThread extends Thread {
767 private final List<TransactionWithAccounts> list;
768 private final MainModel model;
769 private final String accFilter;
770 public ConverterThread(@NonNull MainModel model,
771 @NonNull List<TransactionWithAccounts> list, String accFilter) {
774 this.accFilter = accFilter;
778 TransactionAccumulator accumulator = new TransactionAccumulator(accFilter, accFilter);
780 for (TransactionWithAccounts tr : list) {
781 if (isInterrupted()) {
782 Logger.debug(TAG, "ConverterThread bailing out on interrupt");
785 accumulator.put(new LedgerTransaction(tr));
788 if (isInterrupted()) {
789 Logger.debug(TAG, "ConverterThread bailing out on interrupt");
793 Logger.debug(TAG, "ConverterThread publishing results");
795 Misc.onMainThread(() -> accumulator.publishResults(model));