<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_currency_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CurrencyListActivity">

    <!-- Кнопка "Назад" -->
    <Button
        android:id="@+id/button_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Назад"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <!-- Кнопка "Добавить пару" -->
    <Button
        android:id="@+id/button_add_pair"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Добавить пару"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/text_empty"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Нет избранных пар валют."
        android:textSize="18sp"
        android:textColor="#757575"
        android:gravity="center"
        android:visibility="gone"
        app:layout_constraintTop_toBottomOf="@id/button_back"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
    
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/currency_recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:padding="8dp"
        app:layout_constraintTop_toBottomOf="@id/button_back"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>package com.example.myapplication.api;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;

import com.example.myapplication.CurrencyRate;
import com.example.myapplication.CurrencyRepository;

import org.json.JSONObject;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

public class LoadCurrencyRatesTask extends AsyncTask<Void, Void, Boolean> {
    private static final String TAG = "LoadCurrencyRatesTask";
    private static final String API_KEY = "05ZPKFI7IKOPLLRM"; // Замените на ваш API ключ Alpha Vantage

    private final Context context;
    private final CurrencyRepository repository;
    private final Callback callback;
    private final List<CurrencyRate> newRates = new ArrayList<>();

    public interface Callback {
        void onRatesLoaded();

        void onError(Exception e);
    }

    public LoadCurrencyRatesTask(Context context, CurrencyRepository repository, Callback callback) {
        this.context = context;
        this.repository = repository;
        this.callback = callback;
    }

    @Override
    protected Boolean doInBackground(Void... voids) {
        try {
            if (isInternetAvailable()) {
                Log.d(TAG, "Интернет-соединение доступно. Обновление курсов через API.");

                List<String> currencies = Arrays.asList("EUR");
                boolean allSuccess = true;

                for (String targetCurrency : currencies) {
                    boolean success = fetchHistoricalRates("USD", targetCurrency);
                    if (!success) {
                        allSuccess = false;
                        Log.e(TAG, "Ошибка при загрузке курса для " + targetCurrency);
                        break; // Если требуется остановиться при первой ошибке
                    }
                }

                if (allSuccess) {
                    // Все запросы успешны, обновляем базу данных
                    repository.clearAllRates();
                    repository.insertCurrencyRatesSync(newRates);
                    // Вставка обратных курсов, если необходимо
                    for (CurrencyRate rate : newRates) {
                        if (!rate.baseCurrency.equals(rate.targetCurrency)) {
                            double reverseRate = rate.rate != 0 ? 1 / rate.rate : 0;
                            CurrencyRate reverseCurrencyRate = new CurrencyRate(rate.targetCurrency, rate.baseCurrency, reverseRate, rate.date);
                            repository.insertCurrencyRateSync(reverseCurrencyRate);
                        }
                    }
                    return true;
                } else {
                    // Если хотя бы один запрос не удался, не обновляем базу данных
                    return false;
                }
            } else {
                Log.d(TAG, "Нет интернет-соединения. Используются кэшированные данные из базы данных.");
                return true; // Используем кэшированные данные
            }
//            return false;
        } catch (Exception e) {
            Log.e(TAG, "Ошибка при загрузке курсов валют", e);
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean success) {
        if (success) {
            if (callback != null) {
                callback.onRatesLoaded();
            }
        } else {
            if (callback != null) {
                callback.onError(new Exception("Не удалось обновить курсы валют. Используются кэшированные данные."));
            }
        }
    }

    private boolean isInternetAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                Network network = connectivityManager.getActiveNetwork();
                if (network == null) {
                    Log.d(TAG, "Нет активной сети.");
                    return false;
                }
                NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
                boolean hasInternet = capabilities != null &&
                        (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
                                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
                                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
                Log.d(TAG, "Интернет доступен: " + hasInternet);
                return hasInternet;
            } else {
                // Для старых версий Android
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                boolean isConnected = networkInfo != null && networkInfo.isConnected();
                Log.d(TAG, "Интернет доступен (старый метод): " + isConnected);
                return isConnected;
            }
        }
        Log.d(TAG, "ConnectivityManager равен null.");
        return false;
    }

    /**
     * Возвращает true, если курс валют успешно загружен и добавлен в список newRates.
     */
    private boolean fetchHistoricalRates(String baseCurrency, String targetCurrency) {
        try {
            String apiUrl = String.format(
                    Locale.US,
                    "https://www.alphavantage.co/query?function=FX_DAILY&from_symbol=%s&to_symbol=%s&apikey=%s&outputsize=compact",
                    baseCurrency,
                    targetCurrency,
                    API_KEY
            );

            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();

            InputStream inputStream;
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inputStream = connection.getInputStream();
            } else {
                inputStream = connection.getErrorStream();
                Log.e(TAG, "HTTP ошибка при запросе к API: " + responseCode);
                return false;
            }

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(inputStream)
            );
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            String jsonResponseString = response.toString();

            Log.d(TAG, "API Response для " + baseCurrency + "/" + targetCurrency + ": " + jsonResponseString);

            JSONObject jsonResponse = new JSONObject(jsonResponseString);

            if (jsonResponse.has("Error Message")) {
                Log.e(TAG, "Ошибка при получении данных для " + targetCurrency + ": " + jsonResponse.getString("Error Message"));
                return false;
            }

            if (jsonResponse.has("Note")) {
                Log.e(TAG, "Достигнут лимит частоты API вызовов: " + jsonResponse.getString("Note"));
                return false;
            }

            if (!jsonResponse.has("Time Series FX (Daily)")) {
                Log.e(TAG, "Неожиданный формат JSON для валюты: " + targetCurrency);
                return false;
            }

            JSONObject timeSeries = jsonResponse.getJSONObject("Time Series FX (Daily)");

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

            Iterator<String> dates = timeSeries.keys();
            while (dates.hasNext()) {
                String dateStr = dates.next();
                JSONObject dailyData = timeSeries.getJSONObject(dateStr);

                double rate = dailyData.getDouble("4. close");

                Date date = dateFormat.parse(dateStr);

                CurrencyRate currencyRate = new CurrencyRate(baseCurrency, targetCurrency, rate, date);
                newRates.add(currencyRate);
            }

            Log.d(TAG, "Исторические курсы получены для " + baseCurrency + "/" + targetCurrency);
            return true;

        } catch (JSONException e) {
            Log.e(TAG, "Ошибка парсинга JSON для " + targetCurrency, e);
            return false;
        } catch (Exception e) {
            Log.e(TAG, "Ошибка при получении исторических курсов для " + targetCurrency, e);
            return false;
        }
    }
}package com.example.myapplication;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;

