mirror of
https://github.com/Xevion/simple-todo.git
synced 2025-12-05 23:16:22 -06:00
Add double-tap to edit task functionality via custom AlertDialog
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package com.embers.simpleto_do
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.app.Dialog
|
||||
import android.content.DialogInterface
|
||||
import android.os.Bundle
|
||||
import android.text.InputType
|
||||
import android.util.Log
|
||||
import android.widget.EditText
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import java.lang.IllegalStateException
|
||||
|
||||
class EditTaskDialogFragment(private val dismissListener: OnDismissListener) : DialogFragment() {
|
||||
private val TAG: String = this::class.java.name
|
||||
|
||||
interface OnDismissListener {
|
||||
fun onDismiss(text: String, submitted: Boolean)
|
||||
}
|
||||
|
||||
private var text: String = ""
|
||||
private var submitted: Boolean = false
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
return activity?.let {
|
||||
val input = EditText(it)
|
||||
input.inputType = InputType.TYPE_CLASS_TEXT
|
||||
|
||||
val builder = AlertDialog.Builder(it)
|
||||
builder.setMessage(R.string.dialog_edit_task)
|
||||
.setView(input)
|
||||
.setNegativeButton(R.string.dialog_negative) {_, _ ->}
|
||||
.setPositiveButton(R.string.dialog_positive) {_, _ -> text = input.text.toString(); submitted = true}
|
||||
|
||||
builder.create()
|
||||
} ?: throw IllegalStateException("Activity cannot be null")
|
||||
}
|
||||
|
||||
override fun onDismiss(dialog: DialogInterface) {
|
||||
super.onDismiss(dialog)
|
||||
|
||||
dismissListener.onDismiss(text, submitted)
|
||||
}
|
||||
}
|
||||
@@ -73,6 +73,24 @@ class MainActivity : AppCompatActivity() {
|
||||
adapter.notifyItemRemoved(position)
|
||||
saveData()
|
||||
}
|
||||
|
||||
override fun onDoubleClick(position: Int) {
|
||||
// When double clicked and 'submit' is hit in the dialog, the text entered will be
|
||||
val dismissListener = object : EditTaskDialogFragment.OnDismissListener {
|
||||
override fun onDismiss(text: String, submitted: Boolean) {
|
||||
// If the dialog was submitted
|
||||
if (submitted) {
|
||||
// Update data, inform adapter, save to file
|
||||
taskList[position] = text
|
||||
adapter.notifyItemChanged(position)
|
||||
saveData()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val editTaskDialog = EditTaskDialogFragment(dismissListener)
|
||||
editTaskDialog.show(supportFragmentManager, "edit_task")
|
||||
}
|
||||
}
|
||||
|
||||
// Setup RecyclerView
|
||||
|
||||
@@ -2,4 +2,7 @@
|
||||
<string name="app_name">Simple To-do</string>
|
||||
<string name="button_text">Add</string>
|
||||
<string name="textbox_labelfor">Name of new To-do list item</string>
|
||||
<string name="dialog_edit_task">Edit the name of this to-do task</string>
|
||||
<string name="dialog_positive">SUBMIT</string>
|
||||
<string name="dialog_negative">CANCEL</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user