sort event reminders and remove duplicates before saving in db

This commit is contained in:
tibbi 2017-02-06 20:41:49 +01:00
parent 39fd7b0682
commit 9865af1cd6
6 changed files with 17 additions and 14 deletions

View File

@ -269,6 +269,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
return
}
val reminders = sortedSetOf(mReminder1Minutes, mReminder2Minutes, mReminder3Minutes).filter { it != REMINDER_OFF }
val dbHelper = DBHelper(applicationContext, this)
val newDescription = event_description.value
mEvent.apply {
@ -276,9 +277,9 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
endTS = newEndTS
title = newTitle
description = newDescription
reminder1Minutes = mReminder1Minutes
reminder2Minutes = mReminder2Minutes
reminder3Minutes = mReminder3Minutes
reminder1Minutes = reminders.elementAtOrElse(0) { REMINDER_OFF }
reminder2Minutes = reminders.elementAtOrElse(1) { REMINDER_OFF }
reminder3Minutes = reminders.elementAtOrElse(2) { REMINDER_OFF }
repeatInterval = mRepeatInterval
flags = if (event_all_day.isChecked) (mEvent.flags or FLAG_ALL_DAY) else (mEvent.flags.removeFlag(FLAG_ALL_DAY))
repeatLimit = if (repeatInterval == 0) 0 else mRepeatLimit
@ -382,12 +383,12 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
} else {
toast(R.string.event_added)
}
scheduleNotification(event)
scheduleNotifications(event)
finish()
}
override fun eventUpdated(event: Event) {
scheduleNotification(event)
scheduleNotifications(event)
toast(R.string.event_updated)
finish()
}

View File

@ -41,7 +41,7 @@ fun Context.updateListWidget() {
}
}
fun Context.scheduleNextEvent(event: Event) {
fun Context.scheduleNextEventNotifications(event: Event) {
var startTS = event.startTS - event.reminder1Minutes * 60
var newTS = startTS
if (event.repeatInterval == DAY || event.repeatInterval == WEEK || event.repeatInterval == BIWEEK) {
@ -67,11 +67,11 @@ private fun getNewTS(ts: Int, isMonthly: Boolean): Int {
return dateTime.seconds()
}
fun Context.scheduleNotification(event: Event) {
fun Context.scheduleNotifications(event: Event) {
if (event.getRemindersCount() == 0)
return
scheduleNextEvent(event)
scheduleNextEventNotifications(event)
}
fun Context.scheduleEventIn(notifTS: Int, event: Event) {

View File

@ -1,7 +1,7 @@
package com.simplemobiletools.calendar.helpers
import android.content.Context
import com.simplemobiletools.calendar.extensions.scheduleNotification
import com.simplemobiletools.calendar.extensions.scheduleNotifications
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.IcsParser.ImportResult.*
import com.simplemobiletools.calendar.models.Event
@ -60,7 +60,7 @@ class IcsParser {
val event = Event(0, curStart, curEnd, curTitle, curDescription, reminderMinutes, -1, -1, importId = curImportId, flags = curFlags)
dbHelper.insert(event) {
context.scheduleNotification(event)
context.scheduleNotifications(event)
}
eventsImported++
resetValues()

View File

@ -41,4 +41,6 @@ data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var
cnt++
return cnt
}
fun getReminders() = arrayOf(reminder1Minutes, reminder2Minutes, reminder3Minutes).filter { it != REMINDER_OFF }
}

View File

@ -3,7 +3,7 @@ package com.simplemobiletools.calendar.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.simplemobiletools.calendar.extensions.scheduleNextEvent
import com.simplemobiletools.calendar.extensions.scheduleNextEventNotifications
import com.simplemobiletools.calendar.helpers.DBHelper
class BootCompletedReceiver : BroadcastReceiver() {
@ -11,7 +11,7 @@ class BootCompletedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, arg1: Intent) {
val events = DBHelper(context).getEventsAtReboot()
for (event in events) {
context.scheduleNextEvent(event)
context.scheduleNextEventNotifications(event)
}
}
}

View File

@ -10,7 +10,7 @@ import android.net.Uri
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.scheduleNextEvent
import com.simplemobiletools.calendar.extensions.scheduleNextEventNotifications
import com.simplemobiletools.calendar.extensions.updateListWidget
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.EVENT_ID
@ -36,7 +36,7 @@ class NotificationReceiver : BroadcastReceiver() {
notificationManager.notify(id, notification)
if (event.repeatInterval != 0)
context.scheduleNextEvent(event)
context.scheduleNextEventNotifications(event)
}
private fun getEventTime(startTime: String, endTime: String) = if (startTime == endTime) startTime else "$startTime - $endTime"