@Database(entities = {CurrencyRate.class}, version = 4, exportSchema = false)
@TypeConverters({Converters.class})
public abstract class AppDatabase extends RoomDatabase {
    private static volatile AppDatabase INSTANCE;

    public abstract CurrencyRateDao currencyRateDao();

    public static AppDatabase getDatabase(final Context context) {
        if (INSTANCE == null) {
            synchronized (AppDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                                    AppDatabase.class, "currency_database")
                            .fallbackToDestructiveMigration()
                            .build();
                }
            }
        }
        return INSTANCE;
    }
}package com.example.myapplication;

import android.content.Intent; // Добавлено для обработки нажатия на кнопку
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout; // Добавлено для работы с time_range_layout
import android.widget.Spinner;
import android.widget.TextView; // Добавлено для работы с time_range_label
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.button.MaterialButtonToggleGroup;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ChartActivity extends AppCompatActivity {
    private static final String TAG = "ChartActivity";
    private CurrencyRepository repository;
    private LineChart lineChart;

    // Выбранные валюты
    private Spinner spinnerFromChart;
    private Spinner spinnerToChart;
    private MaterialButton buttonSwapChart;
    private MaterialButtonToggleGroup timeRangeToggleGroup;
    private MaterialButton buttonBackToMain;
    private MaterialButton buttonCurrencyList; // Новая кнопка

    private boolean isRatesLoaded = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chart);

        repository = new CurrencyRepository(this);
        lineChart = findViewById(R.id.line_chart);
        timeRangeToggleGroup = findViewById(R.id.time_range_toggle_group_chart);
        buttonBackToMain = findViewById(R.id.button_back_to_main);
        spinnerFromChart = findViewById(R.id.spinner_from_chart);
        spinnerToChart = findViewById(R.id.spinner_to_chart);
        buttonSwapChart = findViewById(R.id.button_swap_chart);

        // Настройка обработчика кнопок выбора периода
        timeRangeToggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -> {
            if (isChecked) {
                Log.d(TAG, "Выбран период: " + checkedId);
                loadChartData(); // Перезагрузка графика при изменении периода
            }
        });

        // Настройка обработчика кнопки "Назад"
        buttonBackToMain.setOnClickListener(v -> finish());

        // Настройка обработчика кнопки свапа
        buttonSwapChart.setOnClickListener(v -> {
            swapCurrencies();
            loadChartData();
        });

        // Настройка слушателя для выбора валют из спиннеров
        AdapterView.OnItemSelectedListener spinnerListener = new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (isRatesLoaded) {
                    loadChartData();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        };

        spinnerFromChart.setOnItemSelectedListener(spinnerListener);
        spinnerToChart.setOnItemSelectedListener(spinnerListener);

        // Загрузка валют в спиннеры
        new LoadCurrenciesTask().execute();
    }

    private void swapCurrencies() {
        int fromPosition = spinnerFromChart.getSelectedItemPosition();
        int toPosition = spinnerToChart.getSelectedItemPosition();

        spinnerFromChart.setSelection(toPosition);
        spinnerToChart.setSelection(fromPosition);
    }

    /**
     * AsyncTask для загрузки списка валют после обновления курсов.
     */
    private class LoadCurrenciesTask extends AsyncTask<Void, Void, List<String>> {
        @Override
        protected void onPreExecute() {
            // Можно добавить индикатор загрузки, если необходимо
        }

        @Override
        protected List<String> doInBackground(Void... voids) {
            // Загрузка списка валют из репозитория
            List<String> currencies = repository.getAllUniqueCurrencies();
            Log.d("LoadCurrenciesTask", "Получены валюты: " + currencies.toString());
            return currencies;
        }

        @Override
        protected void onPostExecute(List<String> currencies) {
            super.onPostExecute(currencies);
            if (currencies != null && !currencies.isEmpty()) {
                isRatesLoaded = true;
                ArrayAdapter<String> adapterFrom = new ArrayAdapter<>(ChartActivity.this, android.R.layout.simple_spinner_item, currencies);
                adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerFromChart.setAdapter(adapterFrom);

                ArrayAdapter<String> adapterTo = new ArrayAdapter<>(ChartActivity.this, android.R.layout.simple_spinner_item, currencies);
                adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerToChart.setAdapter(adapterTo);

                // Устанавливаем стандартные значения
                String defaultFromCurrency = getIntent().getStringExtra("FROM_CURRENCY");
                String defaultToCurrency = getIntent().getStringExtra("TO_CURRENCY");

                if (defaultFromCurrency != null) {
                    int fromIndex = currencies.indexOf(defaultFromCurrency);
                    if (fromIndex >= 0) spinnerFromChart.setSelection(fromIndex);
                }

                if (defaultToCurrency != null) {
                    int toIndex = currencies.indexOf(defaultToCurrency);
                    if (toIndex >= 0) spinnerToChart.setSelection(toIndex);
                }

                // Устанавливаем кнопку по умолчанию в ToggleGroup
                timeRangeToggleGroup.check(R.id.button_7_days_chart);

                loadChartData();

            } else {
                Toast.makeText(ChartActivity.this, "Не удалось загрузить валюты.", Toast.LENGTH_SHORT).show();
                Log.e("LoadCurrenciesTask", "Список валют пуст");
            }
        }
    }

    /**
     * Загружает данные для графика на основе выбранных валют и периода.
     */
    private void loadChartData() {
        int checkedButtonId = timeRangeToggleGroup.getCheckedButtonId();
        int timeRangeDays;

        if (checkedButtonId == R.id.button_30_days_chart) {
            timeRangeDays = 30;
        } else if (checkedButtonId == R.id.button_90_days_chart) {
            timeRangeDays = 90;
        } else {
            timeRangeDays = 7;
        }

        String fromCurrency = spinnerFromChart.getSelectedItem().toString().toUpperCase();
        String toCurrency = spinnerToChart.getSelectedItem().toString().toUpperCase();

        new LoadChartDataTask(fromCurrency, toCurrency, timeRangeDays).execute();
    }

    private class LoadChartDataTask extends AsyncTask<Void, Void, LineData> {
        private final String fromCurrency;
        private final String toCurrency;
        private final int timeRangeDays;
        private List<Date> dates;

        public LoadChartDataTask(String fromCurrency, String toCurrency, int timeRangeDays) {
            this.fromCurrency = fromCurrency;
            this.toCurrency = toCurrency;
            this.timeRangeDays = timeRangeDays;
        }

        @Override
        protected LineData doInBackground(Void... voids) {
            Date endDate = new Date();  // Текущая дата
            Calendar cal = Calendar.getInstance();
            cal.setTime(endDate);
            cal.add(Calendar.DAY_OF_YEAR, -timeRangeDays);
            Date startDate = cal.getTime();

            List<CurrencyRate> historyFrom = repository.getCurrencyRateHistoryWithinDates("USD", fromCurrency, startDate, endDate);
            List<CurrencyRate> historyTo = repository.getCurrencyRateHistoryWithinDates("USD", toCurrency, startDate, endDate);

            if (fromCurrency.equals("USD")) {
                historyFrom = generateUsdHistory(startDate, endDate);
            }

            if (toCurrency.equals("USD")) {
                historyTo = generateUsdHistory(startDate, endDate);
            }

            if (historyFrom == null || historyTo == null || historyFrom.isEmpty() || historyTo.isEmpty()) {
                return null;
            }

            int size = Math.min(historyFrom.size(), historyTo.size());
            List<Entry> entries = new ArrayList<>();
            dates = new ArrayList<>();

            for (int i = 0; i < size; i++) {
                CurrencyRate rateFrom = historyFrom.get(i);
                CurrencyRate rateTo = historyTo.get(i);

                double rateFromUSD = rateFrom.rate;
                double rateToUSD = rateTo.rate;

                double crossRate = rateToUSD / rateFromUSD;

                entries.add(new Entry(i, (float) crossRate));
                dates.add(rateFrom.date);
            }

            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);

            return new LineData(dataSet);
        }

        /**
         * Генерирует список курсов USD/USD (стабильный курс 1.0).
         */
        private List<CurrencyRate> generateUsdHistory(Date startDate, Date endDate) {
            List<Date> allDates = repository.getAllDatesWithinRange(startDate, endDate);
            List<CurrencyRate> usdHistory = new ArrayList<>();
            for (Date date : allDates) {
                usdHistory.add(new CurrencyRate("USD", "USD", 1.0, date));
            }
            return usdHistory;
        }

        @Override
        protected void onPostExecute(LineData lineData) {
            if (lineData != null && lineData.getEntryCount() > 0) {
                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 MMM", Locale.getDefault());

                    @Override
                    public String getFormattedValue(float value) {
                        int index = (int) value;
                        if (index >= 0 && index < dates.size()) {
                            Date date = dates.get(index);
                            return mFormat.format(date);
                        } else {
                            return "";
                        }
                    }
                });

                // Настраиваем ось Y
                lineChart.getAxisRight().setEnabled(false);

                // Обновляем данные графика
                lineChart.setData(lineData);

                // Разрешаем взаимодействие
                lineChart.setTouchEnabled(true);
                lineChart.setDragEnabled(true);
                lineChart.setScaleEnabled(true);

                // Устанавливаем кастомный MarkerView (при наличии)
                // Например:
                // CustomMarkerView markerView = new CustomMarkerView(ChartActivity.this, R.layout.custom_marker_view, dates);
                // markerView.setChartView(lineChart);
                // lineChart.setMarker(markerView);

                lineChart.invalidate();
            } else {
                lineChart.clear();
                lineChart.invalidate();
                Toast.makeText(ChartActivity.this, "Нет данных для отображения графика.", Toast.LENGTH_SHORT).show();
            }
        }
    }
}package com.example.myapplication;

