Search in sources :

Example 1 with ReminderDao

use of nodomain.freeyourgadget.gadgetbridge.entities.ReminderDao in project Gadgetbridge by Freeyourgadget.

the class DBHelper method getReminders.

/**
 * Returns all user-configurable reminders for the given user and device. The list is sorted by
 * {@link Reminder#getDate}. Calendar events that may also be modeled as reminders are not stored
 * in the database and hence not returned by this method.
 * @param gbDevice the device for which the alarms shall be loaded
 * @return the list of reminders for the given device
 */
@NonNull
public static List<Reminder> getReminders(@NonNull GBDevice gbDevice) {
    final DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice);
    final Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()));
    int reservedSlots = prefs.getInt(DeviceSettingsPreferenceConst.PREF_RESERVER_REMINDERS_CALENDAR, 9);
    final int reminderSlots = coordinator.getReminderSlotCount();
    try (DBHandler db = GBApplication.acquireDB()) {
        final DaoSession daoSession = db.getDaoSession();
        final User user = getUser(daoSession);
        final Device dbDevice = DBHelper.findDevice(gbDevice, daoSession);
        if (dbDevice != null) {
            final ReminderDao reminderDao = daoSession.getReminderDao();
            final Long deviceId = dbDevice.getId();
            final QueryBuilder<Reminder> qb = reminderDao.queryBuilder();
            qb.where(ReminderDao.Properties.UserId.eq(user.getId()), ReminderDao.Properties.DeviceId.eq(deviceId)).orderAsc(ReminderDao.Properties.Date).limit(reminderSlots - reservedSlots);
            return qb.build().list();
        }
    } catch (final Exception e) {
        LOG.error("Error reading reminders from db", e);
    }
    return Collections.emptyList();
}
Also used : User(nodomain.freeyourgadget.gadgetbridge.entities.User) ActivityUser(nodomain.freeyourgadget.gadgetbridge.model.ActivityUser) Reminder(nodomain.freeyourgadget.gadgetbridge.entities.Reminder) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) DeviceCoordinator(nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator) IOException(java.io.IOException) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession) ReminderDao(nodomain.freeyourgadget.gadgetbridge.entities.ReminderDao) NonNull(androidx.annotation.NonNull)

Aggregations

NonNull (androidx.annotation.NonNull)1 IOException (java.io.IOException)1 DeviceCoordinator (nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator)1 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)1 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)1 Reminder (nodomain.freeyourgadget.gadgetbridge.entities.Reminder)1 ReminderDao (nodomain.freeyourgadget.gadgetbridge.entities.ReminderDao)1 User (nodomain.freeyourgadget.gadgetbridge.entities.User)1 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)1 ActivityUser (nodomain.freeyourgadget.gadgetbridge.model.ActivityUser)1 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)1