mirror of https://github.com/m2049r/xmrwallet.git
changes based on review
This commit is contained in:
parent
c23597066a
commit
8f7f4ee9f9
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue