// Path: app/src/main/java/dynoapps/exchange_rates/AppDatabase.java
// @Database(entities = {Alarm.class}, version = 1, exportSchema = false)
// public abstract class AppDatabase extends RoomDatabase {
//     /**
//      * The only instance
//      */
//     private static AppDatabase sInstance;
// 
//     /**
//      * Gets the singleton instance of SampleDatabase.
//      *
//      * @param context The context.
//      * @return The singleton instance of SampleDatabase.
//      */
//     public static synchronized AppDatabase getInstance(Context context) {
//         if (sInstance == null) {
//             sInstance = Room
//                     .databaseBuilder(context.getApplicationContext(), AppDatabase.class, "alarms")
//                     .build();
//         }
//         return sInstance;
//     }
// 
//     /**
//      * @return The DAO for the Alarm table.
//      */
//     public abstract AlarmDao alarm();
// }


import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import dynoapps.exchange_rates.AppDatabase;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
    public void getAlarms(final AlarmsLoadCallback alarmsLoadCallback) {
        alarmDao.list().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((alarms, throwable) -> {
            if (alarmsLoadCallback != null) {
                alarmsLoadCallback.onAlarmsLoaded(alarms);
            }
        });
    }

    @SuppressLint("CheckResult")
    @Override
    public void saveAlarm(@NonNull final Alarm alarm, final AlarmUpdateInsertCallback alarmUpdateInsertCallback) {
        alarmDao.insert(alarm).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe((aLong, throwable) -> {
                    alarm.id = aLong;
                    alarmUpdateInsertCallback.onAlarmUpdate(alarm);
                });
    }

    @SuppressLint("CheckResult")
    @Override
    public Single<Alarm> deleteAlarm(@NonNull final Alarm alarm) {
        return alarmDao.deleteById(alarm.id).map(integer -> alarm);
    }

    @Override
    public Single<Alarm> updateAlarm(@NonNull final Alarm alarm) {
        return alarmDao.update(alarm).map(integer -> alarm);
    }

    @Override
// Path: app/src/main/java/dynoapps/exchange_rates/AppDatabase.java
// @Database(entities = {Alarm.class}, version = 1, exportSchema = false)
// public abstract class AppDatabase extends RoomDatabase {
//     /**
//      * The only instance
//      */
//     private static AppDatabase sInstance;
// 
//     /**
//      * Gets the singleton instance of SampleDatabase.
//      *
//      * @param context The context.
//      * @return The singleton instance of SampleDatabase.
//      */
//     public static synchronized AppDatabase getInstance(Context context) {
//         if (sInstance == null) {
//             sInstance = Room
//                     .databaseBuilder(context.getApplicationContext(), AppDatabase.class, "alarms")
//                     .build();
//         }
//         return sInstance;
//     }
// 
//     /**
//      * @return The DAO for the Alarm table.
//      */
//     public abstract AlarmDao alarm();
// }


// Path: app/src/main/java/dynoapps/exchange_rates/alarm/LocalAlarmsDataSource.java
import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import dynoapps.exchange_rates.AppDatabase;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

    public void getAlarms(final AlarmsLoadCallback alarmsLoadCallback) {
        alarmDao.list().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((alarms, throwable) -> {
            if (alarmsLoadCallback != null) {
                alarmsLoadCallback.onAlarmsLoaded(alarms);
            }
        });
    }

    @SuppressLint("CheckResult")
    @Override
    public void saveAlarm(@NonNull final Alarm alarm, final AlarmUpdateInsertCallback alarmUpdateInsertCallback) {
        alarmDao.insert(alarm).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe((aLong, throwable) -> {
                    alarm.id = aLong;
                    alarmUpdateInsertCallback.onAlarmUpdate(alarm);
                });
    }

    @SuppressLint("CheckResult")
    @Override
    public Single<Alarm> deleteAlarm(@NonNull final Alarm alarm) {
        return alarmDao.deleteById(alarm.id).map(integer -> alarm);
    }

    @Override
    public Single<Alarm> updateAlarm(@NonNull final Alarm alarm) {
        return alarmDao.update(alarm).map(integer -> alarm);
    }

    @Override
    public void refreshAlarms() {