import android.os.AsyncTask;
import android.util.Log;







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;

    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) {
        // Get exchange rates relative to USD
        Double rateFromUSD;
        if (fromCurrency.equals("USD")) {
            rateFromUSD = 1.0;
        } else {
            rateFromUSD = repository.getExchangeRate("USD", fromCurrency);
            if (rateFromUSD != null) {
                rateFromUSD = 1 / rateFromUSD; // Convert to rate fromCurrency -> USD
            }
        }

        Double rateToUSD;
        if (toCurrency.equals("USD")) {
            rateToUSD = 1.0;
        } else {
            rateToUSD = repository.getExchangeRate("USD", toCurrency);
            if (rateToUSD != null) {
                rateToUSD = 1 / rateToUSD; // Convert to rate toCurrency -> USD
            }
        }

        if (rateFromUSD == null || rateToUSD == null) {
            Log.e("ConvertCurrencyTask", "Conversion rate not found for " + fromCurrency + " or " + toCurrency);
            return null;
        }

        // Correct cross-rate calculation
        double crossRate = rateFromUSD / rateToUSD;

        // Perform conversion
        return amount * crossRate;
    }



    @Override
    protected void onPostExecute(Double result) {
        callback.onConversionResult(result);
    }


}package com.example.myapplication;

import androidx.room.TypeConverter;

