From 349a5a3aca6dab475eada68d99d59d0dff9378b3 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 18 Apr 2021 14:49:53 +0300 Subject: [PATCH] fix profile deletion / data wipe do not keep a reference to the profile being edited, instead query it by the ID now stored in the model one thing doesn't work yet - reaction to deletion of the current profile --- .../net/ktnx/mobileledger/db/Profile.java | 1 + .../ui/profiles/ProfileDetailFragment.java | 52 +++++++++++++------ .../ui/profiles/ProfileDetailModel.java | 21 +++++--- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/db/Profile.java b/app/src/main/java/net/ktnx/mobileledger/db/Profile.java index ebbf6dda..4753f311 100644 --- a/app/src/main/java/net/ktnx/mobileledger/db/Profile.java +++ b/app/src/main/java/net/ktnx/mobileledger/db/Profile.java @@ -34,6 +34,7 @@ import net.ktnx.mobileledger.utils.Misc; @Entity(tableName = "profiles") public class Profile { + public static final long NO_PROFILE_ID = 0; @ColumnInfo @PrimaryKey(autoGenerate = true) private long id; diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index 10091895..9b0b43a1 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -20,6 +20,7 @@ package net.ktnx.mobileledger.ui.profiles; import android.app.Activity; import android.app.AlertDialog; import android.graphics.Typeface; +import android.os.AsyncTask; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; @@ -63,6 +64,7 @@ import org.jetbrains.annotations.NotNull; import java.net.MalformedURLException; import java.net.URL; import java.util.List; +import java.util.Objects; import static net.ktnx.mobileledger.utils.Logger.debug; @@ -80,7 +82,6 @@ public class ProfileDetailFragment extends Fragment { public static final String ARG_HUE = "hue"; @NonNls - private Profile mProfile; private boolean defaultCommoditySet; private boolean syncingModelFromUI = false; private ProfileDetailBinding binding; @@ -100,21 +101,36 @@ public class ProfileDetailFragment extends Fragment { menuDeleteProfile.setOnMenuItemClickListener(item -> onDeleteProfile()); final List profiles = Data.profiles.getValue(); - if (BuildConfig.DEBUG) { - final MenuItem menuWipeProfileData = menu.findItem(R.id.menuWipeData); + final MenuItem menuWipeProfileData = menu.findItem(R.id.menuWipeData); + if (BuildConfig.DEBUG) menuWipeProfileData.setOnMenuItemClickListener(ignored -> onWipeDataMenuClicked()); - menuWipeProfileData.setVisible(mProfile != null); - } + + getModel().getProfileId() + .observe(getViewLifecycleOwner(), id -> { + menuDeleteProfile.setVisible(id > 0); + if (BuildConfig.DEBUG) + menuWipeProfileData.setVisible(id > 0); + }); } private boolean onDeleteProfile() { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setTitle(mProfile.getName()); + @NotNull ProfileDetailModel model = getModel(); + builder.setTitle(model.getProfileName()); builder.setMessage(R.string.remove_profile_dialog_message); builder.setPositiveButton(R.string.Remove, (dialog, which) -> { - debug("profiles", String.format("[fragment] removing profile %s", mProfile.getId())); + final long profileId = Objects.requireNonNull(model.getProfileId() + .getValue()); + debug("profiles", String.format("[fragment] removing profile %s", profileId)); ProfileDAO dao = DB.get() .getProfileDAO(); - dao.delete(mProfile, () -> dao.updateOrderSync(dao.getAllOrderedSync())); + dao.getById(profileId) + .observe(getViewLifecycleOwner(), profile -> { + if (profile != null) + AsyncTask.execute(() -> { + dao.deleteSync(profile); + dao.updateOrderSync(dao.getAllOrderedSync()); + }); + }); final FragmentActivity activity = getActivity(); if (activity != null) @@ -125,7 +141,14 @@ public class ProfileDetailFragment extends Fragment { } private boolean onWipeDataMenuClicked() { // this is a development option, so no confirmation - mProfile.wipeAllData(); + DB.get() + .getProfileDAO() + .getById(Objects.requireNonNull(getModel().getProfileId() + .getValue())) + .observe(getViewLifecycleOwner(), profile -> { + if (profile != null) + profile.wipeAllData(); + }); return true; } private void hookTextChangeSyncRoutine(TextView view, TextChangeSyncRoutine syncRoutine) { @@ -373,16 +396,15 @@ public class ProfileDetailFragment extends Fragment { ProfileDAO dao = DB.get() .getProfileDAO(); - if (mProfile != null) { - model.updateProfile(mProfile); - dao.update(mProfile, null); + Profile profile = new Profile(); + model.updateProfile(profile); + if (profile.getId() > 0) { + dao.update(profile, null); debug("profiles", "profile stored in DB"); // debug("profiles", String.format("Selected item is %d", mProfile.getThemeHue())); } else { - mProfile = new Profile(); - model.updateProfile(mProfile); - dao.insertLast(mProfile, null); + dao.insertLast(profile, null); } Activity activity = getActivity(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java index e1f956ac..a00ec945 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java @@ -1,5 +1,5 @@ /* - * Copyright © 2020 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -20,6 +20,7 @@ package net.ktnx.mobileledger.ui.profiles; import android.text.TextUtils; import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModel; @@ -44,6 +45,8 @@ import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static net.ktnx.mobileledger.db.Profile.NO_PROFILE_ID; + public class ProfileDetailModel extends ViewModel { private static final String HTTPS_URL_START = "https://"; private final MutableLiveData profileName = new MutableLiveData<>(); @@ -62,6 +65,7 @@ public class ProfileDetailModel extends ViewModel { private final MutableLiveData themeId = new MutableLiveData<>(-1); private final MutableLiveData detectedVersion = new MutableLiveData<>(null); private final MutableLiveData detectingHledgerVersion = new MutableLiveData<>(false); + private final MutableLiveData profileId = new MutableLiveData<>(NO_PROFILE_ID); public int initialThemeHue = Colors.DEFAULT_HUE_DEG; private VersionDetectionThread versionDetectionThread; public ProfileDetailModel() { @@ -225,6 +229,7 @@ public class ProfileDetailModel extends ViewModel { } void setValuesFromProfile(Profile mProfile) { if (mProfile != null) { + profileId.setValue(mProfile.getId()); profileName.setValue(mProfile.getName()); postingPermitted.setValue(mProfile.permitPosting()); showCommentsByDefault.setValue(mProfile.getShowCommentsByDefault()); @@ -236,8 +241,7 @@ public class ProfileDetailModel extends ViewModel { else setDefaultCommodity(comm); } - futureDates.setValue( - FutureDates.valueOf(mProfile.getFutureDates())); + futureDates.setValue(FutureDates.valueOf(mProfile.getFutureDates())); apiVersion.setValue(API.valueOf(mProfile.getApiVersion())); url.setValue(mProfile.getUrl()); useAuthentication.setValue(mProfile.useAuthentication()); @@ -251,6 +255,7 @@ public class ProfileDetailModel extends ViewModel { mProfile.getDetectedVersionMinor())); } else { + profileId.setValue(NO_PROFILE_ID); profileName.setValue(null); url.setValue(HTTPS_URL_START); postingPermitted.setValue(true); @@ -266,6 +271,7 @@ public class ProfileDetailModel extends ViewModel { } } void updateProfile(Profile mProfile) { + mProfile.setId(profileId.getValue()); mProfile.setName(profileName.getValue()); mProfile.setUrl(url.getValue()); mProfile.setPermitPosting(postingPermitted.getValue()); @@ -282,9 +288,9 @@ public class ProfileDetailModel extends ViewModel { mProfile.setApiVersion(apiVersion.getValue() .toInt()); HledgerVersion version = detectedVersion.getValue(); - mProfile.setDetectedVersionPre_1_19(version.isPre_1_20_1()); - mProfile.setDetectedVersionMajor(version.getMajor()); - mProfile.setDetectedVersionMinor(version.getMinor()); + mProfile.setDetectedVersionPre_1_19(version != null && version.isPre_1_20_1()); + mProfile.setDetectedVersionMajor(version != null ? version.getMajor() : -1); + mProfile.setDetectedVersionMinor(version != null ? version.getMinor() : -1); } synchronized public void triggerVersionDetection() { if (versionDetectionThread != null) @@ -293,6 +299,9 @@ public class ProfileDetailModel extends ViewModel { versionDetectionThread = new VersionDetectionThread(this); versionDetectionThread.start(); } + public LiveData getProfileId() { + return profileId; + } static class VersionDetectionThread extends Thread { static final int TARGET_PROCESS_DURATION = 1000; private final Pattern versionPattern = -- 2.39.5