+ if (drawerListener == null) {
+ drawerListener = new DrawerLayout.SimpleDrawerListener() {
+ @Override
+ public void onDrawerClosed(View drawerView) {
+ super.onDrawerClosed(drawerView);
+ collapseProfileList();
+ }
+ };
+ drawer.addDrawerListener(drawerListener);
+ }
+
+ findViewById(R.id.nav_profile_list_head_layout)
+ .setOnClickListener(this::navProfilesHeadClicked);
+ findViewById(R.id.nav_profiles_label).setOnClickListener(this::navProfilesHeadClicked);
+ boolean initialStart = Data.profile.get() == null;
+ setupProfile();
+ if (!initialStart) onProfileChanged(null);
+
+ updateLastUpdateTextFromDB();
+ }
+ private void scheduleDataRetrievalIfStale(Date lastUpdate) {
+ long now = new Date().getTime();
+ if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
+ if (lastUpdate == null) debug("db::", "WEB data never fetched. scheduling a fetch");
+ else debug("db",
+ String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
+ lastUpdate.getTime() / 1000f, now / 1000f));
+
+ scheduleTransactionListRetrieval();
+ }
+ }
+ private void createShortcuts() {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return;
+
+ List<ShortcutInfo> shortcuts = new ArrayList<>();
+ try (LockHolder lh = Data.profiles.lockForReading()) {
+ for (int i = 0; i < Data.profiles.size(); i++) {
+ MobileLedgerProfile p = Data.profiles.get(i);
+ if (!p.isPostingPermitted()) continue;
+
+ ShortcutInfo si = new ShortcutInfo.Builder(this, "new_transaction_" + p.getUuid())
+ .setShortLabel(p.getName())
+ .setIcon(Icon.createWithResource(this, R.drawable.svg_thick_plus_white))
+ .setIntent(new Intent(Intent.ACTION_VIEW, null, this,
+ NewTransactionActivity.class).putExtra("profile_uuid", p.getUuid()))
+ .setRank(i).build();
+ shortcuts.add(si);
+ }
+ }
+ ShortcutManager sm = getSystemService(ShortcutManager.class);
+ sm.setDynamicShortcuts(shortcuts);
+ }
+ private void onProfileListChanged(Object arg) {
+ findViewById(R.id.nav_profile_list).setMinimumHeight(
+ (int) (getResources().getDimension(R.dimen.thumb_row_height) *
+ Data.profiles.size()));
+
+ debug("profiles", "profile list changed");
+ if (arg == null) mProfileListAdapter.notifyDataSetChanged();
+ else mProfileListAdapter.notifyItemChanged((int) arg);
+
+ createShortcuts();
+ }
+ private void onProfileChanged(Object arg) {
+ MobileLedgerProfile profile = Data.profile.get();
+ MainActivity.this.runOnUiThread(() -> {
+
+ boolean haveProfile = profile != null;
+ findViewById(R.id.no_profiles_layout)
+ .setVisibility(haveProfile ? View.GONE : View.VISIBLE);
+ findViewById(R.id.pager_layout)
+ .setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
+
+ if (profile == null) MainActivity.this.setTitle(R.string.app_name);
+ else MainActivity.this.setTitle(profile.getName());
+ MainActivity.this.updateLastUpdateTextFromDB();
+ int old_index = -1;
+ int new_index = -1;
+ if (arg != null) {
+ MobileLedgerProfile old = (MobileLedgerProfile) arg;
+ old_index = Data.getProfileIndex(old);
+ new_index = Data.getProfileIndex(profile);
+ }
+
+ if ((old_index != -1) && (new_index != -1)) {
+ mProfileListAdapter.notifyItemChanged(old_index);
+ mProfileListAdapter.notifyItemChanged(new_index);
+ }
+ else mProfileListAdapter.notifyDataSetChanged();
+
+ MainActivity.this.collapseProfileList();
+
+ int newProfileTheme = (profile == null) ? -1 : profile.getThemeId();
+ if (newProfileTheme != Colors.profileThemeId) {
+ debug("profiles", String.format("profile theme %d → %d", Colors.profileThemeId,
+ newProfileTheme));
+ MainActivity.this.profileThemeChanged();
+ Colors.profileThemeId = newProfileTheme;
+ // profileThemeChanged would restart the activity, so no need to reload the
+ // data sets below
+ return;
+ }
+ drawer.closeDrawers();
+
+ Data.transactions.clear();
+ debug("transactions", "requesting list reload");
+ TransactionListViewModel.scheduleTransactionListReload();
+
+ Data.accounts.clear();
+ AccountSummaryViewModel.scheduleAccountListReload();
+
+ if (profile == null) {
+ mToolbar.setSubtitle(null);
+ fab.hide();
+ }
+ else {
+ if (profile.isPostingPermitted()) {
+ mToolbar.setSubtitle(null);
+ fab.show();
+ }
+ else {
+ mToolbar.setSubtitle(R.string.profile_subitlte_read_only);
+ fab.hide();
+ }