From 20b6578225453e156c24cf41726082ca79af7865 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 17 Aug 2017 15:42:58 +0200 Subject: [PATCH] add a calendar picker to the event creation activity --- .../calendar/activities/EventActivity.kt | 3 + .../dialogs/SelectEventCalendarDialog.kt | 66 +++++++++++++++++++ .../calendar/helpers/Config.kt | 4 ++ .../calendar/helpers/Constants.kt | 1 + 4 files changed, 74 insertions(+) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectEventCalendarDialog.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt index 6e7c95311..9afb30792 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt @@ -390,6 +390,9 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener { event_caldav_calendar_divider.beVisible() event_caldav_calendar_holder.setOnClickListener { val calendars = CalDAVEventsHandler(applicationContext).getCalDAVCalendars().filter { it.canWrite() } + SelectEventCalendarDialog(this, calendars, config.lastUsedCaldavCalendar) { + config.lastUsedCaldavCalendar = it + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectEventCalendarDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectEventCalendarDialog.kt new file mode 100644 index 000000000..af35c4668 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectEventCalendarDialog.kt @@ -0,0 +1,66 @@ +package com.simplemobiletools.calendar.dialogs + +import android.app.Activity +import android.graphics.Color +import android.support.v7.app.AlertDialog +import android.view.ViewGroup +import android.widget.RadioButton +import android.widget.RadioGroup +import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.extensions.config +import com.simplemobiletools.calendar.extensions.dbHelper +import com.simplemobiletools.calendar.models.CalDAVCalendar +import com.simplemobiletools.commons.extensions.setBackgroundWithStroke +import com.simplemobiletools.commons.extensions.setupDialogStuff +import com.simplemobiletools.commons.extensions.updateTextColors +import kotlinx.android.synthetic.main.dialog_select_radio_group.view.* +import kotlinx.android.synthetic.main.radio_button_with_color.view.* + +class SelectEventCalendarDialog(val activity: Activity, val calendars: List, val currCalendarId: Int, val callback: (id: Int) -> Unit) { + val dialog: AlertDialog? + val radioGroup: RadioGroup + var wasInit = false + + init { + val view = activity.layoutInflater.inflate(R.layout.dialog_select_radio_group, null) as ViewGroup + radioGroup = view.dialog_radio_group + + activity.dbHelper.getEventTypes { + activity.runOnUiThread { + calendars.forEach { + addRadioButton(it.displayName, it.id.toInt(), it.color) + } + wasInit = true + activity.updateTextColors(view.dialog_radio_holder) + } + } + + dialog = AlertDialog.Builder(activity) + .create().apply { + activity.setupDialogStuff(view, this) + } + } + + private fun addRadioButton(title: String, typeId: Int, color: Int) { + val view = activity.layoutInflater.inflate(R.layout.radio_button_with_color, null) + (view.dialog_radio_button as RadioButton).apply { + text = title + isChecked = typeId == currCalendarId + id = typeId + } + + if (color != Color.TRANSPARENT) + view.dialog_radio_color.setBackgroundWithStroke(color, activity.config.backgroundColor) + + view.setOnClickListener { viewClicked(typeId) } + radioGroup.addView(view, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) + } + + fun viewClicked(typeId: Int) { + if (!wasInit) + return + + callback.invoke(typeId) + dialog?.dismiss() + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt index 4c1fce4c0..d34e0f259 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt @@ -78,6 +78,10 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getString(CALDAV_SYNCED_CALENDAR_IDS, "") set(calendarIDs) = prefs.edit().putString(CALDAV_SYNCED_CALENDAR_IDS, calendarIDs).apply() + var lastUsedCaldavCalendar: Int + get() = prefs.getInt(LAST_USED_CALDAV_CALENDAR, 1) + set(calendarId) = prefs.edit().putInt(LAST_USED_CALDAV_CALENDAR, calendarId).apply() + fun getCalendarIdsAsList() = caldavSyncedCalendarIDs.split(",").filter { it.trim().isNotEmpty() } as ArrayList fun addDisplayEventType(type: String) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt index d10ec0550..562cbdcc6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt @@ -44,6 +44,7 @@ val DISPLAY_EVENT_TYPES = "display_event_types" val FONT_SIZE = "font_size" val CALDAV_SYNC = "caldav_sync" val CALDAV_SYNCED_CALENDAR_IDS = "caldav_synced_calendar_ids" +val LAST_USED_CALDAV_CALENDAR = "last_used_caldav_calendar" val SNOOZE_DELAY = "snooze_delay" val DISPLAY_PAST_EVENTS = "display_past_events"