From eb1c5157af9f8a7f184cead3d25d85e5e0bdf6ef Mon Sep 17 00:00:00 2001 From: Naveen Date: Fri, 24 Mar 2023 17:42:40 +0530 Subject: [PATCH] Move code to extension --- .../calendar/pro/extensions/Context.kt | 9 +++++++++ .../calendar/pro/receivers/BootCompletedReceiver.kt | 10 +--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index 201217797..d41df061e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -208,6 +208,15 @@ fun Context.cancelScheduledAutomaticBackup() { alarmManager.cancel(getAutomaticBackupIntent()) } +fun Context.checkAndBackupEventsOnBoot() { + val now = getNowSeconds() + val intervalInSeconds = AUTO_BACKUP_INTERVAL_IN_DAYS * DAY + if (config.autoBackup && config.lastAutoBackupTime !in (now - intervalInSeconds)..now) { + // device was probably off at the scheduled time so backup now + backupEventsAndTasks() + } +} + fun Context.backupEventsAndTasks() { ensureBackgroundThread { val config = config diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt index f4a638113..cc50ec102 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt @@ -4,9 +4,6 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import com.simplemobiletools.calendar.pro.extensions.* -import com.simplemobiletools.calendar.pro.helpers.AUTO_BACKUP_INTERVAL_IN_DAYS -import com.simplemobiletools.calendar.pro.helpers.DAY -import com.simplemobiletools.calendar.pro.helpers.getNowSeconds import com.simplemobiletools.commons.helpers.ensureBackgroundThread class BootCompletedReceiver : BroadcastReceiver() { @@ -18,12 +15,7 @@ class BootCompletedReceiver : BroadcastReceiver() { notifyRunningEvents() recheckCalDAVCalendars(true) {} scheduleNextAutomaticBackup() - val now = getNowSeconds() - val intervalInSeconds = AUTO_BACKUP_INTERVAL_IN_DAYS * DAY - if (config.autoBackup && config.lastAutoBackupTime !in (now - intervalInSeconds)..now) { - // device was probably off at the scheduled time so backup now - backupEventsAndTasks() - } + checkAndBackupEventsOnBoot() } } }