diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectCalendarsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectCalendarsDialog.kt index 87cf212cb..c97004fc2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectCalendarsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectCalendarsDialog.kt @@ -2,8 +2,12 @@ package com.simplemobiletools.calendar.dialogs import android.app.Activity import android.support.v7.app.AlertDialog +import android.support.v7.widget.SwitchCompat +import android.text.TextUtils import android.view.ViewGroup +import android.widget.RelativeLayout import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.extensions.getCalDAVCalendars import com.simplemobiletools.commons.extensions.setupDialogStuff import kotlinx.android.synthetic.main.calendar_item_account.view.* @@ -16,6 +20,7 @@ class SelectCalendarsDialog(val activity: Activity, val callback: (calendars: In var view = (activity.layoutInflater.inflate(R.layout.dialog_select_calendars, null) as ViewGroup) init { + val ids = activity.config.caldavSyncedCalendarIDs.split(",") as ArrayList val calendars = activity.getCalDAVCalendars() val sorted = calendars.sortedWith(compareBy({ it.accountName }, { it.displayName })) sorted.forEach { @@ -24,7 +29,7 @@ class SelectCalendarsDialog(val activity: Activity, val callback: (calendars: In addCalendarItem(false, it.accountName) } - addCalendarItem(true, it.displayName) + addCalendarItem(true, it.displayName, it.id, ids.contains(it.id.toString())) } dialog = AlertDialog.Builder(activity) @@ -35,14 +40,18 @@ class SelectCalendarsDialog(val activity: Activity, val callback: (calendars: In } } - private fun addCalendarItem(isEvent: Boolean, text: String) { + private fun addCalendarItem(isEvent: Boolean, text: String, tag: Long = 0L, shouldCheck: Boolean = false) { val calendarItem = activity.layoutInflater.inflate(if (isEvent) R.layout.calendar_item_calendar else R.layout.calendar_item_account, view.dialog_select_calendars_holder, false) if (isEvent) { - calendarItem.calendar_item_calendar_switch.text = text - calendarItem.setOnClickListener { - calendarItem.calendar_item_calendar_switch.toggle() + calendarItem.calendar_item_calendar_switch.apply { + this.tag = tag + this.text = text + isChecked = shouldCheck + calendarItem.setOnClickListener { + toggle() + } } } else { calendarItem.calendar_item_account.text = text @@ -52,6 +61,18 @@ class SelectCalendarsDialog(val activity: Activity, val callback: (calendars: In } private fun confirmSelection() { + val calendarIDs = ArrayList() + val childCnt = view.dialog_select_calendars_holder.childCount + for (i in 0..childCnt) { + val child = view.dialog_select_calendars_holder.getChildAt(i) + if (child is RelativeLayout) { + val check = child.getChildAt(0) + if (check is SwitchCompat && check.isChecked) { + calendarIDs.add(check.tag as Long) + } + } + } + activity.config.caldavSyncedCalendarIDs = TextUtils.join(",", calendarIDs) } } 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 fc3751767..1dbfac987 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt @@ -74,6 +74,10 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getBoolean(CALDAV_SYNC, false) set(caldavSync) = prefs.edit().putBoolean(CALDAV_SYNC, caldavSync).apply() + var caldavSyncedCalendarIDs: String + get() = prefs.getString(CALDAV_SYNCED_CALENDAR_IDS, "") + set(calendarIDs) = prefs.edit().putString(CALDAV_SYNCED_CALENDAR_IDS, calendarIDs).apply() + fun addDisplayEventType(type: String) { addDisplayEventTypes(HashSet(Arrays.asList(type))) } 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 dd6a081ed..cdd6e4698 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt @@ -42,6 +42,7 @@ val REMINDER_MINUTES = "reminder_minutes" 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 SYNC_ACCOUNT_NAME = "sync_account_name" val SNOOZE_DELAY = "snooze_delay" val DISPLAY_PAST_EVENTS = "display_past_events"