From 8f7f4ee9f97505bac57c5c661b18dfac7882004c Mon Sep 17 00:00:00 2001 From: m2049r <30435443+m2049r@users.noreply.github.com> Date: Thu, 23 Nov 2017 08:23:23 +0100 Subject: [PATCH] changes based on review --- .../com/m2049r/xmrwallet/WalletFragment.java | 5 +- .../m2049r/xmrwallet/layout/ExchangeView.java | 3 +- .../exchange/api/ExchangeException.java | 2 +- .../exchange/kraken/ExchangeApiImpl.java | 45 +++++++++++++-- .../exchange/kraken/ExchangeRateImpl.java | 57 ++----------------- .../exchange/kraken/NetworkCallback.java | 27 --------- .../com/m2049r/xmrwallet/util/Helper.java | 12 ---- .../OkHttpClientSingleton.java} | 20 +++++-- 8 files changed, 65 insertions(+), 106 deletions(-) delete mode 100644 app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/NetworkCallback.java rename app/src/main/java/com/m2049r/xmrwallet/{service/exchange/kraken/ExchangeApiCall.java => util/OkHttpClientSingleton.java} (52%) diff --git a/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java b/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java index 7a01c683..b1737aa6 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java @@ -47,6 +47,7 @@ import com.m2049r.xmrwallet.service.exchange.api.ExchangeCallback; import com.m2049r.xmrwallet.service.exchange.api.ExchangeRate; import com.m2049r.xmrwallet.service.exchange.kraken.ExchangeApiImpl; import com.m2049r.xmrwallet.util.Helper; +import com.m2049r.xmrwallet.util.OkHttpClientSingleton; import java.text.NumberFormat; import java.util.List; @@ -151,7 +152,7 @@ public class WalletFragment extends Fragment if (isExchanging) return; // wait for exchange to finish - it will fire this itself then. // at this point selection is XMR in case of error String displayB; - double amountA = Double.parseDouble(Wallet.getDisplayAmount(unlockedBalance)); // assume this cannot fail! + double amountA = Double.parseDouble(Wallet.getDisplayAmount(unlockedBalance)); // crash if this fails! if (!"XMR".equals(balanceCurrency)) { // not XMR double amountB = amountA * balanceRate; displayB = Helper.getFormattedAmount(amountB, false); @@ -164,7 +165,7 @@ public class WalletFragment extends Fragment String balanceCurrency = "XMR"; double balanceRate = 1.0; - private final ExchangeApi exchangeApi = new ExchangeApiImpl(Helper.getOkHttpClient()); + private final ExchangeApi exchangeApi = new ExchangeApiImpl(OkHttpClientSingleton.getOkHttpClient()); void refreshBalance() { if (sCurrency.getSelectedItemPosition() == 0) { // XMR diff --git a/app/src/main/java/com/m2049r/xmrwallet/layout/ExchangeView.java b/app/src/main/java/com/m2049r/xmrwallet/layout/ExchangeView.java index a270c0de..6476ae38 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/layout/ExchangeView.java +++ b/app/src/main/java/com/m2049r/xmrwallet/layout/ExchangeView.java @@ -44,6 +44,7 @@ import com.m2049r.xmrwallet.service.exchange.api.ExchangeCallback; import com.m2049r.xmrwallet.service.exchange.api.ExchangeRate; import com.m2049r.xmrwallet.service.exchange.kraken.ExchangeApiImpl; import com.m2049r.xmrwallet.util.Helper; +import com.m2049r.xmrwallet.util.OkHttpClientSingleton; import java.util.Locale; @@ -285,7 +286,7 @@ public class ExchangeView extends LinearLayout { startExchange(); } - private final ExchangeApi exchangeApi = new ExchangeApiImpl(Helper.getOkHttpClient()); + private final ExchangeApi exchangeApi = new ExchangeApiImpl(OkHttpClientSingleton.getOkHttpClient()); void startExchange() { showProgress(); diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/exchange/api/ExchangeException.java b/app/src/main/java/com/m2049r/xmrwallet/service/exchange/api/ExchangeException.java index 682c6d17..905819d7 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/service/exchange/api/ExchangeException.java +++ b/app/src/main/java/com/m2049r/xmrwallet/service/exchange/api/ExchangeException.java @@ -17,7 +17,7 @@ package com.m2049r.xmrwallet.service.exchange.api; public class ExchangeException extends Exception { - private int code; + private final int code; private final String errorMsg; public String getErrorMsg() { diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/ExchangeApiImpl.java b/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/ExchangeApiImpl.java index 848dc4b0..a1ec5b81 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/ExchangeApiImpl.java +++ b/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/ExchangeApiImpl.java @@ -22,6 +22,7 @@ import android.support.annotation.VisibleForTesting; import com.m2049r.xmrwallet.service.exchange.api.ExchangeApi; import com.m2049r.xmrwallet.service.exchange.api.ExchangeCallback; import com.m2049r.xmrwallet.service.exchange.api.ExchangeException; +import com.m2049r.xmrwallet.service.exchange.api.ExchangeRate; import org.json.JSONArray; import org.json.JSONException; @@ -35,7 +36,7 @@ import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -public class ExchangeApiImpl implements ExchangeApi, ExchangeApiCall { +public class ExchangeApiImpl implements ExchangeApi { @NonNull private final OkHttpClient okHttpClient; @@ -57,11 +58,31 @@ public class ExchangeApiImpl implements ExchangeApi, ExchangeApiCall { @Override public void queryExchangeRate(@NonNull final String baseCurrency, @NonNull final String quoteCurrency, @NonNull final ExchangeCallback callback) { - ExchangeRateImpl.call(this, baseCurrency, quoteCurrency, callback); - } - @Override - public void call(@NonNull final String fiat, @NonNull final NetworkCallback callback) { + if (baseCurrency.equals(quoteCurrency)) { + callback.onSuccess(new ExchangeRateImpl(baseCurrency, quoteCurrency, 1.0)); + return; + } + + boolean inverse = false; + String fiat = null; + + if (baseCurrency.equals("XMR")) { + fiat = quoteCurrency; + inverse = false; + } + + if (quoteCurrency.equals("XMR")) { + fiat = baseCurrency; + inverse = true; + } + + if (fiat == null) { + callback.onError(new IllegalArgumentException("no fiat specified")); + return; + } + + final boolean swapAssets = inverse; final HttpUrl url = baseUrl.newBuilder() .addQueryParameter("pair", "XMR" + fiat) @@ -86,7 +107,7 @@ public class ExchangeApiImpl implements ExchangeApi, ExchangeApiCall { callback.onError(new ExchangeException(response.code(), errorMsg)); } else { final JSONObject jsonResult = json.getJSONObject("result"); - callback.onSuccess(jsonResult); + reportSuccess(jsonResult, swapAssets, callback); } } catch (JSONException ex) { callback.onError(new ExchangeException(ex.getLocalizedMessage())); @@ -98,6 +119,18 @@ public class ExchangeApiImpl implements ExchangeApi, ExchangeApiCall { }); } + void reportSuccess(JSONObject jsonObject, boolean swapAssets, ExchangeCallback callback) { + try { + final ExchangeRate exchangeRate = new ExchangeRateImpl(jsonObject, swapAssets); + callback.onSuccess(exchangeRate); + } catch (JSONException ex) { + callback.onError(new ExchangeException(ex.getLocalizedMessage())); + } catch (ExchangeException ex) { + callback.onError(ex); + } + } + + private Request createHttpRequest(final HttpUrl url) { return new Request.Builder() .url(url) diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/ExchangeRateImpl.java b/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/ExchangeRateImpl.java index 35263bee..c30ae81d 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/ExchangeRateImpl.java +++ b/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/ExchangeRateImpl.java @@ -63,14 +63,14 @@ class ExchangeRateImpl implements ExchangeRate { this.rate = rate; } - ExchangeRateImpl(final JSONObject jsonObject) throws JSONException, ExchangeException { + ExchangeRateImpl(final JSONObject jsonObject, final boolean swapAssets) throws JSONException, ExchangeException { try { final String key = jsonObject.keys().next(); // we expect only one Pattern pattern = Pattern.compile("^X(.*?)Z(.*?)$"); Matcher matcher = pattern.matcher(key); if (matcher.find()) { - this.baseCurrency = matcher.group(1); - this.quoteCurrency = matcher.group(2); + this.baseCurrency = swapAssets ? matcher.group(2) : matcher.group(1); + this.quoteCurrency = swapAssets ? matcher.group(1) : matcher.group(2); } else { throw new ExchangeException("no pair returned!"); } @@ -80,7 +80,8 @@ class ExchangeRateImpl implements ExchangeRate { String closePrice = close.getString(0); if (closePrice != null) { try { - this.rate = Double.parseDouble(closePrice); + double rate = Double.parseDouble(closePrice); + this.rate = swapAssets ? (1 / rate) : rate; } catch (NumberFormatException ex) { throw new ExchangeException(ex.getLocalizedMessage()); } @@ -91,52 +92,4 @@ class ExchangeRateImpl implements ExchangeRate { throw new ExchangeException(ex.getLocalizedMessage()); } } - - public static void call(@NonNull final ExchangeApiCall api, - @NonNull final String baseCurrency, @NonNull final String quoteCurrency, - @NonNull final ExchangeCallback callback) { - - if (baseCurrency.equals(quoteCurrency)) { - callback.onSuccess(new ExchangeRateImpl(baseCurrency, quoteCurrency, 1.0)); - return; - } - - boolean inverse = false; - String fiat = null; - - if (baseCurrency.equals("XMR")) { - fiat = quoteCurrency; - inverse = false; - } - - if (quoteCurrency.equals("XMR")) { - fiat = baseCurrency; - inverse = true; - } - - if (fiat == null) { - callback.onError(new IllegalArgumentException("no fiat specified")); - return; - } - - api.call(fiat, new NetworkCallback() { - @Override - public void onSuccess(JSONObject jsonObject) { - try { - final ExchangeRate exchangeRate = new ExchangeRateImpl(jsonObject); - callback.onSuccess(exchangeRate); - } catch (JSONException ex) { - callback.onError(new ExchangeException(ex.getLocalizedMessage())); - } catch (ExchangeException ex) { - callback.onError(ex); - } - } - - @Override - public void onError(Exception ex) { - callback.onError(ex); - } - }); - } - } diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/NetworkCallback.java b/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/NetworkCallback.java deleted file mode 100644 index 8df2e30f..00000000 --- a/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/NetworkCallback.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2017 m2049r et al. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.m2049r.xmrwallet.service.exchange.kraken; - -import org.json.JSONObject; - -interface NetworkCallback { - - void onSuccess(JSONObject jsonObject); - - void onError(Exception ex); - -} diff --git a/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java b/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java index 2399788d..264142ec 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java +++ b/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java @@ -243,16 +243,4 @@ public class Helper { clipboardManager.setPrimaryClip(clip); } - static private OkHttpClient OkHttpClientSingleton; - - static public final OkHttpClient getOkHttpClient() { - if (OkHttpClientSingleton == null) { - synchronized (Helper.class) { - if (OkHttpClientSingleton == null) { - OkHttpClientSingleton = new OkHttpClient(); - } - } - } - return OkHttpClientSingleton; - } } diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/ExchangeApiCall.java b/app/src/main/java/com/m2049r/xmrwallet/util/OkHttpClientSingleton.java similarity index 52% rename from app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/ExchangeApiCall.java rename to app/src/main/java/com/m2049r/xmrwallet/util/OkHttpClientSingleton.java index 3330508c..f1dae5c9 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/service/exchange/kraken/ExchangeApiCall.java +++ b/app/src/main/java/com/m2049r/xmrwallet/util/OkHttpClientSingleton.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 m2049r et al. + * Copyright (c) 2017 m2049r * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,11 +14,21 @@ * limitations under the License. */ -package com.m2049r.xmrwallet.service.exchange.kraken; +package com.m2049r.xmrwallet.util; -import android.support.annotation.NonNull; +import okhttp3.OkHttpClient; -interface ExchangeApiCall { +public class OkHttpClientSingleton { + static private OkHttpClient Singleton; - void call(@NonNull final String fiat, @NonNull final NetworkCallback callback); + static public final OkHttpClient getOkHttpClient() { + if (Singleton == null) { + synchronized (com.m2049r.xmrwallet.util.OkHttpClientSingleton.class) { + if (Singleton == null) { + Singleton = new OkHttpClient(); + } + } + } + return Singleton; + } }