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.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;
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.GetOptCallback;
59 import net.ktnx.mobileledger.utils.LockHolder;
60 import net.ktnx.mobileledger.utils.MLDB;
62 import org.jetbrains.annotations.NotNull;
64 import java.text.DateFormat;
65 import java.util.ArrayList;
66 import java.util.Date;
67 import java.util.List;
68 import java.util.Locale;
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 private static final String PREF_THEME_ID = "themeId";
88 public AccountSummaryFragment mAccountSummaryFragment;
90 private View profileListHeadMore, profileListHeadCancel, profileListHeadAddProfile;
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 ProfilesRecyclerViewAdapter mProfileListAdapter;
99 private int mCurrentPage;
100 private String mAccountFilter;
101 private boolean mBackMeansToAccountList = false;
102 private Toolbar mToolbar;
103 private DrawerLayout.SimpleDrawerListener drawerListener;
104 private ActionBarDrawerToggle barDrawerToggle;
105 private ViewPager.SimpleOnPageChangeListener pageChangeListener;
106 private MobileLedgerProfile profile;
108 protected void onStart() {
111 debug("flow", "MainActivity.onStart()");
112 mViewPager.setCurrentItem(mCurrentPage, false);
113 if (mAccountFilter != null) showTransactionsFragment(mAccountFilter);
114 else Data.accountFilter.setValue(null);
118 protected void onSaveInstanceState(@NotNull Bundle outState) {
119 super.onSaveInstanceState(outState);
120 outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem());
121 if (mAccountFilter != null) outState.putString(STATE_ACC_FILTER, mAccountFilter);
124 protected void onDestroy() {
125 mSectionsPagerAdapter = null;
126 RecyclerView root = findViewById(R.id.nav_profile_list);
127 if (root != null) root.setAdapter(null);
128 if (drawer != null) drawer.removeDrawerListener(drawerListener);
129 drawerListener = null;
130 if (drawer != null) drawer.removeDrawerListener(barDrawerToggle);
131 barDrawerToggle = null;
132 if (mViewPager != null) mViewPager.removeOnPageChangeListener(pageChangeListener);
133 pageChangeListener = null;
137 protected void setupProfileColors() {
138 SharedPreferences prefs = getPreferences(MODE_PRIVATE);
139 int profileColor = prefs.getInt(PREF_THEME_ID, -2);
140 if (profileColor == -2) profileColor = Data.retrieveCurrentThemeIdFromDb();
141 Colors.setupTheme(this, profileColor);
142 Colors.profileThemeId = profileColor;
143 storeThemeIdInPrefs(profileColor);
146 protected void onCreate(Bundle savedInstanceState) {
147 super.onCreate(savedInstanceState);
148 debug("flow", "MainActivity.onCreate()");
149 setContentView(R.layout.activity_main);
151 fab = findViewById(R.id.btn_add_transaction);
152 profileListHeadMore = findViewById(R.id.nav_profiles_start_edit);
153 profileListHeadCancel = findViewById(R.id.nav_profiles_cancel_edit);
154 LinearLayout profileListHeadMoreAndCancel =
155 findViewById(R.id.nav_profile_list_head_buttons);
156 profileListHeadAddProfile = findViewById(R.id.nav_new_profile_button);
157 drawer = findViewById(R.id.drawer_layout);
158 bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
159 progressBar = findViewById(R.id.transaction_list_progress_bar);
160 progressLayout = findViewById(R.id.transaction_progress_layout);
161 fragmentManager = getSupportFragmentManager();
162 mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
163 mViewPager = findViewById(R.id.root_frame);
165 Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
166 if (extra != null && savedInstanceState == null) savedInstanceState = extra;
169 mToolbar = findViewById(R.id.toolbar);
170 setSupportActionBar(mToolbar);
172 Data.profile.observe(this, this::onProfileChanged);
174 Data.profiles.observe(this, this::onProfileListChanged);
176 if (barDrawerToggle == null) {
177 barDrawerToggle = new ActionBarDrawerToggle(this, drawer, mToolbar,
178 R.string.navigation_drawer_open, R.string.navigation_drawer_close);
179 drawer.addDrawerListener(barDrawerToggle);
181 barDrawerToggle.syncState();
183 TextView ver = drawer.findViewById(R.id.drawer_version_text);
187 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
188 ver.setText(pi.versionName);
190 catch (Exception e) {
194 if (progressBar == null)
195 throw new RuntimeException("Can't get hold on the transaction value progress bar");
196 if (progressLayout == null) throw new RuntimeException(
197 "Can't get hold on the transaction value progress bar layout");
199 markDrawerItemCurrent(R.id.nav_account_summary);
201 mViewPager.setAdapter(mSectionsPagerAdapter);
203 if (pageChangeListener == null) {
204 pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
206 public void onPageSelected(int position) {
209 markDrawerItemCurrent(R.id.nav_account_summary);
212 markDrawerItemCurrent(R.id.nav_latest_transactions);
215 Log.e("MainActivity",
216 String.format("Unexpected page index %d", position));
219 super.onPageSelected(position);
222 mViewPager.addOnPageChangeListener(pageChangeListener);
226 if (savedInstanceState != null) {
227 int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
228 if (currentPage != -1) {
229 mCurrentPage = currentPage;
231 mAccountFilter = savedInstanceState.getString(STATE_ACC_FILTER, null);
233 else mAccountFilter = null;
235 Data.lastUpdateDate.observe(this, this::updateLastUpdateDisplay);
237 findViewById(R.id.btn_no_profiles_add)
238 .setOnClickListener(v -> startEditProfileActivity(null));
240 findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
242 findViewById(R.id.nav_new_profile_button)
243 .setOnClickListener(v -> startEditProfileActivity(null));
245 RecyclerView root = findViewById(R.id.nav_profile_list);
247 throw new RuntimeException("Can't get hold on the transaction value view");
249 if (mProfileListAdapter == null) mProfileListAdapter = new ProfilesRecyclerViewAdapter();
250 root.setAdapter(mProfileListAdapter);
252 mProfileListAdapter.editingProfiles
253 .observe(this, newValue -> {
255 profileListHeadMore.setVisibility(View.GONE);
256 profileListHeadCancel.setVisibility(View.VISIBLE);
257 profileListHeadAddProfile.setVisibility(View.VISIBLE);
258 if (drawer.isDrawerOpen(GravityCompat.START)) {
259 profileListHeadMore.startAnimation(AnimationUtils
260 .loadAnimation(MainActivity.this, R.anim.fade_out));
261 profileListHeadCancel.startAnimation(AnimationUtils
262 .loadAnimation(MainActivity.this, R.anim.fade_in));
263 profileListHeadAddProfile.startAnimation(AnimationUtils
264 .loadAnimation(MainActivity.this, R.anim.fade_in));
268 profileListHeadCancel.setVisibility(View.GONE);
269 profileListHeadMore.setVisibility(View.VISIBLE);
270 profileListHeadAddProfile.setVisibility(View.GONE);
271 if (drawer.isDrawerOpen(GravityCompat.START)) {
272 profileListHeadCancel.startAnimation(AnimationUtils
273 .loadAnimation(MainActivity.this, R.anim.fade_out));
274 profileListHeadMore.startAnimation(AnimationUtils
275 .loadAnimation(MainActivity.this, R.anim.fade_in));
276 profileListHeadAddProfile.startAnimation(AnimationUtils
277 .loadAnimation(MainActivity.this, R.anim.fade_out));
281 mProfileListAdapter.notifyDataSetChanged();
284 LinearLayoutManager llm = new LinearLayoutManager(this);
286 llm.setOrientation(RecyclerView.VERTICAL);
287 root.setLayoutManager(llm);
289 profileListHeadMore.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
290 profileListHeadCancel.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
291 profileListHeadMoreAndCancel
292 .setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
296 private void scheduleDataRetrievalIfStale(Date lastUpdate) {
297 long now = new Date().getTime();
298 if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
299 if (lastUpdate == null) debug("db::", "WEB data never fetched. scheduling a fetch");
300 else debug("db", String.format(Locale.ENGLISH,
301 "WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
302 lastUpdate.getTime() / 1000f, now / 1000f));
304 Data.scheduleTransactionListRetrieval(this);
307 private void createShortcuts(List<MobileLedgerProfile> list) {
308 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;
310 List<ShortcutInfo> shortcuts = new ArrayList<>();
312 for (MobileLedgerProfile p : list) {
313 if (!p.isPostingPermitted()) continue;
315 ShortcutInfo si = new ShortcutInfo.Builder(this, "new_transaction_" + p.getUuid())
316 .setShortLabel(p.getName())
317 .setIcon(Icon.createWithResource(this, R.drawable.svg_thick_plus_white))
319 new Intent(Intent.ACTION_VIEW, null, this, NewTransactionActivity.class)
320 .putExtra("profile_uuid", p.getUuid())).setRank(i).build();
324 ShortcutManager sm = getSystemService(ShortcutManager.class);
325 sm.setDynamicShortcuts(shortcuts);
327 private void onProfileListChanged(List<MobileLedgerProfile> newList) {
328 if (newList == null) {
329 // profiles not yet loaded from DB
330 findViewById(R.id.loading_layout).setVisibility(View.VISIBLE);
331 findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
332 findViewById(R.id.pager_layout).setVisibility(View.GONE);
336 if (newList.isEmpty()) {
337 findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE);
338 findViewById(R.id.pager_layout).setVisibility(View.GONE);
339 findViewById(R.id.loading_layout).setVisibility(View.GONE);
343 findViewById(R.id.pager_layout).setVisibility(View.VISIBLE);
344 findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
345 findViewById(R.id.loading_layout).setVisibility(View.GONE);
347 findViewById(R.id.nav_profile_list).setMinimumHeight(
348 (int) (getResources().getDimension(R.dimen.thumb_row_height) * newList.size()));
350 debug("profiles", "profile list changed");
351 mProfileListAdapter.notifyDataSetChanged();
353 createShortcuts(newList);
356 * called when the current profile has changed
358 private void onProfileChanged(MobileLedgerProfile profile) {
359 boolean haveProfile = profile != null;
360 findViewById(R.id.no_profiles_layout).setVisibility(haveProfile ? View.GONE : View.VISIBLE);
361 findViewById(R.id.pager_layout).setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
363 if (haveProfile) setTitle(profile.getName());
364 else setTitle(R.string.app_name);
366 this.profile = profile;
368 mProfileListAdapter.notifyDataSetChanged();
370 int newProfileTheme = haveProfile ? profile.getThemeId() : -1;
371 if (newProfileTheme != Colors.profileThemeId) {
373 String.format(Locale.ENGLISH, "profile theme %d → %d", Colors.profileThemeId,
375 MainActivity.this.profileThemeChanged();
376 Colors.profileThemeId = newProfileTheme;
377 // profileThemeChanged would restart the activity, so no need to reload the
382 drawer.closeDrawers();
384 Data.transactions.clear();
385 debug("transactions", "requesting list reload");
386 TransactionListViewModel.scheduleTransactionListReload();
388 Data.accounts.clear();
389 AccountSummaryViewModel.scheduleAccountListReload();
392 if (profile.isPostingPermitted()) {
393 mToolbar.setSubtitle(null);
397 mToolbar.setSubtitle(R.string.profile_subitlte_read_only);
402 mToolbar.setSubtitle(null);
406 updateLastUpdateTextFromDB();
408 private void updateLastUpdateDisplay(Date newValue) {
409 LinearLayout l = findViewById(R.id.transactions_last_update_layout);
410 TextView v = findViewById(R.id.transactions_last_update);
411 if (newValue == null) {
412 l.setVisibility(View.INVISIBLE);
413 debug("main", "no last update date :(");
416 final String text = DateFormat.getDateTimeInstance().format(newValue);
418 l.setVisibility(View.VISIBLE);
419 debug("main", String.format("Date formatted: %s", text));
422 scheduleDataRetrievalIfStale(newValue);
424 private void profileThemeChanged() {
425 Bundle bundle = new Bundle();
426 onSaveInstanceState(bundle);
428 storeThemeIdInPrefs(profile.getThemeId());
430 // restart activity to reflect theme change
433 // un-hook all observed LiveData
434 Data.profile.removeObservers(this);
435 Data.profiles.removeObservers(this);
436 Data.lastUpdateDate.removeObservers(this);
437 Intent intent = new Intent(this, this.getClass());
438 intent.putExtra(BUNDLE_SAVED_STATE, bundle);
439 startActivity(intent);
441 private void storeThemeIdInPrefs(int themeId) {
442 // store the new theme id in the preferences
443 SharedPreferences prefs = getPreferences(MODE_PRIVATE);
444 SharedPreferences.Editor e = prefs.edit();
445 e.putInt(PREF_THEME_ID, themeId);
448 public void startEditProfileActivity(MobileLedgerProfile profile) {
449 Intent intent = new Intent(this, ProfileDetailActivity.class);
450 Bundle args = new Bundle();
451 if (profile != null) {
452 int index = Data.getProfileIndex(profile);
453 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
455 intent.putExtras(args);
456 startActivity(intent, args);
458 private void setupProfile() {
459 MLDB.getOption(MLDB.OPT_PROFILE_UUID, null, new GetOptCallback() {
461 protected void onResult(String profileUUID) {
462 MobileLedgerProfile startupProfile;
464 startupProfile = Data.getProfile(profileUUID);
465 Data.setCurrentProfile(startupProfile);
469 public void fabNewTransactionClicked(View view) {
470 Intent intent = new Intent(this, NewTransactionActivity.class);
471 startActivity(intent);
472 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
474 public void navSettingsClicked(View view) {
475 Intent intent = new Intent(this, SettingsActivity.class);
476 startActivity(intent);
477 drawer.closeDrawers();
479 public void markDrawerItemCurrent(int id) {
480 TextView item = drawer.findViewById(id);
481 item.setBackgroundColor(Colors.tableRowDarkBG);
483 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
484 for (int i = 0; i < actions.getChildCount(); i++) {
485 View view = actions.getChildAt(i);
486 if (view.getId() != id) {
487 view.setBackgroundColor(Color.TRANSPARENT);
491 public void onAccountSummaryClicked(View view) {
492 drawer.closeDrawers();
494 showAccountSummaryFragment();
496 private void showAccountSummaryFragment() {
497 mViewPager.setCurrentItem(0, true);
498 Data.accountFilter.setValue(null);
500 public void onLatestTransactionsClicked(View view) {
501 drawer.closeDrawers();
503 showTransactionsFragment((String) null);
505 private void showTransactionsFragment(String accName) {
506 Data.accountFilter.setValue(accName);
507 mViewPager.setCurrentItem(1, true);
509 private void showTransactionsFragment(LedgerAccount account) {
510 showTransactionsFragment((account == null) ? null : account.getName());
512 public void showAccountTransactions(LedgerAccount account) {
513 mBackMeansToAccountList = true;
514 showTransactionsFragment(account);
517 public void onBackPressed() {
518 DrawerLayout drawer = findViewById(R.id.drawer_layout);
519 if (drawer.isDrawerOpen(GravityCompat.START)) {
520 drawer.closeDrawer(GravityCompat.START);
523 if (mBackMeansToAccountList && (mViewPager.getCurrentItem() == 1)) {
524 Data.accountFilter.setValue(null);
525 showAccountSummaryFragment();
526 mBackMeansToAccountList = false;
529 debug("fragments", String.format(Locale.ENGLISH, "manager stack: %d",
530 fragmentManager.getBackStackEntryCount()));
532 super.onBackPressed();
536 public void updateLastUpdateTextFromDB() {
537 if (profile == null) return;
539 long last_update = profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L);
541 debug("transactions", String.format(Locale.ENGLISH, "Last update = %d", last_update));
542 if (last_update == 0) {
543 Data.lastUpdateDate.postValue(null);
546 Data.lastUpdateDate.postValue(new Date(last_update));
549 public void onStopTransactionRefreshClick(View view) {
550 debug("interactive", "Cancelling transactions refresh");
551 Data.stopTransactionsRetrieval();
552 bTransactionListCancelDownload.setEnabled(false);
554 public void onRetrieveDone(String error) {
555 Data.transactionRetrievalDone();
556 progressLayout.setVisibility(View.GONE);
559 updateLastUpdateTextFromDB();
561 new RefreshDescriptionsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
562 TransactionListViewModel.scheduleTransactionListReload();
564 else Toast.makeText(this, error, Toast.LENGTH_LONG).show();
566 public void onRetrieveStart() {
567 bTransactionListCancelDownload.setEnabled(true);
568 progressBar.setIndeterminateTintList(ColorStateList.valueOf(Colors.primary));
569 progressBar.setProgressTintList(ColorStateList.valueOf(Colors.primary));
570 progressBar.setIndeterminate(true);
571 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
572 else progressBar.setProgress(0);
573 progressLayout.setVisibility(View.VISIBLE);
575 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
576 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
577 (progress.getTotal() == 0))
579 progressBar.setIndeterminate(true);
582 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
583 progressBar.setMin(0);
585 progressBar.setMax(progress.getTotal());
586 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
587 progressBar.setProgress(progress.getProgress(), true);
589 else progressBar.setProgress(progress.getProgress());
590 progressBar.setIndeterminate(false);
593 public void fabShouldShow() {
594 if ((profile != null) && profile.isPostingPermitted()) fab.show();
596 public void fabHide() {
599 public void onAccountSummaryRowViewClicked(View view) {
601 if (view.getId() == R.id.account_expander) row = (ViewGroup) view.getParent().getParent();
602 else row = (ViewGroup) view.getParent();
604 LedgerAccount acc = (LedgerAccount) row.getTag();
605 switch (view.getId()) {
606 case R.id.account_row_acc_name:
607 case R.id.account_expander:
608 case R.id.account_expander_container:
609 debug("accounts", "Account expander clicked");
610 if (!acc.hasSubAccounts()) return;
612 boolean wasExpanded = acc.isExpanded();
614 View arrow = row.findViewById(R.id.account_expander_container);
616 arrow.clearAnimation();
617 ViewPropertyAnimator animator = arrow.animate();
619 acc.toggleExpanded();
620 DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
621 new Object[]{acc.isExpanded(), acc.getName(), profile.getUuid()
625 debug("accounts", String.format("Collapsing account '%s'", acc.getName()));
626 arrow.setRotation(0);
627 animator.rotationBy(180);
629 // removing all child accounts from the view
630 int start = -1, count = 0;
631 try (LockHolder ignored = Data.accounts.lockForWriting()) {
632 for (int i = 0; i < Data.accounts.size(); i++) {
633 if (acc.isParentOf(Data.accounts.get(i))) {
634 // debug("accounts", String.format("Found a child '%s' at position %d",
635 // Data.accounts.get(i).getName(), i));
644 // String.format("Found a non-child '%s' at position %d",
645 // Data.accounts.get(i).getName(), i));
652 for (int j = 0; j < count; j++) {
653 // debug("accounts", String.format("Removing item %d: %s", start + j,
654 // Data.accounts.get(start).getName()));
655 Data.accounts.removeQuietly(start);
658 mAccountSummaryFragment.modelAdapter
659 .notifyItemRangeRemoved(start, count);
664 debug("accounts", String.format("Expanding account '%s'", acc.getName()));
665 arrow.setRotation(180);
666 animator.rotationBy(-180);
667 List<LedgerAccount> children = profile.loadVisibleChildAccountsOf(acc);
668 try (LockHolder ignored = Data.accounts.lockForWriting()) {
669 int parentPos = Data.accounts.indexOf(acc);
670 if (parentPos != -1) {
671 // may have disappeared in a concurrent refresh operation
672 Data.accounts.addAllQuietly(parentPos + 1, children);
673 mAccountSummaryFragment.modelAdapter
674 .notifyItemRangeInserted(parentPos + 1, children.size());
679 case R.id.account_row_acc_amounts:
680 if (acc.getAmountCount() > AccountSummaryAdapter.AMOUNT_LIMIT) {
681 acc.toggleAmountsExpanded();
683 .add("update accounts set amounts_expanded=? where name=? and profile=?",
684 new Object[]{acc.amountsExpanded(), acc.getName(),
687 Data.accounts.triggerItemChangedNotification(acc);
693 public class SectionsPagerAdapter extends FragmentPagerAdapter {
695 SectionsPagerAdapter(FragmentManager fm) {
701 public Fragment getItem(int position) {
702 debug("main", String.format(Locale.ENGLISH, "Switching to fragment %d", position));
705 // debug("flow", "Creating account summary fragment");
706 return mAccountSummaryFragment = new AccountSummaryFragment();
708 return new TransactionListFragment();
710 throw new IllegalStateException(
711 String.format("Unexpected fragment index: " + "%d", position));
716 public int getCount() {