diff --git a/app/src/main/java/com/example/dcav2gui/InstanceInterface.java b/app/src/main/java/com/example/dcav2gui/InstanceInterface.java new file mode 100644 index 0000000..2d02e65 --- /dev/null +++ b/app/src/main/java/com/example/dcav2gui/InstanceInterface.java @@ -0,0 +1,105 @@ +package com.example.dcav2gui; + +import static com.example.dcav2gui.MainActivity.globalSettings; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import java.io.IOException; +import java.util.Locale; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +public class InstanceInterface { + private static final String API_BASE_URL = globalSettings.apiUrl; + private static final String API_KEY = globalSettings.apiKey; + private static final OkHttpClient httpClient = new OkHttpClient(); + + public static ProfitStatsData getProfitStatsData() throws IOException { + // Construct the API request URL for stock quote + Request stockRequest = new Request.Builder() + .url(API_BASE_URL + "/statistics_server/combined_totals") + .header("X-API-KEY", API_KEY) + .build(); + + try (Response statsResponse = httpClient.newCall(stockRequest).execute()) { + if (!statsResponse.isSuccessful()) { + throw new IOException("Unexpected code " + statsResponse); + } + String stockResponseBody = statsResponse.body().string(); + + double todaysProfit = getTodaysProfit(stockResponseBody); + double thisMonthProfit = getThisMonthsProfit(stockResponseBody); + + // Return the stock quote data + return new ProfitStatsData( + todaysProfit, + thisMonthProfit + ); + } + } + + private static double getTodaysProfit(String jsonResponse) { + try { + // Parse the JSON response + JsonElement jsonElement = JsonParser.parseString(jsonResponse); + if (!jsonElement.isJsonObject()) { + System.err.println("The parsed JSON response is not a JsonObject."); + return 0.0; + } + JsonObject jsonObject = jsonElement.getAsJsonObject(); + if (jsonObject.has("combined")) { + JsonArray combinedArray = jsonObject.get("combined").getAsJsonArray(); + return combinedArray.get(0).getAsDouble(); + } else { + System.err.println("The parsed JSON response does not contain a 'combined' array."); + return 0.0; + } + } catch (Exception e) { + System.err.println("Error processing combined profit data: " + e.getMessage()); + return 0.0; + } + } + + private static double getThisMonthsProfit(String jsonResponse) { + try { + // Parse the JSON response + JsonElement jsonElement = JsonParser.parseString(jsonResponse); + if (!jsonElement.isJsonObject()) { + System.err.println("The parsed JSON response is not a JsonObject."); + return 0.0; + } + JsonObject jsonObject = jsonElement.getAsJsonObject(); + if (jsonObject.has("combined")) { + JsonArray combinedArray = jsonObject.get("combined").getAsJsonArray(); + return combinedArray.get(1).getAsDouble(); + } else { + System.err.println("The parsed JSON response does not contain a 'combined' array."); + return 0.0; + } + } catch (Exception e) { + System.err.println("Error processing combined profit data: " + e.getMessage()); + return 0.0; + } + } + + + // Class to hold profits stats data + public static class ProfitStatsData { + private final double profitsToday; + private final double profitsThisMonth; + + public ProfitStatsData(double profitsToday, double profitsThisMonth) { + this.profitsToday = profitsToday; + this.profitsThisMonth = profitsThisMonth; + } + + // Getters + public double getProfitsToday() { return profitsToday; } + public double getProfitsThisMonth() { return profitsThisMonth; } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java b/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java index 92d334c..848c560 100644 --- a/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java +++ b/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java @@ -14,6 +14,7 @@ import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; +import com.example.dcav2gui.InstanceInterface; import com.example.dcav2gui.MainActivity; import com.example.dcav2gui.R; import com.example.dcav2gui.TickerTracker; @@ -24,6 +25,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.io.IOException; +import java.util.concurrent.Future; public class HomeFragment extends Fragment { @@ -176,12 +178,16 @@ public class HomeFragment extends Fragment { log4Content = root.findViewById(R.id.log4CardContent); + /* + * + * LOAD CACHED VALUES + * + */ + // Profits today and this month profitsToday.setText(R.string.profits_today_example); profitsThisMonth.setText(R.string.profits_this_month_example); - - // LOAD CACHED VALUES //Prices pricePair1.setText(R.string.default_price_ticker_1); pricePair2.setText(R.string.default_price_ticker_2); @@ -334,9 +340,17 @@ public class HomeFragment extends Fragment { } }); - // Wait for all futures to complete - CompletableFuture allFutures = CompletableFuture.allOf(future1, future2, future3); + CompletableFuture future4 = CompletableFuture.supplyAsync(() -> { + try { + return InstanceInterface.getProfitStatsData(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + }); + // Wait for all futures to complete + CompletableFuture allFutures = CompletableFuture.allOf(future1, future2, future3, future4); // Update UI try { @@ -344,6 +358,9 @@ public class HomeFragment extends Fragment { TickerTracker.PriceChangeData priceData2 = future2.get(); TickerTracker.PriceChangeData priceData3 = future3.get();; + InstanceInterface.ProfitStatsData profitsData = future4.get(); + + pricePair1.setText(String.format(Locale.ROOT,"%.2f", priceData.getCurrentPrice())); pricePair124hPercentage.setText(formatPercentage(priceData.getPriceChangePercent24h())); pricePair17dPercentage.setText(formatPercentage(priceData.getPriceChangePercent7d())); @@ -368,6 +385,10 @@ public class HomeFragment extends Fragment { setPercentageColor(pricePair37dPercentage,priceData3.getPriceChangePercent7d()); setPercentageColor(pricePair330dPercentage,priceData3.getPriceChangePercent30d()); + //Toast.makeText(getContext(), String.format(Locale.ROOT,"%.2f", profitsData.getProfitsToday()), Toast.LENGTH_SHORT).show(); + profitsToday.setText(String.format(Locale.ROOT,"%.2f", profitsData.getProfitsToday())); + profitsThisMonth.setText(String.format(Locale.ROOT,"%.2f", profitsData.getProfitsThisMonth())); + } catch (Exception e) { Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show(); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3e50c0f..1d4412b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,17 +1,17 @@ - DCAv2GUI + DCAv2GUI Open navigation drawer Close navigation drawer - DCAv2 + DCAv2 nicolassanchez@tutanota.com Navigation header Settings Home - Binance - Gate.io - KuCoin - OKX + Binance + Gate.io + KuCoin + OKX Enter the profile name Enter the API URL @@ -27,52 +27,52 @@ Time between queries Log lines to display Last trades to display - API URL - API key - Bot token - Chat ID + API URL + API key + Bot token + Chat ID Profile name General settings - BTCUSDT - ETHUSDT - USDTARS - BTC - - ETH - - USDT - - USDT - USDT - ARS - 24h - 7d - 30d - + BTCUSDT + ETHUSDT + USDTARS + BTC + + ETH + + USDT + + USDT + USDT + ARS + 24h + 7d + 30d + Today: - 420.69 + 420.69 This month: - 8392.39 - USDT + 8392.39 + USDT Exchange semaphore - Binance - Gate.io - KuCoin - OKX + Binance + Gate.io + KuCoin + OKX - 15203.20 - 59393.39 - 169% - 20 - 20/20 - / + 15203.20 + 59393.39 + 169% + 20 + 20/20 + / online Last trades - [2024/12/11 10:41:14] LUMIA/USDT | 2.29 USDT | Binance + [2024/12/11 10:41:14] LUMIA/USDT | 2.29 USDT | Binance [2024/12/11 10:10:18] HARD/USDT | 1.4 USDT | Binance [2024/12/11 09:26:51] LUMIA/USDT | 1.66 USDT | Binance [2024/12/11 09:17:42] TROY/USDT | 0.83 USDT | Binance @@ -83,9 +83,9 @@ [2024/12/11 07:44:02] NEIRO/USDT | 0.27 USDT | Binance [2024/12/11 07:09:51] TROY/USDT | 1.71 USDT | Binance - [2024/12/11 10:41:34] Everything is horrible\n[2024/12/11 10:41:34] Things are going south very rapidly\n[2024/12/11 10:41:34] Can\'t imagine how to even try to fix this\n[2024/12/11 10:41:34] Oh lord, take me now\n[2024/12/11 10:41:34] What the hell is that?!?!\n[2024/12/11 10:41:34] Oh dear\n[2024/12/11 10:41:34] What is that small red light? - Binance log - Gate.io log - Kucoin log - OKX log + [2024/12/11 10:41:34] Everything is horrible\n[2024/12/11 10:41:34] Things are going south very rapidly\n[2024/12/11 10:41:34] Can\'t imagine how to even try to fix this\n[2024/12/11 10:41:34] Oh lord, take me now\n[2024/12/11 10:41:34] What the hell is that?!?!\n[2024/12/11 10:41:34] Oh dear\n[2024/12/11 10:41:34] What is that small red light? + Binance log + Gate.io log + Kucoin log + OKX log \ No newline at end of file