]> git.ktnx.net Git - mobile-ledger.git/commitdiff
move API version enum in a standalone class
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 28 Dec 2020 09:00:08 +0000 (11:00 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 28 Dec 2020 09:00:24 +0000 (11:00 +0200)
will be used for sending too

12 files changed:
app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java
app/src/main/java/net/ktnx/mobileledger/json/API.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java
app/src/main/java/net/ktnx/mobileledger/json/TransactionListParser.java
app/src/main/java/net/ktnx/mobileledger/json/v1_14/AccountListParser.java
app/src/main/java/net/ktnx/mobileledger/json/v1_15/AccountListParser.java
app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/AccountListParser.java
app/src/main/java/net/ktnx/mobileledger/model/HledgerVersion.java
app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java

index d092a1cbfc55f1f0294cef1f6fc583248e14efa6..b3f38355a005089c5d98052175e73b887b324ade 100644 (file)
@@ -28,6 +28,7 @@ import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
 
 import net.ktnx.mobileledger.App;
 import net.ktnx.mobileledger.err.HTTPException;
+import net.ktnx.mobileledger.json.API;
 import net.ktnx.mobileledger.json.AccountListParser;
 import net.ktnx.mobileledger.json.ApiNotSupportedException;
 import net.ktnx.mobileledger.json.TransactionListParser;
@@ -386,11 +387,11 @@ public class RetrieveTransactionsTask extends
     }
     private List<LedgerAccount> retrieveAccountList()
             throws IOException, HTTPException, ApiNotSupportedException {
-        final SendTransactionTask.API apiVersion = profile.getApiVersion();
-        if (apiVersion.equals(SendTransactionTask.API.auto)) {
+        final API apiVersion = profile.getApiVersion();
+        if (apiVersion.equals(API.auto)) {
             return retrieveAccountListAnyVersion();
         }
-        else if (apiVersion.equals(SendTransactionTask.API.html)) {
+        else if (apiVersion.equals(API.html)) {
             Logger.debug("json",
                     "Declining using JSON API for /accounts with configured legacy API version");
             return null;
@@ -401,7 +402,7 @@ public class RetrieveTransactionsTask extends
     }
     private List<LedgerAccount> retrieveAccountListAnyVersion()
             throws HTTPException, ApiNotSupportedException {
-        for (SendTransactionTask.API ver : SendTransactionTask.API.allVersions) {
+        for (API ver : API.allVersions) {
             try {
                 return retrieveAccountListForVersion(ver);
             }
@@ -416,7 +417,7 @@ public class RetrieveTransactionsTask extends
 
         throw new RuntimeException("This should never be reached");
     }
-    private List<LedgerAccount> retrieveAccountListForVersion(SendTransactionTask.API version)
+    private List<LedgerAccount> retrieveAccountListForVersion(API version)
             throws IOException, HTTPException {
         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
         http.setAllowUserInteraction(false);
@@ -467,11 +468,11 @@ public class RetrieveTransactionsTask extends
     }
     private List<LedgerTransaction> retrieveTransactionList()
             throws ParseException, HTTPException, IOException, ApiNotSupportedException {
-        final SendTransactionTask.API apiVersion = profile.getApiVersion();
-        if (apiVersion.equals(SendTransactionTask.API.auto)) {
+        final API apiVersion = profile.getApiVersion();
+        if (apiVersion.equals(API.auto)) {
             return retrieveTransactionListAnyVersion();
         }
-        else if (apiVersion.equals(SendTransactionTask.API.html)) {
+        else if (apiVersion.equals(API.html)) {
             Logger.debug("json",
                     "Declining using JSON API for /accounts with configured legacy API version");
             return null;
@@ -483,7 +484,7 @@ public class RetrieveTransactionsTask extends
     }
     private List<LedgerTransaction> retrieveTransactionListAnyVersion()
             throws ApiNotSupportedException {
-        for (SendTransactionTask.API ver : SendTransactionTask.API.allVersions) {
+        for (API ver : API.allVersions) {
             try {
                 return retrieveTransactionListForVersion(ver);
             }
@@ -498,8 +499,8 @@ public class RetrieveTransactionsTask extends
 
         throw new RuntimeException("This should never be reached");
     }
-    private List<LedgerTransaction> retrieveTransactionListForVersion(
-            SendTransactionTask.API apiVersion) throws IOException, ParseException, HTTPException {
+    private List<LedgerTransaction> retrieveTransactionListForVersion(API apiVersion)
+            throws IOException, ParseException, HTTPException {
         Progress progress = new Progress();
         progress.setTotal(expectedPostingsCount);
 
index 9f441f9ca6148b5ddbabd0e24a6059ae9eaecc10..dc0c24875469a74b5d6404097a21034d1690672a 100644 (file)
 
 package net.ktnx.mobileledger.async;
 
-import android.content.res.Resources;
 import android.os.AsyncTask;
 import android.util.Log;
-import android.util.SparseArray;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.ObjectWriter;
 
-import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
@@ -326,59 +323,4 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
         taskCallback.done(error);
     }
 
-    public enum API {
-        auto(0), html(-1), v1_14(-2), v1_15(-3), v1_19_1(-4);
-        private static final SparseArray<API> map = new SparseArray<>();
-        public static API[] allVersions = {v1_19_1, v1_15, v1_14};
-
-        static {
-            for (API item : API.values()) {
-                map.put(item.value, item);
-            }
-        }
-
-        private final int value;
-
-        API(int value) {
-            this.value = value;
-        }
-        public static API valueOf(int i) {
-            return map.get(i, auto);
-        }
-        public int toInt() {
-            return this.value;
-        }
-        public String getDescription(Resources resources) {
-            switch (this) {
-                case auto:
-                    return resources.getString(R.string.api_auto);
-                case html:
-                    return resources.getString(R.string.api_html);
-                case v1_14:
-                    return resources.getString(R.string.api_1_14);
-                case v1_15:
-                    return resources.getString(R.string.api_1_15);
-                case v1_19_1:
-                    return resources.getString(R.string.api_1_19_1);
-                default:
-                    throw new IllegalStateException("Unexpected value: " + value);
-            }
-        }
-        public String getDescription() {
-            switch (this) {
-                case auto:
-                    return "(automatic)";
-                case html:
-                    return "(HTML)";
-                case v1_14:
-                    return "1.14";
-                case v1_15:
-                    return "1.15";
-                case v1_19_1:
-                    return "1.19.1";
-                default:
-                    throw new IllegalStateException("Unexpected value: " + this);
-            }
-        }
-    }
 }
\ No newline at end of file
diff --git a/app/src/main/java/net/ktnx/mobileledger/json/API.java b/app/src/main/java/net/ktnx/mobileledger/json/API.java
new file mode 100644 (file)
index 0000000..d634f29
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright © 2020 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.json;
+
+import android.content.res.Resources;
+import android.util.SparseArray;
+
+import net.ktnx.mobileledger.R;
+
+public enum API {
+    auto(0), html(-1), v1_14(-2), v1_15(-3), v1_19_1(-4);
+    private static final SparseArray<API> map = new SparseArray<>();
+    public static API[] allVersions = {v1_19_1, v1_15, v1_14};
+
+    static {
+        for (API item : API.values()) {
+            map.put(item.value, item);
+        }
+    }
+
+    private final int value;
+
+    API(int value) {
+        this.value = value;
+    }
+    public static API valueOf(int i) {
+        return map.get(i, auto);
+    }
+    public int toInt() {
+        return this.value;
+    }
+    public String getDescription(Resources resources) {
+        switch (this) {
+            case auto:
+                return resources.getString(R.string.api_auto);
+            case html:
+                return resources.getString(R.string.api_html);
+            case v1_14:
+                return resources.getString(R.string.api_1_14);
+            case v1_15:
+                return resources.getString(R.string.api_1_15);
+            case v1_19_1:
+                return resources.getString(R.string.api_1_19_1);
+            default:
+                throw new IllegalStateException("Unexpected value: " + value);
+        }
+    }
+    public String getDescription() {
+        switch (this) {
+            case auto:
+                return "(automatic)";
+            case html:
+                return "(HTML)";
+            case v1_14:
+                return "1.14";
+            case v1_15:
+                return "1.15";
+            case v1_19_1:
+                return "1.19.1";
+            default:
+                throw new IllegalStateException("Unexpected value: " + this);
+        }
+    }
+}
index 783142e5df0985de21994c24e80ecf140afa66c8..872cac79c7828f6dd06a637431aaa7f36a2dbfab 100644 (file)
@@ -20,7 +20,6 @@ package net.ktnx.mobileledger.json;
 import com.fasterxml.jackson.databind.MappingIterator;
 
 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
-import net.ktnx.mobileledger.async.SendTransactionTask;
 import net.ktnx.mobileledger.model.LedgerAccount;
 
 import java.io.IOException;
@@ -31,8 +30,8 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
 
 abstract public class AccountListParser {
     protected MappingIterator<net.ktnx.mobileledger.json.ParsedLedgerAccount> iterator;
-    public static AccountListParser forApiVersion(SendTransactionTask.API version,
-                                                  InputStream input) throws IOException {
+    public static AccountListParser forApiVersion(API version, InputStream input)
+            throws IOException {
         switch (version) {
             case v1_14:
                 return new net.ktnx.mobileledger.json.v1_14.AccountListParser(input);
@@ -45,7 +44,7 @@ abstract public class AccountListParser {
         }
 
     }
-    public abstract SendTransactionTask.API getApiVersion();
+    public abstract API getApiVersion();
     public LedgerAccount nextAccount(RetrieveTransactionsTask task,
                                      HashMap<String, LedgerAccount> map) {
         if (!iterator.hasNext())
index 99841f84a27eba4f75c706be3ad03edf547487e7..d707c062d8555d89be6fb95dabbc88771c6452f8 100644 (file)
@@ -17,7 +17,6 @@
 
 package net.ktnx.mobileledger.json;
 
-import net.ktnx.mobileledger.async.SendTransactionTask;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 
 import java.io.IOException;
@@ -25,8 +24,8 @@ import java.io.InputStream;
 import java.text.ParseException;
 
 public abstract class TransactionListParser {
-    public static TransactionListParser forApiVersion(SendTransactionTask.API apiVersion,
-                                                      InputStream input) throws IOException {
+    public static TransactionListParser forApiVersion(API apiVersion, InputStream input)
+            throws IOException {
         switch (apiVersion) {
             case v1_14:
                 return new net.ktnx.mobileledger.json.v1_14.TransactionListParser(input);
index 1df52924ca397b4fced7fbe73a57a9294e37fd8f..0c589520d4904a27ce1cfce2ca238e496d44c22f 100644 (file)
@@ -20,7 +20,7 @@ package net.ktnx.mobileledger.json.v1_14;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.ObjectReader;
 
-import net.ktnx.mobileledger.async.SendTransactionTask;
+import net.ktnx.mobileledger.json.API;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -34,7 +34,7 @@ public class AccountListParser extends net.ktnx.mobileledger.json.AccountListPar
         iterator = reader.readValues(input);
     }
     @Override
-    public SendTransactionTask.API getApiVersion() {
-        return SendTransactionTask.API.v1_14;
+    public API getApiVersion() {
+        return API.v1_14;
     }
 }
index 005cd5bad7324349ebc731319278d7e388c19357..f8f4dbb3d417669d17ba611daea6930a0648dfa6 100644 (file)
@@ -20,7 +20,7 @@ package net.ktnx.mobileledger.json.v1_15;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.ObjectReader;
 
-import net.ktnx.mobileledger.async.SendTransactionTask;
+import net.ktnx.mobileledger.json.API;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -34,7 +34,7 @@ public class AccountListParser extends net.ktnx.mobileledger.json.AccountListPar
         iterator = reader.readValues(input);
     }
     @Override
-    public SendTransactionTask.API getApiVersion() {
-        return SendTransactionTask.API.v1_15;
+    public API getApiVersion() {
+        return API.v1_15;
     }
 }
index 9ff89eb73a47734d3f73887ec4854c091336ecb1..85a64c8eef217a1d1bd2a54a55cbe9cfbf17ad76 100644 (file)
@@ -20,7 +20,7 @@ package net.ktnx.mobileledger.json.v1_19_1;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.ObjectReader;
 
-import net.ktnx.mobileledger.async.SendTransactionTask;
+import net.ktnx.mobileledger.json.API;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -34,7 +34,7 @@ public class AccountListParser extends net.ktnx.mobileledger.json.AccountListPar
         iterator = reader.readValues(input);
     }
     @Override
-    public SendTransactionTask.API getApiVersion() {
-        return SendTransactionTask.API.v1_19_1;
+    public API getApiVersion() {
+        return API.v1_19_1;
     }
 }
index 835c5c1a95173327d9910b1241fb8830f1d1c6d3..235f8c729bdfde5e73a8d89c8bd23936ccd02a9f 100644 (file)
@@ -20,7 +20,7 @@ package net.ktnx.mobileledger.model;
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
-import net.ktnx.mobileledger.async.SendTransactionTask;
+import net.ktnx.mobileledger.json.API;
 
 import java.util.Locale;
 
@@ -94,10 +94,10 @@ public class HledgerVersion {
         return ((this.major == major) && (this.minor >= minor)) || (this.major > major);
     }
     @org.jetbrains.annotations.Nullable
-    public SendTransactionTask.API getSuitableApiVersion() {
+    public API getSuitableApiVersion() {
         if (isPre_1_20_1)
             return null;
 
-        return SendTransactionTask.API.v1_19_1;
+        return API.v1_19_1;
     }
 }
index fcbd9203ef0852dcd3cf52d95e947ef0c3265155..a3157cd83c9b9418aaf850a6fa4061a55e87b6a3 100644 (file)
@@ -28,7 +28,7 @@ import androidx.annotation.Nullable;
 import net.ktnx.mobileledger.App;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.DbOpQueue;
-import net.ktnx.mobileledger.async.SendTransactionTask;
+import net.ktnx.mobileledger.json.API;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
 import net.ktnx.mobileledger.utils.SimpleDate;
@@ -59,7 +59,7 @@ public final class MobileLedgerProfile {
     private String authPassword;
     private int themeHue;
     private int orderNo = -1;
-    private SendTransactionTask.API apiVersion = SendTransactionTask.API.auto;
+    private API apiVersion = API.auto;
     private FutureDates futureDates = FutureDates.None;
     private boolean accountsLoaded;
     private boolean transactionsLoaded;
@@ -232,14 +232,14 @@ public final class MobileLedgerProfile {
         else
             this.defaultCommodity = String.valueOf(defaultCommodity);
     }
-    public SendTransactionTask.API getApiVersion() {
+    public API getApiVersion() {
         return apiVersion;
     }
-    public void setApiVersion(SendTransactionTask.API apiVersion) {
+    public void setApiVersion(API apiVersion) {
         this.apiVersion = apiVersion;
     }
     public void setApiVersion(int apiVersion) {
-        this.apiVersion = SendTransactionTask.API.valueOf(apiVersion);
+        this.apiVersion = API.valueOf(apiVersion);
     }
     public FutureDates getFutureDates() {
         return futureDates;
index 10f43667992fb27bfb17e5e74fe404230de26b92..6eb5239d95cbb36ce8a41a63b2323db2a2e64220 100644 (file)
@@ -46,7 +46,7 @@ import com.google.android.material.textfield.TextInputLayout;
 
 import net.ktnx.mobileledger.BuildConfig;
 import net.ktnx.mobileledger.R;
-import net.ktnx.mobileledger.async.SendTransactionTask;
+import net.ktnx.mobileledger.json.API;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
@@ -393,23 +393,23 @@ public class ProfileDetailFragment extends Fragment {
         PopupMenu menu = new PopupMenu(context, v);
         menu.inflate(R.menu.api_version);
         menu.setOnMenuItemClickListener(item -> {
-            SendTransactionTask.API apiVer;
+            API apiVer;
             switch (item.getItemId()) {
                 case R.id.api_version_menu_html:
-                    apiVer = SendTransactionTask.API.html;
+                    apiVer = API.html;
                     break;
                 case R.id.api_version_menu_1_19_1:
-                    apiVer = SendTransactionTask.API.v1_19_1;
+                    apiVer = API.v1_19_1;
                     break;
                 case R.id.api_version_menu_1_15:
-                    apiVer = SendTransactionTask.API.v1_15;
+                    apiVer = API.v1_15;
                     break;
                 case R.id.api_version_menu_1_14:
-                    apiVer = SendTransactionTask.API.v1_14;
+                    apiVer = API.v1_14;
                     break;
                 case R.id.api_version_menu_auto:
                 default:
-                    apiVer = SendTransactionTask.API.auto;
+                    apiVer = API.auto;
             }
             model.setApiVersion(apiVer);
             apiVersionText.setText(apiVer.getDescription(getResources()));
index 4cbbfe3d6fda715fa69b0668995aa51f0b61df97..5de553481684c8a78c12a3edd2a8e182b4e0acad 100644 (file)
@@ -24,7 +24,7 @@ import androidx.lifecycle.MutableLiveData;
 import androidx.lifecycle.Observer;
 import androidx.lifecycle.ViewModel;
 
-import net.ktnx.mobileledger.async.SendTransactionTask;
+import net.ktnx.mobileledger.json.API;
 import net.ktnx.mobileledger.model.Currency;
 import net.ktnx.mobileledger.model.HledgerVersion;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
@@ -53,8 +53,7 @@ public class ProfileDetailModel extends ViewModel {
     private final MutableLiveData<Boolean> showCommodityByDefault = new MutableLiveData<>(false);
     private final MutableLiveData<Boolean> showCommentsByDefault = new MutableLiveData<>(true);
     private final MutableLiveData<Boolean> useAuthentication = new MutableLiveData<>(false);
-    private final MutableLiveData<SendTransactionTask.API> apiVersion =
-            new MutableLiveData<>(SendTransactionTask.API.auto);
+    private final MutableLiveData<API> apiVersion = new MutableLiveData<>(API.auto);
     private final MutableLiveData<String> url = new MutableLiveData<>(null);
     private final MutableLiveData<String> authUserName = new MutableLiveData<>(null);
     private final MutableLiveData<String> authPassword = new MutableLiveData<>(null);
@@ -137,14 +136,14 @@ public class ProfileDetailModel extends ViewModel {
     void observeUseAuthentication(LifecycleOwner lfo, Observer<Boolean> o) {
         useAuthentication.observe(lfo, o);
     }
-    SendTransactionTask.API getApiVersion() {
+    API getApiVersion() {
         return apiVersion.getValue();
     }
-    void setApiVersion(SendTransactionTask.API newValue) {
+    void setApiVersion(API newValue) {
         if (newValue != apiVersion.getValue())
             apiVersion.setValue(newValue);
     }
-    void observeApiVersion(LifecycleOwner lfo, Observer<SendTransactionTask.API> o) {
+    void observeApiVersion(LifecycleOwner lfo, Observer<API> o) {
         apiVersion.observe(lfo, o);
     }
     HledgerVersion getDetectedVersion() { return detectedVersion.getValue(); }
@@ -254,7 +253,7 @@ public class ProfileDetailModel extends ViewModel {
             showCommentsByDefault.setValue(true);
             showCommodityByDefault.setValue(false);
             setFutureDates(MobileLedgerProfile.FutureDates.None);
-            setApiVersion(SendTransactionTask.API.auto);
+            setApiVersion(API.auto);
             useAuthentication.setValue(false);
             authUserName.setValue("");
             authPassword.setValue("");