2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.activity;
20 import android.content.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.AsyncTask;
28 import android.os.Build;
29 import android.os.Bundle;
30 import android.util.Log;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.view.ViewPropertyAnimator;
34 import android.view.animation.Animation;
35 import android.view.animation.AnimationUtils;
36 import android.widget.LinearLayout;
37 import android.widget.ProgressBar;
38 import android.widget.TextView;
39 import android.widget.Toast;
41 import com.google.android.material.floatingactionbutton.FloatingActionButton;
43 import net.ktnx.mobileledger.R;
44 import net.ktnx.mobileledger.async.DbOpQueue;
45 import net.ktnx.mobileledger.async.RefreshDescriptionsTask;
46 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
47 import net.ktnx.mobileledger.model.Data;
48 import net.ktnx.mobileledger.model.LedgerAccount;
49 import net.ktnx.mobileledger.model.MobileLedgerProfile;
50 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryAdapter;
51 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
52 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryViewModel;
53 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
54 import net.ktnx.mobileledger.ui.profiles.ProfilesRecyclerViewAdapter;
55 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
56 import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel;
57 import net.ktnx.mobileledger.utils.Colors;
58 import net.ktnx.mobileledger.utils.LockHolder;
59 import net.ktnx.mobileledger.utils.MLDB;
61 import org.jetbrains.annotations.NotNull;
63 import java.lang.ref.WeakReference;
64 import java.text.DateFormat;
65 import java.util.ArrayList;
66 import java.util.Date;
67 import java.util.List;
68 import java.util.Observer;
70 import androidx.appcompat.app.ActionBarDrawerToggle;
71 import androidx.appcompat.widget.Toolbar;
72 import androidx.core.view.GravityCompat;
73 import androidx.drawerlayout.widget.DrawerLayout;
74 import androidx.fragment.app.Fragment;
75 import androidx.fragment.app.FragmentManager;
76 import androidx.fragment.app.FragmentPagerAdapter;
77 import androidx.recyclerview.widget.LinearLayoutManager;
78 import androidx.recyclerview.widget.RecyclerView;
79 import androidx.viewpager.widget.ViewPager;
81 import static net.ktnx.mobileledger.utils.Logger.debug;
83 public class MainActivity extends ProfileThemedActivity {
84 public static final String STATE_CURRENT_PAGE = "current_page";
85 public static final String BUNDLE_SAVED_STATE = "bundle_savedState";
86 public static final String STATE_ACC_FILTER = "account_filter";
87 public AccountSummaryFragment mAccountSummaryFragment;
89 private LinearLayout profileListContainer;
90 private View profileListHeadArrow, profileListHeadMore, profileListHeadCancel;
91 private LinearLayout profileListHeadMoreAndCancel;
92 private FragmentManager fragmentManager;
93 private TextView tvLastUpdate;
94 private RetrieveTransactionsTask retrieveTransactionsTask;
95 private View bTransactionListCancelDownload;
96 private ProgressBar progressBar;
97 private LinearLayout progressLayout;
98 private SectionsPagerAdapter mSectionsPagerAdapter;
99 private ViewPager mViewPager;
100 private FloatingActionButton fab;
101 private boolean profileModificationEnabled = false;
102 private boolean profileListExpanded = false;
103 private ProfilesRecyclerViewAdapter mProfileListAdapter;
104 private int mCurrentPage;
105 private String mAccountFilter;
106 private boolean mBackMeansToAccountList = false;
107 private Observer profileObserver;
108 private Observer profilesObserver;
109 private Toolbar mToolbar;
110 private DrawerLayout.SimpleDrawerListener drawerListener;
111 private ActionBarDrawerToggle barDrawerToggle;
112 private ViewPager.SimpleOnPageChangeListener pageChangeListener;
113 private Observer editingProfilesObserver;
115 protected void onStart() {
118 debug("flow", "MainActivity.onStart()");
119 mViewPager.setCurrentItem(mCurrentPage, false);
120 if (mAccountFilter != null) showTransactionsFragment(mAccountFilter);
121 else Data.accountFilter.setValue(null);
125 protected void onSaveInstanceState(Bundle outState) {
126 super.onSaveInstanceState(outState);
127 outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem());
128 if (mAccountFilter != null)
129 outState.putString(STATE_ACC_FILTER, mAccountFilter);
132 protected void onDestroy() {
133 mSectionsPagerAdapter = null;
134 Data.profile.deleteObserver(profileObserver);
135 profileObserver = null;
136 Data.profiles.deleteObserver(profilesObserver);
137 profilesObserver = null;
138 RecyclerView root = findViewById(R.id.nav_profile_list);
139 if (root != null) root.setAdapter(null);
140 if (drawer != null) drawer.removeDrawerListener(drawerListener);
141 drawerListener = null;
142 if (drawer != null) drawer.removeDrawerListener(barDrawerToggle);
143 barDrawerToggle = null;
144 if (mViewPager != null) mViewPager.removeOnPageChangeListener(pageChangeListener);
145 pageChangeListener = null;
146 if (mProfileListAdapter != null)
147 mProfileListAdapter.deleteEditingProfilesObserver(editingProfilesObserver);
148 editingProfilesObserver = null;
152 protected void onCreate(Bundle savedInstanceState) {
153 super.onCreate(savedInstanceState);
154 debug("flow", "MainActivity.onCreate()");
155 int profileColor = Data.retrieveCurrentThemeIdFromDb();
156 Colors.setupTheme(this, profileColor);
157 Colors.profileThemeId = profileColor;
158 setContentView(R.layout.activity_main);
160 fab = findViewById(R.id.btn_add_transaction);
161 profileListContainer = findViewById(R.id.nav_profile_list_container);
162 profileListHeadArrow = findViewById(R.id.nav_profiles_arrow);
163 profileListHeadMore = findViewById(R.id.nav_profiles_start_edit);
164 profileListHeadCancel = findViewById(R.id.nav_profiles_cancel_edit);
165 profileListHeadMoreAndCancel = findViewById(R.id.nav_profile_list_head_buttons);
166 drawer = findViewById(R.id.drawer_layout);
167 tvLastUpdate = findViewById(R.id.transactions_last_update);
168 bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
169 progressBar = findViewById(R.id.transaction_list_progress_bar);
170 progressLayout = findViewById(R.id.transaction_progress_layout);
171 fragmentManager = getSupportFragmentManager();
172 mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
173 mViewPager = findViewById(R.id.root_frame);
175 Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
176 if (extra != null && savedInstanceState == null) savedInstanceState = extra;
179 mToolbar = findViewById(R.id.toolbar);
180 setSupportActionBar(mToolbar);
182 if (profileObserver == null) {
183 profileObserver = (o, arg) -> onProfileChanged(arg);
184 Data.profile.addObserver(profileObserver);
187 if (profilesObserver == null) {
188 profilesObserver = (o, arg) -> onProfileListChanged(arg);
189 Data.profiles.addObserver(profilesObserver);
192 if (barDrawerToggle == null) {
193 barDrawerToggle = new ActionBarDrawerToggle(this, drawer, mToolbar,
194 R.string.navigation_drawer_open, R.string.navigation_drawer_close);
195 drawer.addDrawerListener(barDrawerToggle);
197 barDrawerToggle.syncState();
199 TextView ver = drawer.findViewById(R.id.drawer_version_text);
203 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
204 ver.setText(pi.versionName);
206 catch (Exception e) {
210 if (progressBar == null)
211 throw new RuntimeException("Can't get hold on the transaction value progress bar");
212 if (progressLayout == null) throw new RuntimeException(
213 "Can't get hold on the transaction value progress bar layout");
215 markDrawerItemCurrent(R.id.nav_account_summary);
217 mViewPager.setAdapter(mSectionsPagerAdapter);
219 if (pageChangeListener == null) {
220 pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
222 public void onPageSelected(int position) {
225 markDrawerItemCurrent(R.id.nav_account_summary);
228 markDrawerItemCurrent(R.id.nav_latest_transactions);
231 Log.e("MainActivity",
232 String.format("Unexpected page index %d", position));
235 super.onPageSelected(position);
238 mViewPager.addOnPageChangeListener(pageChangeListener);
242 if (savedInstanceState != null) {
243 int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
244 if (currentPage != -1) {
245 mCurrentPage = currentPage;
247 mAccountFilter = savedInstanceState.getString(STATE_ACC_FILTER, null);
249 else mAccountFilter = null;
251 Data.lastUpdateDate.observe(this, this::updateLastUpdateDisplay);
253 findViewById(R.id.btn_no_profiles_add)
254 .setOnClickListener(v -> startEditProfileActivity(null));
256 findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
258 findViewById(R.id.nav_new_profile_button)
259 .setOnClickListener(v -> startEditProfileActivity(null));
261 RecyclerView root = findViewById(R.id.nav_profile_list);
263 throw new RuntimeException("Can't get hold on the transaction value view");
265 if (mProfileListAdapter == null) mProfileListAdapter = new ProfilesRecyclerViewAdapter();
266 root.setAdapter(mProfileListAdapter);
268 if (editingProfilesObserver == null) {
269 editingProfilesObserver = (o, arg) -> {
270 if (mProfileListAdapter.isEditingProfiles()) {
271 profileListHeadArrow.clearAnimation();
272 profileListHeadArrow.setVisibility(View.GONE);
273 profileListHeadMore.setVisibility(View.GONE);
274 profileListHeadCancel.setVisibility(View.VISIBLE);
277 profileListHeadArrow.setRotation(180f);
278 profileListHeadArrow.setVisibility(View.VISIBLE);
279 profileListHeadCancel.setVisibility(View.GONE);
280 profileListHeadMore.setVisibility(View.GONE);
282 .setVisibility(profileListExpanded ? View.VISIBLE : View.GONE);
285 mProfileListAdapter.addEditingProfilesObserver(editingProfilesObserver);
288 LinearLayoutManager llm = new LinearLayoutManager(this);
290 llm.setOrientation(RecyclerView.VERTICAL);
291 root.setLayoutManager(llm);
293 profileListHeadMore.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
294 profileListHeadCancel.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
295 profileListHeadMoreAndCancel
296 .setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
298 if (drawerListener == null) {
299 drawerListener = new DrawerLayout.SimpleDrawerListener() {
301 public void onDrawerClosed(View drawerView) {
302 super.onDrawerClosed(drawerView);
303 collapseProfileList();
306 drawer.addDrawerListener(drawerListener);
309 findViewById(R.id.nav_profile_list_head_layout)
310 .setOnClickListener(this::navProfilesHeadClicked);
311 findViewById(R.id.nav_profiles_label).setOnClickListener(this::navProfilesHeadClicked);
313 onProfileChanged(null);
315 updateLastUpdateTextFromDB();
317 private void scheduleDataRetrievalIfStale(Date lastUpdate) {
318 long now = new Date().getTime();
319 if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
320 if (lastUpdate == null) debug("db::", "WEB data never fetched. scheduling a fetch");
322 String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
323 lastUpdate.getTime() / 1000f, now / 1000f));
325 scheduleTransactionListRetrieval();
328 private void createShortcuts() {
329 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return;
331 List<ShortcutInfo> shortcuts = new ArrayList<>();
332 try (LockHolder lh = Data.profiles.lockForReading()) {
333 for (int i = 0; i < Data.profiles.size(); i++) {
334 MobileLedgerProfile p = Data.profiles.get(i);
335 if (!p.isPostingPermitted()) continue;
337 ShortcutInfo si = new ShortcutInfo.Builder(this, "new_transaction_" + p.getUuid())
338 .setShortLabel(p.getName())
339 .setIcon(Icon.createWithResource(this, R.drawable.svg_thick_plus_white))
340 .setIntent(new Intent(Intent.ACTION_VIEW, null, this,
341 NewTransactionActivity.class).putExtra("profile_uuid", p.getUuid()))
346 ShortcutManager sm = getSystemService(ShortcutManager.class);
347 sm.setDynamicShortcuts(shortcuts);
349 private void onProfileListChanged(Object arg) {
350 findViewById(R.id.nav_profile_list).setMinimumHeight(
351 (int) (getResources().getDimension(R.dimen.thumb_row_height) *
352 Data.profiles.size()));
354 debug("profiles", "profile list changed");
355 if (arg == null) mProfileListAdapter.notifyDataSetChanged();
356 else mProfileListAdapter.notifyItemChanged((int) arg);
360 private void onProfileChanged(Object arg) {
361 MobileLedgerProfile profile = Data.profile.get();
362 MainActivity.this.runOnUiThread(() -> {
364 boolean haveProfile = profile != null;
365 findViewById(R.id.no_profiles_layout)
366 .setVisibility(haveProfile ? View.GONE : View.VISIBLE);
367 findViewById(R.id.pager_layout)
368 .setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
370 Data.transactions.clear();
371 debug("transactions", "requesting list reload");
372 TransactionListViewModel.scheduleTransactionListReload();
374 Data.accounts.clear();
375 AccountSummaryViewModel.scheduleAccountListReload();
377 if (profile == null) MainActivity.this.setTitle(R.string.app_name);
378 else MainActivity.this.setTitle(profile.getName());
379 MainActivity.this.updateLastUpdateTextFromDB();
383 MobileLedgerProfile old = (MobileLedgerProfile) arg;
384 old_index = Data.getProfileIndex(old);
385 new_index = Data.getProfileIndex(profile);
388 if ((old_index != -1) && (new_index != -1)) {
389 mProfileListAdapter.notifyItemChanged(old_index);
390 mProfileListAdapter.notifyItemChanged(new_index);
392 else mProfileListAdapter.notifyDataSetChanged();
394 MainActivity.this.collapseProfileList();
396 int newProfileTheme = (profile == null) ? -1 : profile.getThemeId();
397 if (newProfileTheme != Colors.profileThemeId) {
398 debug("profiles", String.format("profile theme %d → %d", Colors.profileThemeId,
400 MainActivity.this.profileThemeChanged();
401 Colors.profileThemeId = newProfileTheme;
402 // profileThemeChanged would restart the activity, so no need to reload the
406 drawer.closeDrawers();
408 if (profile == null) {
409 mToolbar.setSubtitle(null);
413 if (profile.isPostingPermitted()) {
414 mToolbar.setSubtitle(null);
418 mToolbar.setSubtitle(R.string.profile_subitlte_read_only);
423 updateLastUpdateTextFromDB();
426 private void updateLastUpdateDisplay(Date newValue) {
427 LinearLayout l = findViewById(R.id.transactions_last_update_layout);
428 TextView v = findViewById(R.id.transactions_last_update);
429 if (newValue == null) {
430 l.setVisibility(View.INVISIBLE);
431 debug("main", "no last update date :(");
434 final String text = DateFormat.getDateTimeInstance().format(newValue);
436 l.setVisibility(View.VISIBLE);
437 debug("main", String.format("Date formatted: %s", text));
440 scheduleDataRetrievalIfStale(newValue);
443 public void finish() {
444 if (profilesObserver != null) {
445 Data.profiles.deleteObserver(profilesObserver);
446 profilesObserver = null;
449 if (profileObserver != null) {
450 Data.profile.deleteObserver(profileObserver);
451 profileObserver = null;
456 private void profileThemeChanged() {
457 setupProfileColors();
459 Bundle bundle = new Bundle();
460 onSaveInstanceState(bundle);
461 // restart activity to reflect theme change
463 Intent intent = new Intent(this, this.getClass());
464 intent.putExtra(BUNDLE_SAVED_STATE, bundle);
465 startActivity(intent);
467 public void startEditProfileActivity(MobileLedgerProfile profile) {
468 Intent intent = new Intent(this, ProfileDetailActivity.class);
469 Bundle args = new Bundle();
470 if (profile != null) {
471 int index = Data.getProfileIndex(profile);
472 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
474 intent.putExtras(args);
475 startActivity(intent, args);
477 private void setupProfile() {
478 String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
479 MobileLedgerProfile profile;
481 profile = Data.getProfile(profileUUID);
483 if (Data.profiles.isEmpty()) {
484 findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE);
485 findViewById(R.id.pager_layout).setVisibility(View.GONE);
489 findViewById(R.id.pager_layout).setVisibility(View.VISIBLE);
490 findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
492 if (profile == null) profile = Data.profiles.get(0);
494 if (profile == null) throw new AssertionError("profile must have a value");
496 Data.setCurrentProfile(profile);
498 public void fabNewTransactionClicked(View view) {
499 Intent intent = new Intent(this, NewTransactionActivity.class);
500 startActivity(intent);
501 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
503 public void navSettingsClicked(View view) {
504 Intent intent = new Intent(this, SettingsActivity.class);
505 startActivity(intent);
506 drawer.closeDrawers();
508 public void markDrawerItemCurrent(int id) {
509 TextView item = drawer.findViewById(id);
510 item.setBackgroundColor(Colors.tableRowDarkBG);
512 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
513 for (int i = 0; i < actions.getChildCount(); i++) {
514 View view = actions.getChildAt(i);
515 if (view.getId() != id) {
516 view.setBackgroundColor(Color.TRANSPARENT);
520 public void onAccountSummaryClicked(View view) {
521 drawer.closeDrawers();
523 showAccountSummaryFragment();
525 private void showAccountSummaryFragment() {
526 mViewPager.setCurrentItem(0, true);
527 Data.accountFilter.setValue(null);
528 // FragmentTransaction ft = fragmentManager.beginTransaction();
529 // accountSummaryFragment = new AccountSummaryFragment();
530 // ft.replace(R.id.root_frame, accountSummaryFragment);
532 // currentFragment = accountSummaryFragment;
534 public void onLatestTransactionsClicked(View view) {
535 drawer.closeDrawers();
537 showTransactionsFragment((String) null);
539 private void resetFragmentBackStack() {
540 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
542 private void showTransactionsFragment(String accName) {
543 Data.accountFilter.setValue(accName);
544 mViewPager.setCurrentItem(1, true);
546 private void showTransactionsFragment(LedgerAccount account) {
547 showTransactionsFragment((account == null) ? (String) null : account.getName());
548 // FragmentTransaction ft = fragmentManager.beginTransaction();
549 // if (transactionListFragment == null) {
550 // debug("flow", "MainActivity creating TransactionListFragment");
551 // transactionListFragment = new TransactionListFragment();
553 // Bundle bundle = new Bundle();
554 // if (account != null) {
555 // bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
556 // account.getName());
558 // transactionListFragment.setArguments(bundle);
559 // ft.replace(R.id.root_frame, transactionListFragment);
560 // if (account != null)
561 // ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
564 // currentFragment = transactionListFragment;
566 public void showAccountTransactions(LedgerAccount account) {
567 mBackMeansToAccountList = true;
568 showTransactionsFragment(account);
571 public void onBackPressed() {
572 DrawerLayout drawer = findViewById(R.id.drawer_layout);
573 if (drawer.isDrawerOpen(GravityCompat.START)) {
574 drawer.closeDrawer(GravityCompat.START);
577 if (mBackMeansToAccountList && (mViewPager.getCurrentItem() == 1)) {
578 Data.accountFilter.setValue(null);
579 showAccountSummaryFragment();
580 mBackMeansToAccountList = false;
583 debug("fragments", String.format("manager stack: %d",
584 fragmentManager.getBackStackEntryCount()));
586 super.onBackPressed();
590 public void updateLastUpdateTextFromDB() {
591 final MobileLedgerProfile profile = Data.profile.get();
592 long last_update = (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
594 debug("transactions", String.format("Last update = %d", last_update));
595 if (last_update == 0) {
596 Data.lastUpdateDate.postValue(null);
599 Data.lastUpdateDate.postValue(new Date(last_update));
602 public void scheduleTransactionListRetrieval() {
603 if (Data.profile.get() == null) return;
605 retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
607 retrieveTransactionsTask.execute();
609 public void onStopTransactionRefreshClick(View view) {
610 debug("interactive", "Cancelling transactions refresh");
611 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
612 bTransactionListCancelDownload.setEnabled(false);
614 public void onRetrieveDone(String error) {
615 progressLayout.setVisibility(View.GONE);
618 updateLastUpdateTextFromDB();
620 new RefreshDescriptionsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
621 TransactionListViewModel.scheduleTransactionListReload();
623 else Toast.makeText(this, error, Toast.LENGTH_LONG).show();
625 public void onRetrieveStart() {
626 bTransactionListCancelDownload.setEnabled(true);
627 progressBar.setIndeterminateTintList(ColorStateList.valueOf(Colors.primary));
628 progressBar.setProgressTintList(ColorStateList.valueOf(Colors.primary));
629 progressBar.setIndeterminate(true);
630 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
631 else progressBar.setProgress(0);
632 progressLayout.setVisibility(View.VISIBLE);
634 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
635 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
636 (progress.getTotal() == 0))
638 progressBar.setIndeterminate(true);
641 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
642 progressBar.setMin(0);
644 progressBar.setMax(progress.getTotal());
645 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
646 progressBar.setProgress(progress.getProgress(), true);
648 else progressBar.setProgress(progress.getProgress());
649 progressBar.setIndeterminate(false);
652 public void fabShouldShow() {
653 MobileLedgerProfile profile = Data.profile.get();
654 if ((profile != null) && profile.isPostingPermitted()) fab.show();
656 public void navProfilesHeadClicked(View view) {
657 if (profileListExpanded) {
658 collapseProfileList();
664 private void expandProfileList() {
665 profileListExpanded = true;
668 profileListContainer.setVisibility(View.VISIBLE);
669 profileListContainer.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_down));
670 profileListHeadArrow.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180));
671 profileListHeadMore.setVisibility(View.VISIBLE);
672 profileListHeadMore.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
673 findViewById(R.id.nav_profile_list).setMinimumHeight(
674 (int) (getResources().getDimension(R.dimen.thumb_row_height) *
675 Data.profiles.size()));
677 private void collapseProfileList() {
678 profileListExpanded = false;
680 final Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_up);
681 animation.setAnimationListener(new Animation.AnimationListener() {
683 public void onAnimationStart(Animation animation) {
687 public void onAnimationEnd(Animation animation) {
688 profileListContainer.setVisibility(View.GONE);
691 public void onAnimationRepeat(Animation animation) {
695 mProfileListAdapter.stopEditingProfiles();
697 profileListContainer.startAnimation(animation);
698 profileListHeadArrow.setRotation(0f);
700 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180_back));
701 profileListHeadMore.setVisibility(View.GONE);
703 public void onAccountSummaryRowViewClicked(View view) {
705 if (view.getId() == R.id.account_expander) row = (ViewGroup) view.getParent().getParent();
706 else row = (ViewGroup) view.getParent();
708 LedgerAccount acc = (LedgerAccount) row.getTag();
709 switch (view.getId()) {
710 case R.id.account_row_acc_name:
711 case R.id.account_expander:
712 case R.id.account_expander_container:
713 debug("accounts", "Account expander clicked");
714 if (!acc.hasSubAccounts()) return;
716 boolean wasExpanded = acc.isExpanded();
718 View arrow = row.findViewById(R.id.account_expander_container);
720 arrow.clearAnimation();
721 ViewPropertyAnimator animator = arrow.animate();
723 acc.toggleExpanded();
724 DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
725 new Object[]{acc.isExpanded(), acc.getName(), Data.profile.get().getUuid()
729 debug("accounts", String.format("Collapsing account '%s'", acc.getName()));
730 arrow.setRotation(0);
731 animator.rotationBy(180);
733 // removing all child accounts from the view
734 int start = -1, count = 0;
735 try (LockHolder lh = Data.accounts.lockForWriting()) {
736 for (int i = 0; i < Data.accounts.size(); i++) {
737 if (acc.isParentOf(Data.accounts.get(i))) {
738 // debug("accounts", String.format("Found a child '%s' at position %d",
739 // Data.accounts.get(i).getName(), i));
748 // String.format("Found a non-child '%s' at position %d",
749 // Data.accounts.get(i).getName(), i));
756 for (int j = 0; j < count; j++) {
757 // debug("accounts", String.format("Removing item %d: %s", start + j,
758 // Data.accounts.get(start).getName()));
759 Data.accounts.removeQuietly(start);
762 mAccountSummaryFragment.modelAdapter
763 .notifyItemRangeRemoved(start, count);
768 debug("accounts", String.format("Expanding account '%s'", acc.getName()));
769 arrow.setRotation(180);
770 animator.rotationBy(-180);
771 List<LedgerAccount> children =
772 Data.profile.get().loadVisibleChildAccountsOf(acc);
773 try (LockHolder lh = Data.accounts.lockForWriting()) {
774 int parentPos = Data.accounts.indexOf(acc);
775 if (parentPos != -1) {
776 // may have disappeared in a concurrent refresh operation
777 Data.accounts.addAllQuietly(parentPos + 1, children);
778 mAccountSummaryFragment.modelAdapter
779 .notifyItemRangeInserted(parentPos + 1, children.size());
784 case R.id.account_row_acc_amounts:
785 if (acc.getAmountCount() > AccountSummaryAdapter.AMOUNT_LIMIT) {
786 acc.toggleAmountsExpanded();
788 .add("update accounts set amounts_expanded=? where name=? and profile=?",
789 new Object[]{acc.amountsExpanded(), acc.getName(),
790 Data.profile.get().getUuid()
792 Data.accounts.triggerItemChangedNotification(acc);
798 public class SectionsPagerAdapter extends FragmentPagerAdapter {
800 public SectionsPagerAdapter(FragmentManager fm) {
806 public Fragment getItem(int position) {
807 debug("main", String.format("Switching to fragment %d", position));
810 // debug("flow", "Creating account summary fragment");
811 return mAccountSummaryFragment = new AccountSummaryFragment();
813 return new TransactionListFragment();
815 throw new IllegalStateException(
816 String.format("Unexpected fragment index: " + "%d", position));
821 public int getCount() {