From ee7367446816ca2ee4eef33da79a684e91c32ddf Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 13 Feb 2019 22:22:26 +0100 Subject: [PATCH] avoid showing unwritable calendars in some places --- .../calendar/pro/activities/EventActivity.kt | 4 ++-- .../calendar/pro/activities/MainActivity.kt | 2 +- .../pro/activities/ManageEventTypesActivity.kt | 2 +- .../calendar/pro/activities/SettingsActivity.kt | 2 +- .../calendar/pro/dialogs/ExportEventsDialog.kt | 2 +- .../calendar/pro/dialogs/FilterEventTypesDialog.kt | 2 +- .../calendar/pro/dialogs/ImportEventsDialog.kt | 2 +- .../calendar/pro/dialogs/SelectEventTypeDialog.kt | 4 ++-- .../calendar/pro/fragments/WeekFragment.kt | 2 +- .../calendar/pro/helpers/EventsHelper.kt | 12 ++++++++++-- 10 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index 87a82e3b5..d85dd9d3b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -548,7 +548,7 @@ class EventActivity : SimpleActivity() { private fun showEventTypeDialog() { hideKeyboard() - SelectEventTypeDialog(this, mEventTypeId, false, true, false) { + SelectEventTypeDialog(this, mEventTypeId, false, true, false, true) { mEventTypeId = it.id!! updateEventType() } @@ -613,7 +613,7 @@ class EventActivity : SimpleActivity() { event_caldav_calendar_divider.beVisible() val calendars = calDAVHelper.getCalDAVCalendars("", true).filter { - config.getSyncedCalendarIdsAsList().contains(it.id) + it.canWrite() && config.getSyncedCalendarIdsAsList().contains(it.id) } updateCurrentCalendarInfo(if (mEventCalendarId == STORED_LOCALLY_ONLY) null else getCalendarWithId(calendars, getCalendarId())) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt index 0dd684540..e48a0e349 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt @@ -105,7 +105,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { updateViewPager() } - eventsHelper.getEventTypes(this) { + eventsHelper.getEventTypes(this, false) { val newShouldFilterBeVisible = it.size > 1 || config.displayEventTypes.isEmpty() if (newShouldFilterBeVisible != mShouldFilterBeVisible) { mShouldFilterBeVisible = newShouldFilterBeVisible diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/ManageEventTypesActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/ManageEventTypesActivity.kt index 02e9a9983..ba6c42b89 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/ManageEventTypesActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/ManageEventTypesActivity.kt @@ -30,7 +30,7 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteEventTypesListener { } private fun getEventTypes() { - eventsHelper.getEventTypes(this) { + eventsHelper.getEventTypes(this, false) { val adapter = ManageEventTypesAdapter(this, it, this, manage_event_types_list) { showEventTypeDialog(it as EventType) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt index f348cdf11..0f18612d9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt @@ -617,7 +617,7 @@ class SettingsActivity : SimpleActivity() { updateDefaultEventTypeText() settings_default_event_type.text = getString(R.string.last_used_one) settings_default_event_type_holder.setOnClickListener { - SelectEventTypeDialog(this, config.defaultEventTypeId, true, false, true) { + SelectEventTypeDialog(this, config.defaultEventTypeId, true, false, true, true) { config.defaultEventTypeId = it.id!! updateDefaultEventTypeText() } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ExportEventsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ExportEventsDialog.kt index cbfd56dd0..f7aace260 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ExportEventsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ExportEventsDialog.kt @@ -19,7 +19,7 @@ class ExportEventsDialog(val activity: SimpleActivity, val path: String, val cal export_events_folder.text = activity.humanizePath(path) export_events_filename.setText("${activity.getString(R.string.events)}_${activity.getCurrentFormattedDateTime()}") - activity.eventsHelper.getEventTypes(activity) { + activity.eventsHelper.getEventTypes(activity, false) { val eventTypes = HashSet() it.mapTo(eventTypes) { it.id.toString() } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/FilterEventTypesDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/FilterEventTypesDialog.kt index ddbaa0318..63ba012fc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/FilterEventTypesDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/FilterEventTypesDialog.kt @@ -14,7 +14,7 @@ class FilterEventTypesDialog(val activity: SimpleActivity, val callback: () -> U private val view = activity.layoutInflater.inflate(R.layout.dialog_filter_event_types, null) init { - activity.eventsHelper.getEventTypes(activity) { + activity.eventsHelper.getEventTypes(activity, false) { val displayEventTypes = activity.config.displayEventTypes view.filter_event_types_list.adapter = FilterEventTypeAdapter(activity, it, displayEventTypes) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt index c6f11c8c5..f4ff29e81 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt @@ -49,7 +49,7 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal val view = (activity.layoutInflater.inflate(R.layout.dialog_import_events, null) as ViewGroup).apply { updateEventType(this) import_event_type_holder.setOnClickListener { - SelectEventTypeDialog(activity, currEventTypeId, true, true, false) { + SelectEventTypeDialog(activity, currEventTypeId, true, true, false, true) { currEventTypeId = it.id!! currEventTypeCalDAVCalendarId = it.caldavCalendarId diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeDialog.kt index 4e17a356b..3af5d5462 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeDialog.kt @@ -19,7 +19,7 @@ import kotlinx.android.synthetic.main.radio_button_with_color.view.* import java.util.* class SelectEventTypeDialog(val activity: Activity, val currEventType: Long, val showCalDAVCalendars: Boolean, val showNewEventTypeOption: Boolean, - val addLastUsedOneAsFirstOption: Boolean, val callback: (eventType: EventType) -> Unit) { + val addLastUsedOneAsFirstOption: Boolean, val showOnlyWritable: Boolean, val callback: (eventType: EventType) -> Unit) { private val NEW_EVENT_TYPE_ID = -2L private val LAST_USED_EVENT_TYPE_ID = -1L @@ -32,7 +32,7 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Long, val val view = activity.layoutInflater.inflate(R.layout.dialog_select_radio_group, null) as ViewGroup radioGroup = view.dialog_radio_group - activity.eventsHelper.getEventTypes(activity) { + activity.eventsHelper.getEventTypes(activity, showOnlyWritable) { eventTypes = it activity.runOnUiThread { if (addLastUsedOneAsFirstOption) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt index ddf6a4ec4..02ab3cbea 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt @@ -67,7 +67,7 @@ class WeekFragment : Fragment(), WeeklyCalendar { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - context!!.eventsHelper.getEventTypes(activity!!) { + context!!.eventsHelper.getEventTypes(activity!!, false) { it.map { eventTypeColors.put(it.id!!, it.color) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index 9bd2d4d8a..52aa5af7f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -13,9 +13,17 @@ class EventsHelper(val context: Context) { private val eventsDB = context.eventsDB private val eventTypesDB = context.eventTypesDB - fun getEventTypes(activity: Activity, callback: (notes: ArrayList) -> Unit) { + fun getEventTypes(activity: Activity, showWritableOnly: Boolean, callback: (notes: ArrayList) -> Unit) { Thread { - val eventTypes = eventTypesDB.getEventTypes().toMutableList() as ArrayList + var eventTypes = eventTypesDB.getEventTypes().toMutableList() as ArrayList + if (showWritableOnly) { + val caldavCalendars = activity.calDAVHelper.getCalDAVCalendars("", true) + eventTypes = eventTypes.filter { + val eventType = it + it.caldavCalendarId == 0 || caldavCalendars.firstOrNull { it.id == eventType.caldavCalendarId }?.canWrite() == true + }.toMutableList() as ArrayList + } + activity.runOnUiThread { callback(eventTypes) }