diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt index 35f1a689c..f0533c93b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt @@ -10,10 +10,8 @@ import com.simplemobiletools.calendar.R.id.event_item_holder import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.dbHelper import com.simplemobiletools.calendar.extensions.seconds -import com.simplemobiletools.calendar.helpers.DBHelper import com.simplemobiletools.calendar.helpers.EVENT_ID import com.simplemobiletools.calendar.helpers.Formatter -import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.ListEvent import com.simplemobiletools.calendar.models.ListItem import com.simplemobiletools.calendar.models.ListSection @@ -98,26 +96,24 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV override fun onDataSetChanged() { val fromTS = DateTime().seconds() val toTS = DateTime().plusYears(1).seconds() - context.dbHelper.getEventsInBackground(fromTS, toTS, object : DBHelper.GetEventsListener { - override fun gotEvents(events: MutableList) { - val listItems = ArrayList(events.size) - val sorted = events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { it.description })) - val sublist = sorted.subList(0, Math.min(sorted.size, 100)) - var prevCode = "" - sublist.forEach { - val code = Formatter.getDayCodeFromTS(it.startTS) - if (code != prevCode) { - val day = Formatter.getDayTitle(context, code) - if (day != todayDate) - listItems.add(ListSection(day)) - prevCode = code - } - listItems.add(ListEvent(it.id, it.startTS, it.endTS, it.title, it.description, it.isAllDay)) + context.dbHelper.getEventsInBackground(fromTS, toTS) { + val listItems = ArrayList(it.size) + val sorted = it.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { it.description })) + val sublist = sorted.subList(0, Math.min(sorted.size, 100)) + var prevCode = "" + sublist.forEach { + val code = Formatter.getDayCodeFromTS(it.startTS) + if (code != prevCode) { + val day = Formatter.getDayTitle(context, code) + if (day != todayDate) + listItems.add(ListSection(day)) + prevCode = code } - - this@EventListWidgetAdapter.events = listItems + listItems.add(ListEvent(it.id, it.startTS, it.endTS, it.title, it.description, it.isAllDay)) } - }) + + this@EventListWidgetAdapter.events = listItems + } } override fun hasStableIds() = true diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt index 06b9bdd9f..7f410d91e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt @@ -32,7 +32,7 @@ import kotlinx.android.synthetic.main.top_navigation.view.* import org.joda.time.DateTime import java.util.* -class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEventsListener, DeleteEventsListener { +class DayFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEventsListener { private var mTextColor = 0 private var mDayCode = "" private var mListener: NavigationListener? = null @@ -112,7 +112,18 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents fun checkEvents() { val startTS = Formatter.getDayStartTS(mDayCode) val endTS = Formatter.getDayEndTS(mDayCode) - DBHelper.newInstance(context, this).getEvents(startTS, endTS, this) + DBHelper.newInstance(context, this).getEvents(startTS, endTS) { + receivedEvents(it) + } + } + + private fun receivedEvents(events: List) { + val sorted = ArrayList(events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { it.description }))) + val filtered = context.getFilteredEvents(sorted) + + activity?.runOnUiThread { + updateEvents(filtered) + } } private fun updateEvents(events: List) { @@ -159,11 +170,6 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents } override fun gotEvents(events: MutableList) { - val sorted = ArrayList(events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { it.description }))) - val filtered = context.getFilteredEvents(sorted) - - activity?.runOnUiThread { - updateEvents(filtered) - } + receivedEvents(events) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt index e9fbc8c43..dcaef6a30 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt @@ -29,7 +29,7 @@ import kotlinx.android.synthetic.main.fragment_event_list.view.* import org.joda.time.DateTime import java.util.* -class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.EventUpdateListener, DeleteEventsListener { +class EventListFragment : Fragment(), DBHelper.EventUpdateListener, DeleteEventsListener { private var mEvents: List = ArrayList() private var prevEventsHash = 0 lateinit var mView: View @@ -49,10 +49,16 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.Event private fun checkEvents() { val fromTS = DateTime().seconds() val toTS = DateTime().plusYears(1).seconds() - context.dbHelper.getEvents(fromTS, toTS, this) + context.dbHelper.getEvents(fromTS, toTS) { + receivedEvents(it) + } } override fun gotEvents(events: MutableList) { + receivedEvents(events) + } + + private fun receivedEvents(events: MutableList) { if (context == null || activity == null) return @@ -110,7 +116,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.Event override fun deleteEventOccurrences(parentIds: ArrayList, timestamps: ArrayList) { parentIds.forEachIndexed { index, value -> - context.dbHelper.deleteEventOccurrence(parentIds[index], timestamps[index]) + context.dbHelper.deleteEventOccurrence(value, timestamps[index]) } checkEvents() } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index 1f8a26208..f5ec3418e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -353,13 +353,13 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont null } - fun getEvents(fromTS: Int, toTS: Int, callback: GetEventsListener?) { + fun getEvents(fromTS: Int, toTS: Int, callback: (events: MutableList) -> Unit) { Thread({ getEventsInBackground(fromTS, toTS, callback) }).start() } - fun getEventsInBackground(fromTS: Int, toTS: Int, callback: GetEventsListener?) { + fun getEventsInBackground(fromTS: Int, toTS: Int, callback: (events: MutableList) -> Unit) { val events = ArrayList() val selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS NULL" @@ -370,7 +370,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont events.addAll(getRepeatableEventsFor(fromTS, toTS)) val filtered = events.filterNot { it.ignoreEventOccurrences.contains(it.startTS) } as MutableList - callback?.gotEvents(filtered) + callback(filtered) } private fun getRepeatableEventsFor(fromTS: Int, toTS: Int, getRunningEvents: Boolean = false): List { @@ -541,8 +541,4 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont fun gotEvents(events: MutableList) } - - interface GetEventsListener { - fun gotEvents(events: MutableList) - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt index 37384bd18..9732cf142 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt @@ -11,7 +11,7 @@ import com.simplemobiletools.calendar.models.Event import org.joda.time.DateTime import java.util.* -class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context) : DBHelper.GetEventsListener { +class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context) { private val DAYS_CNT = 42 private val YEAR_PATTERN = "YYYY" @@ -30,7 +30,9 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context) mTargetDate = targetDate val startTS = mTargetDate.minusMonths(1).seconds() val endTS = mTargetDate.plusMonths(1).seconds() - mContext.dbHelper.getEvents(startTS, endTS, this) + mContext.dbHelper.getEvents(startTS, endTS) { + gotEvents(it) + } } fun getPrevMonth() { @@ -116,7 +118,7 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context) return month } - override fun gotEvents(events: MutableList) { + fun gotEvents(events: MutableList) { if (mFilterEventTypes) mEvents = mContext.getFilteredEvents(events) else diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/WeeklyCalendarImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/WeeklyCalendarImpl.kt index ec307c02d..3c741a5a8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/WeeklyCalendarImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/WeeklyCalendarImpl.kt @@ -6,7 +6,7 @@ import com.simplemobiletools.calendar.interfaces.WeeklyCalendar import com.simplemobiletools.calendar.models.Event import java.util.* -class WeeklyCalendarImpl(val mCallback: WeeklyCalendar, val mContext: Context) : DBHelper.GetEventsListener { +class WeeklyCalendarImpl(val mCallback: WeeklyCalendar, val mContext: Context) { var mEvents: List init { @@ -16,11 +16,9 @@ class WeeklyCalendarImpl(val mCallback: WeeklyCalendar, val mContext: Context) : fun updateWeeklyCalendar(weekStartTS: Int) { val startTS = weekStartTS val endTS = startTS + WEEK_SECONDS - mContext.dbHelper.getEvents(startTS, endTS, this) - } - - override fun gotEvents(events: MutableList) { - mEvents = events - mCallback.updateWeeklyCalendar(events) + mContext.dbHelper.getEvents(startTS, endTS) { + mEvents = it + mCallback.updateWeeklyCalendar(it) + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt index 1bf1ba968..de12fde3c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt @@ -10,16 +10,18 @@ import com.simplemobiletools.calendar.models.Event import org.joda.time.DateTime import java.util.* -class YearlyCalendarImpl(val callback: YearlyCalendar, val context: Context, val year: Int) : DBHelper.GetEventsListener { +class YearlyCalendarImpl(val callback: YearlyCalendar, val context: Context, val year: Int) { fun getEvents(year: Int) { val startDateTime = DateTime().withTime(0, 0, 0, 0).withDate(year, 1, 1) val startTS = startDateTime.seconds() val endTS = startDateTime.plusYears(1).minusSeconds(1).seconds() - context.dbHelper.getEvents(startTS, endTS, this) + context.dbHelper.getEvents(startTS, endTS) { + gotEvents(it) + } } - override fun gotEvents(events: MutableList) { + fun gotEvents(events: MutableList) { val filtered = context.getFilteredEvents(events) val arr = SparseArray>(12)