2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.activity;
20 import android.content.Intent;
21 import android.content.pm.PackageInfo;
22 import android.content.pm.ShortcutInfo;
23 import android.content.pm.ShortcutManager;
24 import android.content.res.ColorStateList;
25 import android.graphics.Color;
26 import android.graphics.drawable.Icon;
27 import android.os.AsyncTask;
28 import android.os.Build;
29 import android.os.Bundle;
30 import android.util.Log;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.view.ViewPropertyAnimator;
34 import android.view.animation.Animation;
35 import android.view.animation.AnimationUtils;
36 import android.widget.LinearLayout;
37 import android.widget.ProgressBar;
38 import android.widget.TextView;
39 import android.widget.Toast;
41 import com.google.android.material.floatingactionbutton.FloatingActionButton;
43 import net.ktnx.mobileledger.R;
44 import net.ktnx.mobileledger.async.DbOpQueue;
45 import net.ktnx.mobileledger.async.RefreshDescriptionsTask;
46 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
47 import net.ktnx.mobileledger.model.Data;
48 import net.ktnx.mobileledger.model.LedgerAccount;
49 import net.ktnx.mobileledger.model.MobileLedgerProfile;
50 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryAdapter;
51 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
52 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryViewModel;
53 import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
54 import net.ktnx.mobileledger.ui.profiles.ProfilesRecyclerViewAdapter;
55 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
56 import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel;
57 import net.ktnx.mobileledger.utils.Colors;
58 import net.ktnx.mobileledger.utils.LockHolder;
59 import net.ktnx.mobileledger.utils.MLDB;
61 import org.jetbrains.annotations.NotNull;
63 import java.lang.ref.WeakReference;
64 import java.text.DateFormat;
65 import java.util.ArrayList;
66 import java.util.Date;
67 import java.util.List;
68 import java.util.Observer;
70 import androidx.appcompat.app.ActionBarDrawerToggle;
71 import androidx.appcompat.widget.Toolbar;
72 import androidx.core.view.GravityCompat;
73 import androidx.drawerlayout.widget.DrawerLayout;
74 import androidx.fragment.app.Fragment;
75 import androidx.fragment.app.FragmentManager;
76 import androidx.fragment.app.FragmentPagerAdapter;
77 import androidx.recyclerview.widget.LinearLayoutManager;
78 import androidx.recyclerview.widget.RecyclerView;
79 import androidx.viewpager.widget.ViewPager;
81 import static net.ktnx.mobileledger.utils.Logger.debug;
83 public class MainActivity extends ProfileThemedActivity {
84 public static final String STATE_CURRENT_PAGE = "current_page";
85 public static final String BUNDLE_SAVED_STATE = "bundle_savedState";
86 public static final String STATE_ACC_FILTER = "account_filter";
87 public AccountSummaryFragment mAccountSummaryFragment;
89 private LinearLayout profileListContainer;
90 private View profileListHeadArrow, profileListHeadMore, profileListHeadCancel;
91 private FragmentManager fragmentManager;
92 private RetrieveTransactionsTask retrieveTransactionsTask;
93 private View bTransactionListCancelDownload;
94 private ProgressBar progressBar;
95 private LinearLayout progressLayout;
96 private SectionsPagerAdapter mSectionsPagerAdapter;
97 private ViewPager mViewPager;
98 private FloatingActionButton fab;
99 private boolean profileListExpanded = false;
100 private ProfilesRecyclerViewAdapter mProfileListAdapter;
101 private int mCurrentPage;
102 private String mAccountFilter;
103 private boolean mBackMeansToAccountList = false;
104 private Observer profileObserver;
105 private Observer profilesObserver;
106 private Toolbar mToolbar;
107 private DrawerLayout.SimpleDrawerListener drawerListener;
108 private ActionBarDrawerToggle barDrawerToggle;
109 private ViewPager.SimpleOnPageChangeListener pageChangeListener;
110 private Observer editingProfilesObserver;
112 protected void onStart() {
115 debug("flow", "MainActivity.onStart()");
116 mViewPager.setCurrentItem(mCurrentPage, false);
117 if (mAccountFilter != null) showTransactionsFragment(mAccountFilter);
118 else Data.accountFilter.setValue(null);
122 protected void onSaveInstanceState(Bundle outState) {
123 super.onSaveInstanceState(outState);
124 outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem());
125 if (mAccountFilter != null) outState.putString(STATE_ACC_FILTER, mAccountFilter);
128 protected void onDestroy() {
129 mSectionsPagerAdapter = null;
130 Data.profile.deleteObserver(profileObserver);
131 profileObserver = null;
132 Data.profiles.deleteObserver(profilesObserver);
133 profilesObserver = null;
134 RecyclerView root = findViewById(R.id.nav_profile_list);
135 if (root != null) root.setAdapter(null);
136 if (drawer != null) drawer.removeDrawerListener(drawerListener);
137 drawerListener = null;
138 if (drawer != null) drawer.removeDrawerListener(barDrawerToggle);
139 barDrawerToggle = null;
140 if (mViewPager != null) mViewPager.removeOnPageChangeListener(pageChangeListener);
141 pageChangeListener = null;
142 if (mProfileListAdapter != null)
143 mProfileListAdapter.deleteEditingProfilesObserver(editingProfilesObserver);
144 editingProfilesObserver = null;
148 protected void onCreate(Bundle savedInstanceState) {
149 super.onCreate(savedInstanceState);
150 debug("flow", "MainActivity.onCreate()");
151 int profileColor = Data.retrieveCurrentThemeIdFromDb();
152 Colors.setupTheme(this, profileColor);
153 Colors.profileThemeId = profileColor;
154 setContentView(R.layout.activity_main);
156 fab = findViewById(R.id.btn_add_transaction);
157 profileListContainer = findViewById(R.id.nav_profile_list_container);
158 profileListHeadArrow = findViewById(R.id.nav_profiles_arrow);
159 profileListHeadMore = findViewById(R.id.nav_profiles_start_edit);
160 profileListHeadCancel = findViewById(R.id.nav_profiles_cancel_edit);
161 profileListHeadMoreAndCancel = findViewById(R.id.nav_profile_list_head_buttons);
162 drawer = findViewById(R.id.drawer_layout);
163 bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
164 progressBar = findViewById(R.id.transaction_list_progress_bar);
165 progressLayout = findViewById(R.id.transaction_progress_layout);
166 fragmentManager = getSupportFragmentManager();
167 mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
168 mViewPager = findViewById(R.id.root_frame);
170 Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
171 if (extra != null && savedInstanceState == null) savedInstanceState = extra;
174 mToolbar = findViewById(R.id.toolbar);
175 setSupportActionBar(mToolbar);
177 if (profileObserver == null) {
178 profileObserver = (o, arg) -> onProfileChanged(arg);
179 Data.profile.addObserver(profileObserver);
182 if (profilesObserver == null) {
183 profilesObserver = (o, arg) -> onProfileListChanged(arg);
184 Data.profiles.addObserver(profilesObserver);
187 if (barDrawerToggle == null) {
188 barDrawerToggle = new ActionBarDrawerToggle(this, drawer, mToolbar,
189 R.string.navigation_drawer_open, R.string.navigation_drawer_close);
190 drawer.addDrawerListener(barDrawerToggle);
192 barDrawerToggle.syncState();
194 TextView ver = drawer.findViewById(R.id.drawer_version_text);
198 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
199 ver.setText(pi.versionName);
201 catch (Exception e) {
205 if (progressBar == null)
206 throw new RuntimeException("Can't get hold on the transaction value progress bar");
207 if (progressLayout == null) throw new RuntimeException(
208 "Can't get hold on the transaction value progress bar layout");
210 markDrawerItemCurrent(R.id.nav_account_summary);
212 mViewPager.setAdapter(mSectionsPagerAdapter);
214 if (pageChangeListener == null) {
215 pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
217 public void onPageSelected(int position) {
220 markDrawerItemCurrent(R.id.nav_account_summary);
223 markDrawerItemCurrent(R.id.nav_latest_transactions);
226 Log.e("MainActivity",
227 String.format("Unexpected page index %d", position));
230 super.onPageSelected(position);
233 mViewPager.addOnPageChangeListener(pageChangeListener);
237 if (savedInstanceState != null) {
238 int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
239 if (currentPage != -1) {
240 mCurrentPage = currentPage;
242 mAccountFilter = savedInstanceState.getString(STATE_ACC_FILTER, null);
244 else mAccountFilter = null;
246 Data.lastUpdateDate.observe(this, this::updateLastUpdateDisplay);
248 findViewById(R.id.btn_no_profiles_add)
249 .setOnClickListener(v -> startEditProfileActivity(null));
251 findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
253 findViewById(R.id.nav_new_profile_button)
254 .setOnClickListener(v -> startEditProfileActivity(null));
256 RecyclerView root = findViewById(R.id.nav_profile_list);
258 throw new RuntimeException("Can't get hold on the transaction value view");
260 if (mProfileListAdapter == null) mProfileListAdapter = new ProfilesRecyclerViewAdapter();
261 root.setAdapter(mProfileListAdapter);
263 if (editingProfilesObserver == null) {
264 editingProfilesObserver = (o, arg) -> {
265 if (mProfileListAdapter.isEditingProfiles()) {
266 profileListHeadArrow.clearAnimation();
267 profileListHeadArrow.setVisibility(View.GONE);
268 profileListHeadMore.setVisibility(View.GONE);
270 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
271 profileListHeadCancel.setVisibility(View.VISIBLE);
272 profileListHeadCancel
273 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
276 profileListHeadArrow.setRotation(180f);
277 profileListHeadArrow.setVisibility(View.VISIBLE);
278 profileListHeadCancel.setVisibility(View.GONE);
279 profileListHeadCancel
280 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
281 profileListHeadMore.setVisibility(View.GONE);
282 if (profileListExpanded) {
283 profileListHeadMore.setVisibility(View.VISIBLE);
285 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
287 else profileListHeadMore.setVisibility(View.GONE);
290 mProfileListAdapter.addEditingProfilesObserver(editingProfilesObserver);
293 LinearLayoutManager llm = new LinearLayoutManager(this);
295 llm.setOrientation(RecyclerView.VERTICAL);
296 root.setLayoutManager(llm);
298 profileListHeadMore.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
299 profileListHeadCancel.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
300 profileListHeadMoreAndCancel
301 .setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
303 if (drawerListener == null) {
304 drawerListener = new DrawerLayout.SimpleDrawerListener() {
306 public void onDrawerClosed(View drawerView) {
307 super.onDrawerClosed(drawerView);
308 collapseProfileList();
311 drawer.addDrawerListener(drawerListener);
314 findViewById(R.id.nav_profile_list_head_layout)
315 .setOnClickListener(this::navProfilesHeadClicked);
316 findViewById(R.id.nav_profiles_label).setOnClickListener(this::navProfilesHeadClicked);
317 boolean initialStart = Data.profile.get() == null;
319 if (!initialStart) onProfileChanged(null);
321 updateLastUpdateTextFromDB();
323 private void scheduleDataRetrievalIfStale(Date lastUpdate) {
324 long now = new Date().getTime();
325 if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
326 if (lastUpdate == null) debug("db::", "WEB data never fetched. scheduling a fetch");
328 String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
329 lastUpdate.getTime() / 1000f, now / 1000f));
331 scheduleTransactionListRetrieval();
334 private void createShortcuts() {
335 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return;
337 List<ShortcutInfo> shortcuts = new ArrayList<>();
338 try (LockHolder lh = Data.profiles.lockForReading()) {
339 for (int i = 0; i < Data.profiles.size(); i++) {
340 MobileLedgerProfile p = Data.profiles.get(i);
341 if (!p.isPostingPermitted()) continue;
343 ShortcutInfo si = new ShortcutInfo.Builder(this, "new_transaction_" + p.getUuid())
344 .setShortLabel(p.getName())
345 .setIcon(Icon.createWithResource(this, R.drawable.svg_thick_plus_white))
346 .setIntent(new Intent(Intent.ACTION_VIEW, null, this,
347 NewTransactionActivity.class).putExtra("profile_uuid", p.getUuid()))
352 ShortcutManager sm = getSystemService(ShortcutManager.class);
353 sm.setDynamicShortcuts(shortcuts);
355 private void onProfileListChanged(Object arg) {
356 findViewById(R.id.nav_profile_list).setMinimumHeight(
357 (int) (getResources().getDimension(R.dimen.thumb_row_height) *
358 Data.profiles.size()));
360 debug("profiles", "profile list changed");
361 if (arg == null) mProfileListAdapter.notifyDataSetChanged();
362 else mProfileListAdapter.notifyItemChanged((int) arg);
366 private void onProfileChanged(Object arg) {
367 MobileLedgerProfile profile = Data.profile.get();
368 MainActivity.this.runOnUiThread(() -> {
370 boolean haveProfile = profile != null;
371 findViewById(R.id.no_profiles_layout)
372 .setVisibility(haveProfile ? View.GONE : View.VISIBLE);
373 findViewById(R.id.pager_layout)
374 .setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
376 if (profile == null) MainActivity.this.setTitle(R.string.app_name);
377 else MainActivity.this.setTitle(profile.getName());
378 MainActivity.this.updateLastUpdateTextFromDB();
382 MobileLedgerProfile old = (MobileLedgerProfile) arg;
383 old_index = Data.getProfileIndex(old);
384 new_index = Data.getProfileIndex(profile);
387 if ((old_index != -1) && (new_index != -1)) {
388 mProfileListAdapter.notifyItemChanged(old_index);
389 mProfileListAdapter.notifyItemChanged(new_index);
391 else mProfileListAdapter.notifyDataSetChanged();
393 MainActivity.this.collapseProfileList();
395 int newProfileTheme = (profile == null) ? -1 : profile.getThemeId();
396 if (newProfileTheme != Colors.profileThemeId) {
397 debug("profiles", String.format("profile theme %d → %d", Colors.profileThemeId,
399 MainActivity.this.profileThemeChanged();
400 Colors.profileThemeId = newProfileTheme;
401 // profileThemeChanged would restart the activity, so no need to reload the
405 drawer.closeDrawers();
407 Data.transactions.clear();
408 debug("transactions", "requesting list reload");
409 TransactionListViewModel.scheduleTransactionListReload();
411 Data.accounts.clear();
412 AccountSummaryViewModel.scheduleAccountListReload();
414 if (profile == null) {
415 mToolbar.setSubtitle(null);
419 if (profile.isPostingPermitted()) {
420 mToolbar.setSubtitle(null);
424 mToolbar.setSubtitle(R.string.profile_subitlte_read_only);
429 updateLastUpdateTextFromDB();
432 private void updateLastUpdateDisplay(Date newValue) {
433 LinearLayout l = findViewById(R.id.transactions_last_update_layout);
434 TextView v = findViewById(R.id.transactions_last_update);
435 if (newValue == null) {
436 l.setVisibility(View.INVISIBLE);
437 debug("main", "no last update date :(");
440 final String text = DateFormat.getDateTimeInstance().format(newValue);
442 l.setVisibility(View.VISIBLE);
443 debug("main", String.format("Date formatted: %s", text));
446 scheduleDataRetrievalIfStale(newValue);
449 public void finish() {
450 if (profilesObserver != null) {
451 Data.profiles.deleteObserver(profilesObserver);
452 profilesObserver = null;
455 if (profileObserver != null) {
456 Data.profile.deleteObserver(profileObserver);
457 profileObserver = null;
462 private void profileThemeChanged() {
463 setupProfileColors();
465 Bundle bundle = new Bundle();
466 onSaveInstanceState(bundle);
467 // restart activity to reflect theme change
469 Intent intent = new Intent(this, this.getClass());
470 intent.putExtra(BUNDLE_SAVED_STATE, bundle);
471 startActivity(intent);
473 public void startEditProfileActivity(MobileLedgerProfile profile) {
474 Intent intent = new Intent(this, ProfileDetailActivity.class);
475 Bundle args = new Bundle();
476 if (profile != null) {
477 int index = Data.getProfileIndex(profile);
478 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
480 intent.putExtras(args);
481 startActivity(intent, args);
483 private void setupProfile() {
484 String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
485 MobileLedgerProfile profile;
487 profile = Data.getProfile(profileUUID);
489 if (Data.profiles.isEmpty()) {
490 findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE);
491 findViewById(R.id.pager_layout).setVisibility(View.GONE);
495 findViewById(R.id.pager_layout).setVisibility(View.VISIBLE);
496 findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
498 if (profile == null) profile = Data.profiles.get(0);
500 if (profile == null) throw new AssertionError("profile must have a value");
502 Data.setCurrentProfile(profile);
504 public void fabNewTransactionClicked(View view) {
505 Intent intent = new Intent(this, NewTransactionActivity.class);
506 startActivity(intent);
507 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
509 public void navSettingsClicked(View view) {
510 Intent intent = new Intent(this, SettingsActivity.class);
511 startActivity(intent);
512 drawer.closeDrawers();
514 public void markDrawerItemCurrent(int id) {
515 TextView item = drawer.findViewById(id);
516 item.setBackgroundColor(Colors.tableRowDarkBG);
518 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
519 for (int i = 0; i < actions.getChildCount(); i++) {
520 View view = actions.getChildAt(i);
521 if (view.getId() != id) {
522 view.setBackgroundColor(Color.TRANSPARENT);
526 public void onAccountSummaryClicked(View view) {
527 drawer.closeDrawers();
529 showAccountSummaryFragment();
531 private void showAccountSummaryFragment() {
532 mViewPager.setCurrentItem(0, true);
533 Data.accountFilter.setValue(null);
535 public void onLatestTransactionsClicked(View view) {
536 drawer.closeDrawers();
538 showTransactionsFragment((String) null);
540 private void showTransactionsFragment(String accName) {
541 Data.accountFilter.setValue(accName);
542 mViewPager.setCurrentItem(1, true);
544 private void showTransactionsFragment(LedgerAccount account) {
545 showTransactionsFragment((account == null) ? (String) null : account.getName());
547 public void showAccountTransactions(LedgerAccount account) {
548 mBackMeansToAccountList = true;
549 showTransactionsFragment(account);
552 public void onBackPressed() {
553 DrawerLayout drawer = findViewById(R.id.drawer_layout);
554 if (drawer.isDrawerOpen(GravityCompat.START)) {
555 drawer.closeDrawer(GravityCompat.START);
558 if (mBackMeansToAccountList && (mViewPager.getCurrentItem() == 1)) {
559 Data.accountFilter.setValue(null);
560 showAccountSummaryFragment();
561 mBackMeansToAccountList = false;
564 debug("fragments", String.format("manager stack: %d",
565 fragmentManager.getBackStackEntryCount()));
567 super.onBackPressed();
571 public void updateLastUpdateTextFromDB() {
572 final MobileLedgerProfile profile = Data.profile.get();
573 long last_update = (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
575 debug("transactions", String.format("Last update = %d", last_update));
576 if (last_update == 0) {
577 Data.lastUpdateDate.postValue(null);
580 Data.lastUpdateDate.postValue(new Date(last_update));
583 public void scheduleTransactionListRetrieval() {
584 if (Data.profile.get() == null) return;
586 retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
588 retrieveTransactionsTask.execute();
590 public void onStopTransactionRefreshClick(View view) {
591 debug("interactive", "Cancelling transactions refresh");
592 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
593 bTransactionListCancelDownload.setEnabled(false);
595 public void onRetrieveDone(String error) {
596 progressLayout.setVisibility(View.GONE);
599 updateLastUpdateTextFromDB();
601 new RefreshDescriptionsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
602 TransactionListViewModel.scheduleTransactionListReload();
604 else Toast.makeText(this, error, Toast.LENGTH_LONG).show();
606 public void onRetrieveStart() {
607 bTransactionListCancelDownload.setEnabled(true);
608 progressBar.setIndeterminateTintList(ColorStateList.valueOf(Colors.primary));
609 progressBar.setProgressTintList(ColorStateList.valueOf(Colors.primary));
610 progressBar.setIndeterminate(true);
611 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
612 else progressBar.setProgress(0);
613 progressLayout.setVisibility(View.VISIBLE);
615 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
616 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
617 (progress.getTotal() == 0))
619 progressBar.setIndeterminate(true);
622 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
623 progressBar.setMin(0);
625 progressBar.setMax(progress.getTotal());
626 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
627 progressBar.setProgress(progress.getProgress(), true);
629 else progressBar.setProgress(progress.getProgress());
630 progressBar.setIndeterminate(false);
633 public void fabShouldShow() {
634 MobileLedgerProfile profile = Data.profile.get();
635 if ((profile != null) && profile.isPostingPermitted()) fab.show();
637 public void navProfilesHeadClicked(View view) {
638 if (profileListExpanded) {
639 collapseProfileList();
645 private void expandProfileList() {
646 profileListExpanded = true;
649 profileListContainer.setVisibility(View.VISIBLE);
650 profileListContainer.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_down));
651 profileListHeadArrow.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180));
652 profileListHeadMore.setVisibility(View.VISIBLE);
653 profileListHeadMore.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
654 findViewById(R.id.nav_profile_list).setMinimumHeight(
655 (int) (getResources().getDimension(R.dimen.thumb_row_height) *
656 Data.profiles.size()));
658 private void collapseProfileList() {
659 boolean wasExpanded = profileListExpanded;
660 profileListExpanded = false;
663 final Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_up);
664 animation.setAnimationListener(new Animation.AnimationListener() {
666 public void onAnimationStart(Animation animation) {
670 public void onAnimationEnd(Animation animation) {
671 profileListContainer.setVisibility(View.GONE);
674 public void onAnimationRepeat(Animation animation) {
678 mProfileListAdapter.stopEditingProfiles();
680 profileListContainer.startAnimation(animation);
681 profileListHeadArrow.setRotation(0f);
683 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180_back));
684 final Animation moreAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
685 moreAnimation.setAnimationListener(new Animation.AnimationListener() {
687 public void onAnimationStart(Animation animation) {
690 public void onAnimationEnd(Animation animation) {
691 profileListHeadMore.setVisibility(View.GONE);
694 public void onAnimationRepeat(Animation animation) {
697 profileListHeadMore.startAnimation(moreAnimation);
700 profileListContainer.setVisibility(View.GONE);
701 profileListHeadArrow.setRotation(0f);
702 profileListHeadMore.setVisibility(View.GONE);
705 public void onAccountSummaryRowViewClicked(View view) {
707 if (view.getId() == R.id.account_expander) row = (ViewGroup) view.getParent().getParent();
708 else row = (ViewGroup) view.getParent();
710 LedgerAccount acc = (LedgerAccount) row.getTag();
711 switch (view.getId()) {
712 case R.id.account_row_acc_name:
713 case R.id.account_expander:
714 case R.id.account_expander_container:
715 debug("accounts", "Account expander clicked");
716 if (!acc.hasSubAccounts()) return;
718 boolean wasExpanded = acc.isExpanded();
720 View arrow = row.findViewById(R.id.account_expander_container);
722 arrow.clearAnimation();
723 ViewPropertyAnimator animator = arrow.animate();
725 acc.toggleExpanded();
726 DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
727 new Object[]{acc.isExpanded(), acc.getName(), Data.profile.get().getUuid()
731 debug("accounts", String.format("Collapsing account '%s'", acc.getName()));
732 arrow.setRotation(0);
733 animator.rotationBy(180);
735 // removing all child accounts from the view
736 int start = -1, count = 0;
737 try (LockHolder lh = Data.accounts.lockForWriting()) {
738 for (int i = 0; i < Data.accounts.size(); i++) {
739 if (acc.isParentOf(Data.accounts.get(i))) {
740 // debug("accounts", String.format("Found a child '%s' at position %d",
741 // Data.accounts.get(i).getName(), i));
750 // String.format("Found a non-child '%s' at position %d",
751 // Data.accounts.get(i).getName(), i));
758 for (int j = 0; j < count; j++) {
759 // debug("accounts", String.format("Removing item %d: %s", start + j,
760 // Data.accounts.get(start).getName()));
761 Data.accounts.removeQuietly(start);
764 mAccountSummaryFragment.modelAdapter
765 .notifyItemRangeRemoved(start, count);
770 debug("accounts", String.format("Expanding account '%s'", acc.getName()));
771 arrow.setRotation(180);
772 animator.rotationBy(-180);
773 List<LedgerAccount> children =
774 Data.profile.get().loadVisibleChildAccountsOf(acc);
775 try (LockHolder lh = Data.accounts.lockForWriting()) {
776 int parentPos = Data.accounts.indexOf(acc);
777 if (parentPos != -1) {
778 // may have disappeared in a concurrent refresh operation
779 Data.accounts.addAllQuietly(parentPos + 1, children);
780 mAccountSummaryFragment.modelAdapter
781 .notifyItemRangeInserted(parentPos + 1, children.size());
786 case R.id.account_row_acc_amounts:
787 if (acc.getAmountCount() > AccountSummaryAdapter.AMOUNT_LIMIT) {
788 acc.toggleAmountsExpanded();
790 .add("update accounts set amounts_expanded=? where name=? and profile=?",
791 new Object[]{acc.amountsExpanded(), acc.getName(),
792 Data.profile.get().getUuid()
794 Data.accounts.triggerItemChangedNotification(acc);
800 public class SectionsPagerAdapter extends FragmentPagerAdapter {
802 public SectionsPagerAdapter(FragmentManager fm) {
808 public Fragment getItem(int position) {
809 debug("main", String.format("Switching to fragment %d", position));
812 // debug("flow", "Creating account summary fragment");
813 return mAccountSummaryFragment = new AccountSummaryFragment();
815 return new TransactionListFragment();
817 throw new IllegalStateException(
818 String.format("Unexpected fragment index: " + "%d", position));
823 public int getCount() {