From 0b8ae27d0087ee2f5b73f1de32e690a514d7b211 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 22 Feb 2017 00:02:20 +0100 Subject: [PATCH] fix #101, add a button for creating new event at the events list widget --- .../adapters/EventListWidgetAdapter.kt | 16 ++------ .../calendar/extensions/Context.kt | 6 +-- .../calendar/helpers/MyWidgetListProvider.kt | 41 +++++++++++++++++++ app/src/main/res/layout/widget_event_list.xml | 29 +++++++++++-- 4 files changed, 74 insertions(+), 18 deletions(-) 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 3d665aab6..86890e500 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt @@ -2,8 +2,6 @@ package com.simplemobiletools.calendar.adapters import android.content.Context import android.content.Intent -import android.text.SpannableString -import android.text.style.UnderlineSpan import android.view.View import android.widget.RemoteViews import android.widget.RemoteViewsService @@ -31,7 +29,7 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV var todayDate = "" val allDayString = context.resources.getString(R.string.all_day) - override fun getViewAt(position: Int): RemoteViews { + override fun getViewAt(position: Int): RemoteViews? { val type = getItemViewType(position) val remoteView: RemoteViews @@ -76,14 +74,7 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV val item = events[position] as ListSection remoteView = RemoteViews(context.packageName, R.layout.event_list_section_widget).apply { setInt(R.id.event_item_title, "setTextColor", textColor) - - if (item.title == todayDate) { - val underlinedText = SpannableString(item.title) - underlinedText.setSpan(UnderlineSpan(), 0, item.title.length, 0) - setTextViewText(R.id.event_item_title, underlinedText) - } else { - setTextViewText(R.id.event_item_title, item.title) - } + setTextViewText(R.id.event_item_title, item.title) } } @@ -117,7 +108,8 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV val code = Formatter.getDayCodeFromTS(it.startTS) if (code != prevCode) { val day = Formatter.getDayTitle(context, code) - listItems.add(ListSection(day)) + if (day != todayDate) + listItems.add(ListSection(day)) prevCode = code } listItems.add(ListEvent(it.id, it.startTS, it.endTS, it.title, it.description, it.isAllDay)) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt index 48796ef6b..531fd90de 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -153,10 +153,10 @@ fun Context.getFilteredEvents(events: List): List { return filtered } -fun Context.launchNewEventIntent(startNewTask: Boolean = false) { - val tomorrowCode = Formatter.getDayCodeFromDateTime(DateTime(DateTimeZone.getDefault()).plusDays(1)) +fun Context.launchNewEventIntent(startNewTask: Boolean = false, today: Boolean = false) { + val code = Formatter.getDayCodeFromDateTime(DateTime(DateTimeZone.getDefault()).plusDays(if (today) 0 else 1)) Intent(applicationContext, EventActivity::class.java).apply { - putExtra(NEW_EVENT_START_TS, getNewEventTimestampFromCode(tomorrowCode)) + putExtra(NEW_EVENT_START_TS, getNewEventTimestampFromCode(code)) if (startNewTask) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) startActivity(this) 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 3f5146657..c4a1a4410 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetListProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetListProvider.kt @@ -10,11 +10,18 @@ import android.content.res.Resources import android.net.Uri import android.widget.RemoteViews import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.activities.DayActivity import com.simplemobiletools.calendar.activities.SplashActivity import com.simplemobiletools.calendar.extensions.config +import com.simplemobiletools.calendar.extensions.launchNewEventIntent import com.simplemobiletools.calendar.services.WidgetService +import com.simplemobiletools.commons.extensions.getColoredIcon +import org.joda.time.DateTime class MyWidgetListProvider : AppWidgetProvider() { + private val NEW_EVENT = "new_event" + private val LAUNCH_TODAY = "launch_today" + companion object { private var mTextColor = 0 @@ -22,6 +29,7 @@ class MyWidgetListProvider : AppWidgetProvider() { lateinit var mRes: Resources lateinit var mWidgetManager: AppWidgetManager lateinit var mIntent: Intent + lateinit var mContext: Context } override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { @@ -30,6 +38,7 @@ class MyWidgetListProvider : AppWidgetProvider() { } private fun initVariables(context: Context) { + mContext = context mRes = context.resources mTextColor = context.config.widgetTextColor @@ -41,6 +50,16 @@ class MyWidgetListProvider : AppWidgetProvider() { mRemoteViews.setInt(R.id.widget_event_list_holder, "setBackgroundColor", context.config.widgetBgColor) mRemoteViews.setInt(R.id.widget_event_list_empty, "setTextColor", mTextColor) + mRemoteViews.setInt(R.id.widget_event_list_today, "setTextColor", mTextColor) + + val now = (System.currentTimeMillis() / 1000).toInt() + val todayCode = Formatter.getDayCodeFromTS(now) + val todayText = Formatter.getDayTitle(context, todayCode) + mRemoteViews.setTextViewText(R.id.widget_event_list_today, todayText) + + mRemoteViews.setImageViewBitmap(R.id.widget_event_new_event, context.resources.getColoredIcon(mTextColor, R.drawable.ic_plus)) + setupIntent(NEW_EVENT, R.id.widget_event_new_event) + setupIntent(LAUNCH_TODAY, R.id.widget_event_list_today) Intent(context, WidgetService::class.java).apply { data = Uri.parse(this.toUri(Intent.URI_INTENT_SCHEME)) @@ -56,4 +75,26 @@ class MyWidgetListProvider : AppWidgetProvider() { mWidgetManager.updateAppWidget(appWidgetIds, mRemoteViews) mWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_event_list) } + + private fun setupIntent(action: String, id: Int) { + mIntent.action = action + val pendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent, 0) + mRemoteViews.setOnClickPendingIntent(id, pendingIntent) + } + + override fun onReceive(context: Context, intent: Intent) { + when (intent.action) { + NEW_EVENT -> context.launchNewEventIntent(true, true) + LAUNCH_TODAY -> launchDayActivity() + else -> super.onReceive(context, intent) + } + } + + private fun launchDayActivity() { + Intent(mContext, DayActivity::class.java).apply { + putExtra(DAY_CODE, Formatter.getDayCodeFromDateTime(DateTime())) + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + mContext.startActivity(this) + } + } } diff --git a/app/src/main/res/layout/widget_event_list.xml b/app/src/main/res/layout/widget_event_list.xml index a9199b215..9e01ec324 100644 --- a/app/src/main/res/layout/widget_event_list.xml +++ b/app/src/main/res/layout/widget_event_list.xml @@ -5,23 +5,46 @@ android:layout_width="match_parent" android:layout_height="match_parent"> + + + + + android:paddingLeft="@dimen/activity_margin"/>