public class LoadCurrencyRatesTask extends AsyncTask<Void, Void, Void> {
private static final String TAG = "LoadCurrencyRatesTask";
private static final String API_URL = "https://api.exchangerate-api.com/v4/latest/USD";
private final CurrencyRepository repository;
code
Assistant
private final Callback callback;
public interface Callback {
void onRatesLoaded();
void onError(Exception e);
}
public LoadCurrencyRatesTask(CurrencyRepository repository, Callback callback) {
this.repository = repository;
this.callback = callback;
}
@Override
protected Void doInBackground(Void... voids) {
try {
// Загрузка текущих курсов
URL url = new URL(API_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
Log.e(TAG, "Failed to connect to API. Response Code: " + responseCode);
if (callback != null) {
callback.onError(new Exception("Failed to connect to API. Response Code: " + responseCode));
}
return null;
}
// Чтение ответа
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream())
);
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
Log.d(TAG, "API response received.");
// Разбор JSON ответа
JSONObject jsonResponse = new JSONObject(response.toString());
JSONObject rates = jsonResponse.getJSONObject("rates");
Log.d(TAG, "Parsing JSON response.");
for (Iterator<String> it = rates.keys(); it.hasNext(); ) {
String currency = it.next().toUpperCase();
double rate = rates.getDouble(currency);
Date currentDate = new Date();
// Вставляем текущий курс
repository.insertCurrencyRateSync(new CurrencyRate("USD", currency, rate, currentDate));
repository.insertCurrencyRateSync(new CurrencyRate(currency, "USD", 1 / rate, currentDate));
// Генерируем и вставляем исторические курсы
generateMockHistory("USD", currency, rate);
generateMockHistory(currency, "USD", 1 / rate);
}
if (callback != null) {
callback.onRatesLoaded();
}
} catch (Exception e) {
Log.e(TAG, "Error loading currency rates", e);
if (callback != null) {
callback.onError(e);
}
}
return null;
}
private void generateMockHistory(String baseCurrency, String targetCurrency, double currentRate) {
Random random = new Random();
Calendar calendar = Calendar.getInstance();
Date currentDate = new Date();
calendar.setTime(currentDate);
for (int i = 30; i >= 0; i--) {
calendar.setTime(currentDate);
calendar.add(Calendar.DAY_OF_MONTH, -i);
// Set time components consistently
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 1);
calendar.set(Calendar.MILLISECOND, 1);
Date date = calendar.getTime();
double fluctuation = (random.nextDouble() - 0.5) * 0.1; // ±5%
double historicalRate = currentRate * (1 + fluctuation);
CurrencyRate historyRate = new CurrencyRate(
baseCurrency,
targetCurrency,
historicalRate,
date
);
repository.insertCurrencyRateSync(historyRate);
// Add detailed logging
Log.d("LoadCurrencyRatesTask", "Inserted CurrencyRate: baseCurrency=" + baseCurrency +
", targetCurrency=" + targetCurrency + ", date=" + date + ", rate=" + historicalRate);
}
}
public class ConvertCurrencyTask extends AsyncTask<Void, Void, Double> {
private final String fromCurrency;
private final String toCurrency;
private final double amount;
private final CurrencyRepository repository;
private final Callback callback;
code
public interface Callback {
void onConversionResult(Double result);
}
public ConvertCurrencyTask(String fromCurrency, String toCurrency, double amount, CurrencyRepository repository, Callback callback) {
this.fromCurrency = fromCurrency;
this.toCurrency = toCurrency;
this.amount = amount;
this.repository = repository;
this.callback = callback;
}
@Override
protected Double doInBackground(Void... voids) {
// Convert using USD as an intermediary
if (fromCurrency.equals("USD")) {
// Direct conversion from USD to the target currency
Double rateToTarget = repository.getExchangeRate("USD", toCurrency);
if (rateToTarget != null) {
return amount * rateToTarget;
}
} else if (toCurrency.equals("USD")) {
// Direct conversion from source currency to USD
Double rateFromSource = repository.getExchangeRate(fromCurrency, "USD");
if (rateFromSource != null) {
return amount * rateFromSource;
}
} else {
// Conversion from source currency to USD, then USD to target currency
Double rateFromSource = repository.getExchangeRate(fromCurrency, "USD");
Double rateToTarget = repository.getExchangeRate("USD", toCurrency);
if (rateFromSource != null && rateToTarget != null) {
// First convert amount to USD, then from USD to target currency
double amountInUSD = amount * rateFromSource;
return amountInUSD * rateToTarget;
}
}
Log.e("ConvertCurrencyTask", "Conversion rate not found for " + fromCurrency + " to " + toCurrency);
return null; // Return null if conversion is not possible
}
@Override
protected void onPostExecute(Double result) {
callback.onConversionResult(result);
}
@Entity(tableName = "currency_rates",
indices = {@Index(value = {"baseCurrency", "targetCurrency", "date"}, unique = true)})
@TypeConverters({Converters.class})
public class CurrencyRate {
@PrimaryKey(autoGenerate = true)
public int id;
code
public String baseCurrency;
public String targetCurrency;
public double rate;
public Date date;
public CurrencyRate(String baseCurrency, String targetCurrency, double rate, Date date) {
this.baseCurrency = baseCurrency;
this.targetCurrency = targetCurrency;
this.rate = rate;
this.date = date;
}
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertCurrencyRate(CurrencyRate rate);
@Query("SELECT DISTINCT baseCurrency FROM currency_rates")
List<String> getAllBaseCurrencies();
@Query("SELECT DISTINCT targetCurrency FROM currency_rates")
List<String> getAllTargetCurrencies();
@Query("SELECT rate FROM currency_rates WHERE baseCurrency = :fromCurrency AND targetCurrency = :toCurrency ORDER BY date DESC LIMIT 1")
Double getLatestExchangeRate(String fromCurrency, String toCurrency);
@Query("SELECT * FROM currency_rates WHERE UPPER(baseCurrency) = UPPER(:baseCurrency) AND UPPER(targetCurrency) = UPPER(:targetCurrency) ORDER BY date ASC")
List<CurrencyRate> getCurrencyRateHistory(String baseCurrency, String targetCurrency);
public class CurrencyRepository {
private final CurrencyRateDao currencyRateDao;
code
public CurrencyRepository(Context context) {
AppDatabase db = AppDatabase.getDatabase(context);
currencyRateDao = db.currencyRateDao();
}
// Синхронный метод вставки курса
public void insertCurrencyRateSync(CurrencyRate rate) {
currencyRateDao.insertCurrencyRate(rate);
}
// Метод для получения списка уникальных валют
public List<String> getAllUniqueCurrencies() {
List<String> baseCurrencies = currencyRateDao.getAllBaseCurrencies();
List<String> targetCurrencies = currencyRateDao.getAllTargetCurrencies();
Set<String> allCurrencies = new HashSet<>(baseCurrencies);
allCurrencies.addAll(targetCurrencies);
return new ArrayList<>(allCurrencies);
}
// Получение последнего курса для конвертации
public Double getExchangeRate(String fromCurrency, String toCurrency) {
return currencyRateDao.getLatestExchangeRate(fromCurrency, toCurrency);
}
// Получение истории курсов для графика
public List<CurrencyRate> getCurrencyRateHistory(String baseCurrency, String targetCurrency) {
return currencyRateDao.getCurrencyRateHistory(baseCurrency, targetCurrency);
}
// Загрузка курсов из API
public void loadAllCurrencyRatesFromApi(LoadCurrencyRatesTask.Callback callback) {
new LoadCurrencyRatesTask(this, callback).execute();
}
private LineChart lineChart; // Инициализация графика
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
repository = new CurrencyRepository(this);
spinnerFrom = findViewById(R.id.spinner_from);
spinnerTo = findViewById(R.id.spinner_to);
inputFrom = findViewById(R.id.input_from);
inputTo = findViewById(R.id.input_to);
loadingIndicator = findViewById(R.id.loading_indicator);
lineChart = findViewById(R.id.line_chart); // Инициализация графика
// Показываем индикатор загрузки и загружаем курсы
loadingIndicator.setVisibility(View.VISIBLE);
repository.loadAllCurrencyRatesFromApi(new LoadCurrencyRatesTask.Callback() {
@Override
public void onRatesLoaded() {
runOnUiThread(() -> {
new LoadCurrenciesTask().execute();
});
}
@Override
public void onError(Exception e) {
runOnUiThread(() -> {
loadingIndicator.setVisibility(View.GONE);
showToast("Ошибка загрузки курсов валют: " + e.getMessage());
});
}
});
inputFrom.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// No action needed here
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// No action needed here
}
@Override
public void afterTextChanged(Editable s) {
if (isRatesLoaded) {
performConversion();
} else {
showToast("Подождите, пока загрузятся курсы валют.");
}
}
});
AdapterView.OnItemSelectedListener spinnerListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (isRatesLoaded) {
performConversion();
loadChartData(); // Загрузить данные для графика при изменении выбора
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
};
spinnerFrom.setOnItemSelectedListener(spinnerListener);
spinnerTo.setOnItemSelectedListener(spinnerListener);
}
private void loadChartData() {
String fromCurrency = spinnerFrom.getSelectedItem().toString().toUpperCase();
String toCurrency = spinnerTo.getSelectedItem().toString().toUpperCase();
new LoadChartDataTask(fromCurrency, toCurrency).execute();
}
private class LoadChartDataTask extends AsyncTask<Void, Void, LineData> {
private final String fromCurrency;
private final String toCurrency;
public LoadChartDataTask(String fromCurrency, String toCurrency) {
this.fromCurrency = fromCurrency;
this.toCurrency = toCurrency;
}
protected LineData doInBackground(Void... voids) {
List<CurrencyRate> historyList = repository.getCurrencyRateHistory(fromCurrency, toCurrency);
// Adjust current date to set time components to 1
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 1);
calendar.set(Calendar.MILLISECOND, 1);
Date currentDate = calendar.getTime();
// Fetch the data using the adjusted currentDate
Log.d("LoadChartDataTask", "Количество записей истории: " + historyList.size());
if (historyList == null || historyList.isEmpty()) {
return null;
}
// Create list of entries for the chart
List<Entry> entries = new ArrayList<>();
for (int i = 0; i < historyList.size(); i++) {
CurrencyRate history = historyList.get(i);
// Use index as x-coordinate, rate as y-coordinate
entries.add(new Entry(i, (float) history.rate));
}
LineDataSet dataSet = new LineDataSet(entries, "Курс " + fromCurrency + "/" + toCurrency);
dataSet.setColor(ColorTemplate.getHoloBlue());
dataSet.setCircleColor(ColorTemplate.getHoloBlue());
dataSet.setLineWidth(1.5f);
dataSet.setCircleRadius(3f);
dataSet.setDrawCircleHole(false);
dataSet.setValueTextSize(9f);
dataSet.setDrawFilled(true);
dataSet.setFillColor(ColorTemplate.getHoloBlue());
return new LineData(dataSet);
}
@Override
protected void onPostExecute(LineData lineData) {
if (lineData != null) {
lineChart.setData(lineData);
// Настраиваем ось X для отображения дат
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
int index = (int) value;
List<CurrencyRate> historyList = repository.getCurrencyRateHistory(fromCurrency, toCurrency);
if (index >= 0 && index < historyList.size()) {
Date date = historyList.get(index).date;
return new SimpleDateFormat("dd.MM", Locale.getDefault()).format(date);
} else {
return "";
}
}
});
lineChart.invalidate(); // обновляем график
} else {
lineChart.clear();
lineChart.invalidate();
showToast("Нет данных для отображения графика");
}
}
}
private class LoadCurrenciesTask extends AsyncTask<Void, Void, List<String>> {
@Override
protected void onPreExecute() {
loadingIndicator.setVisibility(View.VISIBLE);
}
@Override
protected List<String> doInBackground(Void... voids) {
// Загрузка списка валют из репозитория
return repository.getAllUniqueCurrencies();
}
@Override
protected void onPostExecute(List<String> currencies) {
loadingIndicator.setVisibility(View.GONE);
if (currencies != null && !currencies.isEmpty()) {
isRatesLoaded = true;
ArrayAdapter<String> adapterFrom = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, currencies);
adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerFrom.setAdapter(adapterFrom);
ArrayAdapter<String> adapterTo = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, currencies);
adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTo.setAdapter(adapterTo);
// Устанавливаем стандартные значения
spinnerFrom.setSelection(0);
spinnerTo.setSelection(1);
// Выполняем конвертацию и загружаем данные для графика
performConversion();
loadChartData();
} else {
showToast("Не удалось загрузить валюты");
}
}
}
private void performConversion() {
String fromCurrency = spinnerFrom.getSelectedItem().toString().toUpperCase();
String toCurrency = spinnerTo.getSelectedItem().toString().toUpperCase();
String amountStr = inputFrom.getText().toString().toUpperCase();
if (amountStr.isEmpty()) {
inputTo.setText("");
return;
}
if (fromCurrency.equals(toCurrency)) {
inputTo.setText(amountStr);
return;
}
try {
double amount = Double.parseDouble(amountStr);
// Perform conversion
new ConvertCurrencyTask(fromCurrency, toCurrency, amount, repository, result -> {
if (result != null) {
inputTo.setText(String.format("%.2f", result));
} else {
showToast("Ошибка при конвертации валюты: Курс не найден");
inputTo.setText("");
}
}).execute();
} catch (NumberFormatException e) {
inputTo.setText("");
}
}
private void showToast(String message) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
} добавь сам сюда график для отображения динамики изменения курсов
Чтобы добавить график для отображения динамики изменения курсов валют, можно использовать библиотеку MPAndroidChart, которая предоставляет удобные инструменты для построения графиков в Android-приложениях.
Вот пошаговая инструкция по добавлению графика в ваше приложение:
1. Добавьте зависимость MPAndroidChart в build.gradle
В файле build.gradle вашего модуля приложения добавьте следующую зависимость:
У вас уже есть методы loadChartData() и LoadChartDataTask, которые загружают данные и отображают их на графике. Убедитесь, что они корректно работают и вызывают обновление графика при изменении валют.
Ниже приведен пересмотренный код класса LoadChartDataTask с комментариями и исправлениями:
java
private class LoadChartDataTask extends AsyncTask<Void, Void, LineData> {
private final String fromCurrency;
private final String toCurrency;
private List<CurrencyRate> historyList; // Сохраним историю для использования в форматтере оси X
public LoadChartDataTask(String fromCurrency, String toCurrency) {
this.fromCurrency = fromCurrency;
this.toCurrency = toCurrency;
}
protected LineData doInBackground(Void... voids) {
historyList = repository.getCurrencyRateHistory(fromCurrency, toCurrency);
if (historyList == null || historyList.isEmpty()) {
return null;
}
// Создаем список точек для графика
List<Entry> entries = new ArrayList<>();
for (int i = 0; i < historyList.size(); i++) {
CurrencyRate history = historyList.get(i);
// Используем индекс как X-координату (или timestamp, если хотите)
// Здесь используется индекс, чтобы X-значения были последовательными
entries.add(new Entry(i, (float) history.rate));
}
LineDataSet dataSet = new LineDataSet(entries, "Курс " + fromCurrency + "/" + toCurrency);
dataSet.setColor(ColorTemplate.getHoloBlue());
dataSet.setLineWidth(2f);
dataSet.setDrawCircles(false);
dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
dataSet.setDrawFilled(true);
dataSet.setFillAlpha(50);
dataSet.setFillColor(ColorTemplate.getHoloBlue());
dataSet.setDrawValues(false); // Убираем значения над точками
LineData lineData = new LineData(dataSet);
return lineData;
}
@Override
protected void onPostExecute(LineData lineData) {
if (lineData != null) {
// Настраиваем ось X для отображения дат
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setLabelRotationAngle(-45f);
xAxis.setGranularity(1f);
xAxis.setDrawGridLines(false);
xAxis.setValueFormatter(new ValueFormatter() {
private final SimpleDateFormat mFormat = new SimpleDateFormat("dd.MM", Locale.getDefault());
@Override
public String getFormattedValue(float value) {
int index = (int) value;
if (index >= 0 && index < historyList.size()) {
Date date = historyList.get(index).date;
return mFormat.format(date);
} else {
return "";
}
}
});
// Настраиваем ось Y
lineChart.getAxisRight().setEnabled(false); // Отключаем правую ось Y
// Обновляем данные графика
lineChart.setData(lineData);
lineChart.invalidate(); // перерисовываем график
} else {
lineChart.clear();
lineChart.invalidate();
showToast("Нет данных для отображения графика");
}
}
}
5. Обновите метод performConversion()
Убедитесь, что метод performConversion() вызывает loadChartData() после выполнения конвертации, чтобы график обновлялся при изменении валют или суммы.
java
private void performConversion() {
// Ваш существующий код
// ...
// После выполнения конвертации загружаем данные для графика
loadChartData();
}
6. Обновите слушатели Spinner'ов
Убедитесь, что график обновляется при изменении выбора валют. В вашем коде вы уже вызываете loadChartData() в слушателе:
java
AdapterView.OnItemSelectedListener spinnerListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (isRatesLoaded) {
performConversion();
loadChartData(); // Загрузить данные для графика при изменении выбора
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
};
spinnerFrom.setOnItemSelectedListener(spinnerListener);
spinnerTo.setOnItemSelectedListener(spinnerListener);
7. Убедитесь, что данные истории корректно сохраняются
Проверьте, что в методе generateMockHistory() вы корректно сохраняете исторические данные курсов валют в базу данных. Вы уже выполняете генерацию исторических данных в LoadCurrencyRatesTask.
8. Тестирование приложения
Запустите приложение и проверьте:
Валюты загружаются и отображаются в Spinner'ах
Конвертация валют работает корректно
При изменении валют или суммы график обновляется и отображает динамику изменения курса за последние 30 дней
Дополнительные советы
Вы можете настроить внешний вид графика, изменяя свойства LineDataSet и других компонентов графика.
Если вы хотите отображать реальные исторические данные, вместо генерации фиктивных данных в generateMockHistory(), вы можете использовать API для получения исторических курсов валют.
Пример полного класса MainActivity.java с внесенными изменениями
Добавив вышеописанные изменения, вы получите график, отображающий динамику изменения курса выбранных валют за последние 30 дней. График будет автоматически обновляться при изменении выбора валют или вводимой суммы.
Пожалуйста, попробуйте внести эти изменения в ваш проект. Если у вас возникнут вопросы или потребуется дополнительная помощь, не стесняйтесь обращаться!