From a36fb09bc3a72d7c50a9ab73ed00b69d4be93ec0 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Thu, 28 Nov 2019 15:11:23 +0100 Subject: [PATCH] [Dialog/Event] Add event adding in the new event manual dialog. --- .../pl/szczodrzynski/edziennik/Extensions.kt | 7 ++ .../ui/dialogs/event/EventManualV2Dialog.kt | 105 ++++++++++++++++-- .../dialogs/timetable/LessonDetailsDialog.kt | 2 +- 3 files changed, 104 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt index 0334cde6..5720b618 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt @@ -587,3 +587,10 @@ fun Context.timeLeft(time: Int, delimiter: String = " "): String { return parts.joinToString(delimiter) { resources.getQuantityString(it.first, it.second, it.second) } } + +inline fun Any?.instanceOfOrNull(): T? { + return when (this) { + is T -> this + else -> null + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualV2Dialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualV2Dialog.kt index 9418af48..889857f7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualV2Dialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualV2Dialog.kt @@ -7,7 +7,9 @@ package pl.szczodrzynski.edziennik.ui.dialogs.event import android.graphics.PorterDuff import android.graphics.PorterDuffColorFilter import android.view.View +import android.widget.Toast import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AlertDialog.BUTTON_POSITIVE import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.Observer import com.google.android.material.datepicker.MaterialDatePicker @@ -18,7 +20,9 @@ import kotlinx.coroutines.* import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.data.db.modules.events.Event import pl.szczodrzynski.edziennik.data.db.modules.events.EventType +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject +import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher import pl.szczodrzynski.edziennik.data.db.modules.teams.Team import pl.szczodrzynski.edziennik.data.db.modules.timetable.Lesson import pl.szczodrzynski.edziennik.data.db.modules.timetable.LessonFull @@ -68,11 +72,21 @@ class EventManualV2Dialog( .setTitle(R.string.dialog_event_manual_title) .setView(b.root) .setNegativeButton(R.string.cancel) { dialog, _ -> dialog.dismiss() } - .setPositiveButton(R.string.save) { _, _ -> saveEvent() } + .setPositiveButton(R.string.save, null) .setOnDismissListener { onDismissListener?.invoke(TAG) } - .show() + .create() + .apply { + setOnShowListener { dialog -> + val positiveButton = (dialog as AlertDialog).getButton(BUTTON_POSITIVE) + positiveButton.setOnClickListener { + saveEvent() + } + } + + show() + } event = editingEvent?.clone() ?: Event().also { event -> event.profileId = profileId @@ -442,17 +456,16 @@ class EventManualV2Dialog( // attach a listener to time dropdown b.timeDropdown.setOnChangeListener { item -> - when { + when (item.id) { // no lessons this day - item.id == -2L -> { + -2L -> { b.timeDropdown.deselect() return@setOnChangeListener false } - // custom start hour - item.id == -1L -> { - return@setOnChangeListener false - } + // custom start hour + -1L -> return@setOnChangeListener false + // selected a specific lesson else -> { if (item.tag is LessonFull) { @@ -461,7 +474,7 @@ class EventManualV2Dialog( b.teamDropdown.deselect() b.subjectDropdown.deselect() b.teacherDropdown.deselect() - item.tag.displayTeamId?.let { + item.tag.displayTeamId?.let { b.teamDropdown.select(it) } item.tag.displaySubjectId?.let { @@ -479,6 +492,80 @@ class EventManualV2Dialog( } private fun saveEvent() { + val date = b.dateDropdown.selected?.tag.instanceOfOrNull() + val lesson = b.timeDropdown.selected?.tag.instanceOfOrNull() + val team = b.teamDropdown.selected?.tag.instanceOfOrNull() + val share = b.shareSwitch.isChecked + val type = b.typeDropdown.selected?.tag.instanceOfOrNull() + val topic = b.topic.text?.toString() + val subject = b.subjectDropdown.selected?.tag.instanceOfOrNull() + val teacher = b.teacherDropdown.selected?.tag.instanceOfOrNull() + b.teamDropdown.error = null + b.typeDropdown.error = null + b.topic.error = null + + var isError = false + + if (share && team == null) { + b.teamDropdown.error = app.getString(R.string.dialog_event_manual_team_choose) + isError = true + } + + if (type == null) { + b.typeDropdown.error = app.getString(R.string.dialog_event_manual_type_choose) + isError = true + } + + if (topic.isNullOrBlank()) { + b.topic.error = app.getString(R.string.dialog_event_manual_topic_choose) + isError = true + } + + if (isError) return + + val id = System.currentTimeMillis() + + val eventObject = Event( + profileId, + id, + date, + lesson?.displayStartTime, + topic, + customColor ?: -1, + type?.id?.toInt() ?: Event.TYPE_DEFAULT, + true, + teacher?.id ?: -1, + subject?.id ?: -1, + team?.id ?: -1 + ) + + val metadataObject = Metadata( + profileId, + when (type?.id?.toInt()) { + Event.TYPE_HOMEWORK -> Metadata.TYPE_HOMEWORK + else -> Metadata.TYPE_EVENT + }, + eventObject.id, + true, + true, + System.currentTimeMillis() + ) + + finishAdding(eventObject, metadataObject) + } + + private fun finishAdding(eventObject: Event, metadataObject: Metadata) { + launch { + val deferred = async(Dispatchers.Default) { + app.db.eventDao().add(eventObject) + app.db.metadataDao().add(metadataObject) + } + + deferred.await() + } + + dialog.dismiss() + Toast.makeText(app, R.string.saved, Toast.LENGTH_SHORT).show() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/timetable/LessonDetailsDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/timetable/LessonDetailsDialog.kt index 46464fc1..9351d3e3 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/timetable/LessonDetailsDialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/timetable/LessonDetailsDialog.kt @@ -42,7 +42,7 @@ class LessonDetailsDialog( .setPositiveButton(R.string.close) { dialog, _ -> dialog.dismiss() } - .setNeutralButton(R.string.add) { dialog, _ -> + .setNeutralButton(R.string.add) { _, _ -> EventManualV2Dialog( activity, lesson.profileId,