From 949a68ec1dd6c216c8b256f4d53cac480b308a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Tue, 31 Mar 2020 09:06:32 +0200 Subject: [PATCH] [Homework] Add mark as done confirmation dialog. Refactor code a bit. --- .../pl/szczodrzynski/edziennik/Extensions.kt | 18 +++-- .../ui/dialogs/event/EventDetailsDialog.kt | 67 +++++++++++-------- .../ui/dialogs/event/EventListAdapter.kt | 37 +--------- .../main/res/layout/dialog_event_details.xml | 24 +++---- app/src/main/res/values/strings.xml | 2 + 5 files changed, 68 insertions(+), 80 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt index 311513f4..ed9b58c5 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt @@ -27,10 +27,7 @@ import android.util.Base64.NO_WRAP import android.util.Base64.encodeToString import android.view.View import android.view.WindowManager -import android.widget.CheckBox -import android.widget.CompoundButton -import android.widget.RadioButton -import android.widget.TextView +import android.widget.* import androidx.annotation.* import androidx.core.app.ActivityCompat import androidx.core.database.getIntOrNull @@ -44,6 +41,7 @@ import androidx.recyclerview.widget.RecyclerView import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import androidx.viewpager.widget.ViewPager import com.google.android.gms.security.ProviderInstaller +import com.google.android.material.button.MaterialButton import com.google.gson.JsonArray import com.google.gson.JsonElement import com.google.gson.JsonObject @@ -743,6 +741,18 @@ inline fun T.onChange(crossinline onChangeListener: (v: T, } } +@Suppress("UNCHECKED_CAST") +inline fun T.onChange(crossinline onChangeListener: (v: T, isChecked: Boolean) -> Unit) { + addOnCheckedChangeListener { buttonView, isChecked -> + onChangeListener(buttonView as T, isChecked) + } +} + +fun View.attachToastHint(stringRes: Int) = onLongClick { + Toast.makeText(it.context, stringRes, Toast.LENGTH_SHORT).show() + true +} + fun LiveData.observeOnce(lifecycleOwner: LifecycleOwner, observer: Observer) { observe(lifecycleOwner, object : Observer { override fun onChanged(t: T?) { 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 f2819dd1..703f4ea3 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 @@ -112,6 +112,35 @@ class EventDetailsDialog( event.sharedByName ?: event.teacherName ?: "" ) + + // MARK AS DONE + b.checkDoneButton.isChecked = event.isDone + b.checkDoneButton.onChange { _, isChecked -> + if (isChecked && !event.isDone) { + b.checkDoneButton.isChecked = false + MaterialAlertDialogBuilder(activity) + .setTitle(R.string.event_mark_as_done_title) + .setMessage(R.string.event_mark_as_done_text) + .setPositiveButton(R.string.ok) { _, _ -> + event.isDone = isChecked + launch(Dispatchers.Default) { + app.db.eventDao().replace(event) + } + b.checkDoneButton.isChecked = true + } + .setNegativeButton(R.string.cancel, null) + .show() + } + else if (!isChecked && event.isDone) { + event.isDone = isChecked + launch(Dispatchers.Default) { + app.db.eventDao().replace(event) + } + } + } + b.checkDoneButton.attachToastHint(R.string.hint_mark_as_done) + + // EDIT EVENT b.editButton.visibility = if (event.addedManually) View.VISIBLE else View.GONE b.editButton.setOnClickListener { EventManualDialog( @@ -122,8 +151,16 @@ class EventDetailsDialog( onDismissListener = onDismissListener ) } + b.editButton.attachToastHint(R.string.hint_edit_event) - b.goToTimetableButton.setOnClickListener { + // SAVE IN CALENDAR + b.saveInCalendarButton.onClick { + openInCalendar() + } + b.saveInCalendarButton.attachToastHint(R.string.hint_save_in_calendar) + + // GO TO TIMETABLE + b.goToTimetableButton.onClick { dialog.dismiss() val dateStr = event.date.stringY_m_d @@ -144,34 +181,8 @@ class EventDetailsDialog( else activity.startActivity(intent) } - b.saveInCalendarButton.setOnClickListener { - openInCalendar() - } + b.goToTimetableButton.attachToastHint(R.string.hint_go_to_timetable) - b.checkDoneButton.setOnLongClickListener { - Toast.makeText(activity, R.string.hint_mark_as_done, Toast.LENGTH_SHORT).show() - true - } - b.goToTimetableButton.setOnLongClickListener { - Toast.makeText(activity, R.string.hint_go_to_timetable, Toast.LENGTH_SHORT).show() - true - } - b.saveInCalendarButton.setOnLongClickListener { - Toast.makeText(activity, R.string.hint_save_in_calendar, Toast.LENGTH_SHORT).show() - true - } - b.editButton.setOnLongClickListener { - Toast.makeText(activity, R.string.hint_edit_event, Toast.LENGTH_SHORT).show() - true - } - - b.checkDoneButton.isChecked = event.isDone - b.checkDoneButton.addOnCheckedChangeListener { _, isChecked -> - event.isDone = isChecked - launch(Dispatchers.Default) { - app.db.eventDao().replace(event) - } - } b.topic.text = event.topic BetterLink.attach(b.topic) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventListAdapter.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventListAdapter.kt index 9c5fb456..cf63e4ea 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventListAdapter.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventListAdapter.kt @@ -7,7 +7,6 @@ package pl.szczodrzynski.edziennik.ui.dialogs.event import android.content.Context import android.view.LayoutInflater import android.view.ViewGroup -import android.widget.Toast import androidx.core.view.isVisible import androidx.recyclerview.widget.RecyclerView import pl.szczodrzynski.edziennik.* @@ -83,43 +82,9 @@ class EventListAdapter( b.editButton.onClick { onEventEditClick?.invoke(event) } + b.editButton.attachToastHint(R.string.hint_edit_event) b.isDone.isVisible = event.isDone - - b.editButton.setOnLongClickListener { - Toast.makeText(context, R.string.hint_edit_event, Toast.LENGTH_SHORT).show() - true - } - - /*with(holder) { - b.eventListItemRoot.background.colorFilter = when (event.type) { - Event.TYPE_HOMEWORK -> PorterDuffColorFilter(0xffffffff.toInt(), PorterDuff.Mode.CLEAR) - else -> PorterDuffColorFilter(event.color, PorterDuff.Mode.MULTIPLY) - } - - b.eventListItemStartTime.text = if (event.startTime == null) app.getString(R.string.event_all_day) else event.startTime?.stringHM - b.eventListItemTeamName.text = bs(event.teamName) - b.eventListItemTeacherName.text = app.getString(R.string.concat_2_strings, bs(null, event.teacherFullName, "\n"), bs(event.subjectLongName)) - b.eventListItemAddedDate.text = Date.fromMillis(event.addedDate).formattedStringShort - b.eventListItemType.text = event.typeName - b.eventListItemTopic.text = event.topic - b.eventListItemHomework.visibility = if (event.type == Event.TYPE_HOMEWORK) View.VISIBLE else View.GONE - b.eventListItemSharedBy.text = app.getString(R.string.event_shared_by_format, if (event.sharedBy == "self") app.getString(R.string.event_shared_by_self) else event.sharedByName) - b.eventListItemSharedBy.visibility = if (event.sharedByName.isNullOrBlank()) View.GONE else View.VISIBLE - - b.eventListItemEdit.visibility = if (event.addedManually) View.VISIBLE else View.GONE - b.eventListItemEdit.setOnClickListener { - parentDialog.dismiss() - - EventManualDialog( - context as MainActivity, - event.profileId, - editingEvent = event, - onShowListener = parentDialog.onShowListener, - onDismissListener = parentDialog.onDismissListener - ) - } - }*/ } override fun getItemCount() = items.size diff --git a/app/src/main/res/layout/dialog_event_details.xml b/app/src/main/res/layout/dialog_event_details.xml index d06c71bb..913f2704 100644 --- a/app/src/main/res/layout/dialog_event_details.xml +++ b/app/src/main/res/layout/dialog_event_details.xml @@ -163,6 +163,18 @@ android:layout_gravity="end" android:orientation="horizontal"> + + - - diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b0cc74fd..5cd4353c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1281,4 +1281,6 @@ Template Pobierz ponownie Oznacz jako wykonane + Oznacz jako wykonane + Czy chcesz oznaczyć to zadanie jako wykonane?\n\nNie będzie ono się wyświetlać na stronie głównej oraz w aktualnych zadaniach domowych. Będzie wciąż dostępne w Terminarzu.