]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
9f2777b2fa01b24bf0257f1803b3b0ee3c8bc574
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
1 /*
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.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
19
20 import android.content.Intent;
21 import android.content.pm.PackageInfo;
22 import android.content.res.ColorStateList;
23 import android.graphics.Color;
24 import android.os.AsyncTask;
25 import android.os.Build;
26 import android.os.Bundle;
27 import android.util.Log;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.ViewPropertyAnimator;
31 import android.view.animation.Animation;
32 import android.view.animation.AnimationUtils;
33 import android.widget.LinearLayout;
34 import android.widget.ProgressBar;
35 import android.widget.TextView;
36 import android.widget.Toast;
37
38 import com.google.android.material.floatingactionbutton.FloatingActionButton;
39
40 import net.ktnx.mobileledger.R;
41 import net.ktnx.mobileledger.async.DbOpQueue;
42 import net.ktnx.mobileledger.async.RefreshDescriptionsTask;
43 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
44 import net.ktnx.mobileledger.model.Data;
45 import net.ktnx.mobileledger.model.LedgerAccount;
46 import net.ktnx.mobileledger.model.MobileLedgerProfile;
47 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
48 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
49 import net.ktnx.mobileledger.ui.profiles.ProfilesRecyclerViewAdapter;
50 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
51 import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel;
52 import net.ktnx.mobileledger.utils.Colors;
53 import net.ktnx.mobileledger.utils.LockHolder;
54 import net.ktnx.mobileledger.utils.MLDB;
55
56 import java.lang.ref.WeakReference;
57 import java.text.DateFormat;
58 import java.util.Date;
59 import java.util.List;
60 import java.util.Observable;
61 import java.util.Observer;
62
63 import androidx.appcompat.app.ActionBarDrawerToggle;
64 import androidx.appcompat.widget.Toolbar;
65 import androidx.core.view.GravityCompat;
66 import androidx.drawerlayout.widget.DrawerLayout;
67 import androidx.fragment.app.Fragment;
68 import androidx.fragment.app.FragmentManager;
69 import androidx.fragment.app.FragmentPagerAdapter;
70 import androidx.recyclerview.widget.LinearLayoutManager;
71 import androidx.recyclerview.widget.RecyclerView;
72 import androidx.viewpager.widget.ViewPager;
73
74 public class MainActivity extends ProfileThemedActivity {
75     private static final String STATE_CURRENT_PAGE = "current_page";
76     private static final String BUNDLE_SAVED_STATE = "bundle_savedState";
77     public AccountSummaryFragment mAccountSummaryFragment;
78     DrawerLayout drawer;
79     private LinearLayout profileListContainer;
80     private View profileListHeadArrow, profileListHeadMore, profileListHeadCancel;
81     private LinearLayout profileListHeadMoreAndCancel;
82     private FragmentManager fragmentManager;
83     private TextView tvLastUpdate;
84     private RetrieveTransactionsTask retrieveTransactionsTask;
85     private View bTransactionListCancelDownload;
86     private ProgressBar progressBar;
87     private LinearLayout progressLayout;
88     private SectionsPagerAdapter mSectionsPagerAdapter;
89     private ViewPager mViewPager;
90     private FloatingActionButton fab;
91     private boolean profileModificationEnabled = false;
92     private boolean profileListExpanded = false;
93     private ProfilesRecyclerViewAdapter mProfileListAdapter;
94     @Override
95     protected void onStart() {
96         super.onStart();
97
98         setupProfile();
99
100         updateLastUpdateTextFromDB();
101         Date lastUpdate = Data.lastUpdateDate.get();
102
103         long now = new Date().getTime();
104         if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
105             if (lastUpdate == null) Log.d("db::", "WEB data never fetched. scheduling a fetch");
106             else Log.d("db",
107                     String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
108                             lastUpdate.getTime() / 1000f, now / 1000f));
109
110             scheduleTransactionListRetrieval();
111         }
112     }
113     @Override
114     protected void onSaveInstanceState(Bundle outState) {
115         super.onSaveInstanceState(outState);
116         outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem());
117     }
118     @Override
119     protected void onCreate(Bundle savedInstanceState) {
120         super.onCreate(savedInstanceState);
121
122         setContentView(R.layout.activity_main);
123
124         fab = findViewById(R.id.btn_add_transaction);
125         profileListContainer = findViewById(R.id.nav_profile_list_container);
126         profileListHeadArrow = findViewById(R.id.nav_profiles_arrow);
127         profileListHeadMore = findViewById(R.id.nav_profiles_start_edit);
128         profileListHeadCancel = findViewById(R.id.nav_profiles_cancel_edit);
129         profileListHeadMoreAndCancel = findViewById(R.id.nav_profile_list_head_buttons);
130         drawer = findViewById(R.id.drawer_layout);
131         tvLastUpdate = findViewById(R.id.transactions_last_update);
132         bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
133         progressBar = findViewById(R.id.transaction_list_progress_bar);
134         progressLayout = findViewById(R.id.transaction_progress_layout);
135         fragmentManager = getSupportFragmentManager();
136         mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
137         mViewPager = findViewById(R.id.root_frame);
138
139         Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
140         if (extra != null && savedInstanceState == null) savedInstanceState = extra;
141
142
143         Toolbar toolbar = findViewById(R.id.toolbar);
144         setSupportActionBar(toolbar);
145
146         Data.profile.addObserver((o, arg) -> {
147             MobileLedgerProfile profile = Data.profile.get();
148             runOnUiThread(() -> {
149                 if (profile == null) setTitle(R.string.app_name);
150                 else setTitle(profile.getName());
151                 updateLastUpdateTextFromDB();
152                 if (profile.isPostingPermitted()) {
153                     toolbar.setSubtitle(null);
154                     fab.show();
155                 }
156                 else {
157                     toolbar.setSubtitle(R.string.profile_subitlte_read_only);
158                     fab.hide();
159                 }
160
161                 int newProfileTheme = profile.getThemeId();
162                 if (newProfileTheme != Colors.profileThemeId) {
163                     Log.d("profiles", String.format("profile theme %d → %d", Colors.profileThemeId,
164                             newProfileTheme));
165                     profileThemeChanged();
166                     Colors.profileThemeId = newProfileTheme;
167                 }
168             });
169         });
170         Data.profiles.addObserver((o, arg) -> {
171             findViewById(R.id.nav_profile_list).setMinimumHeight(
172                     (int) (getResources().getDimension(R.dimen.thumb_row_height) *
173                            Data.profiles.size()));
174             mProfileListAdapter.notifyDataSetChanged();
175         });
176
177         ActionBarDrawerToggle toggle =
178                 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
179                         R.string.navigation_drawer_close);
180         drawer.addDrawerListener(toggle);
181         toggle.syncState();
182
183         TextView ver = drawer.findViewById(R.id.drawer_version_text);
184
185         try {
186             PackageInfo pi =
187                     getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
188             ver.setText(pi.versionName);
189         }
190         catch (Exception e) {
191             e.printStackTrace();
192         }
193
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");
198
199         markDrawerItemCurrent(R.id.nav_account_summary);
200
201         mViewPager.setAdapter(mSectionsPagerAdapter);
202         mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
203             @Override
204             public void onPageSelected(int position) {
205                 switch (position) {
206                     case 0:
207                         markDrawerItemCurrent(R.id.nav_account_summary);
208                         break;
209                     case 1:
210                         markDrawerItemCurrent(R.id.nav_latest_transactions);
211                         break;
212                     default:
213                         Log.e("MainActivity", String.format("Unexpected page index %d", position));
214                 }
215
216                 super.onPageSelected(position);
217             }
218         });
219
220         if (savedInstanceState != null) {
221             int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
222             if (currentPage != -1) {
223                 mViewPager.setCurrentItem(currentPage, false);
224             }
225         }
226
227         Data.lastUpdateDate.addObserver((o, arg) -> {
228             Log.d("main", "lastUpdateDate changed");
229             runOnUiThread(this::updateLastUpdateDisplay);
230         });
231
232         updateLastUpdateDisplay();
233
234         findViewById(R.id.btn_no_profiles_add)
235                 .setOnClickListener(v -> startEditProfileActivity(null));
236
237         findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
238
239         findViewById(R.id.nav_new_profile_button)
240                 .setOnClickListener(v -> startEditProfileActivity(null));
241
242         RecyclerView root = findViewById(R.id.nav_profile_list);
243         if (root == null)
244             throw new RuntimeException("Can't get hold on the transaction value view");
245
246         mProfileListAdapter = new ProfilesRecyclerViewAdapter();
247         root.setAdapter(mProfileListAdapter);
248
249         mProfileListAdapter.addEditingProfilesObserver(new Observer() {
250             @Override
251             public void update(Observable o, Object arg) {
252                 if (mProfileListAdapter.isEditingProfiles()) {
253                     profileListHeadArrow.clearAnimation();
254                     profileListHeadArrow.setVisibility(View.GONE);
255                     profileListHeadMore.setVisibility(View.GONE);
256                     profileListHeadCancel.setVisibility(View.VISIBLE);
257                 }
258                 else {
259                     profileListHeadArrow.setRotation(180f);
260                     profileListHeadArrow.setVisibility(View.VISIBLE);
261                     profileListHeadCancel.setVisibility(View.GONE);
262                     profileListHeadMore.setVisibility(View.GONE);
263                     profileListHeadMore
264                             .setVisibility(profileListExpanded ? View.VISIBLE : View.GONE);
265                 }
266             }
267         });
268
269         LinearLayoutManager llm = new LinearLayoutManager(this);
270
271         llm.setOrientation(RecyclerView.VERTICAL);
272         root.setLayoutManager(llm);
273
274         profileListHeadMore.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
275         profileListHeadCancel.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
276         profileListHeadMoreAndCancel
277                 .setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
278
279         drawer.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
280             @Override
281             public void onDrawerClosed(View drawerView) {
282                 super.onDrawerClosed(drawerView);
283                 collapseProfileList();
284             }
285         });
286     }
287     private void updateLastUpdateDisplay() {
288         TextView v = findViewById(R.id.transactions_last_update);
289         Date date = Data.lastUpdateDate.get();
290         if (date == null) {
291             v.setText(R.string.transaction_last_update_never);
292             Log.d("main", "no last update date :(");
293         }
294         else {
295             final String text = DateFormat.getDateTimeInstance().format(date);
296             v.setText(text);
297             Log.d("main", String.format("Date formatted: %s", text));
298         }
299     }
300     private void profileThemeChanged() {
301         setupProfileColors();
302
303         Bundle bundle = new Bundle();
304         onSaveInstanceState(bundle);
305         // restart activity to reflect theme change
306         finish();
307         Intent intent = new Intent(this, this.getClass());
308         intent.putExtra(BUNDLE_SAVED_STATE, bundle);
309         startActivity(intent);
310     }
311     public void startEditProfileActivity(MobileLedgerProfile profile) {
312         Intent intent = new Intent(this, ProfileDetailActivity.class);
313         Bundle args = new Bundle();
314         if (profile != null) {
315             int index = Data.getProfileIndex(profile);
316             if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
317         }
318         intent.putExtras(args);
319         startActivity(intent, args);
320     }
321     private void setupProfile() {
322         String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
323         MobileLedgerProfile profile;
324
325         profile = MobileLedgerProfile.loadAllFromDB(profileUUID);
326
327         if (Data.profiles.isEmpty()) {
328             findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE);
329             findViewById(R.id.pager_layout).setVisibility(View.GONE);
330             return;
331         }
332
333         findViewById(R.id.pager_layout).setVisibility(View.VISIBLE);
334         findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
335
336         if (profile == null) profile = Data.profiles.get(0);
337
338         if (profile == null) throw new AssertionError("profile must have a value");
339
340         Data.setCurrentProfile(profile);
341     }
342     public void fabNewTransactionClicked(View view) {
343         Intent intent = new Intent(this, NewTransactionActivity.class);
344         startActivity(intent);
345         overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
346     }
347     public void navSettingsClicked(View view) {
348         Intent intent = new Intent(this, SettingsActivity.class);
349         startActivity(intent);
350         drawer.closeDrawers();
351     }
352     public void markDrawerItemCurrent(int id) {
353         TextView item = drawer.findViewById(id);
354         item.setBackgroundColor(Colors.tableRowDarkBG);
355
356         LinearLayout actions = drawer.findViewById(R.id.nav_actions);
357         for (int i = 0; i < actions.getChildCount(); i++) {
358             View view = actions.getChildAt(i);
359             if (view.getId() != id) {
360                 view.setBackgroundColor(Color.TRANSPARENT);
361             }
362         }
363     }
364     public void onAccountSummaryClicked(View view) {
365         drawer.closeDrawers();
366
367         showAccountSummaryFragment();
368     }
369     private void showAccountSummaryFragment() {
370         mViewPager.setCurrentItem(0, true);
371         TransactionListFragment.accountFilter.set(null);
372 //        FragmentTransaction ft = fragmentManager.beginTransaction();
373 //        accountSummaryFragment = new AccountSummaryFragment();
374 //        ft.replace(R.id.root_frame, accountSummaryFragment);
375 //        ft.commit();
376 //        currentFragment = accountSummaryFragment;
377     }
378     public void onLatestTransactionsClicked(View view) {
379         drawer.closeDrawers();
380
381         showTransactionsFragment((String) null);
382     }
383     private void resetFragmentBackStack() {
384 //        fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
385     }
386     private void showTransactionsFragment(String accName) {
387         TransactionListFragment.accountFilter.set(accName);
388         TransactionListFragment.accountFilter.notifyObservers();
389         mViewPager.setCurrentItem(1, true);
390     }
391     private void showTransactionsFragment(LedgerAccount account) {
392         showTransactionsFragment((account == null) ? (String) null : account.getName());
393 //        FragmentTransaction ft = fragmentManager.beginTransaction();
394 //        if (transactionListFragment == null) {
395 //            Log.d("flow", "MainActivity creating TransactionListFragment");
396 //            transactionListFragment = new TransactionListFragment();
397 //        }
398 //        Bundle bundle = new Bundle();
399 //        if (account != null) {
400 //            bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
401 //                    account.getName());
402 //        }
403 //        transactionListFragment.setArguments(bundle);
404 //        ft.replace(R.id.root_frame, transactionListFragment);
405 //        if (account != null)
406 //            ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
407 //        ft.commit();
408 //
409 //        currentFragment = transactionListFragment;
410     }
411     public void showAccountTransactions(LedgerAccount account) {
412         showTransactionsFragment(account);
413     }
414     @Override
415     public void onBackPressed() {
416         DrawerLayout drawer = findViewById(R.id.drawer_layout);
417         if (drawer.isDrawerOpen(GravityCompat.START)) {
418             drawer.closeDrawer(GravityCompat.START);
419         }
420         else {
421             Log.d("fragments",
422                     String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
423
424             super.onBackPressed();
425         }
426     }
427     public void updateLastUpdateTextFromDB() {
428         {
429             final MobileLedgerProfile profile = Data.profile.get();
430             long last_update =
431                     (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
432
433             Log.d("transactions", String.format("Last update = %d", last_update));
434             if (last_update == 0) {
435                 Data.lastUpdateDate.set(null);
436             }
437             else {
438                 Data.lastUpdateDate.set(new Date(last_update));
439             }
440         }
441     }
442     public void scheduleTransactionListRetrieval() {
443         if (Data.profile.get() == null) return;
444
445         retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
446
447         retrieveTransactionsTask.execute();
448     }
449     public void onStopTransactionRefreshClick(View view) {
450         Log.d("interactive", "Cancelling transactions refresh");
451         if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
452         bTransactionListCancelDownload.setEnabled(false);
453     }
454     public void onRetrieveDone(String error) {
455         progressLayout.setVisibility(View.GONE);
456
457         if (error == null) {
458             updateLastUpdateTextFromDB();
459
460             new RefreshDescriptionsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
461             TransactionListViewModel.scheduleTransactionListReload();
462         }
463         else Toast.makeText(this, error, Toast.LENGTH_LONG).show();
464     }
465     public void onRetrieveStart() {
466         bTransactionListCancelDownload.setEnabled(true);
467         progressBar.setIndeterminateTintList(ColorStateList.valueOf(Colors.primary));
468         progressBar.setProgressTintList(ColorStateList.valueOf(Colors.primary));
469         progressBar.setIndeterminate(true);
470         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
471         else progressBar.setProgress(0);
472         progressLayout.setVisibility(View.VISIBLE);
473     }
474     public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
475         if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
476             (progress.getTotal() == 0))
477         {
478             progressBar.setIndeterminate(true);
479         }
480         else {
481             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
482                 progressBar.setMin(0);
483             }
484             progressBar.setMax(progress.getTotal());
485             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
486                 progressBar.setProgress(progress.getProgress(), true);
487             }
488             else progressBar.setProgress(progress.getProgress());
489             progressBar.setIndeterminate(false);
490         }
491     }
492     public void fabShouldShow() {
493         MobileLedgerProfile profile = Data.profile.get();
494         if ((profile != null) && profile.isPostingPermitted()) fab.show();
495     }
496     public void navProfilesHeadClicked(View view) {
497         if (profileListExpanded) {
498             collapseProfileList();
499         }
500         else {
501             expandProfileList();
502         }
503     }
504     private void expandProfileList() {
505         profileListExpanded = true;
506
507
508         profileListContainer.setVisibility(View.VISIBLE);
509         profileListContainer.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_down));
510         profileListHeadArrow.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180));
511         profileListHeadMore.setVisibility(View.VISIBLE);
512         profileListHeadMore.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
513         findViewById(R.id.nav_profile_list).setMinimumHeight(
514                 (int) (getResources().getDimension(R.dimen.thumb_row_height) *
515                        Data.profiles.size()));
516     }
517     private void collapseProfileList() {
518         profileListExpanded = false;
519
520         final Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_up);
521         animation.setAnimationListener(new Animation.AnimationListener() {
522             @Override
523             public void onAnimationStart(Animation animation) {
524
525             }
526             @Override
527             public void onAnimationEnd(Animation animation) {
528                 profileListContainer.setVisibility(View.GONE);
529             }
530             @Override
531             public void onAnimationRepeat(Animation animation) {
532
533             }
534         });
535         mProfileListAdapter.stopEditingProfiles();
536
537         profileListContainer.startAnimation(animation);
538         profileListHeadArrow.setRotation(0f);
539         profileListHeadArrow
540                 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180_back));
541         profileListHeadMore.setVisibility(View.GONE);
542     }
543     public void onProfileRowClicked(View v) {
544         Data.setCurrentProfile((MobileLedgerProfile) v.getTag());
545     }
546     public void enableProfileModifications() {
547         profileModificationEnabled = true;
548         ViewGroup profileList = findViewById(R.id.nav_profile_list);
549         for (int i = 0; i < profileList.getChildCount(); i++) {
550             View aRow = profileList.getChildAt(i);
551             aRow.findViewById(R.id.profile_list_edit_button).setVisibility(View.VISIBLE);
552             aRow.findViewById(R.id.profile_list_rearrange_handle).setVisibility(View.VISIBLE);
553         }
554         // FIXME enable rearranging
555
556     }
557     public void disableProfileModifications() {
558         profileModificationEnabled = false;
559         ViewGroup profileList = findViewById(R.id.nav_profile_list);
560         for (int i = 0; i < profileList.getChildCount(); i++) {
561             View aRow = profileList.getChildAt(i);
562             aRow.findViewById(R.id.profile_list_edit_button).setVisibility(View.GONE);
563             aRow.findViewById(R.id.profile_list_rearrange_handle).setVisibility(View.INVISIBLE);
564         }
565         // FIXME disable rearranging
566
567     }
568     public void onAccountSummaryRowViewClicked(View view) {
569         ViewGroup row = (ViewGroup) view.getParent();
570         LedgerAccount acc = (LedgerAccount) row.getTag();
571         switch (view.getId()) {
572             case R.id.account_row_acc_name:
573             case R.id.account_expander_container:
574                 Log.d("accounts", "Account expander clicked");
575                 if (!acc.hasSubAccounts()) return;
576
577                 boolean wasExpanded = acc.isExpanded();
578
579                 View arrow = row.findViewById(R.id.account_expander_container);
580
581                 arrow.clearAnimation();
582                 ViewPropertyAnimator animator = arrow.animate();
583
584                 acc.toggleExpanded();
585                 DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
586                         new Object[]{acc.isExpanded(), acc.getName(), Data.profile.get().getUuid()
587                         });
588
589                 if (wasExpanded) {
590                     Log.d("accounts", String.format("Collapsing account '%s'", acc.getName()));
591                     arrow.setRotation(0);
592                     animator.rotationBy(180);
593
594                     // removing all child accounts from the view
595                     int start = -1, count = 0;
596                     try (LockHolder lh = Data.accounts.lockForWriting()) {
597                         for (int i = 0; i < Data.accounts.size(); i++) {
598                             if (acc.isParentOf(Data.accounts.get(i))) {
599 //                                Log.d("accounts", String.format("Found a child '%s' at position %d",
600 //                                        Data.accounts.get(i).getName(), i));
601                                 if (start == -1) {
602                                     start = i;
603                                 }
604                                 count++;
605                             }
606                             else {
607                                 if (start != -1) {
608 //                                    Log.d("accounts",
609 //                                            String.format("Found a non-child '%s' at position %d",
610 //                                                    Data.accounts.get(i).getName(), i));
611                                     break;
612                                 }
613                             }
614                         }
615
616                         if (start != -1) {
617                             for (int j = 0; j < count; j++) {
618 //                                Log.d("accounts", String.format("Removing item %d: %s", start + j,
619 //                                        Data.accounts.get(start).getName()));
620                                 Data.accounts.removeQuietly(start);
621                             }
622
623                             mAccountSummaryFragment.modelAdapter
624                                     .notifyItemRangeRemoved(start, count);
625                         }
626                     }
627                 }
628                 else {
629                     Log.d("accounts", String.format("Expanding account '%s'", acc.getName()));
630                     arrow.setRotation(180);
631                     animator.rotationBy(-180);
632                     List<LedgerAccount> children =
633                             Data.profile.get().loadVisibleChildAccountsOf(acc);
634                     try (LockHolder lh = Data.accounts.lockForWriting()) {
635                         int parentPos = Data.accounts.indexOf(acc);
636                         if (parentPos != -1) {
637                             // may have disappeared in a concurrent refresh operation
638                             Data.accounts.addAllQuietly(parentPos + 1, children);
639                             mAccountSummaryFragment.modelAdapter
640                                     .notifyItemRangeInserted(parentPos + 1, children.size());
641                         }
642                     }
643                 }
644                 break;
645             case R.id.account_row_acc_amounts:
646                 showAccountTransactions(acc);
647                 break;
648         }
649     }
650
651     public class SectionsPagerAdapter extends FragmentPagerAdapter {
652
653         public SectionsPagerAdapter(FragmentManager fm) {
654             super(fm);
655         }
656
657         @Override
658         public Fragment getItem(int position) {
659             Log.d("main", String.format("Switching to fragment %d", position));
660             switch (position) {
661                 case 0:
662 //                    Log.d("flow", "Creating account summary fragment");
663                     return mAccountSummaryFragment = new AccountSummaryFragment();
664                 case 1:
665                     return new TransactionListFragment();
666                 default:
667                     throw new IllegalStateException(
668                             String.format("Unexpected fragment index: " + "%d", position));
669             }
670         }
671
672         @Override
673         public int getCount() {
674             return 2;
675         }
676     }
677 }