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.text.DateFormat;
64 import java.util.ArrayList;
65 import java.util.Date;
66 import java.util.List;
67 import java.util.Locale;
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 FragmentManager fragmentManager;
92 private View bTransactionListCancelDownload;
93 private ProgressBar progressBar;
94 private LinearLayout progressLayout;
95 private SectionsPagerAdapter mSectionsPagerAdapter;
96 private ViewPager mViewPager;
97 private FloatingActionButton fab;
98 private boolean profileListExpanded = false;
99 private ProfilesRecyclerViewAdapter mProfileListAdapter;
100 private int mCurrentPage;
101 private String mAccountFilter;
102 private boolean mBackMeansToAccountList = false;
103 private Toolbar mToolbar;
104 private DrawerLayout.SimpleDrawerListener drawerListener;
105 private ActionBarDrawerToggle barDrawerToggle;
106 private ViewPager.SimpleOnPageChangeListener pageChangeListener;
107 private Observer editingProfilesObserver;
108 private MobileLedgerProfile profile;
110 protected void onStart() {
113 debug("flow", "MainActivity.onStart()");
114 mViewPager.setCurrentItem(mCurrentPage, false);
115 if (mAccountFilter != null) showTransactionsFragment(mAccountFilter);
116 else Data.accountFilter.setValue(null);
120 protected void onSaveInstanceState(@NotNull Bundle outState) {
121 super.onSaveInstanceState(outState);
122 outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem());
123 if (mAccountFilter != null) outState.putString(STATE_ACC_FILTER, mAccountFilter);
126 protected void onDestroy() {
127 mSectionsPagerAdapter = null;
128 RecyclerView root = findViewById(R.id.nav_profile_list);
129 if (root != null) root.setAdapter(null);
130 if (drawer != null) drawer.removeDrawerListener(drawerListener);
131 drawerListener = null;
132 if (drawer != null) drawer.removeDrawerListener(barDrawerToggle);
133 barDrawerToggle = null;
134 if (mViewPager != null) mViewPager.removeOnPageChangeListener(pageChangeListener);
135 pageChangeListener = null;
136 if (mProfileListAdapter != null)
137 mProfileListAdapter.deleteEditingProfilesObserver(editingProfilesObserver);
138 editingProfilesObserver = null;
142 protected void setupProfileColors() {
143 int profileColor = Data.retrieveCurrentThemeIdFromDb();
144 Colors.setupTheme(this, profileColor);
145 Colors.profileThemeId = profileColor;
148 protected void onCreate(Bundle savedInstanceState) {
149 super.onCreate(savedInstanceState);
150 debug("flow", "MainActivity.onCreate()");
151 setContentView(R.layout.activity_main);
153 fab = findViewById(R.id.btn_add_transaction);
154 profileListContainer = findViewById(R.id.nav_profile_list_container);
155 profileListHeadArrow = findViewById(R.id.nav_profiles_arrow);
156 profileListHeadMore = findViewById(R.id.nav_profiles_start_edit);
157 profileListHeadCancel = findViewById(R.id.nav_profiles_cancel_edit);
158 LinearLayout profileListHeadMoreAndCancel =
159 findViewById(R.id.nav_profile_list_head_buttons);
160 drawer = findViewById(R.id.drawer_layout);
161 bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
162 progressBar = findViewById(R.id.transaction_list_progress_bar);
163 progressLayout = findViewById(R.id.transaction_progress_layout);
164 fragmentManager = getSupportFragmentManager();
165 mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
166 mViewPager = findViewById(R.id.root_frame);
168 Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
169 if (extra != null && savedInstanceState == null) savedInstanceState = extra;
172 mToolbar = findViewById(R.id.toolbar);
173 setSupportActionBar(mToolbar);
175 Data.profile.observe(this, this::onProfileChanged);
177 Data.profiles.observe(this, this::onProfileListChanged);
179 if (barDrawerToggle == null) {
180 barDrawerToggle = new ActionBarDrawerToggle(this, drawer, mToolbar,
181 R.string.navigation_drawer_open, R.string.navigation_drawer_close);
182 drawer.addDrawerListener(barDrawerToggle);
184 barDrawerToggle.syncState();
186 TextView ver = drawer.findViewById(R.id.drawer_version_text);
190 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
191 ver.setText(pi.versionName);
193 catch (Exception e) {
197 if (progressBar == null)
198 throw new RuntimeException("Can't get hold on the transaction value progress bar");
199 if (progressLayout == null) throw new RuntimeException(
200 "Can't get hold on the transaction value progress bar layout");
202 markDrawerItemCurrent(R.id.nav_account_summary);
204 mViewPager.setAdapter(mSectionsPagerAdapter);
206 if (pageChangeListener == null) {
207 pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
209 public void onPageSelected(int position) {
212 markDrawerItemCurrent(R.id.nav_account_summary);
215 markDrawerItemCurrent(R.id.nav_latest_transactions);
218 Log.e("MainActivity",
219 String.format("Unexpected page index %d", position));
222 super.onPageSelected(position);
225 mViewPager.addOnPageChangeListener(pageChangeListener);
229 if (savedInstanceState != null) {
230 int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
231 if (currentPage != -1) {
232 mCurrentPage = currentPage;
234 mAccountFilter = savedInstanceState.getString(STATE_ACC_FILTER, null);
236 else mAccountFilter = null;
238 Data.lastUpdateDate.observe(this, this::updateLastUpdateDisplay);
240 findViewById(R.id.btn_no_profiles_add)
241 .setOnClickListener(v -> startEditProfileActivity(null));
243 findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
245 findViewById(R.id.nav_new_profile_button)
246 .setOnClickListener(v -> startEditProfileActivity(null));
248 RecyclerView root = findViewById(R.id.nav_profile_list);
250 throw new RuntimeException("Can't get hold on the transaction value view");
252 if (mProfileListAdapter == null) mProfileListAdapter = new ProfilesRecyclerViewAdapter();
253 root.setAdapter(mProfileListAdapter);
255 if (editingProfilesObserver == null) {
256 editingProfilesObserver = (o, arg) -> {
257 if (mProfileListAdapter.isEditingProfiles()) {
258 profileListHeadMore.setVisibility(View.GONE);
260 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
261 profileListHeadCancel.setVisibility(View.VISIBLE);
262 profileListHeadCancel
263 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
266 profileListHeadCancel.setVisibility(View.GONE);
267 profileListHeadCancel
268 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
269 profileListHeadMore.setVisibility(View.GONE);
270 if (profileListExpanded) {
271 profileListHeadMore.setVisibility(View.VISIBLE);
273 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
275 else profileListHeadMore.setVisibility(View.GONE);
278 mProfileListAdapter.addEditingProfilesObserver(editingProfilesObserver);
281 LinearLayoutManager llm = new LinearLayoutManager(this);
283 llm.setOrientation(RecyclerView.VERTICAL);
284 root.setLayoutManager(llm);
286 profileListHeadMore.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
287 profileListHeadCancel.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
288 profileListHeadMoreAndCancel
289 .setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
291 if (drawerListener == null) {
292 drawerListener = new DrawerLayout.SimpleDrawerListener() {
294 public void onDrawerClosed(View drawerView) {
295 super.onDrawerClosed(drawerView);
296 collapseProfileList();
299 drawer.addDrawerListener(drawerListener);
302 findViewById(R.id.nav_profile_list_head_layout)
303 .setOnClickListener(this::navProfilesHeadClicked);
304 findViewById(R.id.nav_profiles_label).setOnClickListener(this::navProfilesHeadClicked);
307 private void scheduleDataRetrievalIfStale(Date lastUpdate) {
308 long now = new Date().getTime();
309 if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
310 if (lastUpdate == null) debug("db::", "WEB data never fetched. scheduling a fetch");
311 else debug("db", String.format(Locale.ENGLISH,
312 "WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
313 lastUpdate.getTime() / 1000f, now / 1000f));
315 Data.scheduleTransactionListRetrieval(this);
318 private void createShortcuts(List<MobileLedgerProfile> list) {
319 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;
321 List<ShortcutInfo> shortcuts = new ArrayList<>();
323 for (MobileLedgerProfile p : list) {
324 if (!p.isPostingPermitted()) continue;
326 ShortcutInfo si = new ShortcutInfo.Builder(this, "new_transaction_" + p.getUuid())
327 .setShortLabel(p.getName())
328 .setIcon(Icon.createWithResource(this, R.drawable.svg_thick_plus_white))
330 new Intent(Intent.ACTION_VIEW, null, this, NewTransactionActivity.class)
331 .putExtra("profile_uuid", p.getUuid())).setRank(i).build();
335 ShortcutManager sm = getSystemService(ShortcutManager.class);
336 sm.setDynamicShortcuts(shortcuts);
338 private void onProfileListChanged(List<MobileLedgerProfile> newList) {
339 if (newList.isEmpty()) {
340 findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE);
341 findViewById(R.id.pager_layout).setVisibility(View.GONE);
342 findViewById(R.id.loading_layout).setVisibility(View.GONE);
346 findViewById(R.id.pager_layout).setVisibility(View.VISIBLE);
347 findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
348 findViewById(R.id.loading_layout).setVisibility(View.GONE);
350 findViewById(R.id.nav_profile_list).setMinimumHeight(
351 (int) (getResources().getDimension(R.dimen.thumb_row_height) * newList.size()));
353 debug("profiles", "profile list changed");
354 mProfileListAdapter.notifyDataSetChanged();
356 createShortcuts(newList);
358 /** called when the current profile has changed */
359 private void onProfileChanged(MobileLedgerProfile profile) {
360 boolean haveProfile = profile != null;
361 findViewById(R.id.no_profiles_layout).setVisibility(haveProfile ? View.GONE : View.VISIBLE);
362 findViewById(R.id.pager_layout).setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
364 if (haveProfile) setTitle(profile.getName());
365 else setTitle(R.string.app_name);
367 this.profile = profile;
369 mProfileListAdapter.notifyDataSetChanged();
371 int newProfileTheme = haveProfile ? profile.getThemeId() : -1;
372 if (newProfileTheme != Colors.profileThemeId) {
374 String.format(Locale.ENGLISH, "profile theme %d → %d", Colors.profileThemeId,
376 MainActivity.this.profileThemeChanged();
377 Colors.profileThemeId = newProfileTheme;
378 // profileThemeChanged would restart the activity, so no need to reload the
382 collapseProfileList();
384 drawer.closeDrawers();
386 Data.transactions.clear();
387 debug("transactions", "requesting list reload");
388 TransactionListViewModel.scheduleTransactionListReload();
390 Data.accounts.clear();
391 AccountSummaryViewModel.scheduleAccountListReload();
394 if (profile.isPostingPermitted()) {
395 mToolbar.setSubtitle(null);
399 mToolbar.setSubtitle(R.string.profile_subitlte_read_only);
404 mToolbar.setSubtitle(null);
408 updateLastUpdateTextFromDB();
410 private void updateLastUpdateDisplay(Date newValue) {
411 LinearLayout l = findViewById(R.id.transactions_last_update_layout);
412 TextView v = findViewById(R.id.transactions_last_update);
413 if (newValue == null) {
414 l.setVisibility(View.INVISIBLE);
415 debug("main", "no last update date :(");
418 final String text = DateFormat.getDateTimeInstance().format(newValue);
420 l.setVisibility(View.VISIBLE);
421 debug("main", String.format("Date formatted: %s", text));
424 scheduleDataRetrievalIfStale(newValue);
426 private void profileThemeChanged() {
427 Bundle bundle = new Bundle();
428 onSaveInstanceState(bundle);
429 // restart activity to reflect theme change
432 // un-hook all observed LiveData
433 Data.profile.removeObservers(this);
434 Data.profiles.removeObservers(this);
435 Data.lastUpdateDate.removeObservers(this);
436 Intent intent = new Intent(this, this.getClass());
437 intent.putExtra(BUNDLE_SAVED_STATE, bundle);
438 startActivity(intent);
440 public void startEditProfileActivity(MobileLedgerProfile profile) {
441 Intent intent = new Intent(this, ProfileDetailActivity.class);
442 Bundle args = new Bundle();
443 if (profile != null) {
444 int index = Data.getProfileIndex(profile);
445 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
447 intent.putExtras(args);
448 startActivity(intent, args);
450 private void setupProfile() {
451 String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
452 MobileLedgerProfile startupProfile;
454 startupProfile = Data.getProfile(profileUUID);
455 Data.setCurrentProfile(startupProfile);
457 public void fabNewTransactionClicked(View view) {
458 Intent intent = new Intent(this, NewTransactionActivity.class);
459 startActivity(intent);
460 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
462 public void navSettingsClicked(View view) {
463 Intent intent = new Intent(this, SettingsActivity.class);
464 startActivity(intent);
465 drawer.closeDrawers();
467 public void markDrawerItemCurrent(int id) {
468 TextView item = drawer.findViewById(id);
469 item.setBackgroundColor(Colors.tableRowDarkBG);
471 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
472 for (int i = 0; i < actions.getChildCount(); i++) {
473 View view = actions.getChildAt(i);
474 if (view.getId() != id) {
475 view.setBackgroundColor(Color.TRANSPARENT);
479 public void onAccountSummaryClicked(View view) {
480 drawer.closeDrawers();
482 showAccountSummaryFragment();
484 private void showAccountSummaryFragment() {
485 mViewPager.setCurrentItem(0, true);
486 Data.accountFilter.setValue(null);
488 public void onLatestTransactionsClicked(View view) {
489 drawer.closeDrawers();
491 showTransactionsFragment((String) null);
493 private void showTransactionsFragment(String accName) {
494 Data.accountFilter.setValue(accName);
495 mViewPager.setCurrentItem(1, true);
497 private void showTransactionsFragment(LedgerAccount account) {
498 showTransactionsFragment((account == null) ? null : account.getName());
500 public void showAccountTransactions(LedgerAccount account) {
501 mBackMeansToAccountList = true;
502 showTransactionsFragment(account);
505 public void onBackPressed() {
506 DrawerLayout drawer = findViewById(R.id.drawer_layout);
507 if (drawer.isDrawerOpen(GravityCompat.START)) {
508 drawer.closeDrawer(GravityCompat.START);
511 if (mBackMeansToAccountList && (mViewPager.getCurrentItem() == 1)) {
512 Data.accountFilter.setValue(null);
513 showAccountSummaryFragment();
514 mBackMeansToAccountList = false;
517 debug("fragments", String.format(Locale.ENGLISH, "manager stack: %d",
518 fragmentManager.getBackStackEntryCount()));
520 super.onBackPressed();
524 public void updateLastUpdateTextFromDB() {
525 long last_update = (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
527 debug("transactions", String.format(Locale.ENGLISH, "Last update = %d", last_update));
528 if (last_update == 0) {
529 Data.lastUpdateDate.postValue(null);
532 Data.lastUpdateDate.postValue(new Date(last_update));
535 public void onStopTransactionRefreshClick(View view) {
536 debug("interactive", "Cancelling transactions refresh");
537 Data.stopTransactionsRetrieval();
538 bTransactionListCancelDownload.setEnabled(false);
540 public void onRetrieveDone(String error) {
541 Data.transactionRetrievalDone();
542 progressLayout.setVisibility(View.GONE);
545 updateLastUpdateTextFromDB();
547 new RefreshDescriptionsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
548 TransactionListViewModel.scheduleTransactionListReload();
550 else Toast.makeText(this, error, Toast.LENGTH_LONG).show();
552 public void onRetrieveStart() {
553 bTransactionListCancelDownload.setEnabled(true);
554 progressBar.setIndeterminateTintList(ColorStateList.valueOf(Colors.primary));
555 progressBar.setProgressTintList(ColorStateList.valueOf(Colors.primary));
556 progressBar.setIndeterminate(true);
557 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
558 else progressBar.setProgress(0);
559 progressLayout.setVisibility(View.VISIBLE);
561 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
562 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
563 (progress.getTotal() == 0))
565 progressBar.setIndeterminate(true);
568 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
569 progressBar.setMin(0);
571 progressBar.setMax(progress.getTotal());
572 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
573 progressBar.setProgress(progress.getProgress(), true);
575 else progressBar.setProgress(progress.getProgress());
576 progressBar.setIndeterminate(false);
579 public void fabShouldShow() {
580 if ((profile != null) && profile.isPostingPermitted()) fab.show();
582 public void fabHide() {
585 public void navProfilesHeadClicked(View view) {
586 if (profileListExpanded) {
587 collapseProfileList();
593 private void expandProfileList() {
594 profileListExpanded = true;
597 profileListContainer.setVisibility(View.VISIBLE);
598 profileListContainer.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_down));
599 profileListHeadArrow.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180));
600 profileListHeadMore.setVisibility(View.VISIBLE);
601 profileListHeadMore.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
602 final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
603 findViewById(R.id.nav_profile_list).setMinimumHeight(
604 (int) (getResources().getDimension(R.dimen.thumb_row_height) *
605 (profiles != null ? profiles.size() : 0)));
607 private void collapseProfileList() {
608 boolean wasExpanded = profileListExpanded;
609 profileListExpanded = false;
612 final Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_up);
613 animation.setAnimationListener(new Animation.AnimationListener() {
615 public void onAnimationStart(Animation animation) {
619 public void onAnimationEnd(Animation animation) {
620 profileListContainer.setVisibility(View.GONE);
623 public void onAnimationRepeat(Animation animation) {
627 mProfileListAdapter.stopEditingProfiles();
629 profileListContainer.startAnimation(animation);
630 profileListHeadArrow.setRotation(0f);
632 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180_back));
633 final Animation moreAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
634 moreAnimation.setAnimationListener(new Animation.AnimationListener() {
636 public void onAnimationStart(Animation animation) {
639 public void onAnimationEnd(Animation animation) {
640 profileListHeadMore.setVisibility(View.GONE);
643 public void onAnimationRepeat(Animation animation) {
646 profileListHeadMore.startAnimation(moreAnimation);
649 profileListContainer.setVisibility(View.GONE);
650 profileListHeadArrow.setRotation(0f);
651 profileListHeadMore.setVisibility(View.GONE);
654 public void onAccountSummaryRowViewClicked(View view) {
656 if (view.getId() == R.id.account_expander) row = (ViewGroup) view.getParent().getParent();
657 else row = (ViewGroup) view.getParent();
659 LedgerAccount acc = (LedgerAccount) row.getTag();
660 switch (view.getId()) {
661 case R.id.account_row_acc_name:
662 case R.id.account_expander:
663 case R.id.account_expander_container:
664 debug("accounts", "Account expander clicked");
665 if (!acc.hasSubAccounts()) return;
667 boolean wasExpanded = acc.isExpanded();
669 View arrow = row.findViewById(R.id.account_expander_container);
671 arrow.clearAnimation();
672 ViewPropertyAnimator animator = arrow.animate();
674 acc.toggleExpanded();
675 DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
676 new Object[]{acc.isExpanded(), acc.getName(), profile.getUuid()
680 debug("accounts", String.format("Collapsing account '%s'", acc.getName()));
681 arrow.setRotation(0);
682 animator.rotationBy(180);
684 // removing all child accounts from the view
685 int start = -1, count = 0;
686 try (LockHolder ignored = Data.accounts.lockForWriting()) {
687 for (int i = 0; i < Data.accounts.size(); i++) {
688 if (acc.isParentOf(Data.accounts.get(i))) {
689 // debug("accounts", String.format("Found a child '%s' at position %d",
690 // Data.accounts.get(i).getName(), i));
699 // String.format("Found a non-child '%s' at position %d",
700 // Data.accounts.get(i).getName(), i));
707 for (int j = 0; j < count; j++) {
708 // debug("accounts", String.format("Removing item %d: %s", start + j,
709 // Data.accounts.get(start).getName()));
710 Data.accounts.removeQuietly(start);
713 mAccountSummaryFragment.modelAdapter
714 .notifyItemRangeRemoved(start, count);
719 debug("accounts", String.format("Expanding account '%s'", acc.getName()));
720 arrow.setRotation(180);
721 animator.rotationBy(-180);
722 List<LedgerAccount> children = profile.loadVisibleChildAccountsOf(acc);
723 try (LockHolder ignored = Data.accounts.lockForWriting()) {
724 int parentPos = Data.accounts.indexOf(acc);
725 if (parentPos != -1) {
726 // may have disappeared in a concurrent refresh operation
727 Data.accounts.addAllQuietly(parentPos + 1, children);
728 mAccountSummaryFragment.modelAdapter
729 .notifyItemRangeInserted(parentPos + 1, children.size());
734 case R.id.account_row_acc_amounts:
735 if (acc.getAmountCount() > AccountSummaryAdapter.AMOUNT_LIMIT) {
736 acc.toggleAmountsExpanded();
738 .add("update accounts set amounts_expanded=? where name=? and profile=?",
739 new Object[]{acc.amountsExpanded(), acc.getName(),
742 Data.accounts.triggerItemChangedNotification(acc);
748 public class SectionsPagerAdapter extends FragmentPagerAdapter {
750 SectionsPagerAdapter(FragmentManager fm) {
756 public Fragment getItem(int position) {
757 debug("main", String.format(Locale.ENGLISH, "Switching to fragment %d", position));
760 // debug("flow", "Creating account summary fragment");
761 return mAccountSummaryFragment = new AccountSummaryFragment();
763 return new TransactionListFragment();
765 throw new IllegalStateException(
766 String.format("Unexpected fragment index: " + "%d", position));
771 public int getCount() {