+ @Override
+ public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
+ debug("profiles", "[fragment] Creating profile details options menu");
+ super.onCreateOptionsMenu(menu, inflater);
+ inflater.inflate(R.menu.profile_details, menu);
+ final MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
+ menuDeleteProfile.setOnMenuItemClickListener(item -> {
+ AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
+ builder.setTitle(mProfile.getName());
+ builder.setMessage(R.string.remove_profile_dialog_message);
+ builder.setPositiveButton(R.string.Remove, (dialog, which) -> {
+ debug("profiles",
+ String.format("[fragment] removing profile %s", mProfile.getUuid()));
+ mProfile.removeFromDB();
+ ArrayList<MobileLedgerProfile> oldList = Data.profiles.getValue();
+ if (oldList == null) throw new AssertionError();
+ ArrayList<MobileLedgerProfile> newList = new ArrayList<>(oldList);
+ newList.remove(mProfile);
+ Data.profiles.setValue(newList);
+ if (mProfile.equals(Data.profile.getValue())) {
+ debug("profiles", "[fragment] setting current profile to 0");
+ Data.setCurrentProfile(newList.get(0));
+ }
+
+ final FragmentActivity activity = getActivity();
+ if (activity != null) activity.finish();
+ });
+ builder.show();
+ return false;
+ });
+ final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
+ menuDeleteProfile
+ .setVisible((mProfile != null) && (profiles != null) && (profiles.size() > 1));
+
+ if (BuildConfig.DEBUG) {
+ final MenuItem menuWipeProfileData = menu.findItem(R.id.menuWipeData);
+ menuWipeProfileData.setOnMenuItemClickListener(ignored -> onWipeDataMenuClicked());
+ menuWipeProfileData.setVisible(mProfile != null);
+ }
+ }
+ private boolean onWipeDataMenuClicked() {
+ // this is a development option, so no confirmation
+ mProfile.wipeAllData();
+ if (mProfile.equals(Data.profile.getValue())) triggerProfileChange();
+ return true;
+ }
+ private void triggerProfileChange() {
+ int index = Data.getProfileIndex(mProfile);
+ MobileLedgerProfile newProfile = new MobileLedgerProfile(mProfile);
+ final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
+ if (profiles == null) throw new AssertionError();
+ profiles.set(index, newProfile);
+
+ ProfilesRecyclerViewAdapter prva = ProfilesRecyclerViewAdapter.getInstance();
+ if (prva != null) prva.notifyItemChanged(index);