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;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
+import java.util.Objects;
import static net.ktnx.mobileledger.utils.Logger.debug;
public static final String ARG_HUE = "hue";
@NonNls
- private Profile mProfile;
private boolean defaultCommoditySet;
private boolean syncingModelFromUI = false;
private ProfileDetailBinding binding;
menuDeleteProfile.setOnMenuItemClickListener(item -> onDeleteProfile());
final List<Profile> 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)
}
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) {
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();
/*
- * 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
import android.text.TextUtils;
import androidx.lifecycle.LifecycleOwner;
+import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
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<String> profileName = new MutableLiveData<>();
private final MutableLiveData<Integer> themeId = new MutableLiveData<>(-1);
private final MutableLiveData<HledgerVersion> detectedVersion = new MutableLiveData<>(null);
private final MutableLiveData<Boolean> detectingHledgerVersion = new MutableLiveData<>(false);
+ private final MutableLiveData<Long> profileId = new MutableLiveData<>(NO_PROFILE_ID);
public int initialThemeHue = Colors.DEFAULT_HUE_DEG;
private VersionDetectionThread versionDetectionThread;
public ProfileDetailModel() {
}
void setValuesFromProfile(Profile mProfile) {
if (mProfile != null) {
+ profileId.setValue(mProfile.getId());
profileName.setValue(mProfile.getName());
postingPermitted.setValue(mProfile.permitPosting());
showCommentsByDefault.setValue(mProfile.getShowCommentsByDefault());
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());
mProfile.getDetectedVersionMinor()));
}
else {
+ profileId.setValue(NO_PROFILE_ID);
profileName.setValue(null);
url.setValue(HTTPS_URL_START);
postingPermitted.setValue(true);
}
}
void updateProfile(Profile mProfile) {
+ mProfile.setId(profileId.getValue());
mProfile.setName(profileName.getValue());
mProfile.setUrl(url.getValue());
mProfile.setPermitPosting(postingPermitted.getValue());
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)
versionDetectionThread = new VersionDetectionThread(this);
versionDetectionThread.start();
}
+ public LiveData<Long> getProfileId() {
+ return profileId;
+ }
static class VersionDetectionThread extends Thread {
static final int TARGET_PROCESS_DURATION = 1000;
private final Pattern versionPattern =