[Homework] Add mark as done confirmation dialog. Refactor code a bit.

This commit is contained in:
Kuba Szczodrzyński 2020-03-31 09:06:32 +02:00
parent 93333a8c48
commit 949a68ec1d
5 changed files with 68 additions and 80 deletions

View File

@ -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 : CompoundButton> T.onChange(crossinline onChangeListener: (v: T,
}
}
@Suppress("UNCHECKED_CAST")
inline fun <T : MaterialButton> 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 <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner, observer: Observer<T>) {
observe(lifecycleOwner, object : Observer<T> {
override fun onChanged(t: T?) {

View File

@ -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) {

View File

@ -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

View File

@ -163,6 +163,18 @@
android:layout_gravity="end"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/checkDoneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:minWidth="0dp"
android:checkable="true"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="\uFCE1"
android:textSize="20sp"
android:fontFamily="@font/community_material_font_v3_5_95_1" />
<com.google.android.material.button.MaterialButton
android:id="@+id/editButton"
android:layout_width="wrap_content"
@ -195,18 +207,6 @@
android:text="\uFCDA"
android:textSize="20sp"
android:fontFamily="@font/community_material_font_v3_5_95_1" />
<com.google.android.material.button.MaterialButton
android:id="@+id/checkDoneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:minWidth="0dp"
android:checkable="true"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="\uFCE1"
android:textSize="20sp"
android:fontFamily="@font/community_material_font_v3_5_95_1" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>

View File

@ -1281,4 +1281,6 @@
<string name="menu_template">Template</string>
<string name="messages_attachment_download_again">Pobierz ponownie</string>
<string name="hint_mark_as_done">Oznacz jako wykonane</string>
<string name="event_mark_as_done_title">Oznacz jako wykonane</string>
<string name="event_mark_as_done_text">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.</string>
</resources>