X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fprofiles%2FProfileDetailModel.java;h=a00ec9454b756083d0fe7d8f2df6632ad9c86d7e;hp=e1f956acb3b7e52ccde9ab59b2601ce4fcdeb0d6;hb=349a5a3aca6dab475eada68d99d59d0dff9378b3;hpb=55555f564f5f60dfd6fe702ff61e9621ff3d62a0 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 =