]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfileDetailModel.java
index 5de553481684c8a78c12a3edd2a8e182b4e0acad..09b0a067a66256b30565fa90191b870505445915 100644 (file)
@@ -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,14 +20,16 @@ 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;
 
+import net.ktnx.mobileledger.App;
+import net.ktnx.mobileledger.db.Profile;
 import net.ktnx.mobileledger.json.API;
-import net.ktnx.mobileledger.model.Currency;
+import net.ktnx.mobileledger.model.FutureDates;
 import net.ktnx.mobileledger.model.HledgerVersion;
-import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.utils.Colors;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
@@ -43,13 +45,16 @@ 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<String> profileName = new MutableLiveData<>();
+    private final MutableLiveData<Integer> orderNo = new MutableLiveData<>();
     private final MutableLiveData<Boolean> postingPermitted = new MutableLiveData<>(true);
-    private final MutableLiveData<Currency> defaultCommodity = new MutableLiveData<>(null);
-    private final MutableLiveData<MobileLedgerProfile.FutureDates> futureDates =
-            new MutableLiveData<>(MobileLedgerProfile.FutureDates.None);
+    private final MutableLiveData<String> defaultCommodity = new MutableLiveData<>(null);
+    private final MutableLiveData<FutureDates> futureDates =
+            new MutableLiveData<>(FutureDates.None);
     private final MutableLiveData<Boolean> showCommodityByDefault = new MutableLiveData<>(false);
     private final MutableLiveData<Boolean> showCommentsByDefault = new MutableLiveData<>(true);
     private final MutableLiveData<Boolean> useAuthentication = new MutableLiveData<>(false);
