From 53675122c672ad99f57aaedbca27d45e4e818a4c Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Wed, 8 Jan 2020 20:51:06 +0100 Subject: [PATCH] [Dialog/EventDetails] Add feature for saving events in calendar app. --- .../pl/szczodrzynski/edziennik/Extensions.kt | 4 ++ .../ui/dialogs/event/EventDetailsDialog.kt | 48 ++++++++++++++++++- .../main/res/layout/dialog_event_details.xml | 28 +++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt index 3cd469c4..9f218add 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt @@ -105,6 +105,10 @@ fun CharSequence?.isNotNullNorEmpty(): Boolean { return this != null && this.isNotEmpty() } +fun CharSequence?.isNotNullNorBlank(): Boolean { + return this != null && this.isNotBlank() +} + fun currentTimeUnix() = System.currentTimeMillis() / 1000 fun Bundle?.getInt(key: String, defaultValue: Int): Int { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventDetailsDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventDetailsDialog.kt index f5fc8268..b689611d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventDetailsDialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventDetailsDialog.kt @@ -4,6 +4,10 @@ package pl.szczodrzynski.edziennik.ui.dialogs.event +import android.content.Intent +import android.provider.CalendarContract +import android.provider.CalendarContract.Events +import android.view.View import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity @@ -101,6 +105,21 @@ class EventDetailsDialog( Date.fromMillis(event.addedDate).formattedString, event.sharedByName ?: event.teacherFullName ?: "" ) + + b.editButton.visibility = if (event.addedManually) View.VISIBLE else View.GONE + b.editButton.setOnClickListener { + EventManualDialog( + activity, + event.profileId, + editingEvent = event, + onShowListener = onShowListener, + onDismissListener = onDismissListener + ) + } + + b.saveInCalendarButton.setOnClickListener { + openInCalendar() + } } private fun showRemoveEventDialog() { @@ -133,7 +152,7 @@ class EventDetailsDialog( Toast.makeText(activity, "Unshare + remove own event", Toast.LENGTH_SHORT).show() val response = withContext(Dispatchers.Default) { - api.unshareEvent(event!!) + api.unshareEvent(event) } response?.errors?.ifNotEmpty { @@ -165,4 +184,31 @@ class EventDetailsDialog( if (activity is MainActivity && activity.navTargetId == MainActivity.DRAWER_ITEM_AGENDA) activity.reloadTarget() } + + private fun openInCalendar() { launch { + val title = (event.typeName ?: "") + + (if (event.typeName.isNotNullNorBlank() && event.subjectLongName.isNotNullNorBlank()) " - " else " ") + + (event.subjectLongName ?: "") + + val intent = Intent(Intent.ACTION_EDIT).apply { + data = Events.CONTENT_URI + putExtra(Events.TITLE, title) + putExtra(Events.DESCRIPTION, event.topic) + + if (event.startTime == null) { + putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true) + putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, event.eventDate.inMillis) + putExtra(CalendarContract.EXTRA_EVENT_END_TIME, event.eventDate.inMillis) + } else { + val startTime = event.eventDate.combineWith(event.startTime) + val endTime = startTime + 45 * 60 * 1000 /* 45 min */ + + putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, false) + putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime) + putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime) + } + } + + activity.startActivity(intent) + }} } diff --git a/app/src/main/res/layout/dialog_event_details.xml b/app/src/main/res/layout/dialog_event_details.xml index f872d5bb..8532b212 100644 --- a/app/src/main/res/layout/dialog_event_details.xml +++ b/app/src/main/res/layout/dialog_event_details.xml @@ -149,6 +149,34 @@ android:textAppearance="@style/NavView.TextView.Medium" tools:text="Rozdział II: Panowanie Piastów i Jagiellonów.Przeniesiony z 11 grudnia." /> + + + + + +