/** * Reloads currencies from a remote source. * * @param useRemoteSource */ public void reloadRates(boolean useRemoteSource) { if (!useRemoteSource) { DataSource source = null; try { source = new SQLiteDataSource(); source.connect(getActivity()); List<CurrencyData> ratesList = source.getLastRates(getSelectedCurrenciesLocale()); if (!ratesList.isEmpty()) { Log.v(Defs.LOG_TAG, "Displaying rates from database..."); updateCurrenciesListView(ratesList); } else { useRemoteSource = true; } } catch (DataSourceException e) { Log.e(Defs.LOG_TAG, "Could not load currencies from database!", e); Toast.makeText(getActivity(), R.string.error_db_load_rates, Defs.TOAST_ERR_TIME).show(); } finally { IOUtils.closeQuitely(source); } } if (useRemoteSource) { setRefreshActionButtonState(true); new UpdateRatesTask().execute(); } }
@Override protected void onPostExecute(Map<CurrencyLocales, List<CurrencyData>> result) { // notifyListeners(Notifications.UPDATE_RATES_DONE); setRefreshActionButtonState(false); if (updateOK && !result.isEmpty()) { DataSource source = null; try { source = new SQLiteDataSource(); source.connect(activity); source.addRates(result); } catch (DataSourceException e) { Log.e(Defs.LOG_TAG, "Could not save currencies to database!", e); Toast.makeText(getActivity(), R.string.error_db_load_rates, Defs.TOAST_ERR_TIME).show(); } finally { IOUtils.closeQuitely(source); } updateCurrenciesListView(result.get(getSelectedCurrenciesLocale())); } else { tvLastUpdate.setText(lastUpdateLastValue); Toast.makeText(getActivity(), R.string.error_download_rates, Defs.TOAST_ERR_TIME).show(); } }