replace GetEventsListener with an anonymous callback

This commit is contained in:
tibbi 2017-04-02 22:07:02 +02:00
parent 6db2db029c
commit 7cd1c0e34b
7 changed files with 57 additions and 51 deletions

View File

@ -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,10 +96,9 @@ 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<Event>) {
val listItems = ArrayList<ListItem>(events.size)
val sorted = events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { it.description }))
context.dbHelper.getEventsInBackground(fromTS, toTS) {
val listItems = ArrayList<ListItem>(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 {
@ -117,7 +114,6 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV
this@EventListWidgetAdapter.events = listItems
}
})
}
override fun hasStableIds() = true

View File

@ -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<Event>) {
val sorted = ArrayList<Event>(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<Event>) {
@ -159,11 +170,6 @@ class DayFragment : Fragment(), DBHelper.EventUpdateListener, DBHelper.GetEvents
}
override fun gotEvents(events: MutableList<Event>) {
val sorted = ArrayList<Event>(events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { it.description })))
val filtered = context.getFilteredEvents(sorted)
activity?.runOnUiThread {
updateEvents(filtered)
}
receivedEvents(events)
}
}

View File

@ -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<Event> = 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<Event>) {
receivedEvents(events)
}
private fun receivedEvents(events: MutableList<Event>) {
if (context == null || activity == null)
return
@ -110,7 +116,7 @@ class EventListFragment : Fragment(), DBHelper.GetEventsListener, DBHelper.Event
override fun deleteEventOccurrences(parentIds: ArrayList<Int>, timestamps: ArrayList<Int>) {
parentIds.forEachIndexed { index, value ->
context.dbHelper.deleteEventOccurrence(parentIds[index], timestamps[index])
context.dbHelper.deleteEventOccurrence(value, timestamps[index])
}
checkEvents()
}

View File

@ -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<Event>) -> Unit) {
Thread({
getEventsInBackground(fromTS, toTS, callback)
}).start()
}
fun getEventsInBackground(fromTS: Int, toTS: Int, callback: GetEventsListener?) {
fun getEventsInBackground(fromTS: Int, toTS: Int, callback: (events: MutableList<Event>) -> Unit) {
val events = ArrayList<Event>()
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<Event>
callback?.gotEvents(filtered)
callback(filtered)
}
private fun getRepeatableEventsFor(fromTS: Int, toTS: Int, getRunningEvents: Boolean = false): List<Event> {
@ -541,8 +541,4 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
fun gotEvents(events: MutableList<Event>)
}
interface GetEventsListener {
fun gotEvents(events: MutableList<Event>)
}
}

View File

@ -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<Event>) {
fun gotEvents(events: MutableList<Event>) {
if (mFilterEventTypes)
mEvents = mContext.getFilteredEvents(events)
else

View File

@ -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<Event>
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)
mContext.dbHelper.getEvents(startTS, endTS) {
mEvents = it
mCallback.updateWeeklyCalendar(it)
}
override fun gotEvents(events: MutableList<Event>) {
mEvents = events
mCallback.updateWeeklyCalendar(events)
}
}

View File

@ -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<Event>) {
fun gotEvents(events: MutableList<Event>) {
val filtered = context.getFilteredEvents(events)
val arr = SparseArray<ArrayList<Int>>(12)