@@ -61,6 +66,7 @@ public class ProfileDetailModel extends ViewModel {
     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() {
@@ -96,24 +102,24 @@ public class ProfileDetailModel extends ViewModel {
     void observeShowCommentsByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
         showCommentsByDefault.observe(lfo, o);
     }
-    MobileLedgerProfile.FutureDates getFutureDates() {
+    FutureDates getFutureDates() {
         return futureDates.getValue();
     }
-    void setFutureDates(MobileLedgerProfile.FutureDates newValue) {
+    void setFutureDates(FutureDates newValue) {
         if (newValue != futureDates.getValue())
             futureDates.setValue(newValue);
     }
-    void observeFutureDates(LifecycleOwner lfo, Observer<MobileLedgerProfile.FutureDates> o) {
+    void observeFutureDates(LifecycleOwner lfo, Observer<FutureDates> o) {
         futureDates.observe(lfo, o);
     }
-    Currency getDefaultCommodity() {
+    String getDefaultCommodity() {
         return defaultCommodity.getValue();
     }
-    void setDefaultCommodity(Currency newValue) {
-        if (newValue != defaultCommodity.getValue())
+    void setDefaultCommodity(String newValue) {
+        if (!Misc.equalStrings(newValue, defaultCommodity.getValue()))
             defaultCommodity.setValue(newValue);
     }
-    void observeDefaultCommodity(LifecycleOwner lfo, Observer<Currency> o) {
+    void observeDefaultCommodity(LifecycleOwner lfo, Observer<String> o) {
         defaultCommodity.observe(lfo, o);
     }
     Boolean getShowCommodityByDefault() {
@@ -126,7 +132,7 @@ public class ProfileDetailModel extends ViewModel {
     void observeShowCommodityByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
         showCommodityByDefault.observe(lfo, o);
     }
-    Boolean getUseAuthentication() {
+    public Boolean getUseAuthentication() {
         return useAuthentication.getValue();
     }
     void setUseAuthentication(boolean newValue) {
@@ -154,7 +160,7 @@ public class ProfileDetailModel extends ViewModel {
     void observeDetectedVersion(LifecycleOwner lfo, Observer<HledgerVersion> o) {
         detectedVersion.observe(lfo, o);
     }
-    String getUrl() {
+    public String getUrl() {
         return url.getValue();
     }
     void setUrl(String newValue) {
@@ -168,7 +174,7 @@ public class ProfileDetailModel extends ViewModel {
     void observeUrl(LifecycleOwner lfo, Observer<String> o) {
         url.observe(lfo, o);
     }
-    String getAuthUserName() {
+    public String getAuthUserName() {
         return authUserName.getValue();
     }
     void setAuthUserName(String newValue) {
@@ -182,7 +188,7 @@ public class ProfileDetailModel extends ViewModel {
     void observeUserName(LifecycleOwner lfo, Observer<String> o) {
         authUserName.observe(lfo, o);
     }
-    String getAuthPassword() {
+    public String getAuthPassword() {
         return authPassword.getValue();
     }
     void setAuthPassword(String newValue) {
@@ -222,11 +228,12 @@ public class ProfileDetailModel extends ViewModel {
     void observeDetectingHledgerVersion(LifecycleOwner lfo, Observer<Boolean> o) {
         detectingHledgerVersion.observe(lfo, o);
     }
-    void setValuesFromProfile(MobileLedgerProfile mProfile, int newProfileHue) {
-        final int profileThemeId;
+    void setValuesFromProfile(Profile mProfile) {
         if (mProfile != null) {
+            profileId.setValue(mProfile.getId());
             profileName.setValue(mProfile.getName());
-            postingPermitted.setValue(mProfile.isPostingPermitted());
+            orderNo.setValue(mProfile.getOrderNo());
+            postingPermitted.setValue(mProfile.permitPosting());
             showCommentsByDefault.setValue(mProfile.getShowCommentsByDefault());
             showCommodityByDefault.setValue(mProfile.getShowCommodityByDefault());
             {
@@ -234,50 +241,60 @@ public class ProfileDetailModel extends ViewModel {
                 if (TextUtils.isEmpty(comm))
                     setDefaultCommodity(null);
                 else
-                    setDefaultCommodity(new Currency(-1, comm));
+                    setDefaultCommodity(comm);
             }
-            futureDates.setValue(mProfile.getFutureDates());
-            apiVersion.setValue(mProfile.getApiVersion());
+            futureDates.setValue(FutureDates.valueOf(mProfile.getFutureDates()));
+            apiVersion.setValue(API.valueOf(mProfile.getApiVersion()));
             url.setValue(mProfile.getUrl());
-            useAuthentication.setValue(mProfile.isAuthEnabled());
-            authUserName.setValue(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
-            authPassword.setValue(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
+            useAuthentication.setValue(mProfile.useAuthentication());
+            authUserName.setValue(mProfile.useAuthentication() ? mProfile.getAuthUser() : "");
+            authPassword.setValue(mProfile.useAuthentication() ? mProfile.getAuthPassword() : "");
             preferredAccountsFilter.setValue(mProfile.getPreferredAccountsFilter());
-            themeId.setValue(mProfile.getThemeHue());
-            detectedVersion.setValue(mProfile.getDetectedVersion());
+            themeId.setValue(mProfile.getTheme());
+            detectedVersion.setValue(mProfile.detectedVersionPre_1_19() ? new HledgerVersion(true)
+                                                                        : new HledgerVersion(
+                                                                                mProfile.getDetectedVersionMajor(),
+                                                                                mProfile.getDetectedVersionMinor()));
         }
         else {
+            profileId.setValue(NO_PROFILE_ID);
+            orderNo.setValue(-1);
             profileName.setValue(null);
             url.setValue(HTTPS_URL_START);
             postingPermitted.setValue(true);
             showCommentsByDefault.setValue(true);
             showCommodityByDefault.setValue(false);
-            setFutureDates(MobileLedgerProfile.FutureDates.None);
+            setFutureDates(FutureDates.None);
             setApiVersion(API.auto);
             useAuthentication.setValue(false);
             authUserName.setValue("");
             authPassword.setValue("");
             preferredAccountsFilter.setValue(null);
-            themeId.setValue(newProfileHue);
             detectedVersion.setValue(null);
         }
     }
-    void updateProfile(MobileLedgerProfile mProfile) {
+    void updateProfile(Profile mProfile) {
+        mProfile.setId(profileId.getValue());
         mProfile.setName(profileName.getValue());
+        mProfile.setOrderNo(orderNo.getValue());
         mProfile.setUrl(url.getValue());
-        mProfile.setPostingPermitted(postingPermitted.getValue());
+        mProfile.setPermitPosting(postingPermitted.getValue());
         mProfile.setShowCommentsByDefault(showCommentsByDefault.getValue());
-        Currency c = defaultCommodity.getValue();
-        mProfile.setDefaultCommodity((c == null) ? null : c.getName());
+        mProfile.setDefaultCommodity(defaultCommodity.getValue());
         mProfile.setShowCommodityByDefault(showCommodityByDefault.getValue());
         mProfile.setPreferredAccountsFilter(preferredAccountsFilter.getValue());
-        mProfile.setAuthEnabled(useAuthentication.getValue());
-        mProfile.setAuthUserName(authUserName.getValue());
+        mProfile.setUseAuthentication(useAuthentication.getValue());
+        mProfile.setAuthUser(authUserName.getValue());
         mProfile.setAuthPassword(authPassword.getValue());
-        mProfile.setThemeHue(themeId.getValue());
-        mProfile.setFutureDates(futureDates.getValue());
-        mProfile.setApiVersion(apiVersion.getValue());
-        mProfile.setDetectedVersion(detectedVersion.getValue());
+        mProfile.setTheme(themeId.getValue());
+        mProfile.setFutureDates(futureDates.getValue()
+                                           .toInt());
+        mProfile.setApiVersion(apiVersion.getValue()
+                                         .toInt());
+        HledgerVersion version = detectedVersion.getValue();
+        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)
@@ -286,6 +303,9 @@ public class ProfileDetailModel extends ViewModel {
         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 =
@@ -295,7 +315,8 @@ public class ProfileDetailModel extends ViewModel {
             this.model = model;
         }
         private HledgerVersion detectVersion() {
-            HttpURLConnection http = null;
+            App.setAuthenticationDataFromProfileModel(model);
+            HttpURLConnection http;
             try {
                 http = NetworkUtil.prepareConnection(model.getUrl(), "version",
                         model.getUseAuthentication());
@@ -317,8 +338,9 @@ public class ProfileDetailModel extends ViewModel {
                 if (m.matches()) {
                     int major = Integer.parseInt(Objects.requireNonNull(m.group(1)));
                     int minor = Integer.parseInt(Objects.requireNonNull(m.group(2)));
-                    final boolean hasPatch = m.groupCount() >= 3;
-                    int patch = hasPatch ? Integer.parseInt(Objects.requireNonNull(m.group(3))) : 0;
+                    final String patchText = m.group(3);
+                    final boolean hasPatch = patchText != null;
+                    int patch = hasPatch ? Integer.parseInt(patchText) : 0;
 
                     return hasPatch ? new HledgerVersion(major, minor, patch)
                                     : new HledgerVersion(major, minor);
@@ -333,6 +355,9 @@ public class ProfileDetailModel extends ViewModel {
                 e.printStackTrace();
                 return null;
             }
+            finally {
+                App.resetAuthenticationData();
+            }
         }
         @Override
         public void run() {