+ 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);
+ setupProfile();
+ }
+ 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(Locale.ENGLISH,
+ "WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
+ lastUpdate.getTime() / 1000f, now / 1000f));
+
+ Data.scheduleTransactionListRetrieval(this);
+ }
+ }
+ private void createShortcuts(List<MobileLedgerProfile> list) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;
+
+ List<ShortcutInfo> shortcuts = new ArrayList<>();
+ int i = 0;
+ for (MobileLedgerProfile p : list) {
+ 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);
+ i++;
+ }
+ ShortcutManager sm = getSystemService(ShortcutManager.class);
+ sm.setDynamicShortcuts(shortcuts);
+ }
+ private void onProfileListChanged(List<MobileLedgerProfile> newList) {
+ if (newList.isEmpty()) {
+ findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE);
+ findViewById(R.id.pager_layout).setVisibility(View.GONE);
+ findViewById(R.id.loading_layout).setVisibility(View.GONE);
+ return;
+ }
+
+ findViewById(R.id.pager_layout).setVisibility(View.VISIBLE);
+ findViewById(R.id.no_profiles_layout).setVisibility(View.GONE);
+ findViewById(R.id.loading_layout).setVisibility(View.GONE);
+
+ findViewById(R.id.nav_profile_list).setMinimumHeight(
+ (int) (getResources().getDimension(R.dimen.thumb_row_height) * newList.size()));
+
+ debug("profiles", "profile list changed");
+ mProfileListAdapter.notifyDataSetChanged();
+
+ createShortcuts(newList);
+ }
+ /** called when the current profile has changed */
+ private void onProfileChanged(MobileLedgerProfile profile) {
+ 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 (haveProfile) setTitle(profile.getName());
+ else setTitle(R.string.app_name);
+
+ this.profile = profile;
+
+ mProfileListAdapter.notifyDataSetChanged();
+
+ int newProfileTheme = haveProfile ? profile.getThemeId() : -1;
+ if (newProfileTheme != Colors.profileThemeId) {
+ debug("profiles",
+ String.format(Locale.ENGLISH, "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;
+ }
+ collapseProfileList();
+
+ drawer.closeDrawers();
+
+ Data.transactions.clear();
+ debug("transactions", "requesting list reload");
+ TransactionListViewModel.scheduleTransactionListReload();
+
+ Data.accounts.clear();
+ AccountSummaryViewModel.scheduleAccountListReload();
+
+ if (haveProfile) {
+ if (profile.isPostingPermitted()) {
+ mToolbar.setSubtitle(null);
+ fab.show();