add a button to monthly widget for easy new event creation

This commit is contained in:
tibbi 2017-02-21 21:08:18 +01:00
parent 4ecc5f5f37
commit 0b5109a55f
4 changed files with 34 additions and 12 deletions

View file

@ -19,7 +19,7 @@ import com.simplemobiletools.calendar.dialogs.ChangeViewDialog
import com.simplemobiletools.calendar.dialogs.FilterEventTypesDialog
import com.simplemobiletools.calendar.dialogs.ImportEventsDialog
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.getNewEventTimestampFromCode
import com.simplemobiletools.calendar.extensions.launchNewEventIntent
import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.extensions.updateWidgets
import com.simplemobiletools.calendar.fragments.EventListFragment
@ -36,7 +36,6 @@ import com.simplemobiletools.commons.helpers.LICENSE_STETHO
import com.simplemobiletools.commons.models.Release
import kotlinx.android.synthetic.main.activity_main.*
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import java.util.*
class MainActivity : SimpleActivity(), NavigationListener {
@ -64,7 +63,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
calendar_fab.setOnClickListener { addNewEvent() }
calendar_fab.setOnClickListener { launchNewEventIntent() }
checkWhatsNewDialog()
}
@ -231,14 +230,6 @@ class MainActivity : SimpleActivity(), NavigationListener {
startAboutActivity(R.string.app_name, LICENSE_KOTLIN or LICENSE_JODA or LICENSE_STETHO, BuildConfig.VERSION_NAME)
}
private fun addNewEvent() {
val tomorrowCode = Formatter.getDayCodeFromDateTime(DateTime(DateTimeZone.getDefault()).plusDays(1))
Intent(applicationContext, EventActivity::class.java).apply {
putExtra(NEW_EVENT_START_TS, getNewEventTimestampFromCode(tomorrowCode))
startActivity(this)
}
}
private fun resetTitle() {
title = getString(R.string.app_launcher_name)
supportActionBar?.subtitle = ""

View file

@ -10,10 +10,13 @@ import android.graphics.Color
import android.os.Build
import android.os.SystemClock
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.EventActivity
import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.models.Event
import com.simplemobiletools.calendar.receivers.NotificationReceiver
import com.simplemobiletools.commons.extensions.getContrastColor
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
fun Context.updateWidgets() {
val widgetsCnt = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, MyWidgetMonthlyProvider::class.java))
@ -150,6 +153,16 @@ 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))
Intent(applicationContext, EventActivity::class.java).apply {
putExtra(NEW_EVENT_START_TS, getNewEventTimestampFromCode(tomorrowCode))
if (startNewTask)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(this)
}
}
fun Context.getNewEventTimestampFromCode(dayCode: String) = Formatter.getLocalDateTimeFromCode(dayCode).withTime(13, 0, 0, 0).seconds()
val Context.config: Config get() = Config.newInstance(this)

View file

@ -16,6 +16,7 @@ import android.widget.RemoteViews
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.SplashActivity
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.launchNewEventIntent
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
import com.simplemobiletools.calendar.models.Day
import com.simplemobiletools.commons.extensions.adjustAlpha
@ -25,6 +26,7 @@ class MyWidgetMonthlyProvider : AppWidgetProvider(), MonthlyCalendar {
companion object {
private val PREV = "prev"
private val NEXT = "next"
private val NEW_EVENT = "new_event"
private var mTextColor = 0
private var mWeakTextColor = 0
@ -94,6 +96,7 @@ class MyWidgetMonthlyProvider : AppWidgetProvider(), MonthlyCalendar {
private fun setupButtons() {
setupIntent(PREV, R.id.top_left_arrow)
setupIntent(NEXT, R.id.top_right_arrow)
setupIntent(NEW_EVENT, R.id.top_new_event)
setupAppOpenIntent(R.id.top_value)
}
@ -106,6 +109,7 @@ class MyWidgetMonthlyProvider : AppWidgetProvider(), MonthlyCalendar {
when (action) {
PREV -> mCalendar?.getPrevMonth()
NEXT -> mCalendar?.getNextMonth()
NEW_EVENT -> mContext.launchNewEventIntent(true)
else -> super.onReceive(context, intent)
}
}
@ -177,6 +181,9 @@ class MyWidgetMonthlyProvider : AppWidgetProvider(), MonthlyCalendar {
bmp = getColoredIcon(mContext, mTextColor, R.drawable.ic_pointer_right)
mRemoteViews.setImageViewBitmap(R.id.top_right_arrow, bmp)
bmp = getColoredIcon(mContext, mTextColor, R.drawable.ic_plus)
mRemoteViews.setImageViewBitmap(R.id.top_new_event, bmp)
}
fun updateMonth(month: String) {

View file

@ -31,9 +31,20 @@
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/top_value"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/top_value"
android:layout_toLeftOf="@+id/top_new_event"
android:padding="@dimen/activity_margin"
android:src="@drawable/ic_pointer_right"/>
<ImageView
android:id="@+id/top_new_event"
style="@style/ArrowStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/top_value"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/top_value"
android:padding="@dimen/activity_margin"
android:src="@drawable/ic_plus"/>
</merge>