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.
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.SharedPreferences;
22 import android.content.pm.PackageInfo;
23 import android.content.pm.ShortcutInfo;
24 import android.content.pm.ShortcutManager;
25 import android.content.res.ColorStateList;
26 import android.graphics.Color;
27 import android.graphics.drawable.Icon;
28 import android.os.AsyncTask;
29 import android.os.Build;
30 import android.os.Bundle;
31 import android.util.Log;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.view.ViewPropertyAnimator;
35 import android.view.animation.AnimationUtils;
36 import android.widget.LinearLayout;
37 import android.widget.ProgressBar;
38 import android.widget.TextView;
40 import androidx.annotation.NonNull;
41 import androidx.appcompat.app.ActionBarDrawerToggle;
42 import androidx.appcompat.widget.Toolbar;
43 import androidx.core.view.GravityCompat;
44 import androidx.drawerlayout.widget.DrawerLayout;
45 import androidx.fragment.app.Fragment;
46 import androidx.fragment.app.FragmentManager;
47 import androidx.fragment.app.FragmentPagerAdapter;
48 import androidx.recyclerview.widget.LinearLayoutManager;
49 import androidx.recyclerview.widget.RecyclerView;
50 import androidx.viewpager.widget.ViewPager;
52 import com.google.android.material.floatingactionbutton.FloatingActionButton;
53 import com.google.android.material.snackbar.Snackbar;
55 import net.ktnx.mobileledger.R;
56 import net.ktnx.mobileledger.async.DbOpQueue;
57 import net.ktnx.mobileledger.async.RefreshDescriptionsTask;
58 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
59 import net.ktnx.mobileledger.model.Data;
60 import net.ktnx.mobileledger.model.LedgerAccount;
61 import net.ktnx.mobileledger.model.MobileLedgerProfile;
62 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryAdapter;
63 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
64 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryViewModel;
65 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
66 import net.ktnx.mobileledger.ui.profiles.ProfilesRecyclerViewAdapter;
67 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
68 import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel;
69 import net.ktnx.mobileledger.utils.Colors;
70 import net.ktnx.mobileledger.utils.GetOptCallback;
71 import net.ktnx.mobileledger.utils.LockHolder;
72 import net.ktnx.mobileledger.utils.MLDB;
74 import org.jetbrains.annotations.NotNull;
76 import java.text.DateFormat;
77 import java.util.ArrayList;
78 import java.util.Date;
79 import java.util.List;
80 import java.util.Locale;
82 import static net.ktnx.mobileledger.utils.Logger.debug;
88 public class MainActivity extends ProfileThemedActivity {
89 public static final String STATE_CURRENT_PAGE = "current_page";
90 public static final String BUNDLE_SAVED_STATE = "bundle_savedState";
91 public static final String STATE_ACC_FILTER = "account_filter";
92 private static final String PREF_THEME_ID = "themeId";
93 public AccountSummaryFragment mAccountSummaryFragment;
95 private View profileListHeadMore, profileListHeadCancel, profileListHeadAddProfile;
96 private View bTransactionListCancelDownload;
97 private SectionsPagerAdapter mSectionsPagerAdapter;
98 private ViewPager mViewPager;
99 private FloatingActionButton fab;
100 private ProfilesRecyclerViewAdapter mProfileListAdapter;
101 private int mCurrentPage;
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 MobileLedgerProfile profile;
109 protected void onStart() {
112 mViewPager.setCurrentItem(mCurrentPage, false);
115 protected void onSaveInstanceState(@NotNull Bundle outState) {
116 super.onSaveInstanceState(outState);
117 outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem());
118 if (Data.accountFilter.getValue() != null)
119 outState.putString(STATE_ACC_FILTER, Data.accountFilter.getValue());
122 protected void onDestroy() {
123 mSectionsPagerAdapter = null;
124 RecyclerView root = findViewById(R.id.nav_profile_list);
126 root.setAdapter(null);
128 drawer.removeDrawerListener(drawerListener);
129 drawerListener = null;
131 drawer.removeDrawerListener(barDrawerToggle);
132 barDrawerToggle = null;
133 if (mViewPager != null)
134 mViewPager.removeOnPageChangeListener(pageChangeListener);
135 pageChangeListener = null;
139 protected void setupProfileColors() {
140 SharedPreferences prefs = getPreferences(MODE_PRIVATE);
141 int profileColor = prefs.getInt(PREF_THEME_ID, -2);
142 if (profileColor == -2)
143 profileColor = Data.retrieveCurrentThemeIdFromDb();
144 Colors.setupTheme(this, profileColor);
145 Colors.profileThemeId = profileColor;
146 storeThemeIdInPrefs(profileColor);
149 protected void onResume() {
154 protected void onCreate(Bundle savedInstanceState) {
155 super.onCreate(savedInstanceState);
156 debug("flow", "MainActivity.onCreate()");
157 setContentView(R.layout.activity_main);
159 fab = findViewById(R.id.btn_add_transaction);
160 profileListHeadMore = findViewById(R.id.nav_profiles_start_edit);
161 profileListHeadCancel = findViewById(R.id.nav_profiles_cancel_edit);
162 LinearLayout profileListHeadMoreAndCancel =
163 findViewById(R.id.nav_profile_list_head_buttons);
164 profileListHeadAddProfile = findViewById(R.id.nav_new_profile_button);
165 drawer = findViewById(R.id.drawer_layout);
166 bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
167 mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
168 mViewPager = findViewById(R.id.root_frame);
170 Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
171 if (extra != null && savedInstanceState == null)
172 savedInstanceState = extra;
175 mToolbar = findViewById(R.id.toolbar);
176 setSupportActionBar(mToolbar);
178 Data.profile.observe(this, this::onProfileChanged);
180 Data.profiles.observe(this, this::onProfileListChanged);
182 if (barDrawerToggle == null) {
183 barDrawerToggle = new ActionBarDrawerToggle(this, drawer, mToolbar,
184 R.string.navigation_drawer_open, R.string.navigation_drawer_close);
185 drawer.addDrawerListener(barDrawerToggle);
187 barDrawerToggle.syncState();
190 PackageInfo pi = getApplicationContext().getPackageManager()
191 .getPackageInfo(getPackageName(), 0);
192 ((TextView) findViewById(R.id.nav_upper).findViewById(
193 R.id.drawer_version_text)).setText(pi.versionName);
194 ((TextView) findViewById(R.id.no_profiles_layout).findViewById(
195 R.id.drawer_version_text)).setText(pi.versionName);
197 catch (Exception e) {
201 markDrawerItemCurrent(R.id.nav_account_summary);
203 mViewPager.setAdapter(mSectionsPagerAdapter);
205 if (pageChangeListener == null) {
206 pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
208 public void onPageSelected(int position) {
209 mCurrentPage = 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 Data.accountFilter.setValue(savedInstanceState.getString(STATE_ACC_FILTER, null));
237 Data.lastUpdateDate.observe(this, this::updateLastUpdateDisplay);
239 findViewById(R.id.btn_no_profiles_add).setOnClickListener(
240 v -> startEditProfileActivity(null));
242 findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
244 findViewById(R.id.nav_new_profile_button).setOnClickListener(
245 v -> startEditProfileActivity(null));
247 RecyclerView root = findViewById(R.id.nav_profile_list);
249 throw new RuntimeException("Can't get hold on the transaction value view");
251 if (mProfileListAdapter == null)
252 mProfileListAdapter = new ProfilesRecyclerViewAdapter();
253 root.setAdapter(mProfileListAdapter);
255 mProfileListAdapter.editingProfiles.observe(this, newValue -> {
257 profileListHeadMore.setVisibility(View.GONE);
258 profileListHeadCancel.setVisibility(View.VISIBLE);
259 profileListHeadAddProfile.setVisibility(View.VISIBLE);
260 if (drawer.isDrawerOpen(GravityCompat.START)) {
261 profileListHeadMore.startAnimation(
262 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
263 profileListHeadCancel.startAnimation(
264 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
265 profileListHeadAddProfile.startAnimation(
266 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
270 profileListHeadCancel.setVisibility(View.GONE);
271 profileListHeadMore.setVisibility(View.VISIBLE);
272 profileListHeadAddProfile.setVisibility(View.GONE);
273 if (drawer.isDrawerOpen(GravityCompat.START)) {
274 profileListHeadCancel.startAnimation(
275 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
276 profileListHeadMore.startAnimation(
277 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in));
278 profileListHeadAddProfile.startAnimation(
279 AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out));
283 mProfileListAdapter.notifyDataSetChanged();
286 LinearLayoutManager llm = new LinearLayoutManager(this);
288 llm.setOrientation(RecyclerView.VERTICAL);
289 root.setLayoutManager(llm);
291 profileListHeadMore.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
292 profileListHeadCancel.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
293 profileListHeadMoreAndCancel.setOnClickListener(
294 (v) -> mProfileListAdapter.flipEditingProfiles());
295 if (drawerListener == null) {
296 drawerListener = new DrawerLayout.SimpleDrawerListener() {
298 public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
299 if (slideOffset > 0.2)
303 public void onDrawerClosed(View drawerView) {
304 super.onDrawerClosed(drawerView);
305 mProfileListAdapter.setAnimationsEnabled(false);
306 mProfileListAdapter.editingProfiles.setValue(false);
307 Data.drawerOpen.setValue(false);
311 public void onDrawerOpened(View drawerView) {
312 super.onDrawerOpened(drawerView);
313 mProfileListAdapter.setAnimationsEnabled(true);
314 Data.drawerOpen.setValue(true);
318 drawer.addDrawerListener(drawerListener);
321 Data.drawerOpen.observe(this, open -> {
328 private void scheduleDataRetrievalIfStale(Date lastUpdate) {
329 long now = new Date().getTime();
330 if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
331 if (lastUpdate == null)
332 debug("db::", "WEB data never fetched. scheduling a fetch");
334 debug("db", String.format(Locale.ENGLISH,
335 "WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
336 lastUpdate.getTime() / 1000f, now / 1000f));
338 Data.scheduleTransactionListRetrieval(this);
341 private void createShortcuts(List<MobileLedgerProfile> list) {
342 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
345 ShortcutManager sm = getSystemService(ShortcutManager.class);
346 List<ShortcutInfo> shortcuts = new ArrayList<>();
348 for (MobileLedgerProfile p : list) {
349 if (shortcuts.size() >= sm.getMaxShortcutCountPerActivity())
352 if (!p.isPostingPermitted())
355 final ShortcutInfo.Builder builder =
356 new ShortcutInfo.Builder(this, "new_transaction_" + p.getUuid());
357 ShortcutInfo si = builder.setShortLabel(p.getName())
358 .setIcon(Icon.createWithResource(this,
359 R.drawable.thick_plus_icon))
360 .setIntent(new Intent(Intent.ACTION_VIEW, null, this,
361 NewTransactionActivity.class).putExtra("profile_uuid",
368 sm.setDynamicShortcuts(shortcuts);
370 private void onProfileListChanged(List<MobileLedgerProfile> newList) {
371 if (newList == null) {
372 // profiles not yet loaded from DB
373 findViewById(R.id.loading_layout).setVisibility(View.VISIBLE);
374 findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
375 findViewById(R.id.main_app_layout).setVisibility(View.GONE);
379 if (newList.isEmpty()) {
380 findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE);
381 findViewById(R.id.main_app_layout).setVisibility(View.GONE);
382 findViewById(R.id.loading_layout).setVisibility(View.GONE);
386 findViewById(R.id.main_app_layout).setVisibility(View.VISIBLE);
387 findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
388 findViewById(R.id.loading_layout).setVisibility(View.GONE);
390 findViewById(R.id.nav_profile_list).setMinimumHeight(
391 (int) (getResources().getDimension(R.dimen.thumb_row_height) * newList.size()));
393 debug("profiles", "profile list changed");
394 mProfileListAdapter.notifyDataSetChanged();
396 createShortcuts(newList);
399 * called when the current profile has changed
401 private void onProfileChanged(MobileLedgerProfile profile) {
402 boolean haveProfile = profile != null;
405 setTitle(profile.getName());
407 setTitle(R.string.app_name);
409 this.profile = profile;
411 int newProfileTheme = haveProfile ? profile.getThemeHue() : -1;
412 if (newProfileTheme != Colors.profileThemeId) {
414 String.format(Locale.ENGLISH, "profile theme %d → %d", Colors.profileThemeId,
416 Colors.profileThemeId = newProfileTheme;
417 profileThemeChanged();
418 // profileThemeChanged would restart the activity, so no need to reload the
423 findViewById(R.id.no_profiles_layout).setVisibility(haveProfile ? View.GONE : View.VISIBLE);
424 findViewById(R.id.pager_layout).setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
426 mProfileListAdapter.notifyDataSetChanged();
428 Data.transactions.clear();
429 debug("transactions", "requesting list reload");
430 TransactionListViewModel.scheduleTransactionListReload();
432 Data.accounts.clear();
433 AccountSummaryViewModel.scheduleAccountListReload();
436 if (profile.isPostingPermitted()) {
437 mToolbar.setSubtitle(null);
441 mToolbar.setSubtitle(R.string.profile_subtitle_read_only);
446 mToolbar.setSubtitle(null);
450 updateLastUpdateTextFromDB();
452 private void updateLastUpdateDisplay(Date newValue) {
453 LinearLayout l = findViewById(R.id.transactions_last_update_layout);
454 TextView v = findViewById(R.id.transactions_last_update);
455 if (newValue == null) {
456 l.setVisibility(View.INVISIBLE);
457 debug("main", "no last update date :(");
460 final String text = DateFormat.getDateTimeInstance()
463 l.setVisibility(View.VISIBLE);
464 debug("main", String.format("Date formatted: %s", text));
467 scheduleDataRetrievalIfStale(newValue);
469 private void profileThemeChanged() {
470 storeThemeIdInPrefs(profile.getThemeHue());
472 // un-hook all observed LiveData
473 Data.profile.removeObservers(this);
474 Data.profiles.removeObservers(this);
475 Data.lastUpdateDate.removeObservers(this);
479 private void storeThemeIdInPrefs(int themeId) {
480 // store the new theme id in the preferences
481 SharedPreferences prefs = getPreferences(MODE_PRIVATE);
482 SharedPreferences.Editor e = prefs.edit();
483 e.putInt(PREF_THEME_ID, themeId);
486 public void startEditProfileActivity(MobileLedgerProfile profile) {
487 Intent intent = new Intent(this, ProfileDetailActivity.class);
488 Bundle args = new Bundle();
489 if (profile != null) {
490 int index = Data.getProfileIndex(profile);
492 intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
494 intent.putExtras(args);
495 startActivity(intent, args);
497 private void setupProfile() {
498 MLDB.getOption(MLDB.OPT_PROFILE_UUID, null, new GetOptCallback() {
500 protected void onResult(String profileUUID) {
501 MobileLedgerProfile startupProfile;
503 startupProfile = Data.getProfile(profileUUID);
504 Data.setCurrentProfile(startupProfile);
508 public void fabNewTransactionClicked(View view) {
509 Intent intent = new Intent(this, NewTransactionActivity.class);
510 startActivity(intent);
511 overridePendingTransition(R.anim.slide_in_up, R.anim.dummy);
513 public void markDrawerItemCurrent(int id) {
514 TextView item = drawer.findViewById(id);
515 item.setBackgroundColor(Colors.tableRowDarkBG);
517 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
518 for (int i = 0; i < actions.getChildCount(); i++) {
519 View view = actions.getChildAt(i);
520 if (view.getId() != id) {
521 view.setBackgroundColor(Color.TRANSPARENT);
525 public void onAccountSummaryClicked(View view) {
526 drawer.closeDrawers();
528 showAccountSummaryFragment();
530 private void showAccountSummaryFragment() {
531 mViewPager.setCurrentItem(0, true);
532 Data.accountFilter.setValue(null);
534 public void onLatestTransactionsClicked(View view) {
535 drawer.closeDrawers();
537 showTransactionsFragment((String) null);
539 private void showTransactionsFragment(String accName) {
540 Data.accountFilter.setValue(accName);
541 mViewPager.setCurrentItem(1, true);
543 private void showTransactionsFragment(LedgerAccount account) {
544 showTransactionsFragment((account == null) ? null : account.getName());
546 public void showAccountTransactions(LedgerAccount account) {
547 mBackMeansToAccountList = true;
548 showTransactionsFragment(account);
551 public void onBackPressed() {
552 DrawerLayout drawer = findViewById(R.id.drawer_layout);
553 if (drawer.isDrawerOpen(GravityCompat.START)) {
554 drawer.closeDrawer(GravityCompat.START);
557 if (mBackMeansToAccountList && (mViewPager.getCurrentItem() == 1)) {
558 Data.accountFilter.setValue(null);
559 showAccountSummaryFragment();
560 mBackMeansToAccountList = false;
563 debug("fragments", String.format(Locale.ENGLISH, "manager stack: %d",
564 getSupportFragmentManager().getBackStackEntryCount()));
566 super.onBackPressed();
570 public void updateLastUpdateTextFromDB() {
574 long last_update = profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L);
576 debug("transactions", String.format(Locale.ENGLISH, "Last update = %d", last_update));
577 if (last_update == 0) {
578 Data.lastUpdateDate.postValue(null);
581 Data.lastUpdateDate.postValue(new Date(last_update));
584 public void onStopTransactionRefreshClick(View view) {
585 debug("interactive", "Cancelling transactions refresh");
586 Data.stopTransactionsRetrieval();
587 bTransactionListCancelDownload.setEnabled(false);
589 public void onRetrieveDone(String error) {
590 Data.transactionRetrievalDone();
591 findViewById(R.id.transaction_progress_layout).setVisibility(View.GONE);
594 updateLastUpdateTextFromDB();
596 new RefreshDescriptionsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
597 TransactionListViewModel.scheduleTransactionListReload();
600 Snackbar.make(mViewPager, error, Snackbar.LENGTH_LONG)
603 public void onRetrieveStart() {
604 ProgressBar progressBar = findViewById(R.id.transaction_list_progress_bar);
605 bTransactionListCancelDownload.setEnabled(true);
606 ColorStateList csl = Colors.getColorStateList();
607 progressBar.setIndeterminateTintList(csl);
608 progressBar.setProgressTintList(csl);
609 progressBar.setIndeterminate(true);
610 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
611 progressBar.setProgress(0, false);
613 progressBar.setProgress(0);
614 findViewById(R.id.transaction_progress_layout).setVisibility(View.VISIBLE);
616 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
617 ProgressBar progressBar = findViewById(R.id.transaction_list_progress_bar);
618 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
619 (progress.getTotal() == 0))
621 progressBar.setIndeterminate(true);
624 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
625 progressBar.setMin(0);
627 progressBar.setMax(progress.getTotal());
628 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
629 progressBar.setProgress(progress.getProgress(), true);
632 progressBar.setProgress(progress.getProgress());
633 progressBar.setIndeterminate(false);
636 public void fabShouldShow() {
637 if ((profile != null) && profile.isPostingPermitted() && !drawer.isOpen())
642 public void fabHide() {
645 public void onAccountSummaryRowViewClicked(View view) {
647 switch (view.getId()) {
648 case R.id.account_expander:
649 row = (ViewGroup) view.getParent()
653 case R.id.account_expander_container:
654 case R.id.account_row_acc_name:
655 row = (ViewGroup) view.getParent()
659 row = (ViewGroup) view.getParent();
663 LedgerAccount acc = (LedgerAccount) row.getTag();
664 switch (view.getId()) {
665 case R.id.account_row_acc_name:
666 case R.id.account_expander:
667 case R.id.account_expander_container:
668 debug("accounts", "Account expander clicked");
669 if (!acc.hasSubAccounts())
672 boolean wasExpanded = acc.isExpanded();
674 View arrow = row.findViewById(R.id.account_expander_container);
676 arrow.clearAnimation();
677 ViewPropertyAnimator animator = arrow.animate();
679 acc.toggleExpanded();
680 DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
681 new Object[]{acc.isExpanded(), acc.getName(), profile.getUuid()
685 debug("accounts", String.format("Collapsing account '%s'", acc.getName()));
686 arrow.setRotation(0);
687 animator.rotationBy(180);
689 // removing all child accounts from the view
690 int start = -1, count = 0;
691 try (LockHolder ignored = Data.accounts.lockForWriting()) {
692 for (int i = 0; i < Data.accounts.size(); i++) {
693 if (acc.isParentOf(Data.accounts.get(i))) {
694 // debug("accounts", String.format("Found a child '%s' at position
696 // Data.accounts.get(i).getName(), i));
705 // String.format("Found a non-child '%s' at position %d",
706 // Data.accounts.get(i).getName(), i));
713 for (int j = 0; j < count; j++) {
714 // debug("accounts", String.format("Removing item %d: %s", start + j,
715 // Data.accounts.get(start).getName()));
716 Data.accounts.removeQuietly(start);
719 mAccountSummaryFragment.modelAdapter.notifyItemRangeRemoved(start,
725 debug("accounts", String.format("Expanding account '%s'", acc.getName()));
726 arrow.setRotation(180);
727 animator.rotationBy(-180);
728 List<LedgerAccount> children = profile.loadVisibleChildAccountsOf(acc);
729 try (LockHolder ignored = Data.accounts.lockForWriting()) {
730 int parentPos = Data.accounts.indexOf(acc);
731 if (parentPos != -1) {
732 // may have disappeared in a concurrent refresh operation
733 Data.accounts.addAllQuietly(parentPos + 1, children);
734 mAccountSummaryFragment.modelAdapter.notifyItemRangeInserted(
735 parentPos + 1, children.size());
740 case R.id.account_row_acc_amounts:
741 if (acc.getAmountCount() > AccountSummaryAdapter.AMOUNT_LIMIT) {
742 acc.toggleAmountsExpanded();
744 "update accounts set amounts_expanded=? where name=? and profile=?",
745 new Object[]{acc.amountsExpanded(), acc.getName(), profile.getUuid()
747 Data.accounts.triggerItemChangedNotification(acc);
753 public class SectionsPagerAdapter extends FragmentPagerAdapter {
755 SectionsPagerAdapter(FragmentManager fm) {
756 super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
761 public Fragment getItem(int position) {
762 debug("main", String.format(Locale.ENGLISH, "Switching to fragment %d", position));
765 // debug("flow", "Creating account summary fragment");
766 return mAccountSummaryFragment = new AccountSummaryFragment();
768 return new TransactionListFragment();
770 throw new IllegalStateException(
771 String.format("Unexpected fragment index: " + "%d", position));
776 public int getCount() {