fix #101, add a button for creating new event at the events list widget

This commit is contained in:
tibbi 2017-02-22 00:02:20 +01:00
parent b7bcfe8d28
commit 0b8ae27d00
4 changed files with 74 additions and 18 deletions

View File

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

View File

@ -153,10 +153,10 @@ fun Context.getFilteredEvents(events: List<Event>): List<Event> {
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)

View File

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

View File

@ -5,23 +5,46 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/widget_event_list_today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_margin"
android:textSize="@dimen/actionbar_text_size"/>
<ImageView
android:id="@+id/widget_event_new_event"
style="@style/ArrowStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@+id/widget_event_list"
android:layout_alignBottom="@+id/widget_event_list_today"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/widget_event_list_today"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:scaleType="fitCenter"
android:src="@drawable/ic_plus"/>
<ListView
android:id="@+id/widget_event_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/widget_event_list_today"
android:clipToPadding="false"
android:divider="@null"
android:dividerHeight="@dimen/medium_margin"
android:paddingBottom="@dimen/activity_margin"
android:paddingLeft="@dimen/activity_margin"
android:paddingTop="@dimen/medium_margin"/>
android:paddingLeft="@dimen/activity_margin"/>
<TextView
android:id="@+id/widget_event_list_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/widget_event_list_today"
android:gravity="center_horizontal"
android:padding="@dimen/big_margin"
android:paddingLeft="@dimen/big_margin"
android:paddingRight="@dimen/big_margin"
android:text="@string/no_upcoming_events"
android:textSize="@dimen/bigger_text_size"/>