From: Damyan Ivanov Date: Mon, 28 Dec 2020 09:00:08 +0000 (+0200) Subject: move API version enum in a standalone class X-Git-Tag: v0.16.0~12 X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=commitdiff_plain;h=bb789332571609eeb1bef6e39b7ad359227d1045 move API version enum in a standalone class will be used for sending too --- diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java index d092a1cb..b3f38355 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -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 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 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 retrieveAccountListForVersion(SendTransactionTask.API version) + private List 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 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 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 retrieveTransactionListForVersion( - SendTransactionTask.API apiVersion) throws IOException, ParseException, HTTPException { + private List retrieveTransactionListForVersion(API apiVersion) + throws IOException, ParseException, HTTPException { Progress progress = new Progress(); progress.setTotal(expectedPostingsCount); diff --git a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java index 9f441f9c..dc0c2487 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java @@ -17,15 +17,12 @@ 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 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 index 00000000..d634f29e --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/API.java @@ -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 . + */ + +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 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); + } + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java index 783142e5..872cac79 100644 --- a/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java +++ b/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java @@ -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 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 map) { if (!iterator.hasNext()) diff --git a/app/src/main/java/net/ktnx/mobileledger/json/TransactionListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/TransactionListParser.java index 99841f84..d707c062 100644 --- a/app/src/main/java/net/ktnx/mobileledger/json/TransactionListParser.java +++ b/app/src/main/java/net/ktnx/mobileledger/json/TransactionListParser.java @@ -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); diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_14/AccountListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/AccountListParser.java index 1df52924..0c589520 100644 --- a/app/src/main/java/net/ktnx/mobileledger/json/v1_14/AccountListParser.java +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/AccountListParser.java @@ -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; } } diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_15/AccountListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/AccountListParser.java index 005cd5ba..f8f4dbb3 100644 --- a/app/src/main/java/net/ktnx/mobileledger/json/v1_15/AccountListParser.java +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/AccountListParser.java @@ -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; } } diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/AccountListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/AccountListParser.java index 9ff89eb7..85a64c8e 100644 --- a/app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/AccountListParser.java +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/AccountListParser.java @@ -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; } } diff --git a/app/src/main/java/net/ktnx/mobileledger/model/HledgerVersion.java b/app/src/main/java/net/ktnx/mobileledger/model/HledgerVersion.java index 835c5c1a..235f8c72 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/HledgerVersion.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/HledgerVersion.java @@ -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; } } diff --git a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java index fcbd9203..a3157cd8 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -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; 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 10f43667..6eb5239d 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 @@ -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())); 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 4cbbfe3d..5de55348 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 @@ -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 showCommodityByDefault = new MutableLiveData<>(false); private final MutableLiveData showCommentsByDefault = new MutableLiveData<>(true); private final MutableLiveData useAuthentication = new MutableLiveData<>(false); - private final MutableLiveData apiVersion = - new MutableLiveData<>(SendTransactionTask.API.auto); + private final MutableLiveData apiVersion = new MutableLiveData<>(API.auto); private final MutableLiveData url = new MutableLiveData<>(null); private final MutableLiveData authUserName = new MutableLiveData<>(null); private final MutableLiveData authPassword = new MutableLiveData<>(null); @@ -137,14 +136,14 @@ public class ProfileDetailModel extends ViewModel { void observeUseAuthentication(LifecycleOwner lfo, Observer 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 o) { + void observeApiVersion(LifecycleOwner lfo, Observer 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("");