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