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 java.lang.ref.WeakReference;
62 import java.text.DateFormat;
63 import java.util.ArrayList;
64 import java.util.Date;
65 import java.util.List;
66 import java.util.Observer;
68 import androidx.appcompat.app.ActionBarDrawerToggle;
69 import androidx.appcompat.widget.Toolbar;
70 import androidx.core.view.GravityCompat;
71 import androidx.drawerlayout.widget.DrawerLayout;
72 import androidx.fragment.app.Fragment;
73 import androidx.fragment.app.FragmentManager;
74 import androidx.fragment.app.FragmentPagerAdapter;
75 import androidx.recyclerview.widget.LinearLayoutManager;
76 import androidx.recyclerview.widget.RecyclerView;
77 import androidx.viewpager.widget.ViewPager;
79 public class MainActivity extends ProfileThemedActivity {
80 public static final String STATE_CURRENT_PAGE = "current_page";
81 public static final String BUNDLE_SAVED_STATE = "bundle_savedState";
82 public static final String STATE_ACC_FILTER = "account_filter";
83 public AccountSummaryFragment mAccountSummaryFragment;
85 private LinearLayout profileListContainer;
86 private View profileListHeadArrow, profileListHeadMore, profileListHeadCancel;
87 private LinearLayout profileListHeadMoreAndCancel;
88 private FragmentManager fragmentManager;
89 private TextView tvLastUpdate;
90 private RetrieveTransactionsTask retrieveTransactionsTask;
91 private View bTransactionListCancelDownload;
92 private ProgressBar progressBar;
93 private LinearLayout progressLayout;
94 private SectionsPagerAdapter mSectionsPagerAdapter;
95 private ViewPager mViewPager;
96 private FloatingActionButton fab;
97 private boolean profileModificationEnabled = false;
98 private boolean profileListExpanded = false;
99 private ProfilesRecyclerViewAdapter mProfileListAdapter;
100 private int mCurrentPage;
101 private String mAccountFilter;
102 private boolean mBackMeansToAccountList = false;
103 private Observer profileObserver;
104 private Observer profilesObserver;
105 private Toolbar mToolbar;
106 private DrawerLayout.SimpleDrawerListener drawerListener;
107 private ActionBarDrawerToggle barDrawerToggle;
108 private ViewPager.SimpleOnPageChangeListener pageChangeListener;
109 private Observer editingProfilesObserver;
111 protected void onStart() {
114 Log.d("flow", "MainActivity.onStart()");
115 mViewPager.setCurrentItem(mCurrentPage, false);
116 if (mAccountFilter != null) showTransactionsFragment(mAccountFilter);
117 else Data.accountFilter.set(null);
121 protected void onSaveInstanceState(Bundle outState) {
122 super.onSaveInstanceState(outState);
123 outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem());
124 if (Data.accountFilter.get() != null)
125 outState.putString(STATE_ACC_FILTER, Data.accountFilter.get());
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 Log.d("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 tvLastUpdate = findViewById(R.id.transactions_last_update);
164 bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
165 progressBar = findViewById(R.id.transaction_list_progress_bar);
166 progressLayout = findViewById(R.id.transaction_progress_layout);
167 fragmentManager = getSupportFragmentManager();
168 mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
169 mViewPager = findViewById(R.id.root_frame);
171 Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
172 if (extra != null && savedInstanceState == null) savedInstanceState = extra;
175 mToolbar = findViewById(R.id.toolbar);
176 setSupportActionBar(mToolbar);
178 if (profileObserver == null) {
179 profileObserver = (o, arg) -> onProfileChanged(arg);
180 Data.profile.addObserver(profileObserver);
183 if (profilesObserver == null) {
184 profilesObserver = (o, arg) -> onProfileListChanged(arg);
185 Data.profiles.addObserver(profilesObserver);
188 if (barDrawerToggle == null) {
189 barDrawerToggle = new ActionBarDrawerToggle(this, drawer, mToolbar,
190 R.string.navigation_drawer_open, R.string.navigation_drawer_close);
191 drawer.addDrawerListener(barDrawerToggle);
193 barDrawerToggle.syncState();
195 TextView ver = drawer.findViewById(R.id.drawer_version_text);
199 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
200 ver.setText(pi.versionName);
202 catch (Exception e) {
206 if (progressBar == null)
207 throw new RuntimeException("Can't get hold on the transaction value progress bar");
208 if (progressLayout == null) throw new RuntimeException(
209 "Can't get hold on the transaction value progress bar layout");
211 markDrawerItemCurrent(R.id.nav_account_summary);
213 mViewPager.setAdapter(mSectionsPagerAdapter);
215 if (pageChangeListener == null) {
216 pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
218 public void onPageSelected(int position) {
221 markDrawerItemCurrent(R.id.nav_account_summary);
224 markDrawerItemCurrent(R.id.nav_latest_transactions);
227 Log.e("MainActivity",
228 String.format("Unexpected page index %d", position));
231 super.onPageSelected(position);
234 mViewPager.addOnPageChangeListener(pageChangeListener);
238 if (savedInstanceState != null) {
239 int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
240 if (currentPage != -1) {
241 mCurrentPage = currentPage;
243 mAccountFilter = savedInstanceState.getString(STATE_ACC_FILTER, null);
245 else mAccountFilter = null;
247 Data.lastUpdateDate.observe(this, this::updateLastUpdateDisplay);
249 findViewById(R.id.btn_no_profiles_add)
250 .setOnClickListener(v -> startEditProfileActivity(null));
252 findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
254 findViewById(R.id.nav_new_profile_button)
255 .setOnClickListener(v -> startEditProfileActivity(null));
257 RecyclerView root = findViewById(R.id.nav_profile_list);
259 throw new RuntimeException("Can't get hold on the transaction value view");
261 if (mProfileListAdapter == null) mProfileListAdapter = new ProfilesRecyclerViewAdapter();
262 root.setAdapter(mProfileListAdapter);
264 if (editingProfilesObserver == null) {
265 editingProfilesObserver = (o, arg) -> {
266 if (mProfileListAdapter.isEditingProfiles()) {
267 profileListHeadArrow.clearAnimation();
268 profileListHeadArrow.setVisibility(View.GONE);
269 profileListHeadMore.setVisibility(View.GONE);
270 profileListHeadCancel.setVisibility(View.VISIBLE);
273 profileListHeadArrow.setRotation(180f);
274 profileListHeadArrow.setVisibility(View.VISIBLE);
275 profileListHeadCancel.setVisibility(View.GONE);
276 profileListHeadMore.setVisibility(View.GONE);
278 .setVisibility(profileListExpanded ? View.VISIBLE : View.GONE);
281 mProfileListAdapter.addEditingProfilesObserver(editingProfilesObserver);
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());
294 if (drawerListener == null) {
295 drawerListener = new DrawerLayout.SimpleDrawerListener() {
297 public void onDrawerClosed(View drawerView) {
298 super.onDrawerClosed(drawerView);
299 collapseProfileList();
302 drawer.addDrawerListener(drawerListener);
305 findViewById(R.id.nav_profile_list_head_layout)
306 .setOnClickListener(this::navProfilesHeadClicked);
307 findViewById(R.id.nav_profiles_label).setOnClickListener(this::navProfilesHeadClicked);
309 onProfileChanged(null);
311 updateLastUpdateTextFromDB();
313 private void scheduleDataRetrievalIfStale(Date lastUpdate) {
314 long now = new Date().getTime();
315 if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
316 if (lastUpdate == null) Log.d("db::", "WEB data never fetched. scheduling a fetch");
318 String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
319 lastUpdate.getTime() / 1000f, now / 1000f));
321 scheduleTransactionListRetrieval();
324 private void createShortcuts() {
325 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return;
327 List<ShortcutInfo> shortcuts = new ArrayList<>();
328 try (LockHolder lh = Data.profiles.lockForReading()) {
329 for (int i = 0; i < Data.profiles.size(); i++) {
330 MobileLedgerProfile p = Data.profiles.get(i);
331 if (!p.isPostingPermitted()) continue;
333 ShortcutInfo si = new ShortcutInfo.Builder(this, "new_transaction_" + p.getUuid())
334 .setShortLabel(p.getName())
335 .setIcon(Icon.createWithResource(this, R.drawable.svg_thick_plus_white))
336 .setIntent(new Intent(Intent.ACTION_VIEW, null, this,
337 NewTransactionActivity.class).putExtra("profile_uuid", p.getUuid()))
342 ShortcutManager sm = getSystemService(ShortcutManager.class);
343 sm.setDynamicShortcuts(shortcuts);
345 private void onProfileListChanged(Object arg) {
346 findViewById(R.id.nav_profile_list).setMinimumHeight(
347 (int) (getResources().getDimension(R.dimen.thumb_row_height) *
348 Data.profiles.size()));
350 Log.d("profiles", "profile list changed");
351 if (arg == null) mProfileListAdapter.notifyDataSetChanged();
352 else mProfileListAdapter.notifyItemChanged((int) arg);
356 private void onProfileChanged(Object arg) {
357 MobileLedgerProfile profile = Data.profile.get();
358 MainActivity.this.runOnUiThread(() -> {
360 boolean haveProfile = profile != null;
361 findViewById(R.id.no_profiles_layout)
362 .setVisibility(haveProfile ? View.GONE : View.VISIBLE);
363 findViewById(R.id.pager_layout)
364 .setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
366 Data.transactions.clear();
367 Log.d("transactions", "requesting list reload");
368 TransactionListViewModel.scheduleTransactionListReload();
370 Data.accounts.clear();
371 AccountSummaryViewModel.scheduleAccountListReload();
373 if (profile == null) MainActivity.this.setTitle(R.string.app_name);
374 else MainActivity.this.setTitle(profile.getName());
375 MainActivity.this.updateLastUpdateTextFromDB();
379 MobileLedgerProfile old = (MobileLedgerProfile) arg;
380 old_index = Data.getProfileIndex(old);
381 new_index = Data.getProfileIndex(profile);
384 if ((old_index != -1) && (new_index != -1)) {
385 mProfileListAdapter.notifyItemChanged(old_index);
386 mProfileListAdapter.notifyItemChanged(new_index);
388 else mProfileListAdapter.notifyDataSetChanged();
390 MainActivity.this.collapseProfileList();
392 int newProfileTheme = (profile == null) ? -1 : profile.getThemeId();
393 if (newProfileTheme != Colors.profileThemeId) {
394 Log.d("profiles", String.format("profile theme %d → %d", Colors.profileThemeId,
396 MainActivity.this.profileThemeChanged();
397 Colors.profileThemeId = newProfileTheme;
398 // profileThemeChanged would restart the activity, so no need to reload the
402 drawer.closeDrawers();
404 if (profile == null) {
405 mToolbar.setSubtitle(null);
409 if (profile.isPostingPermitted()) {
410 mToolbar.setSubtitle(null);
414 mToolbar.setSubtitle(R.string.profile_subitlte_read_only);
419 updateLastUpdateTextFromDB();
422 private void updateLastUpdateDisplay(Date newValue) {
423 LinearLayout l = findViewById(R.id.transactions_last_update_layout);
424 TextView v = findViewById(R.id.transactions_last_update);
425 if (newValue == null) {
426 l.setVisibility(View.INVISIBLE);
427 Log.d("main", "no last update date :(");
430 final String text = DateFormat.getDateTimeInstance().format(newValue);
432 l.setVisibility(View.VISIBLE);
433 Log.d("main", String.format("Date formatted: %s", text));
436 scheduleDataRetrievalIfStale(newValue);
439 public void finish() {
440 if (profilesObserver != null) {
441 Data.profiles.deleteObserver(profilesObserver);
442 profilesObserver = null;
445 if (profileObserver != null) {
446 Data.profile.deleteObserver(profileObserver);
447 profileObserver = null;
452 private void profileThemeChanged() {
453 setupProfileColors();
455 Bundle bundle = new Bundle();
456 onSaveInstanceState(bundle);
457 // restart activity to reflect theme change
459 Intent intent = new Intent(this, this.getClass());
460 intent.putExtra(BUNDLE_SAVED_STATE, bundle);
461 startActivity(intent);
463 public void startEditProfileActivity(MobileLedgerProfile profile) {
464 Intent intent = new Intent(this, ProfileDetailActivity.class);
465 Bundle args = new Bundle();
466 if (profile != null) {
467 int index = Data.getProfileIndex(profile);
468 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
470 intent.putExtras(args);
471 startActivity(intent, args);
473 private void setupProfile() {
474 String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
475 MobileLedgerProfile profile;
477 profile = Data.getProfile(profileUUID);
479 if (Data.profiles.isEmpty()) {
480 findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE);
481 findViewById(R.id.pager_layout).setVisibility(View.GONE);
485 findViewById(R.id.pager_layout).setVisibility(View.VISIBLE);
486 findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
488 if (profile == null) profile = Data.profiles.get(0);
490 if (profile == null) throw new AssertionError("profile must have a value");
492 Data.setCurrentProfile(profile);
494 public void fabNewTransactionClicked(View view) {
495 Intent intent = new Intent(this, NewTransactionActivity.class);
496 startActivity(intent);
497 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
499 public void navSettingsClicked(View view) {
500 Intent intent = new Intent(this, SettingsActivity.class);
501 startActivity(intent);
502 drawer.closeDrawers();
504 public void markDrawerItemCurrent(int id) {
505 TextView item = drawer.findViewById(id);
506 item.setBackgroundColor(Colors.tableRowDarkBG);
508 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
509 for (int i = 0; i < actions.getChildCount(); i++) {
510 View view = actions.getChildAt(i);
511 if (view.getId() != id) {
512 view.setBackgroundColor(Color.TRANSPARENT);
516 public void onAccountSummaryClicked(View view) {
517 drawer.closeDrawers();
519 showAccountSummaryFragment();
521 private void showAccountSummaryFragment() {
522 mViewPager.setCurrentItem(0, true);
523 Data.accountFilter.set(null);
524 // FragmentTransaction ft = fragmentManager.beginTransaction();
525 // accountSummaryFragment = new AccountSummaryFragment();
526 // ft.replace(R.id.root_frame, accountSummaryFragment);
528 // currentFragment = accountSummaryFragment;
530 public void onLatestTransactionsClicked(View view) {
531 drawer.closeDrawers();
533 showTransactionsFragment((String) null);
535 private void resetFragmentBackStack() {
536 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
538 private void showTransactionsFragment(String accName) {
539 Data.accountFilter.set(accName);
540 Data.accountFilter.notifyObservers();
541 mViewPager.setCurrentItem(1, true);
543 private void showTransactionsFragment(LedgerAccount account) {
544 showTransactionsFragment((account == null) ? (String) null : account.getName());
545 // FragmentTransaction ft = fragmentManager.beginTransaction();
546 // if (transactionListFragment == null) {
547 // Log.d("flow", "MainActivity creating TransactionListFragment");
548 // transactionListFragment = new TransactionListFragment();
550 // Bundle bundle = new Bundle();
551 // if (account != null) {
552 // bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
553 // account.getName());
555 // transactionListFragment.setArguments(bundle);
556 // ft.replace(R.id.root_frame, transactionListFragment);
557 // if (account != null)
558 // ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
561 // currentFragment = transactionListFragment;
563 public void showAccountTransactions(LedgerAccount account) {
564 mBackMeansToAccountList = true;
565 showTransactionsFragment(account);
568 public void onBackPressed() {
569 DrawerLayout drawer = findViewById(R.id.drawer_layout);
570 if (drawer.isDrawerOpen(GravityCompat.START)) {
571 drawer.closeDrawer(GravityCompat.START);
574 if (mBackMeansToAccountList && (mViewPager.getCurrentItem() == 1)) {
575 Data.accountFilter.set(null);
576 showAccountSummaryFragment();
577 mBackMeansToAccountList = false;
580 Log.d("fragments", String.format("manager stack: %d",
581 fragmentManager.getBackStackEntryCount()));
583 super.onBackPressed();
587 public void updateLastUpdateTextFromDB() {
588 final MobileLedgerProfile profile = Data.profile.get();
589 long last_update = (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
591 Log.d("transactions", String.format("Last update = %d", last_update));
592 if (last_update == 0) {
593 Data.lastUpdateDate.postValue(null);
596 Data.lastUpdateDate.postValue(new Date(last_update));
599 public void scheduleTransactionListRetrieval() {
600 if (Data.profile.get() == null) return;
602 retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
604 retrieveTransactionsTask.execute();
606 public void onStopTransactionRefreshClick(View view) {
607 Log.d("interactive", "Cancelling transactions refresh");
608 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
609 bTransactionListCancelDownload.setEnabled(false);
611 public void onRetrieveDone(String error) {
612 progressLayout.setVisibility(View.GONE);
615 updateLastUpdateTextFromDB();
617 new RefreshDescriptionsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
618 TransactionListViewModel.scheduleTransactionListReload();
620 else Toast.makeText(this, error, Toast.LENGTH_LONG).show();
622 public void onRetrieveStart() {
623 bTransactionListCancelDownload.setEnabled(true);
624 progressBar.setIndeterminateTintList(ColorStateList.valueOf(Colors.primary));
625 progressBar.setProgressTintList(ColorStateList.valueOf(Colors.primary));
626 progressBar.setIndeterminate(true);
627 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
628 else progressBar.setProgress(0);
629 progressLayout.setVisibility(View.VISIBLE);
631 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
632 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
633 (progress.getTotal() == 0))
635 progressBar.setIndeterminate(true);
638 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
639 progressBar.setMin(0);
641 progressBar.setMax(progress.getTotal());
642 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
643 progressBar.setProgress(progress.getProgress(), true);
645 else progressBar.setProgress(progress.getProgress());
646 progressBar.setIndeterminate(false);
649 public void fabShouldShow() {
650 MobileLedgerProfile profile = Data.profile.get();
651 if ((profile != null) && profile.isPostingPermitted()) fab.show();
653 public void navProfilesHeadClicked(View view) {
654 if (profileListExpanded) {
655 collapseProfileList();
661 private void expandProfileList() {
662 profileListExpanded = true;
665 profileListContainer.setVisibility(View.VISIBLE);
666 profileListContainer.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_down));
667 profileListHeadArrow.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180));
668 profileListHeadMore.setVisibility(View.VISIBLE);
669 profileListHeadMore.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
670 findViewById(R.id.nav_profile_list).setMinimumHeight(
671 (int) (getResources().getDimension(R.dimen.thumb_row_height) *
672 Data.profiles.size()));
674 private void collapseProfileList() {
675 profileListExpanded = false;
677 final Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_up);
678 animation.setAnimationListener(new Animation.AnimationListener() {
680 public void onAnimationStart(Animation animation) {
684 public void onAnimationEnd(Animation animation) {
685 profileListContainer.setVisibility(View.GONE);
688 public void onAnimationRepeat(Animation animation) {
692 mProfileListAdapter.stopEditingProfiles();
694 profileListContainer.startAnimation(animation);
695 profileListHeadArrow.setRotation(0f);
697 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180_back));
698 profileListHeadMore.setVisibility(View.GONE);
700 public void onAccountSummaryRowViewClicked(View view) {
702 if (view.getId() == R.id.account_expander) row = (ViewGroup) view.getParent().getParent();
703 else row = (ViewGroup) view.getParent();
705 LedgerAccount acc = (LedgerAccount) row.getTag();
706 switch (view.getId()) {
707 case R.id.account_row_acc_name:
708 case R.id.account_expander:
709 case R.id.account_expander_container:
710 Log.d("accounts", "Account expander clicked");
711 if (!acc.hasSubAccounts()) return;
713 boolean wasExpanded = acc.isExpanded();
715 View arrow = row.findViewById(R.id.account_expander_container);
717 arrow.clearAnimation();
718 ViewPropertyAnimator animator = arrow.animate();
720 acc.toggleExpanded();
721 DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
722 new Object[]{acc.isExpanded(), acc.getName(), Data.profile.get().getUuid()
726 Log.d("accounts", String.format("Collapsing account '%s'", acc.getName()));
727 arrow.setRotation(0);
728 animator.rotationBy(180);
730 // removing all child accounts from the view
731 int start = -1, count = 0;
732 try (LockHolder lh = Data.accounts.lockForWriting()) {
733 for (int i = 0; i < Data.accounts.size(); i++) {
734 if (acc.isParentOf(Data.accounts.get(i))) {
735 // Log.d("accounts", String.format("Found a child '%s' at position %d",
736 // Data.accounts.get(i).getName(), i));
745 // String.format("Found a non-child '%s' at position %d",
746 // Data.accounts.get(i).getName(), i));
753 for (int j = 0; j < count; j++) {
754 // Log.d("accounts", String.format("Removing item %d: %s", start + j,
755 // Data.accounts.get(start).getName()));
756 Data.accounts.removeQuietly(start);
759 mAccountSummaryFragment.modelAdapter
760 .notifyItemRangeRemoved(start, count);
765 Log.d("accounts", String.format("Expanding account '%s'", acc.getName()));
766 arrow.setRotation(180);
767 animator.rotationBy(-180);
768 List<LedgerAccount> children =
769 Data.profile.get().loadVisibleChildAccountsOf(acc);
770 try (LockHolder lh = Data.accounts.lockForWriting()) {
771 int parentPos = Data.accounts.indexOf(acc);
772 if (parentPos != -1) {
773 // may have disappeared in a concurrent refresh operation
774 Data.accounts.addAllQuietly(parentPos + 1, children);
775 mAccountSummaryFragment.modelAdapter
776 .notifyItemRangeInserted(parentPos + 1, children.size());
781 case R.id.account_row_acc_amounts:
782 if (acc.getAmountCount() > AccountSummaryAdapter.AMOUNT_LIMIT) {
783 acc.toggleAmountsExpanded();
785 .add("update accounts set amounts_expanded=? where name=? and profile=?",
786 new Object[]{acc.amountsExpanded(), acc.getName(),
787 Data.profile.get().getUuid()
789 Data.accounts.triggerItemChangedNotification(acc);
795 public class SectionsPagerAdapter extends FragmentPagerAdapter {
797 public SectionsPagerAdapter(FragmentManager fm) {
802 public Fragment getItem(int position) {
803 Log.d("main", String.format("Switching to fragment %d", position));
806 // Log.d("flow", "Creating account summary fragment");
807 return mAccountSummaryFragment = new AccountSummaryFragment();
809 return new TransactionListFragment();
811 throw new IllegalStateException(
812 String.format("Unexpected fragment index: " + "%d", position));
817 public int getCount() {