display real data on the list widget

This commit is contained in:
tibbi 2016-11-29 23:31:14 +01:00
parent 029ebfc983
commit dc70775ac2
3 changed files with 37 additions and 38 deletions

View file

@ -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<Event>) {
val listItems = ArrayList<ListItem>(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<ListItem> {
val listItems = ArrayList<ListItem>(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
}
}

View file

@ -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<Event>()
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<Event>()
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<Event> {
val newEvents = ArrayList<Event>()

View file

@ -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)