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 public class MainActivity extends ProfileThemedActivity {
82 public static final String STATE_CURRENT_PAGE = "current_page";
83 public static final String BUNDLE_SAVED_STATE = "bundle_savedState";
84 public static final String STATE_ACC_FILTER = "account_filter";
85 public AccountSummaryFragment mAccountSummaryFragment;
87 private LinearLayout profileListContainer;
88 private View profileListHeadArrow, profileListHeadMore, profileListHeadCancel;
89 private LinearLayout profileListHeadMoreAndCancel;
90 private FragmentManager fragmentManager;
91 private TextView tvLastUpdate;
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 profileModificationEnabled = false;
100 private boolean profileListExpanded = false;
101 private ProfilesRecyclerViewAdapter mProfileListAdapter;
102 private int mCurrentPage;
103 private String mAccountFilter;
104 private boolean mBackMeansToAccountList = false;
105 private Observer profileObserver;
106 private Observer profilesObserver;
107 private Toolbar mToolbar;
108 private DrawerLayout.SimpleDrawerListener drawerListener;
109 private ActionBarDrawerToggle barDrawerToggle;
110 private ViewPager.SimpleOnPageChangeListener pageChangeListener;
111 private Observer editingProfilesObserver;
113 protected void onStart() {
116 Log.d("flow", "MainActivity.onStart()");
117 mViewPager.setCurrentItem(mCurrentPage, false);
118 if (mAccountFilter != null) showTransactionsFragment(mAccountFilter);
119 else Data.accountFilter.setValue(null);
123 protected void onSaveInstanceState(Bundle outState) {
124 super.onSaveInstanceState(outState);
125 outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem());
126 if (mAccountFilter != null)
127 outState.putString(STATE_ACC_FILTER, mAccountFilter);
130 protected void onDestroy() {
131 mSectionsPagerAdapter = null;
132 Data.profile.deleteObserver(profileObserver);
133 profileObserver = null;
134 Data.profiles.deleteObserver(profilesObserver);
135 profilesObserver = null;
136 RecyclerView root = findViewById(R.id.nav_profile_list);
137 if (root != null) root.setAdapter(null);
138 if (drawer != null) drawer.removeDrawerListener(drawerListener);
139 drawerListener = null;
140 if (drawer != null) drawer.removeDrawerListener(barDrawerToggle);
141 barDrawerToggle = null;
142 if (mViewPager != null) mViewPager.removeOnPageChangeListener(pageChangeListener);
143 pageChangeListener = null;
144 if (mProfileListAdapter != null)
145 mProfileListAdapter.deleteEditingProfilesObserver(editingProfilesObserver);
146 editingProfilesObserver = null;
150 protected void onCreate(Bundle savedInstanceState) {
151 super.onCreate(savedInstanceState);
152 Log.d("flow", "MainActivity.onCreate()");
153 int profileColor = Data.retrieveCurrentThemeIdFromDb();
154 Colors.setupTheme(this, profileColor);
155 Colors.profileThemeId = profileColor;
156 setContentView(R.layout.activity_main);
158 fab = findViewById(R.id.btn_add_transaction);
159 profileListContainer = findViewById(R.id.nav_profile_list_container);
160 profileListHeadArrow = findViewById(R.id.nav_profiles_arrow);
161 profileListHeadMore = findViewById(R.id.nav_profiles_start_edit);
162 profileListHeadCancel = findViewById(R.id.nav_profiles_cancel_edit);
163 profileListHeadMoreAndCancel = findViewById(R.id.nav_profile_list_head_buttons);
164 drawer = findViewById(R.id.drawer_layout);
165 tvLastUpdate = findViewById(R.id.transactions_last_update);
166 bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
167 progressBar = findViewById(R.id.transaction_list_progress_bar);
168 progressLayout = findViewById(R.id.transaction_progress_layout);
169 fragmentManager = getSupportFragmentManager();
170 mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
171 mViewPager = findViewById(R.id.root_frame);
173 Bundle extra = getIntent().getBundleExtra(BUNDLE_SAVED_STATE);
174 if (extra != null && savedInstanceState == null) savedInstanceState = extra;
177 mToolbar = findViewById(R.id.toolbar);
178 setSupportActionBar(mToolbar);
180 if (profileObserver == null) {
181 profileObserver = (o, arg) -> onProfileChanged(arg);
182 Data.profile.addObserver(profileObserver);
185 if (profilesObserver == null) {
186 profilesObserver = (o, arg) -> onProfileListChanged(arg);
187 Data.profiles.addObserver(profilesObserver);
190 if (barDrawerToggle == null) {
191 barDrawerToggle = new ActionBarDrawerToggle(this, drawer, mToolbar,
192 R.string.navigation_drawer_open, R.string.navigation_drawer_close);
193 drawer.addDrawerListener(barDrawerToggle);
195 barDrawerToggle.syncState();
197 TextView ver = drawer.findViewById(R.id.drawer_version_text);
201 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
202 ver.setText(pi.versionName);
204 catch (Exception e) {
208 if (progressBar == null)
209 throw new RuntimeException("Can't get hold on the transaction value progress bar");
210 if (progressLayout == null) throw new RuntimeException(
211 "Can't get hold on the transaction value progress bar layout");
213 markDrawerItemCurrent(R.id.nav_account_summary);
215 mViewPager.setAdapter(mSectionsPagerAdapter);
217 if (pageChangeListener == null) {
218 pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
220 public void onPageSelected(int position) {
223 markDrawerItemCurrent(R.id.nav_account_summary);
226 markDrawerItemCurrent(R.id.nav_latest_transactions);
229 Log.e("MainActivity",
230 String.format("Unexpected page index %d", position));
233 super.onPageSelected(position);
236 mViewPager.addOnPageChangeListener(pageChangeListener);
240 if (savedInstanceState != null) {
241 int currentPage = savedInstanceState.getInt(STATE_CURRENT_PAGE, -1);
242 if (currentPage != -1) {
243 mCurrentPage = currentPage;
245 mAccountFilter = savedInstanceState.getString(STATE_ACC_FILTER, null);
247 else mAccountFilter = null;
249 Data.lastUpdateDate.observe(this, this::updateLastUpdateDisplay);
251 findViewById(R.id.btn_no_profiles_add)
252 .setOnClickListener(v -> startEditProfileActivity(null));
254 findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
256 findViewById(R.id.nav_new_profile_button)
257 .setOnClickListener(v -> startEditProfileActivity(null));
259 RecyclerView root = findViewById(R.id.nav_profile_list);
261 throw new RuntimeException("Can't get hold on the transaction value view");
263 if (mProfileListAdapter == null) mProfileListAdapter = new ProfilesRecyclerViewAdapter();
264 root.setAdapter(mProfileListAdapter);
266 if (editingProfilesObserver == null) {
267 editingProfilesObserver = (o, arg) -> {
268 if (mProfileListAdapter.isEditingProfiles()) {
269 profileListHeadArrow.clearAnimation();
270 profileListHeadArrow.setVisibility(View.GONE);
271 profileListHeadMore.setVisibility(View.GONE);
272 profileListHeadCancel.setVisibility(View.VISIBLE);
275 profileListHeadArrow.setRotation(180f);
276 profileListHeadArrow.setVisibility(View.VISIBLE);
277 profileListHeadCancel.setVisibility(View.GONE);
278 profileListHeadMore.setVisibility(View.GONE);
280 .setVisibility(profileListExpanded ? View.VISIBLE : View.GONE);
283 mProfileListAdapter.addEditingProfilesObserver(editingProfilesObserver);
286 LinearLayoutManager llm = new LinearLayoutManager(this);
288 llm.setOrientation(RecyclerView.VERTICAL);
289 root.setLayoutManager(llm);
291 profileListHeadMore.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
292 profileListHeadCancel.setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
293 profileListHeadMoreAndCancel
294 .setOnClickListener((v) -> mProfileListAdapter.flipEditingProfiles());
296 if (drawerListener == null) {
297 drawerListener = new DrawerLayout.SimpleDrawerListener() {
299 public void onDrawerClosed(View drawerView) {
300 super.onDrawerClosed(drawerView);
301 collapseProfileList();
304 drawer.addDrawerListener(drawerListener);
307 findViewById(R.id.nav_profile_list_head_layout)
308 .setOnClickListener(this::navProfilesHeadClicked);
309 findViewById(R.id.nav_profiles_label).setOnClickListener(this::navProfilesHeadClicked);
311 onProfileChanged(null);
313 updateLastUpdateTextFromDB();
315 private void scheduleDataRetrievalIfStale(Date lastUpdate) {
316 long now = new Date().getTime();
317 if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
318 if (lastUpdate == null) Log.d("db::", "WEB data never fetched. scheduling a fetch");
320 String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
321 lastUpdate.getTime() / 1000f, now / 1000f));
323 scheduleTransactionListRetrieval();
326 private void createShortcuts() {
327 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return;
329 List<ShortcutInfo> shortcuts = new ArrayList<>();
330 try (LockHolder lh = Data.profiles.lockForReading()) {
331 for (int i = 0; i < Data.profiles.size(); i++) {
332 MobileLedgerProfile p = Data.profiles.get(i);
333 if (!p.isPostingPermitted()) continue;
335 ShortcutInfo si = new ShortcutInfo.Builder(this, "new_transaction_" + p.getUuid())
336 .setShortLabel(p.getName())
337 .setIcon(Icon.createWithResource(this, R.drawable.svg_thick_plus_white))
338 .setIntent(new Intent(Intent.ACTION_VIEW, null, this,
339 NewTransactionActivity.class).putExtra("profile_uuid", p.getUuid()))
344 ShortcutManager sm = getSystemService(ShortcutManager.class);
345 sm.setDynamicShortcuts(shortcuts);
347 private void onProfileListChanged(Object arg) {
348 findViewById(R.id.nav_profile_list).setMinimumHeight(
349 (int) (getResources().getDimension(R.dimen.thumb_row_height) *
350 Data.profiles.size()));
352 Log.d("profiles", "profile list changed");
353 if (arg == null) mProfileListAdapter.notifyDataSetChanged();
354 else mProfileListAdapter.notifyItemChanged((int) arg);
358 private void onProfileChanged(Object arg) {
359 MobileLedgerProfile profile = Data.profile.get();
360 MainActivity.this.runOnUiThread(() -> {
362 boolean haveProfile = profile != null;
363 findViewById(R.id.no_profiles_layout)
364 .setVisibility(haveProfile ? View.GONE : View.VISIBLE);
365 findViewById(R.id.pager_layout)
366 .setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
368 Data.transactions.clear();
369 Log.d("transactions", "requesting list reload");
370 TransactionListViewModel.scheduleTransactionListReload();
372 Data.accounts.clear();
373 AccountSummaryViewModel.scheduleAccountListReload();
375 if (profile == null) MainActivity.this.setTitle(R.string.app_name);
376 else MainActivity.this.setTitle(profile.getName());
377 MainActivity.this.updateLastUpdateTextFromDB();
381 MobileLedgerProfile old = (MobileLedgerProfile) arg;
382 old_index = Data.getProfileIndex(old);
383 new_index = Data.getProfileIndex(profile);
386 if ((old_index != -1) && (new_index != -1)) {
387 mProfileListAdapter.notifyItemChanged(old_index);
388 mProfileListAdapter.notifyItemChanged(new_index);
390 else mProfileListAdapter.notifyDataSetChanged();
392 MainActivity.this.collapseProfileList();
394 int newProfileTheme = (profile == null) ? -1 : profile.getThemeId();
395 if (newProfileTheme != Colors.profileThemeId) {
396 Log.d("profiles", String.format("profile theme %d → %d", Colors.profileThemeId,
398 MainActivity.this.profileThemeChanged();
399 Colors.profileThemeId = newProfileTheme;
400 // profileThemeChanged would restart the activity, so no need to reload the
404 drawer.closeDrawers();
406 if (profile == null) {
407 mToolbar.setSubtitle(null);
411 if (profile.isPostingPermitted()) {
412 mToolbar.setSubtitle(null);
416 mToolbar.setSubtitle(R.string.profile_subitlte_read_only);
421 updateLastUpdateTextFromDB();
424 private void updateLastUpdateDisplay(Date newValue) {
425 LinearLayout l = findViewById(R.id.transactions_last_update_layout);
426 TextView v = findViewById(R.id.transactions_last_update);
427 if (newValue == null) {
428 l.setVisibility(View.INVISIBLE);
429 Log.d("main", "no last update date :(");
432 final String text = DateFormat.getDateTimeInstance().format(newValue);
434 l.setVisibility(View.VISIBLE);
435 Log.d("main", String.format("Date formatted: %s", text));
438 scheduleDataRetrievalIfStale(newValue);
441 public void finish() {
442 if (profilesObserver != null) {
443 Data.profiles.deleteObserver(profilesObserver);
444 profilesObserver = null;
447 if (profileObserver != null) {
448 Data.profile.deleteObserver(profileObserver);
449 profileObserver = null;
454 private void profileThemeChanged() {
455 setupProfileColors();
457 Bundle bundle = new Bundle();
458 onSaveInstanceState(bundle);
459 // restart activity to reflect theme change
461 Intent intent = new Intent(this, this.getClass());
462 intent.putExtra(BUNDLE_SAVED_STATE, bundle);
463 startActivity(intent);
465 public void startEditProfileActivity(MobileLedgerProfile profile) {
466 Intent intent = new Intent(this, ProfileDetailActivity.class);
467 Bundle args = new Bundle();
468 if (profile != null) {
469 int index = Data.getProfileIndex(profile);
470 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
472 intent.putExtras(args);
473 startActivity(intent, args);
475 private void setupProfile() {
476 String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
477 MobileLedgerProfile profile;
479 profile = Data.getProfile(profileUUID);
481 if (Data.profiles.isEmpty()) {
482 findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE);
483 findViewById(R.id.pager_layout).setVisibility(View.GONE);
487 findViewById(R.id.pager_layout).setVisibility(View.VISIBLE);
488 findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
490 if (profile == null) profile = Data.profiles.get(0);
492 if (profile == null) throw new AssertionError("profile must have a value");
494 Data.setCurrentProfile(profile);
496 public void fabNewTransactionClicked(View view) {
497 Intent intent = new Intent(this, NewTransactionActivity.class);
498 startActivity(intent);
499 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
501 public void navSettingsClicked(View view) {
502 Intent intent = new Intent(this, SettingsActivity.class);
503 startActivity(intent);
504 drawer.closeDrawers();
506 public void markDrawerItemCurrent(int id) {
507 TextView item = drawer.findViewById(id);
508 item.setBackgroundColor(Colors.tableRowDarkBG);
510 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
511 for (int i = 0; i < actions.getChildCount(); i++) {
512 View view = actions.getChildAt(i);
513 if (view.getId() != id) {
514 view.setBackgroundColor(Color.TRANSPARENT);
518 public void onAccountSummaryClicked(View view) {
519 drawer.closeDrawers();
521 showAccountSummaryFragment();
523 private void showAccountSummaryFragment() {
524 mViewPager.setCurrentItem(0, true);
525 Data.accountFilter.setValue(null);
526 // FragmentTransaction ft = fragmentManager.beginTransaction();
527 // accountSummaryFragment = new AccountSummaryFragment();
528 // ft.replace(R.id.root_frame, accountSummaryFragment);
530 // currentFragment = accountSummaryFragment;
532 public void onLatestTransactionsClicked(View view) {
533 drawer.closeDrawers();
535 showTransactionsFragment((String) null);
537 private void resetFragmentBackStack() {
538 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
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());
546 // FragmentTransaction ft = fragmentManager.beginTransaction();
547 // if (transactionListFragment == null) {
548 // Log.d("flow", "MainActivity creating TransactionListFragment");
549 // transactionListFragment = new TransactionListFragment();
551 // Bundle bundle = new Bundle();
552 // if (account != null) {
553 // bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
554 // account.getName());
556 // transactionListFragment.setArguments(bundle);
557 // ft.replace(R.id.root_frame, transactionListFragment);
558 // if (account != null)
559 // ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
562 // currentFragment = transactionListFragment;
564 public void showAccountTransactions(LedgerAccount account) {
565 mBackMeansToAccountList = true;
566 showTransactionsFragment(account);
569 public void onBackPressed() {
570 DrawerLayout drawer = findViewById(R.id.drawer_layout);
571 if (drawer.isDrawerOpen(GravityCompat.START)) {
572 drawer.closeDrawer(GravityCompat.START);
575 if (mBackMeansToAccountList && (mViewPager.getCurrentItem() == 1)) {
576 Data.accountFilter.setValue(null);
577 showAccountSummaryFragment();
578 mBackMeansToAccountList = false;
581 Log.d("fragments", String.format("manager stack: %d",
582 fragmentManager.getBackStackEntryCount()));
584 super.onBackPressed();
588 public void updateLastUpdateTextFromDB() {
589 final MobileLedgerProfile profile = Data.profile.get();
590 long last_update = (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
592 Log.d("transactions", String.format("Last update = %d", last_update));
593 if (last_update == 0) {
594 Data.lastUpdateDate.postValue(null);
597 Data.lastUpdateDate.postValue(new Date(last_update));
600 public void scheduleTransactionListRetrieval() {
601 if (Data.profile.get() == null) return;
603 retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
605 retrieveTransactionsTask.execute();
607 public void onStopTransactionRefreshClick(View view) {
608 Log.d("interactive", "Cancelling transactions refresh");
609 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
610 bTransactionListCancelDownload.setEnabled(false);
612 public void onRetrieveDone(String error) {
613 progressLayout.setVisibility(View.GONE);
616 updateLastUpdateTextFromDB();
618 new RefreshDescriptionsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
619 TransactionListViewModel.scheduleTransactionListReload();
621 else Toast.makeText(this, error, Toast.LENGTH_LONG).show();
623 public void onRetrieveStart() {
624 bTransactionListCancelDownload.setEnabled(true);
625 progressBar.setIndeterminateTintList(ColorStateList.valueOf(Colors.primary));
626 progressBar.setProgressTintList(ColorStateList.valueOf(Colors.primary));
627 progressBar.setIndeterminate(true);
628 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
629 else progressBar.setProgress(0);
630 progressLayout.setVisibility(View.VISIBLE);
632 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
633 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
634 (progress.getTotal() == 0))
636 progressBar.setIndeterminate(true);
639 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
640 progressBar.setMin(0);
642 progressBar.setMax(progress.getTotal());
643 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
644 progressBar.setProgress(progress.getProgress(), true);
646 else progressBar.setProgress(progress.getProgress());
647 progressBar.setIndeterminate(false);
650 public void fabShouldShow() {
651 MobileLedgerProfile profile = Data.profile.get();
652 if ((profile != null) && profile.isPostingPermitted()) fab.show();
654 public void navProfilesHeadClicked(View view) {
655 if (profileListExpanded) {
656 collapseProfileList();
662 private void expandProfileList() {
663 profileListExpanded = true;
666 profileListContainer.setVisibility(View.VISIBLE);
667 profileListContainer.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_down));
668 profileListHeadArrow.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180));
669 profileListHeadMore.setVisibility(View.VISIBLE);
670 profileListHeadMore.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
671 findViewById(R.id.nav_profile_list).setMinimumHeight(
672 (int) (getResources().getDimension(R.dimen.thumb_row_height) *
673 Data.profiles.size()));
675 private void collapseProfileList() {
676 profileListExpanded = false;
678 final Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_up);
679 animation.setAnimationListener(new Animation.AnimationListener() {
681 public void onAnimationStart(Animation animation) {
685 public void onAnimationEnd(Animation animation) {
686 profileListContainer.setVisibility(View.GONE);
689 public void onAnimationRepeat(Animation animation) {
693 mProfileListAdapter.stopEditingProfiles();
695 profileListContainer.startAnimation(animation);
696 profileListHeadArrow.setRotation(0f);
698 .startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_180_back));
699 profileListHeadMore.setVisibility(View.GONE);
701 public void onAccountSummaryRowViewClicked(View view) {
703 if (view.getId() == R.id.account_expander) row = (ViewGroup) view.getParent().getParent();
704 else row = (ViewGroup) view.getParent();
706 LedgerAccount acc = (LedgerAccount) row.getTag();
707 switch (view.getId()) {
708 case R.id.account_row_acc_name:
709 case R.id.account_expander:
710 case R.id.account_expander_container:
711 Log.d("accounts", "Account expander clicked");
712 if (!acc.hasSubAccounts()) return;
714 boolean wasExpanded = acc.isExpanded();
716 View arrow = row.findViewById(R.id.account_expander_container);
718 arrow.clearAnimation();
719 ViewPropertyAnimator animator = arrow.animate();
721 acc.toggleExpanded();
722 DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
723 new Object[]{acc.isExpanded(), acc.getName(), Data.profile.get().getUuid()
727 Log.d("accounts", String.format("Collapsing account '%s'", acc.getName()));
728 arrow.setRotation(0);
729 animator.rotationBy(180);
731 // removing all child accounts from the view
732 int start = -1, count = 0;
733 try (LockHolder lh = Data.accounts.lockForWriting()) {
734 for (int i = 0; i < Data.accounts.size(); i++) {
735 if (acc.isParentOf(Data.accounts.get(i))) {
736 // Log.d("accounts", String.format("Found a child '%s' at position %d",
737 // Data.accounts.get(i).getName(), i));
746 // String.format("Found a non-child '%s' at position %d",
747 // Data.accounts.get(i).getName(), i));
754 for (int j = 0; j < count; j++) {
755 // Log.d("accounts", String.format("Removing item %d: %s", start + j,
756 // Data.accounts.get(start).getName()));
757 Data.accounts.removeQuietly(start);
760 mAccountSummaryFragment.modelAdapter
761 .notifyItemRangeRemoved(start, count);
766 Log.d("accounts", String.format("Expanding account '%s'", acc.getName()));
767 arrow.setRotation(180);
768 animator.rotationBy(-180);
769 List<LedgerAccount> children =
770 Data.profile.get().loadVisibleChildAccountsOf(acc);
771 try (LockHolder lh = Data.accounts.lockForWriting()) {
772 int parentPos = Data.accounts.indexOf(acc);
773 if (parentPos != -1) {
774 // may have disappeared in a concurrent refresh operation
775 Data.accounts.addAllQuietly(parentPos + 1, children);
776 mAccountSummaryFragment.modelAdapter
777 .notifyItemRangeInserted(parentPos + 1, children.size());
782 case R.id.account_row_acc_amounts:
783 if (acc.getAmountCount() > AccountSummaryAdapter.AMOUNT_LIMIT) {
784 acc.toggleAmountsExpanded();
786 .add("update accounts set amounts_expanded=? where name=? and profile=?",
787 new Object[]{acc.amountsExpanded(), acc.getName(),
788 Data.profile.get().getUuid()
790 Data.accounts.triggerItemChangedNotification(acc);
796 public class SectionsPagerAdapter extends FragmentPagerAdapter {
798 public SectionsPagerAdapter(FragmentManager fm) {
804 public Fragment getItem(int position) {
805 Log.d("main", String.format("Switching to fragment %d", position));
808 // Log.d("flow", "Creating account summary fragment");
809 return mAccountSummaryFragment = new AccountSummaryFragment();
811 return new TransactionListFragment();
813 throw new IllegalStateException(
814 String.format("Unexpected fragment index: " + "%d", position));
819 public int getCount() {