import java.util.Date;

public class Converters {
    @TypeConverter
    public static Date fromTimestamp(Long value) {
        return value == null ? null : new Date(value);
    }

    @TypeConverter
    public static Long dateToTimestamp(Date date) {
        return date == null ? null : date.getTime();
    }
}package com.example.myapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * Adapter class for managing and binding currency pairs to the RecyclerView.
 */
public class CurrencyAdapter extends RecyclerView.Adapter<CurrencyAdapter.ViewHolder> {

    /**
     * Interface to handle favorite (remove) button clicks.
     */
    public interface OnFavoriteClickListener {
        /**
         * Called when the favorite button is clicked.
         *
         * @param currencyPair The currency pair that was clicked.
         * @param isFavorite   Indicates whether the pair is being added or removed from favorites.
         */
        void onFavoriteClick(String currencyPair, boolean isFavorite);
    }

    private List<String> currencyPairs; // List of currency pairs to display
    private OnFavoriteClickListener listener; // Listener for favorite button clicks

    /**
     * Constructor for the adapter.
     *
     * @param currencyPairs List of currency pairs to display.
     * @param listener      Listener to handle favorite button clicks.
     */
    public CurrencyAdapter(List<String> currencyPairs, OnFavoriteClickListener listener) {
        this.currencyPairs = currencyPairs;
        this.listener = listener;
    }

