From de684764424c33597af3e68150ef7b563c0dfd48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Fri, 22 Nov 2019 18:42:45 +0100 Subject: [PATCH] [UI/Event] Add Sync text to manual event dialog. --- .../pl/szczodrzynski/edziennik/Extensions.kt | 8 +++ .../ui/dialogs/event/EventManualV2Dialog.kt | 25 +++++++++ .../edziennik/utils/TextInputKeyboardEdit.kt | 56 +++++++++++++++++++ .../res/layout/dialog_event_manual_v2.xml | 2 +- 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/utils/TextInputKeyboardEdit.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt index 1632a3bf..8a014e1c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt @@ -17,6 +17,7 @@ import android.util.LongSparseArray import android.util.SparseArray import android.util.TypedValue import android.view.View +import android.widget.CompoundButton import android.widget.TextView import androidx.annotation.* import androidx.core.app.ActivityCompat @@ -441,6 +442,13 @@ inline fun T.onClick(crossinline onClickListener: (v: T) -> Unit) { } } +@Suppress("UNCHECKED_CAST") +inline fun T.onChange(crossinline onChangeListener: (v: T, isChecked: Boolean) -> Unit) { + setOnCheckedChangeListener { buttonView, isChecked -> + onChangeListener(buttonView as T, isChecked) + } +} + 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/EventManualV2Dialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/event/EventManualV2Dialog.kt index 527acf15..9418af48 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 @@ -6,6 +6,7 @@ package pl.szczodrzynski.edziennik.ui.dialogs.event import android.graphics.PorterDuff import android.graphics.PorterDuffColorFilter +import android.view.View import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.Observer @@ -86,6 +87,7 @@ class EventManualV2Dialog( defaultType?.let { event.type = it }*/ + b.shareSwitch.isChecked = event.sharedBy != null } b.showMore.onClick { // TODO iconics is broken @@ -99,9 +101,32 @@ class EventManualV2Dialog( } } + updateShareText() + b.shareSwitch.onChange { _, isChecked -> + updateShareText(isChecked) + } + loadLists() }} + private fun updateShareText(checked: Boolean = b.shareSwitch.isChecked) { + val editingShared = editingEvent?.sharedBy != null + val editingOwn = editingEvent?.sharedBy == "self" + + b.shareDetails.visibility = if (checked || editingShared) + View.VISIBLE + else View.GONE + + val text = when { + checked && editingShared && editingOwn -> R.string.dialog_event_manual_share_will_change + checked && editingShared -> R.string.dialog_event_manual_share_will_request + !checked && editingShared -> R.string.dialog_event_manual_share_will_remove + else -> R.string.dialog_event_manual_share_first_notice + } + + b.shareDetails.setText(text) + } + private fun loadLists() { launch { val deferred = async(Dispatchers.Default) { // get the team list diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/TextInputKeyboardEdit.kt b/app/src/main/java/pl/szczodrzynski/edziennik/utils/TextInputKeyboardEdit.kt new file mode 100644 index 00000000..477e0d27 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/TextInputKeyboardEdit.kt @@ -0,0 +1,56 @@ +/* + * Copyright (c) Kuba SzczodrzyƄski 2019-11-22. + */ + +package pl.szczodrzynski.edziennik.utils + +import android.content.Context +import android.graphics.Rect +import android.util.AttributeSet +import android.view.KeyEvent +import android.view.KeyEvent.KEYCODE_BACK +import androidx.annotation.NonNull +import com.google.android.material.textfield.TextInputEditText + +class TextInputKeyboardEdit : TextInputEditText { + + /** + * Keyboard Listener + */ + internal var listener: KeyboardListener? = null + + constructor(context: Context) : super(context) + + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) + + constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) + + override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) { + super.onFocusChanged(focused, direction, previouslyFocusedRect) + if (listener != null) + listener!!.onStateChanged(this, true) + } + + override fun onKeyPreIme(keyCode: Int, @NonNull event: KeyEvent): Boolean { + if (event.keyCode == KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) { + if (listener != null) + listener!!.onStateChanged(this, false) + + // Hide cursor + isFocusable = false + + // Set EditText to be focusable again + isFocusable = true + isFocusableInTouchMode = true + } + return super.onKeyPreIme(keyCode, event) + } + + fun setOnKeyboardListener(listener: KeyboardListener) { + this.listener = listener + } + + interface KeyboardListener { + fun onStateChanged(keyboardEditText: TextInputKeyboardEdit, showing: Boolean) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_event_manual_v2.xml b/app/src/main/res/layout/dialog_event_manual_v2.xml index 82cc6e52..3af87287 100644 --- a/app/src/main/res/layout/dialog_event_manual_v2.xml +++ b/app/src/main/res/layout/dialog_event_manual_v2.xml @@ -117,7 +117,7 @@ android:layout_marginTop="8dp" android:hint="@string/dialog_event_manual_topic"> -