]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
3a210ef07720d04b9396e5b36d37ca504952e987
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
1 /*
2  * Copyright © 2020 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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
19
20 import android.content.Intent;
21 import android.content.SharedPreferences;
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.AsyncTask;
29 import android.os.Build;
30 import android.os.Bundle;
31 import android.util.Log;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.view.animation.AnimationUtils;
35 import android.widget.LinearLayout;
36 import android.widget.ProgressBar;
37 import android.widget.TextView;
38
39 import androidx.annotation.NonNull;
40 import androidx.appcompat.app.ActionBarDrawerToggle;
41 import androidx.appcompat.widget.Toolbar;
42 import androidx.core.view.GravityCompat;
43 import androidx.drawerlayout.widget.DrawerLayout;
44 import androidx.fragment.app.Fragment;
45 import androidx.fragment.app.FragmentManager;
46 import androidx.fragment.app.FragmentPagerAdapter;
47 import androidx.recyclerview.widget.LinearLayoutManager;
48 import androidx.recyclerview.widget.RecyclerView;
49 import androidx.viewpager.widget.ViewPager;
50
51 import com.google.android.material.floatingactionbutton.FloatingActionButton;
52 import com.google.android.material.snackbar.Snackbar;
53
54 import net.ktnx.mobileledger.R;
55 import net.ktnx.mobileledger.async.RefreshDescriptionsTask;
56 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
57 import net.ktnx.mobileledger.model.Data;
58 import net.ktnx.mobileledger.model.LedgerAccount;
59 import net.ktnx.mobileledger.model.MobileLedgerProfile;
60 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
61 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
62 import net.ktnx.mobileledger.ui.profiles.ProfilesRecyclerViewAdapter;
63 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
64 import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel;
65 import net.ktnx.mobileledger.utils.Colors;
66 import net.ktnx.mobileledger.utils.GetOptCallback;
67 import net.ktnx.mobileledger.utils.Logger;
68 import net.ktnx.mobileledger.utils.MLDB;
69
70 import org.jetbrains.annotations.NotNull;
71
72 import java.text.DateFormat;
73 import java.util.ArrayList;
74 import java.util.Date;
75 import java.util.List;
76 import java.util.Locale;
77
78 import static net.ktnx.mobileledger.utils.Logger.debug;
79
80 /*
81  * TODO: reports
82  *  */
83
84 public class MainActivity extends ProfileThemedActivity {
85     public static final String STATE_CURRENT_PAGE = "current_page";
86     public static final String BUNDLE_SAVED_STATE = "bundle_savedState";
87     public static final String STATE_ACC_FILTER = "account_filter";
88     private static final String PREF_THEME_ID = "themeId";
89     DrawerLayout drawer;
90     private View profileListHeadMore, profileListHeadCancel, profileListHeadAddProfile;
91     private View bTransactionListCancelDownload;
92     private SectionsPagerAdapter mSectionsPagerAdapter;
93     private ViewPager mViewPager;
94     private FloatingActionButton fab;
95     private ProfilesRecyclerViewAdapter mProfileListAdapter;
96     private int mCurrentPage;
97     private boolean mBackMeansToAccountList = false;
98     private Toolbar mToolbar;
99     private DrawerLayout.SimpleDrawerListener drawerListener;
100     private ActionBarDrawerToggle barDrawerToggle;
101     private ViewPager.SimpleOnPageChangeListener pageChangeListener;
102     private MobileLedgerProfile profile;
103     @Override
104     protected void onStart() {
105         super.onStart();
106
107         Logger.debug("MainActivity", "onStart()");
108
109         mViewPager.setCurrentItem(mCurrentPage, false);
110     }
111     @Override
112     protected void onSaveInstanceState(@NotNull Bundle outState) {
113         super.onSaveInstanceState(outState);
114         outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem());
115         if (Data.accountFilter.getValue() != null)
116             outState.putString(STATE_ACC_FILTER, Data.accountFilter.getValue());
117     }
118     @Override
119     protected void onDestroy() {
120         mSectionsPagerAdapter = null;
121         RecyclerView root = findViewById(R.id.nav_profile_list);
122         if (root != null)
123             root.setAdapter(null);
124         if (drawer != null)
125             drawer.removeDrawerListener(drawerListener);
126         drawerListener = null;
127         if (drawer != null)
128             drawer.removeDrawerListener(barDrawerToggle);
129         barDrawerToggle = null;
130         if (mViewPager != null)
131             mViewPager.removeOnPageChangeListener(pageChangeListener);
132         pageChangeListener = null;
133         super.onDestroy();
134     }
135     @Override
136     protected void setupProfileColors() {
137         SharedPreferences prefs = getPreferences(MODE_PRIVATE);
138         int profileColor = prefs.getInt(PREF_THEME_ID, -2);
139         if (profileColor == -2)
140             profileColor = Data.retrieveCurrentThemeIdFromDb();
141         Colors.setupTheme(this, profileColor);
142         Colors.profileThemeId = profileColor;
143         storeThemeIdInPrefs(profileColor);
144     }
145     @Override
146     protected void onResume() {
147         super.onResume();
148         fabShouldShow();
149     }
150     @Override
151     protected void onCreate(Bundle savedInstanceState) {
152         debug("MainActivity", "onCreate()/entry");
153         super.onCreate(savedInstanceState);
154         debug("MainActivity", "onCreate()/after super");
155         setContentView(R.layout.activity_main);
156
157         fab = findViewById(R.id.btn_add_transaction);
158         profileListHeadMore = findViewById(R.id.nav_profiles_start_edit);
159         profileListHeadCancel = findViewById(R.id.nav_profiles_cancel_edit);
160         LinearLayout profileListHeadMoreAndCancel =
161                 findViewById(R.id.nav_profile_list_head_buttons);
162         profileListHeadAddProfile = findViewById(R.id.nav_new_profile_button);
163         drawer = findViewById(R.id.drawer_layout);
164         bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
165         mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
166         mViewPager = findViewById(R.id.root_frame);
167
168         Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
169         if (extra != null && savedInstanceState == null)
170             savedInstanceState = extra;
171
172
173         mToolbar = findViewById(R.id.toolbar);
174         setSupportActionBar(mToolbar);
175
176         Data.profile.observe(this, this::onProfileChanged);
177
178         Data.profiles.observe(this, this::onProfileListChanged);
179
180         if (barDrawerToggle == null) {
181             barDrawerToggle = new ActionBarDrawerToggle(this, drawer, mToolbar,
182                     R.string.navigation_drawer_open, R.string.navigation_drawer_close);
183             drawer.addDrawerListener(barDrawerToggle);
184         }
185         barDrawerToggle.syncState();
186
187         try {
188             PackageInfo pi = getApplicationContext().getPackageManager()
189                                                     .getPackageInfo(getPackageName(), 0);
190             ((TextView) findViewById(R.id.nav_upper).findViewById(
191                     R.id.drawer_version_text)).setText(pi.versionName);
192             ((TextView) findViewById(R.id.no_profiles_layout).findViewById(
193                     R.id.drawer_version_text)).setText(pi.versionName);
194         }
195         catch (Exception e) {
196             e.printStackTrace();
197         }
198
199         markDrawerItemCurrent(R.id.nav_account_summary);
200
201         mViewPager.setAdapter(mSectionsPagerAdapter);
202
203         if (pageChangeListener == null) {
204             pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
205                 @Override
206                 public void onPageSelected(int position) {
207                     mCurrentPage = position;
208                     switch (position) {
209                         case 0:
210                             markDrawerItemCurrent(R.id.nav_account_summary);
211                             break;
212                         case 1:
213                             markDrawerItemCurrent(R.id.nav_latest_transactions);
214                             break;
215                         default:
216                             Log.e("MainActivity",
217                                     String.format("Unexpected page index %d", position));
218                     }
219
220                     super.onPageSelected(position);
221                 }
222             };
223             mViewPager.addOnPageChangeListener(pageChangeListener);
224         }
225
226         mCurrentPage = 0;
227         if (savedInstanceState != null) {
228             int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
229             if (currentPage != -1) {
230                 mCurrentPage = currentPage;
231             }
232             Data.accountFilter.setValue(savedInstanceState.getString(STATE_ACC_FILTER, null));
233         }
234
235         Data.lastUpdateDate.observe(this, this::updateLastUpdateDisplay);
236
237         findViewById(R.id.btn_no_profiles_add).setOnClickListener(
238                 v -> startEditProfileActivity(null));
239
240         findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
241
242         findViewById(R.id.nav_new_profile_button).setOnClickListener(
243                 v -> startEditProfileActivity(null));
244
245         RecyclerView root = findViewById(R.id.nav_profile_list);
246         if (root == null)
247             throw new RuntimeException("Can't get hold on the transaction value view");
248
249         if (mProfileListAdapter == null)
250             mProfileListAdapter = new ProfilesRecyclerViewAdapter();
251         root.setAdapter(mProfileListAdapter);
252
253         mProfileListAdapter.editingProfiles.observe(this, newValue -> {
254             if (newValue) {
255                 profileListHeadMore.setVisibility(View.GONE);
256                 profileListHeadCancel.setVisibility(View.VISIBLE);
257                 profileListHeadAddProfile.setVisibility(View.VISIBLE);
258                 if (drawer.isDrawerOpen(GravityCompat.START)) {
259                     profileListHeadMore.startAnimation(
260                             AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
261                     profileListHeadCancel.startAnimation(
262                             AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
263                     profileListHeadAddProfile.startAnimation(
264                             AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
265                 }
266             }
267             else {
268                 profileListHeadCancel.setVisibility(View.GONE);
269                 profileListHeadMore.setVisibility(View.VISIBLE);
270                 profileListHeadAddProfile.setVisibility(View.GONE);
271                 if (drawer.isDrawerOpen(GravityCompat.START)) {
272                     profileListHeadCancel.startAnimation(
273                             AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
274                     profileListHeadMore.startAnimation(
275                             AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
276                     profileListHeadAddProfile.startAnimation(
277                             AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
278                 }
279             }
280
281             mProfileListAdapter.notifyDataSetChanged();
282         });
283
284         LinearLayoutManager llm = new LinearLayoutManager(this);
285
286         llm.setOrientation(RecyclerView.VERTICAL);
287         root.setLayoutManager(llm);
288
289         profileListHeadMore.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
290         profileListHeadCancel.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
291         profileListHeadMoreAndCancel.setOnClickListener(
292                 (v) -> mProfileListAdapter.flipEditingProfiles());
293         if (drawerListener == null) {
294             drawerListener = new DrawerLayout.SimpleDrawerListener() {
295                 @Override
296                 public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
297                     if (slideOffset > 0.2)
298                         fabHide();
299                 }
300                 @Override
301                 public void onDrawerClosed(View drawerView) {
302                     super.onDrawerClosed(drawerView);
303                     mProfileListAdapter.setAnimationsEnabled(false);
304                     mProfileListAdapter.editingProfiles.setValue(false);
305                     Data.drawerOpen.setValue(false);
306                     fabShouldShow();
307                 }
308                 @Override
309                 public void onDrawerOpened(View drawerView) {
310                     super.onDrawerOpened(drawerView);
311                     mProfileListAdapter.setAnimationsEnabled(true);
312                     Data.drawerOpen.setValue(true);
313                     fabHide();
314                 }
315             };
316             drawer.addDrawerListener(drawerListener);
317         }
318
319         Data.drawerOpen.observe(this, open -> {
320             if (open)
321                 drawer.open();
322             else
323                 drawer.close();
324         });
325     }
326     private void scheduleDataRetrievalIfStale(Date lastUpdate) {
327         long now = new Date().getTime();
328         if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
329             if (lastUpdate == null)
330                 debug("db::", "WEB data never fetched. scheduling a fetch");
331             else
332                 debug("db", String.format(Locale.ENGLISH,
333                         "WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
334                         lastUpdate.getTime() / 1000f, now / 1000f));
335
336             Data.scheduleTransactionListRetrieval(this);
337         }
338     }
339     private void createShortcuts(List<MobileLedgerProfile> list) {
340         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
341             return;
342
343         ShortcutManager sm = getSystemService(ShortcutManager.class);
344         List<ShortcutInfo> shortcuts = new ArrayList<>();
345         int i = 0;
346         for (MobileLedgerProfile p : list) {
347             if (shortcuts.size() >= sm.getMaxShortcutCountPerActivity())
348                 break;
349
350             if (!p.isPostingPermitted())
351                 continue;
352
353             final ShortcutInfo.Builder builder =
354                     new ShortcutInfo.Builder(this, "new_transaction_" + p.getUuid());
355             ShortcutInfo si = builder.setShortLabel(p.getName())
356                                      .setIcon(Icon.createWithResource(this,
357                                              R.drawable.thick_plus_icon))
358                                      .setIntent(new Intent(Intent.ACTION_VIEW, null, this,
359                                              NewTransactionActivity.class).putExtra("profile_uuid",
360                                              p.getUuid()))
361                                      .setRank(i)
362                                      .build();
363             shortcuts.add(si);
364             i++;
365         }
366         sm.setDynamicShortcuts(shortcuts);
367     }
368     private void onProfileListChanged(List<MobileLedgerProfile> newList) {
369         if ((newList == null) || newList.isEmpty()) {
370             findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE);
371             findViewById(R.id.main_app_layout).setVisibility(View.GONE);
372             return;
373         }
374
375         findViewById(R.id.main_app_layout).setVisibility(View.VISIBLE);
376         findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
377
378         findViewById(R.id.nav_profile_list).setMinimumHeight(
379                 (int) (getResources().getDimension(R.dimen.thumb_row_height) * newList.size()));
380
381         debug("profiles", "profile list changed");
382         mProfileListAdapter.notifyDataSetChanged();
383
384         createShortcuts(newList);
385     }
386     /**
387      * called when the current profile has changed
388      */
389     private void onProfileChanged(MobileLedgerProfile profile) {
390         boolean haveProfile = profile != null;
391
392         if (haveProfile)
393             setTitle(profile.getName());
394         else
395             setTitle(R.string.app_name);
396
397         if (this.profile != null)
398             this.profile.getAccounts()
399                         .removeObservers(this);
400
401         this.profile = profile;
402
403         int newProfileTheme = haveProfile ? profile.getThemeHue() : -1;
404         if (newProfileTheme != Colors.profileThemeId) {
405             debug("profiles",
406                     String.format(Locale.ENGLISH, "profile theme %d → %d", Colors.profileThemeId,
407                             newProfileTheme));
408             Colors.profileThemeId = newProfileTheme;
409             profileThemeChanged();
410             // profileThemeChanged would restart the activity, so no need to reload the
411             // data sets below
412             return;
413         }
414
415         findViewById(R.id.no_profiles_layout).setVisibility(haveProfile ? View.GONE : View.VISIBLE);
416         findViewById(R.id.pager_layout).setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
417
418         mProfileListAdapter.notifyDataSetChanged();
419
420         Data.transactions.clear();
421         debug("transactions", "requesting list reload");
422         TransactionListViewModel.scheduleTransactionListReload();
423
424         if (haveProfile) {
425             profile.scheduleAccountListReload();
426
427             if (profile.isPostingPermitted()) {
428                 mToolbar.setSubtitle(null);
429                 fab.show();
430             }
431             else {
432                 mToolbar.setSubtitle(R.string.profile_subtitle_read_only);
433                 fab.hide();
434             }
435         }
436         else {
437             mToolbar.setSubtitle(null);
438             fab.hide();
439         }
440
441         updateLastUpdateTextFromDB();
442     }
443     private void updateLastUpdateDisplay(Date newValue) {
444         LinearLayout l = findViewById(R.id.transactions_last_update_layout);
445         TextView v = findViewById(R.id.transactions_last_update);
446         if (newValue == null) {
447             l.setVisibility(View.INVISIBLE);
448             debug("main", "no last update date :(");
449         }
450         else {
451             final String text = DateFormat.getDateTimeInstance()
452                                           .format(newValue);
453             v.setText(text);
454             l.setVisibility(View.VISIBLE);
455             debug("main", String.format("Date formatted: %s", text));
456         }
457
458         scheduleDataRetrievalIfStale(newValue);
459     }
460     private void profileThemeChanged() {
461         storeThemeIdInPrefs(profile.getThemeHue());
462
463         // un-hook all observed LiveData
464         Data.profile.removeObservers(this);
465         Data.profiles.removeObservers(this);
466         Data.lastUpdateDate.removeObservers(this);
467
468         recreate();
469     }
470     private void storeThemeIdInPrefs(int themeId) {
471         // store the new theme id in the preferences
472         SharedPreferences prefs = getPreferences(MODE_PRIVATE);
473         SharedPreferences.Editor e = prefs.edit();
474         e.putInt(PREF_THEME_ID, themeId);
475         e.apply();
476     }
477     public void startEditProfileActivity(MobileLedgerProfile profile) {
478         Intent intent = new Intent(this, ProfileDetailActivity.class);
479         Bundle args = new Bundle();
480         if (profile != null) {
481             int index = Data.getProfileIndex(profile);
482             if (index != -1)
483                 intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
484         }
485         intent.putExtras(args);
486         startActivity(intent, args);
487     }
488     private void setupProfile() {
489         MLDB.getOption(MLDB.OPT_PROFILE_UUID, null, new GetOptCallback() {
490             @Override
491             protected void onResult(String profileUUID) {
492                 MobileLedgerProfile startupProfile;
493
494                 startupProfile = Data.getProfile(profileUUID);
495                 Data.setCurrentProfile(startupProfile);
496             }
497         });
498     }
499     public void fabNewTransactionClicked(View view) {
500         Intent intent = new Intent(this, NewTransactionActivity.class);
501         startActivity(intent);
502         overridePendingTransition(R.anim.slide_in_up, R.anim.dummy);
503     }
504     public void markDrawerItemCurrent(int id) {
505         TextView item = drawer.findViewById(id);
506         item.setBackgroundColor(Colors.tableRowDarkBG);
507
508         LinearLayout actions = drawer.findViewById(R.id.nav_actions);
509         for (int i = 0; i < actions.getChildCount(); i++) {
510             View view = actions.getChildAt(i);
511             if (view.getId() != id) {
512                 view.setBackgroundColor(Color.TRANSPARENT);
513             }
514         }
515     }
516     public void onAccountSummaryClicked(View view) {
517         drawer.closeDrawers();
518
519         showAccountSummaryFragment();
520     }
521     private void showAccountSummaryFragment() {
522         mViewPager.setCurrentItem(0, true);
523         Data.accountFilter.setValue(null);
524     }
525     public void onLatestTransactionsClicked(View view) {
526         drawer.closeDrawers();
527
528         showTransactionsFragment((String) null);
529     }
530     public void showTransactionsFragment(String accName) {
531         Data.accountFilter.setValue(accName);
532         mViewPager.setCurrentItem(1, true);
533     }
534     public void showAccountTransactions(String accountName) {
535         mBackMeansToAccountList = true;
536         showTransactionsFragment(accountName);
537     }
538     @Override
539     public void onBackPressed() {
540         DrawerLayout drawer = findViewById(R.id.drawer_layout);
541         if (drawer.isDrawerOpen(GravityCompat.START)) {
542             drawer.closeDrawer(GravityCompat.START);
543         }
544         else {
545             if (mBackMeansToAccountList && (mViewPager.getCurrentItem() == 1)) {
546                 Data.accountFilter.setValue(null);
547                 showAccountSummaryFragment();
548                 mBackMeansToAccountList = false;
549             }
550             else {
551                 debug("fragments", String.format(Locale.ENGLISH, "manager stack: %d",
552                         getSupportFragmentManager().getBackStackEntryCount()));
553
554                 super.onBackPressed();
555             }
556         }
557     }
558     public void updateLastUpdateTextFromDB() {
559         if (profile == null)
560             return;
561
562         long last_update = profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L);
563
564         debug("transactions", String.format(Locale.ENGLISH, "Last update = %d", last_update));
565         if (last_update == 0) {
566             Data.lastUpdateDate.postValue(null);
567         }
568         else {
569             Data.lastUpdateDate.postValue(new Date(last_update));
570         }
571     }
572     public void onStopTransactionRefreshClick(View view) {
573         debug("interactive", "Cancelling transactions refresh");
574         Data.stopTransactionsRetrieval();
575         bTransactionListCancelDownload.setEnabled(false);
576     }
577     public void onRetrieveDone(String error) {
578         Data.transactionRetrievalDone();
579         findViewById(R.id.transaction_progress_layout).setVisibility(View.GONE);
580
581         if (error == null) {
582             updateLastUpdateTextFromDB();
583
584             new RefreshDescriptionsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
585             TransactionListViewModel.scheduleTransactionListReload();
586         }
587         else
588             Snackbar.make(mViewPager, error, Snackbar.LENGTH_LONG)
589                     .show();
590     }
591     public void onRetrieveStart() {
592         ProgressBar progressBar = findViewById(R.id.transaction_list_progress_bar);
593         bTransactionListCancelDownload.setEnabled(true);
594         ColorStateList csl = Colors.getColorStateList();
595         progressBar.setIndeterminateTintList(csl);
596         progressBar.setProgressTintList(csl);
597         progressBar.setIndeterminate(true);
598         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
599             progressBar.setProgress(0, false);
600         else
601             progressBar.setProgress(0);
602         findViewById(R.id.transaction_progress_layout).setVisibility(View.VISIBLE);
603     }
604     public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
605         ProgressBar progressBar = findViewById(R.id.transaction_list_progress_bar);
606         if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
607             (progress.getTotal() == 0))
608         {
609             progressBar.setIndeterminate(true);
610         }
611         else {
612             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
613                 progressBar.setMin(0);
614             }
615             progressBar.setMax(progress.getTotal());
616             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
617                 progressBar.setProgress(progress.getProgress(), true);
618             }
619             else
620                 progressBar.setProgress(progress.getProgress());
621             progressBar.setIndeterminate(false);
622         }
623     }
624     public void fabShouldShow() {
625         if ((profile != null) && profile.isPostingPermitted() && !drawer.isOpen())
626             fab.show();
627         else
628             fabHide();
629     }
630     public void fabHide() {
631         fab.hide();
632     }
633     public void onAccountSummaryRowViewClicked(View view) {
634         ViewGroup row;
635         switch (view.getId()) {
636             case R.id.account_expander:
637                 row = (ViewGroup) view.getParent()
638                                       .getParent()
639                                       .getParent();
640                 break;
641             case R.id.account_expander_container:
642             case R.id.account_row_acc_name:
643                 row = (ViewGroup) view.getParent()
644                                       .getParent();
645                 break;
646             default:
647                 row = (ViewGroup) view.getParent();
648                 break;
649         }
650
651         LedgerAccount acc = (LedgerAccount) row.getTag();
652         switch (view.getId()) {
653             case R.id.account_row_acc_name:
654             case R.id.account_expander:
655             case R.id.account_expander_container:
656                 break;
657             case R.id.account_row_acc_amounts:
658                 break;
659         }
660     }
661
662     public class SectionsPagerAdapter extends FragmentPagerAdapter {
663
664         SectionsPagerAdapter(FragmentManager fm) {
665             super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
666         }
667
668         @NotNull
669         @Override
670         public Fragment getItem(int position) {
671             debug("main", String.format(Locale.ENGLISH, "Switching to fragment %d", position));
672             switch (position) {
673                 case 0:
674 //                    debug("flow", "Creating account summary fragment");
675                     return new AccountSummaryFragment();
676                 case 1:
677                     return new TransactionListFragment();
678                 default:
679                     throw new IllegalStateException(
680                             String.format("Unexpected fragment index: " + "%d", position));
681             }
682         }
683
684         @Override
685         public int getCount() {
686             return 2;
687         }
688     }
689 }