    /**
     * ViewHolder class that holds references to the views in each RecyclerView item.
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textViewPair; // TextView to display the currency pair
        public ImageButton buttonFavorite; // Button to remove the pair from favorites

        public ViewHolder(View view) {
            super(view);
            textViewPair = view.findViewById(R.id.text_view_pair);
            buttonFavorite = view.findViewById(R.id.button_favorite);
        }
    }

    @Override
    public CurrencyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Inflate the item layout for each currency pair
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_currency_pair, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CurrencyAdapter.ViewHolder holder, int position) {
        String pair = currencyPairs.get(position); // Get the currency pair at this position

        holder.textViewPair.setText(pair); // Set the currency pair text

        // Set the favorite button icon as filled heart since all displayed pairs are favorites
        holder.buttonFavorite.setImageResource(R.drawable.star);

        // Handle favorite button click to remove the pair from favorites
        holder.buttonFavorite.setOnClickListener(v -> {
            // Notify the listener (CurrencyListActivity) to handle removal
            listener.onFavoriteClick(pair, false);
        });
    }

    @Override
    public int getItemCount() {
        return currencyPairs.size(); // Return the total number of currency pairs
    }

    /**
     * Adds a new currency pair to the adapter and notifies the RecyclerView.
     *
     * @param pair The currency pair to add.
     */
    public void addCurrencyPair(String pair) {
        currencyPairs.add(pair); // Add the new pair to the list
        notifyItemInserted(currencyPairs.size() - 1); // Notify the adapter of the new item
    }

