]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
1 /*
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.
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.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;
35
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;
51
52 import com.google.android.material.snackbar.Snackbar;
53
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;
76
77 import org.jetbrains.annotations.NotNull;
78
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;
84
85 /*
86  * TODO: reports
87  *  */
88
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;
109     @Override
110     protected void onStart() {
111         super.onStart();
112
113         Logger.debug(TAG, "onStart()");
114
115         b.mainPager.setCurrentItem(mCurrentPage, false);
116     }
117     @Override
118     protected void onSaveInstanceState(@NotNull Bundle outState) {
119         super.onSaveInstanceState(outState);
120         outState.putInt(STATE_CURRENT_PAGE, b.mainPager.getCurrentItem());
121         if (mainModel.getAccountFilter()
122                      .getValue() != null)
123             outState.putString(STATE_ACC_FILTER, mainModel.getAccountFilter()
124                                                           .getValue());
125     }
126     @Override
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;
136         super.onDestroy();
137     }
138     @Override
139     protected void onResume() {
140         super.onResume();
141         fabShouldShow();
142     }
143     @Override
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());
150
151         mainModel = new ViewModelProvider(this).get(MainModel.class);
152
153         mSectionsPagerAdapter = new SectionsPagerAdapter(this);
154
155         Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
156         if (extra != null && savedInstanceState == null)
157             savedInstanceState = extra;
158
159
160         setSupportActionBar(b.toolbar);
161
162         Data.observeProfile(this, this::onProfileChanged);
163
164         Data.profiles.observe(this, this::onProfileListChanged);
165
166         Data.backgroundTaskProgress.observe(this, this::onRetrieveProgress);
167         Data.backgroundTasksRunning.observe(this, this::onRetrieveRunningChanged);
168
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);
172         }
173         barDrawerToggle.syncState();
174
175         try {
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);
180         }
181         catch (Exception e) {
182             e.printStackTrace();
183         }
184
185         markDrawerItemCurrent(R.id.nav_account_summary);
186
187         b.mainPager.setAdapter(mSectionsPagerAdapter);
188         b.mainPager.setOffscreenPageLimit(1);
189
190         if (pageChangeCallback == null) {
191             pageChangeCallback = new ViewPager2.OnPageChangeCallback() {
192                 @Override
193                 public void onPageSelected(int position) {
194                     mCurrentPage = position;
195                     switch (position) {
196                         case 0:
197                             markDrawerItemCurrent(R.id.nav_account_summary);
198                             break;
199                         case 1:
200                             markDrawerItemCurrent(R.id.nav_latest_transactions);
201                             break;
202                         default:
203                             Log.e(TAG, String.format("Unexpected page index %d", position));
204                     }
205
206                     super.onPageSelected(position);
207                 }
208             };
209             b.mainPager.registerOnPageChangeCallback(pageChangeCallback);
210         }
211
212         mCurrentPage = 0;
213         if (savedInstanceState != null) {
214             int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
215             if (currentPage != -1) {
216                 mCurrentPage = currentPage;
217             }
218             mainModel.getAccountFilter()
219                      .setValue(savedInstanceState.getString(STATE_ACC_FILTER, null));
220         }
221
222         b.btnNoProfilesAdd.setOnClickListener(v -> ProfileDetailActivity.start(this, null));
223         b.btnRestore.setOnClickListener(v -> BackupsActivity.start(this));
224
225         b.btnAddTransaction.setOnClickListener(this::fabNewTransactionClicked);
226
227         b.navNewProfileButton.setOnClickListener(v -> ProfileDetailActivity.start(this, null));
228
229         b.transactionListCancelDownload.setOnClickListener(this::onStopTransactionRefreshClick);
230
231         if (mProfileListAdapter == null)
232             mProfileListAdapter = new ProfilesRecyclerViewAdapter();
233         b.navProfileList.setAdapter(mProfileListAdapter);
234
235         mProfileListAdapter.editingProfiles.observe(this, newValue -> {
236             if (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));
247                 }
248             }
249             else {
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));
260                 }
261             }
262
263             mProfileListAdapter.notifyDataSetChanged();
264         });
265
266         fabManager = new FabManager(b.btnAddTransaction);
267
268         LinearLayoutManager llm = new LinearLayoutManager(this);
269
270         llm.setOrientation(RecyclerView.VERTICAL);
271         b.navProfileList.setLayoutManager(llm);
272
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() {
278                 @Override
279                 public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
280                     if (slideOffset > 0.2)
281                         fabManager.hideFab();
282                 }
283                 @Override
284                 public void onDrawerClosed(View drawerView) {
285                     super.onDrawerClosed(drawerView);
286                     mProfileListAdapter.setAnimationsEnabled(false);
287                     mProfileListAdapter.editingProfiles.setValue(false);
288                     Data.drawerOpen.setValue(false);
289                     fabShouldShow();
290                 }
291                 @Override
292                 public void onDrawerOpened(View drawerView) {
293                     super.onDrawerOpened(drawerView);
294                     mProfileListAdapter.setAnimationsEnabled(true);
295                     Data.drawerOpen.setValue(true);
296                     fabManager.hideFab();
297                 }
298             };
299             b.drawerLayout.addDrawerListener(drawerListener);
300         }
301
302         Data.drawerOpen.observe(this, open -> {
303             if (open)
304                 b.drawerLayout.open();
305             else
306                 b.drawerLayout.close();
307         });
308
309         mainModel.getUpdateError()
310                  .observe(this, (error) -> {
311                      if (error == null)
312                          return;
313
314                      Snackbar.make(b.mainPager, error, Snackbar.LENGTH_INDEFINITE)
315                              .show();
316                      mainModel.clearUpdateError();
317                  });
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);
326     }
327     private void onBackupRestoreClick(View view) {
328         Intent intent = new Intent(this, BackupsActivity.class);
329         startActivity(intent);
330     }
331     private void onPatternsClick(View view) {
332         Intent intent = new Intent(this, TemplatesActivity.class);
333         startActivity(intent);
334     }
335     private void scheduleDataRetrievalIfStale(long lastUpdate) {
336         long now = new Date().getTime();
337         if ((lastUpdate == 0) || (now > (lastUpdate + (24 * 3600 * 1000)))) {
338             if (lastUpdate == 0)
339                 Logger.debug("db::", "WEB data never fetched. scheduling a fetch");
340             else
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));
344
345             mainModel.scheduleTransactionListRetrieval();
346         }
347     }
348     private void createShortcuts(@NotNull List<Profile> list) {
349         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
350             return;
351
352         ShortcutManager sm = getSystemService(ShortcutManager.class);
353         List<ShortcutInfo> shortcuts = new ArrayList<>();
354         int i = 0;
355         for (Profile p : list) {
356             if (shortcuts.size() >= sm.getMaxShortcutCountPerActivity())
357                 break;
358
359             if (!p.permitPosting())
360                 continue;
361
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())
370                                                                           .putExtra(
371                                                                                   ProfileThemedActivity.PARAM_THEME,
372                                                                                   p.getTheme()))
373                                      .setRank(i)
374                                      .build();
375             shortcuts.add(si);
376             i++;
377         }
378         sm.setDynamicShortcuts(shortcuts);
379     }
380     private void onProfileListChanged(@NotNull List<Profile> newList) {
381         createShortcuts(newList);
382
383         if (newList.isEmpty()) {
384             b.noProfilesLayout.setVisibility(View.VISIBLE);
385             b.mainAppLayout.setVisibility(View.GONE);
386             return;
387         }
388
389         b.mainAppLayout.setVisibility(View.VISIBLE);
390         b.noProfilesLayout.setVisibility(View.GONE);
391
392         b.navProfileList.setMinimumHeight(
393                 (int) (getResources().getDimension(R.dimen.thumb_row_height) * newList.size()));
394
395         Logger.debug("profiles", "profile list changed");
396         mProfileListAdapter.setProfileList(newList);
397
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;
404                     break;
405                 }
406             }
407         }
408
409         if (replacementProfile == null) {
410             Logger.debug(TAG, "Switching profile because the current is no longer available");
411             Data.setCurrentProfile(newList.get(0));
412         }
413         else {
414             Data.setCurrentProfile(replacementProfile);
415         }
416     }
417     /**
418      * called when the current profile has changed
419      */
420     private void onProfileChanged(@Nullable Profile newProfile) {
421         if (this.profile != null) {
422             if (this.profile.equals(newProfile))
423                 return;
424         }
425
426         boolean haveProfile = newProfile != null;
427
428         if (haveProfile)
429             setTitle(newProfile.getName());
430         else
431             setTitle(R.string.app_name);
432
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,
437                             newProfileTheme));
438             Colors.profileThemeId = newProfileTheme;
439             profileThemeChanged();
440             // profileThemeChanged would restart the activity, so no need to reload the
441             // data sets below
442             return;
443         }
444
445         final boolean sameProfileId = (newProfile != null) && (this.profile != null) &&
446                                       this.profile.getId() == newProfile.getId();
447
448         this.profile = newProfile;
449
450         b.noProfilesLayout.setVisibility(haveProfile ? View.GONE : View.VISIBLE);
451         b.pagerLayout.setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
452
453         mProfileListAdapter.notifyDataSetChanged();
454
455         if (haveProfile) {
456             if (newProfile.permitPosting()) {
457                 b.toolbar.setSubtitle(null);
458                 b.btnAddTransaction.show();
459             }
460             else {
461                 b.toolbar.setSubtitle(R.string.profile_subtitle_read_only);
462                 b.btnAddTransaction.hide();
463             }
464         }
465         else {
466             b.toolbar.setSubtitle(null);
467             b.btnAddTransaction.hide();
468         }
469
470         updateLastUpdateTextFromDB();
471
472         if (sameProfileId) {
473             Logger.debug(TAG, String.format(Locale.ROOT, "Short-cut profile 'changed' to %d",
474                     newProfile.getId()));
475             return;
476         }
477
478         mainModel.getAccountFilter()
479                  .observe(this, this::onAccountFilterChanged);
480
481         mainModel.stopTransactionsRetrieval();
482         mainModel.clearTransactions();
483     }
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()
492                                  .getTransactionDAO()
493                                  .getAllWithAccounts(profile.getId());
494             }
495             else {
496                 transactions = DB.get()
497                                  .getTransactionDAO()
498                                  .getAllWithAccountsFiltered(profile.getId(), accFilter);
499             }
500         }
501
502         transactions.observe(this, list -> {
503             Logger.debug(TAG,
504                     String.format(Locale.ROOT, "got transaction list from DB (%d transactions)",
505                             list.size()));
506
507             if (converterThread != null)
508                 converterThread.interrupt();
509             converterThread = new ConverterThread(mainModel, list, accFilter);
510             converterThread.start();
511         });
512     }
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);
520
521         Logger.debug(TAG, "profileThemeChanged(): recreating activity");
522         recreate();
523     }
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);
530     }
531     public void markDrawerItemCurrent(int id) {
532         TextView item = b.drawerLayout.findViewById(id);
533         item.setBackgroundColor(Colors.tableRowDarkBG);
534
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);
539             }
540         }
541     }
542     public void onAccountSummaryClicked(View view) {
543         b.drawerLayout.closeDrawers();
544
545         showAccountSummaryFragment();
546     }
547     private void showAccountSummaryFragment() {
548         b.mainPager.setCurrentItem(0, true);
549         mainModel.getAccountFilter()
550                  .setValue(null);
551     }
552     public void onLatestTransactionsClicked(View view) {
553         b.drawerLayout.closeDrawers();
554
555         showTransactionsFragment(null);
556     }
557     public void showTransactionsFragment(String accName) {
558         mainModel.getAccountFilter()
559                  .setValue(accName);
560         b.mainPager.setCurrentItem(1, true);
561     }
562     public void showAccountTransactions(String accountName) {
563         mBackMeansToAccountList = true;
564         showTransactionsFragment(accountName);
565     }
566     @Override
567     public void onBackPressed() {
568         if (b.drawerLayout.isDrawerOpen(GravityCompat.START)) {
569             b.drawerLayout.closeDrawer(GravityCompat.START);
570         }
571         else {
572             if (mBackMeansToAccountList && (b.mainPager.getCurrentItem() == 1)) {
573                 mainModel.getAccountFilter()
574                          .setValue(null);
575                 showAccountSummaryFragment();
576                 mBackMeansToAccountList = false;
577             }
578             else {
579                 Logger.debug(TAG, String.format(Locale.ENGLISH, "manager stack: %d",
580                         getSupportFragmentManager().getBackStackEntryCount()));
581
582                 super.onBackPressed();
583             }
584         }
585     }
586     public void updateLastUpdateTextFromDB() {
587         if (profile == null)
588             return;
589
590         DB.get()
591           .getOptionDAO()
592           .load(profile.getId(), Option.OPT_LAST_SCRAPE)
593           .observe(this, opt -> {
594               long lastUpdate = 0;
595               if (opt != null) {
596                   try {
597                       lastUpdate = Long.parseLong(opt.getValue());
598                   }
599                   catch (NumberFormatException ex) {
600                       Logger.debug(TAG, String.format("Error parsing '%s' as long", opt.getValue()),
601                               ex);
602                   }
603               }
604
605               if (lastUpdate == 0) {
606                   Data.lastUpdateDate.postValue(null);
607               }
608               else {
609                   Data.lastUpdateDate.postValue(new Date(lastUpdate));
610               }
611
612               scheduleDataRetrievalIfStale(lastUpdate);
613           });
614     }
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("----");
627         }
628         else {
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)));
638         }
639     }
640     public void onStopTransactionRefreshClick(View view) {
641         Logger.debug(TAG, "Cancelling transactions refresh");
642         mainModel.stopTransactionsRetrieval();
643         b.transactionListCancelDownload.setEnabled(false);
644     }
645     public void onRetrieveRunningChanged(Boolean running) {
646         if (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);
654             }
655             else {
656                 b.transactionListProgressBar.setProgress(0);
657             }
658             b.transactionProgressLayout.setVisibility(View.VISIBLE);
659         }
660         else {
661             b.transactionProgressLayout.setVisibility(View.GONE);
662         }
663     }
664     public void onRetrieveProgress(@Nullable RetrieveTransactionsTask.Progress progress) {
665         if (progress == null ||
666             progress.getState() == RetrieveTransactionsTask.ProgressState.FINISHED)
667         {
668             Logger.debug(TAG, "progress: Done");
669             b.transactionProgressLayout.setVisibility(View.GONE);
670
671             mainModel.transactionRetrievalDone();
672
673             String error = (progress == null) ? null : progress.getError();
674             if (error != null) {
675                 if (error.equals(RetrieveTransactionsTask.Result.ERR_JSON_PARSER_ERROR))
676                     error = getResources().getString(R.string.err_json_parser_error);
677
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);
683                 });
684                 builder.create()
685                        .show();
686                 return;
687             }
688
689             return;
690         }
691
692
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);
699 //        else
700 //            progressBar.setProgress(0);
701         b.transactionProgressLayout.setVisibility(View.VISIBLE);
702
703         if (progress.isIndeterminate() || (progress.getTotal() <= 0)) {
704             b.transactionListProgressBar.setIndeterminate(true);
705             Logger.debug(TAG, "progress: indeterminate");
706         }
707         else {
708             if (b.transactionListProgressBar.isIndeterminate()) {
709                 b.transactionListProgressBar.setIndeterminate(false);
710             }
711 //            Logger.debug(TAG,
712 //                    String.format(Locale.US, "progress: %d/%d", progress.getProgress(),
713 //                    progress.getTotal
714 //                    ()));
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);
720             else
721                 b.transactionListProgressBar.setProgress(progress.getProgress());
722         }
723     }
724     public void fabShouldShow() {
725         if ((profile != null) && profile.permitPosting() && !b.drawerLayout.isOpen())
726             fabManager.showFab();
727     }
728     @Override
729     public Context getContext() {
730         return this;
731     }
732     @Override
733     public void showManagedFab() {
734         fabShouldShow();
735     }
736     @Override
737     public void hideManagedFab() {
738         fabManager.hideFab();
739     }
740     public static class SectionsPagerAdapter extends FragmentStateAdapter {
741
742         public SectionsPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
743             super(fragmentActivity);
744         }
745         @NotNull
746         @Override
747         public Fragment createFragment(int position) {
748             Logger.debug(TAG, String.format(Locale.ENGLISH, "Switching to fragment %d", position));
749             switch (position) {
750                 case 0:
751 //                    debug(TAG, "Creating account summary fragment");
752                     return new AccountSummaryFragment();
753                 case 1:
754                     return new TransactionListFragment();
755                 default:
756                     throw new IllegalStateException(
757                             String.format("Unexpected fragment index: " + "%d", position));
758             }
759         }
760
761         @Override
762         public int getItemCount() {
763             return 2;
764         }
765     }
766
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) {
773             this.model = model;
774             this.list = list;
775             this.accFilter = accFilter;
776         }
777         @Override
778         public void run() {
779             TransactionAccumulator accumulator = new TransactionAccumulator(accFilter, accFilter);
780
781             for (TransactionWithAccounts tr : list) {
782                 if (isInterrupted()) {
783                     Logger.debug(TAG, "ConverterThread bailing out on interrupt");
784                     return;
785                 }
786                 accumulator.put(new LedgerTransaction(tr));
787             }
788
789             if (isInterrupted()) {
790                 Logger.debug(TAG, "ConverterThread bailing out on interrupt");
791                 return;
792             }
793
794             Logger.debug(TAG, "ConverterThread publishing results");
795
796             Misc.onMainThread(() -> accumulator.publishResults(model));
797         }
798     }
799 }