package com.checktipsplitter.network;/*
* By Jorge E. Hernandez (@lalongooo) 2015
* */

import android.content.Context;

import com.checktipsplitter.model.Currency;
import com.checktipsplitter.rest.mashape.MashapeCurrencyExchangeRestClient;
import com.checktipsplitter.rest.openexchangerates.OpenExchangeRatesRestClient;
import com.checktipsplitter.utils.PrefUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit.Callback;
import retrofit.ResponseCallback;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.mime.TypedByteArray;

public class ExchangeRateUpdater {

    public interface OnExchangeRateSyncComplete {
        void onExchangeRateSyncComplete();
    }

    public static void updateExchangeRates(final Context context, final OnExchangeRateSyncComplete[] listeners) {
        if (PrefUtils.shouldSyncExchangeRates(context)) {
            MashapeCurrencyExchangeRestClient.get().getListQuotes(new Callback<ArrayList<String>>() {
                @Override
                public void success(final ArrayList<String> strings, Response response) {
                    OpenExchangeRatesRestClient.get().getCurrencies(new ResponseCallback() {
                        @Override
                        public void success(Response response) {

                            List<Currency> currencies = new ArrayList<>();

                            String responseText = new String(
                                    ((TypedByteArray) response.getBody()).getBytes()
                            );

                            JsonObject jsonObject = new JsonParser()
                                    .parse(responseText)
                                    .getAsJsonObject();

                            for (Map.Entry entry : jsonObject.entrySet()) {
                                JsonElement jsonElement = (JsonElement) entry.getValue();

                                if (strings.contains(entry.getKey().toString())) {
                                    currencies.add(new Currency(entry.getKey().toString(), jsonElement.getAsString()));
                                }
                            }


                            PrefUtils.saveLastExchangeRateSync(context);
                            PrefUtils.saveCurrencyValuesDescription(context, new Gson().toJson(currencies));

                            if (listeners != null && listeners.length > 0) {
                                for (OnExchangeRateSyncComplete nExchangeRateSyncComplete : listeners) {
                                    nExchangeRateSyncComplete.onExchangeRateSyncComplete();
                                }
                            }
                        }

                        @Override
                        public void failure(RetrofitError error) {

                        }
                    });
                }

                @Override
                public void failure(RetrofitError error) {
                }
            });
        } else {
            if (listeners != null && listeners.length > 0) {
                for (OnExchangeRateSyncComplete nExchangeRateSyncComplete : listeners) {
                    nExchangeRateSyncComplete.onExchangeRateSyncComplete();
                }
            }
        }
    }
}

@@ -1034,24 +1034,116 @@
 (context)) %7B
+%0A%0A            // First call to the Mashape Currency Exchange API to retrieve the list quotes
 %0A           
@@ -1348,16 +1348,119 @@
 ponse) %7B
+%0A%0A                    // Second call to the Open Exchange Rates API to retrieve the quotes descriptions
 %0A       
@@ -1677,55 +1677,98 @@
     
-List%3CCurrency%3E currencies = new ArrayList%3C%3E();%0A
+// The quotes descriptions are stored in a json file, so we read that content as a string.
 %0A   
@@ -1939,24 +1939,82 @@
         );%0A%0A
+                            // Parse the content to JSON.%0A
             
@@ -2180,24 +2180,99 @@
 nObject();%0A%0A
+                            List%3CCurrency%3E currencies = new ArrayList%3C%3E();%0A
             
@@ -2655,32 +2655,32 @@
               %7D%0A
+
                 
@@ -2694,17 +2694,16 @@
      %7D%0A%0A
-%0A
         

Add comments