    /**
     * Removes a currency pair from the adapter at the specified position and notifies the RecyclerView.
     *
     * @param position The position of the currency pair to remove.
     */
    public void removeCurrencyPair(int position) {
        if (position >= 0 && position < currencyPairs.size()) {
            currencyPairs.remove(position); // Remove the pair from the list
            notifyItemRemoved(position); // Notify the adapter of the removed item
        }
    }
}package com.example.myapplication;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CurrencyListActivity extends AppCompatActivity {

    // RecyclerView to display the list of favorite currency pairs
    private RecyclerView recyclerView;

    // Adapter to manage the data and bind it to the RecyclerView
    private CurrencyAdapter currencyAdapter;

    // Repository to fetch currency data
    private CurrencyRepository repository;

    // Set to store favorite currency pairs
    private Set<String> favoriteCurrencyPairs;

    // TextView to display when no favorite pairs exist
    private TextView textEmpty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_currency_list);

        // Initialize the repository
        repository = new CurrencyRepository(this);

        // Retrieve favorite currency pairs from SharedPreferences
        favoriteCurrencyPairs = getFavoriteCurrencyPairs();

        // Initialize RecyclerView and set its layout manager
        recyclerView = findViewById(R.id.currency_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // Reference to the empty state TextView
        textEmpty = findViewById(R.id.text_empty);

        // Initialize the adapter with the list of favorite pairs and set the click listener
        currencyAdapter = new CurrencyAdapter(new ArrayList<>(favoriteCurrencyPairs), new CurrencyAdapter.OnFavoriteClickListener() {
            @Override
            public void onFavoriteClick(String currencyPair, boolean isFavorite) {
                // Handle the favorite button click

                if (isFavorite) {
                    // If marking as favorite (adding)
                    favoriteCurrencyPairs.add(currencyPair);
                    Toast.makeText(CurrencyListActivity.this, "Добавлено в избранное: " + currencyPair, Toast.LENGTH_SHORT).show();
                } else {
                    // If unmarking as favorite (removing)
                    favoriteCurrencyPairs.remove(currencyPair);
                    Toast.makeText(CurrencyListActivity.this, "Удалено из избранного: " + currencyPair, Toast.LENGTH_SHORT).show();
                }

                // Save the updated favorite pairs to SharedPreferences
                saveFavoriteCurrencyPairs(favoriteCurrencyPairs);

                // Update the UI to reflect changes
                updateUI();
            }
        });

        // Set the adapter to the RecyclerView
        recyclerView.setAdapter(currencyAdapter);

        // Initialize and set the "Back" button listener to finish the activity
        Button backButton = findViewById(R.id.button_back);
        backButton.setOnClickListener(v -> finish());

        // Initialize and set the "Add Pair" button listener to open the add pair dialog
        Button addPairButton = findViewById(R.id.button_add_pair);
        addPairButton.setOnClickListener(v -> new LoadCurrenciesTaskForDialog().execute());

        // Update the UI based on whether there are favorite pairs
        updateUI();
    }

    /**
     * Retrieves the set of favorite currency pairs from SharedPreferences.
     *
     * @return A new HashSet containing the favorite currency pairs.
     */
    private Set<String> getFavoriteCurrencyPairs() {
        SharedPreferences prefs = getSharedPreferences("favorites", MODE_PRIVATE);
        Set<String> defaultSet = new HashSet<>();
        Set<String> storedSet = prefs.getStringSet("favorite_pairs", defaultSet);
        // Return a new HashSet to avoid modifying the original set from SharedPreferences
        return new HashSet<>(storedSet);
    }

    /**
     * Saves the updated set of favorite currency pairs to SharedPreferences.
     *
     * @param favorites The set of favorite currency pairs to save.
     */
    private void saveFavoriteCurrencyPairs(Set<String> favorites) {
        SharedPreferences prefs = getSharedPreferences("favorites", MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putStringSet("favorite_pairs", favorites);
        editor.apply();
    }

    /**
     * Updates the UI by toggling the visibility of the RecyclerView and the empty state TextView.
     */
    private void updateUI() {
        if (favoriteCurrencyPairs.isEmpty()) {
            // If there are no favorite pairs, show the empty state message and hide the RecyclerView
            recyclerView.setVisibility(View.GONE);
            textEmpty.setVisibility(View.VISIBLE);
        } else {
            // If there are favorite pairs, show the RecyclerView and hide the empty state message
            recyclerView.setVisibility(View.VISIBLE);
            textEmpty.setVisibility(View.GONE);
        }
    }

    /**
     * AsyncTask to load the list of currencies in the background for the add pair dialog.
     */
    private class LoadCurrenciesTaskForDialog extends AsyncTask<Void, Void, List<String>> {
        @Override
        protected List<String> doInBackground(Void... voids) {
            // Fetch all unique currencies from the repository
            return repository.getAllUniqueCurrencies();
        }

        @Override
        protected void onPostExecute(List<String> currencies) {
            if (currencies != null && !currencies.isEmpty()) {
                // If currencies are available, show the add pair dialog
                showAddPairDialog(currencies);
            } else {
                // If no currencies are available, inform the user
                Toast.makeText(CurrencyListActivity.this, "Нет доступных валют для добавления.", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * Displays an AlertDialog allowing the user to select and add a new currency pair.
     *
     * @param currencies A list of available currency codes.
     */
    private void showAddPairDialog(List<String> currencies) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Добавить валютную пару");

        // Inflate the custom dialog layout
        LayoutInflater inflater = LayoutInflater.from(this);
        View dialogView = inflater.inflate(R.layout.dialog_add_pair, null);
        Spinner spinnerBase = dialogView.findViewById(R.id.spinner_base);
        Spinner spinnerTarget = dialogView.findViewById(R.id.spinner_target);

        // Set up the adapters for the spinners
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, currencies);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerBase.setAdapter(adapter);
        spinnerTarget.setAdapter(adapter);

        // Set the custom view to the dialog
        builder.setView(dialogView);

        // Define the positive button action (Add)
        builder.setPositiveButton("Добавить", (dialog, which) -> {
            String base = spinnerBase.getSelectedItem().toString();
            String target = spinnerTarget.getSelectedItem().toString();

            // Validate that base and target currencies are not the same
            if (base.equals(target)) {
                Toast.makeText(this, "Базовая и целевая валюты не могут быть одинаковыми.", Toast.LENGTH_SHORT).show();
                return;
            }

            String pair = base + "/" + target;

            // Check if the pair already exists in favorites
            if (favoriteCurrencyPairs.contains(pair)) {
                Toast.makeText(this, "Эта пара уже добавлена в избранное.", Toast.LENGTH_SHORT).show();
                return;
            }

            // Add the new pair to favorites
            favoriteCurrencyPairs.add(pair);
            saveFavoriteCurrencyPairs(favoriteCurrencyPairs);

            // Update the adapter with the new pair
            currencyAdapter.addCurrencyPair(pair);

            // Inform the user
            Toast.makeText(this, "Добавлено в избранное: " + pair, Toast.LENGTH_SHORT).show();

            // Update the UI to reflect changes
            updateUI();
        });

        // Define the negative button action (Cancel)
        builder.setNegativeButton("Отмена", (dialog, which) -> dialog.dismiss());

        // Show the dialog
        builder.show();
    }
}package com.example.myapplication;

import android.content.Context;
import android.util.Log;

import com.example.myapplication.api.LoadCurrencyRatesTask;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CurrencyRepository {
    private final CurrencyRateDao currencyRateDao;
    private final Context context;

    public CurrencyRepository(Context context) {
        this.context = context;
        AppDatabase db = AppDatabase.getDatabase(context);
        currencyRateDao = db.currencyRateDao();
    }

    public void insertCurrencyRateSync(CurrencyRate rate) {
        currencyRateDao.insertCurrencyRate(rate);
        Log.d("CurrencyRepository", "Вставлен курс: " + rate.toString());
    }

    /**
     * Пакетная вставка курсов валют.
     */
    public void insertCurrencyRatesSync(List<CurrencyRate> rates) {
        currencyRateDao.insertCurrencyRates(rates);
        Log.d("CurrencyRepository", "Вставлены курсы валют: " + rates.size());
    }

    public void clearAllRates() {
        currencyRateDao.clearAllRates();
        Log.d("CurrencyRepository", "Все курсы очищены из базы данных.");
    }

    public List<Date> getAllDates() {
        return currencyRateDao.getAllDates();
    }

    // Метод для получения дат в заданном диапазоне
    public List<Date> getAllDatesWithinRange(Date startDate, Date endDate) {
        List<Date> dates = currencyRateDao.getAllDatesWithinRange(startDate, endDate);
        Log.d("CurrencyRepository", "Получены даты в диапазоне: " + dates.toString());
        return dates;
    }

    // Метод для получения исторических данных курса за заданный период
    public List<CurrencyRate> getCurrencyRateHistoryWithinDates(String baseCurrency, String targetCurrency, Date startDate, Date endDate) {
        List<CurrencyRate> rates = currencyRateDao.getCurrencyRateHistoryWithinDates(baseCurrency, targetCurrency, startDate, endDate);
        Log.d("CurrencyRepository", "Получены курсы для " + baseCurrency + "/" + targetCurrency + " в диапазоне: " + rates.toString());
        return rates;
    }

    public List<String> getAllUniqueCurrencies() {
        List<String> baseCurrencies = currencyRateDao.getAllBaseCurrencies();
        List<String> targetCurrencies = currencyRateDao.getAllTargetCurrencies();
        Set<String> allCurrencies = new HashSet<>(baseCurrencies);
        allCurrencies.addAll(targetCurrencies);
        List<String> uniqueCurrencies = new ArrayList<>(allCurrencies);
        Log.d("CurrencyRepository", "Уникальные валюты: " + uniqueCurrencies.toString());
        return uniqueCurrencies;
    }

    public Double getExchangeRate(String fromCurrency, String toCurrency) {
        Double rate = currencyRateDao.getLatestExchangeRate(fromCurrency, toCurrency);
        Log.d("CurrencyRepository", "Последний курс с " + fromCurrency + " на " + toCurrency + ": " + rate);
        return rate;
    }

    public void loadAllCurrencyRatesFromApi(LoadCurrencyRatesTask.Callback callback) {
        new LoadCurrencyRatesTask(context, this, callback).execute();
    }

    public List<CurrencyPair> getAllUniqueCurrencyPairs() {
        List<String> currencies = getAllUniqueCurrencies();
        List<CurrencyPair> pairs = new ArrayList<>();
        for (int i = 0; i < currencies.size(); i++) {
            for (int j = i + 1; j < currencies.size(); j++) {
                pairs.add(new CurrencyPair(currencies.get(i), currencies.get(j)));
                pairs.add(new CurrencyPair(currencies.get(j), currencies.get(i)));
            }
        }
        return pairs;
    }
}package com.example.myapplication;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;

import java.util.Date;
import java.util.List;

@Dao
public interface CurrencyRateDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertCurrencyRate(CurrencyRate rate);

    /**
     * Пакетная вставка курсов валют.
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertCurrencyRates(List<CurrencyRate> rates);

    @Query("DELETE FROM currency_rate")
    void clearAllRates();

    @Query("SELECT DISTINCT baseCurrency FROM currency_rate")
    List<String> getAllBaseCurrencies();

    @Query("SELECT DISTINCT targetCurrency FROM currency_rate")
    List<String> getAllTargetCurrencies();

    @Query("SELECT DISTINCT date FROM currency_rate WHERE date BETWEEN :startDate AND :endDate ORDER BY date DESC")
    List<Date> getAllDatesWithinRange(Date startDate, Date endDate);

    @Query("SELECT * FROM currency_rate WHERE baseCurrency = :baseCurrency AND targetCurrency = :targetCurrency AND date BETWEEN :startDate AND :endDate ORDER BY date DESC")
    List<CurrencyRate> getCurrencyRateHistoryWithinDates(String baseCurrency, String targetCurrency, Date startDate, Date endDate);

    @Query("SELECT rate FROM currency_rate WHERE baseCurrency = :fromCurrency AND targetCurrency = :toCurrency ORDER BY date DESC LIMIT 1")
    Double getLatestExchangeRate(String fromCurrency, String toCurrency);

    @Query("SELECT DISTINCT date FROM currency_rate ORDER BY date DESC")
    List<Date> getAllDates();
}package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log; // Добавлено для логирования
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.api.LoadCurrencyRatesTask;
import com.google.android.material.button.MaterialButton;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Spinner spinnerFrom;
    private Spinner spinnerTo;
    private EditText inputFrom;
    private EditText inputTo;
    private ProgressBar loadingIndicator;
    private CurrencyRepository repository;
    private MaterialButton buttonSwap;
    private MaterialButton buttonShowChart; // Кнопка для перехода к графику
    private Button buttonCurrencyList;

    private boolean isRatesLoaded = false;

    @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);
        buttonSwap = findViewById(R.id.button_swap);
        buttonShowChart = findViewById(R.id.button_show_chart); // Кнопка для показа графика
        buttonCurrencyList = findViewById(R.id.button_currency_list); // Инициализация кнопки избранных валют

        // Настройка обработчика кнопки избранных валют
        buttonCurrencyList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CurrencyListActivity.class);
                startActivity(intent);
            }
        });

        // Настройка обработчика кнопки перехода к графику
        buttonShowChart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isRatesLoaded) {
                    Intent intent = new Intent(MainActivity.this, ChartActivity.class);
                    String selectedFromCurrency = spinnerFrom.getSelectedItem().toString();
                    String selectedToCurrency = spinnerTo.getSelectedItem().toString();
                    intent.putExtra("FROM_CURRENCY", selectedFromCurrency);
                    intent.putExtra("TO_CURRENCY", selectedToCurrency);
                    startActivity(intent);
                } else {
                    showToast("Подождите, пока загрузятся курсы валют.");
                }
            }
        });

        // Настройка обработчика кнопки свапа
        buttonSwap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int fromPosition = spinnerFrom.getSelectedItemPosition();
                int toPosition = spinnerTo.getSelectedItemPosition();

                spinnerFrom.setSelection(toPosition);
                spinnerTo.setSelection(fromPosition);

                // Обмен значениями в полях ввода (опционально)
                String inputFromValue = inputFrom.getText().toString();
                String inputToValue = inputTo.getText().toString();
                inputFrom.setText(inputToValue);
                inputTo.setText(inputFromValue);

                if (isRatesLoaded) {
                    performConversion();
                } else {
                    showToast("Подождите, пока загрузятся курсы валют.");
                }
            }
        });

        // Показываем индикатор загрузки и загружаем курсы
        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());
                    Log.e("MainActivity", "Error updating currency rates", e);
                    // Теперь загружаем данные из кэша
                    new LoadCurrenciesTask().execute();
                });
            }
        });

        // Настройка слушателя для изменения текста в поле ввода суммы
        inputFrom.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // Без действий
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // Без действий
            }
            @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();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        };

        spinnerFrom.setOnItemSelectedListener(spinnerListener);
        spinnerTo.setOnItemSelectedListener(spinnerListener);
    }

    /**
     * AsyncTask для загрузки списка валют после обновления курсов.
     */
    private class LoadCurrenciesTask extends AsyncTask<Void, Void, List<String>> {
        @Override
        protected void onPreExecute() {
            loadingIndicator.setVisibility(View.VISIBLE);
        }

        @Override
        protected List<String> doInBackground(Void... voids) {
            // Загрузка списка валют из репозитория
            List<String> currencies = repository.getAllUniqueCurrencies();
            Log.d("LoadCurrenciesTask", "Получены валюты: " + currencies.toString());
            return currencies;
        }

        @Override
        protected void onPostExecute(List<String> currencies) {
            super.onPostExecute(currencies);
            loadingIndicator.setVisibility(View.GONE);
            Log.d("LoadCurrenciesTask", "onPostExecute с валютами: " + currencies);
            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();

            } else {
                if (!isInternetAvailable()) { // Метод для проверки сети
                    showToast("Нет интернет-соединения и нет кэшированных данных.");
                } else {
                    showToast("Не удалось загрузить валюты.");
                }
                Log.e("LoadCurrenciesTask", "Список валют пуст");
            }
        }
    }

    /**
     * Выполняет конвертацию валюты.
     */
    private void performConversion() {
        String fromCurrency = spinnerFrom.getSelectedItem().toString().toUpperCase();
        String toCurrency = spinnerTo.getSelectedItem().toString().toUpperCase();
        String amountStr = inputFrom.getText().toString();

        if (amountStr.isEmpty()) {
            inputTo.setText("");
            return;
        }

        if (fromCurrency.equals(toCurrency)) {
            inputTo.setText(amountStr);
            return;
        }

        try {
            double amount = Double.parseDouble(amountStr);

            // Выполняем конвертацию
            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 boolean isInternetAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                Network network = connectivityManager.getActiveNetwork();
                if (network == null) return false;
                NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
                return capabilities != null &&
                        (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
                                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
                                capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET));
            } else {
                // Для старых версий Android
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                return networkInfo != null && networkInfo.isConnected();
            }
        }
        return false;
    }

    /**
     * Показывает сообщение Toast.
     */
    private void showToast(String message) {
        Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
    }
} нужно добавить в активность списка возможность вкючить ежедневные оповещения о курсе валют, чтобы можно было протестировать время в системе на прошлый день и чтобы в 0:01 пришло оповещение о курсе избранных валют