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 1aa91a27a..5605257cc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt @@ -12,12 +12,13 @@ import com.simplemobiletools.calendar.R.id.event_item_holder import com.simplemobiletools.calendar.extensions.adjustAlpha import com.simplemobiletools.calendar.helpers.* 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 import org.joda.time.DateTime import java.util.* - +import kotlin.comparisons.compareBy class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsService.RemoteViewsFactory { val ITEM_EVENT = 0 @@ -76,12 +77,32 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV override fun getViewTypeCount() = 2 override fun onCreate() { - events = getListItems() + } override fun getItemId(position: Int) = position.toLong() override fun onDataSetChanged() { + val fromTS = (DateTime().millis / 1000).toInt() + val toTS = (DateTime().plusMonths(6).millis / 1000).toInt() + DBHelper(context).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 })) + var prevCode = "" + sorted.forEach { + val code = Formatter.getDayCodeFromTS(it.startTS) + if (code != prevCode) { + val day = Formatter.getDayTitle(context, code) + listItems.add(ListSection(day)) + prevCode = code + } + listItems.add(ListEvent(it.id, it.startTS, it.endTS, it.title, it.description)) + } + + this@EventListWidgetAdapter.events = listItems + } + }) } override fun hasStableIds() = true @@ -90,29 +111,4 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV override fun onDestroy() { } - - private fun getListItems(): ArrayList { - val listItems = ArrayList(10) - var dateTime = DateTime.now().withTime(0, 0, 0, 0).plusDays(1) - var code = Formatter.getDayCodeFromTS((dateTime.millis / 1000).toInt()) - var day = Formatter.getDayTitle(context, code) - listItems.add(ListSection(day)) - - var time = dateTime.withHourOfDay(7) - listItems.add(ListEvent(1, (time.millis / 1000).toInt(), (time.plusMinutes(30).millis / 1000).toInt(), "Workout", "Leg day")) - time = dateTime.withHourOfDay(8) - listItems.add(ListEvent(2, (time.millis / 1000).toInt(), (time.plusHours(1).millis / 1000).toInt(), "Meeting with John", "In Rockstone Garden")) - - dateTime = dateTime.plusDays(1) - code = Formatter.getDayCodeFromTS((dateTime.millis / 1000).toInt()) - day = Formatter.getDayTitle(context, code) - listItems.add(ListSection(day)) - - time = dateTime.withHourOfDay(13) - listItems.add(ListEvent(3, (time.millis / 1000).toInt(), (time.plusHours(1).millis / 1000).toInt(), "Lunch with Mary", "In the Plaza")) - time = dateTime.withHourOfDay(18) - listItems.add(ListEvent(4, (time.millis / 1000).toInt(), (time.plusMinutes(10).millis / 1000).toInt(), "Coffee time", "")) - - return listItems - } } 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 73550d5cc..f346b530b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -155,18 +155,22 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V fun getEvents(fromTS: Int, toTS: Int, callback: GetEventsListener?) { Thread({ - val events = ArrayList() - events.addAll(getEventsFor(fromTS, toTS)) - - val selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS NULL" - val selectionArgs = arrayOf(toTS.toString(), fromTS.toString()) - val cursor = getEventsCursor(selection, selectionArgs) - events.addAll(fillEvents(cursor)) - - callback?.gotEvents(events) + getEventsInBackground(fromTS, toTS, callback) }).start() } + fun getEventsInBackground(fromTS: Int, toTS: Int, callback: GetEventsListener?) { + val events = ArrayList() + events.addAll(getEventsFor(fromTS, toTS)) + + val selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS NULL" + val selectionArgs = arrayOf(toTS.toString(), fromTS.toString()) + val cursor = getEventsCursor(selection, selectionArgs) + events.addAll(fillEvents(cursor)) + + callback?.gotEvents(events) + } + private fun getEventsFor(fromTS: Int, toTS: Int): List { val newEvents = ArrayList() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetListProvider.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetListProvider.kt index b93a9cbd8..b10dcea2f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetListProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetListProvider.kt @@ -56,9 +56,8 @@ class MyWidgetListProvider : AppWidgetProvider() { val startActivityPendingIntent = PendingIntent.getActivity(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT) mRemoteViews.setPendingIntentTemplate(R.id.widget_event_list, startActivityPendingIntent) - val thisWidget = ComponentName(mContext, MyWidgetListProvider::class.java) - AppWidgetManager.getInstance(mContext).updateAppWidget(thisWidget, mRemoteViews) + mWidgetManager.updateAppWidget(thisWidget, mRemoteViews) } private fun initPrefs(context: Context) = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)