~williamvds/taskwarrior-android

5bf91c7ab2181f74a052cb82cafdb20dd9fae4c5 — williamvds 5 years ago 2fe7f18
Convert AnnotationDialog to an AppForm
2 files changed, 25 insertions(+), 66 deletions(-)

M app/src/main/java/kvj/taskw/ui/AnnotationDialog.kt
M app/src/main/res/values/strings.xml
M app/src/main/java/kvj/taskw/ui/AnnotationDialog.kt => app/src/main/java/kvj/taskw/ui/AnnotationDialog.kt +22 -66
@@ 1,12 1,10 @@
package kvj.taskw.ui

import android.app.Activity
import android.content.Intent
import java.util.UUID

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.text.TextUtils

import kvj.taskw.App
import kvj.taskw.R


@@ 15,86 13,44 @@ import kvj.taskw.ui.AppActivity.Companion.Style
import kotlinx.android.parcel.Parcelize
import kotlinx.android.synthetic.main.dialog_add_annotation.*

class AnnotationDialog : AppDialog() {
class AnnotationDialog : AppForm<AnnotationDialog.Form>() {
    override val style = Style.DIALOG

    internal var controller = App.controller<Controller>()
    private lateinit var form: Form
    override val layout = R.layout.dialog_add_annotation

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.dialog_add_annotation)

        if (!loadForm(intent.extras))
            loadForm(savedInstanceState)

        ann_cancel_btn.setOnClickListener { doFinish() }
        ann_ok_btn.setOnClickListener { doSave() }
    }

    override fun onBackPressed() = doFinish()

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        saveForm(outState)
        ann_cancel_btn.setOnClickListener { cancel() }
        ann_ok_btn.setOnClickListener { submit() }
    }

    private fun doSave() {
        applyToForm()
        val text = form.annotation

        if (TextUtils.isEmpty(text)) { // Nothing to save
            controller.messageShort("Input is mandatory")
            return
        }

        SaveTask(this).execute(text)
    }

    private fun doFinish() {
        applyToForm()

        if (!TextUtils.isEmpty(form.annotation)) { // Ask for confirmation
            controller.question(this,
                "There are some changes, discard?",
                Runnable { finish() },
                null)
        } else {
            finish()
        }
    override fun submit() {
        super.submit()
        SaveTask(this).execute(data.annotation)
    }

    private fun loadForm(data: Bundle?): Boolean {
        val stored = data?.getParcelable<Form?>(App.KEY_EDIT_DATA)
        stored?.let { form = it }

        ann_text.setText(form.annotation)

        return stored != null
    override fun loadFromForm() {
        ann_text.setText(data.annotation)
    }

    private fun applyToForm() {
        form.annotation = ann_text.text.toString()
    override fun saveToForm() {
        data.annotation = ann_text.text.toString()
    }

    private fun saveForm(data: Bundle) {
        applyToForm()
        data.putParcelable(App.KEY_EDIT_DATA, form)
    }
    override fun hasChanges() = !data.annotation.isNullOrBlank()

    @Parcelize
    data class Form @JvmOverloads constructor(
            val account: String,
            val uuid: UUID,
            var annotation: String? = null
    ) : Parcelable
        val account: String,
        val uuid: UUID,
        var annotation: String? = null
    ) : FormData

    companion object {
        @JvmStatic
        fun start(activity: Activity, form: Form) {
        fun start(activity: Activity, data: Form) {
            val intent = Intent(activity, AnnotationDialog::class.java).apply {
                putExtra(App.KEY_EDIT_DATA, form)
                putExtra(App.KEY_EDIT_DATA, data)
            }

            activity.startActivityForResult(intent, App.ANNOTATE_REQUEST)


@@ 103,8 59,8 @@ class AnnotationDialog : AppDialog() {
        private class SaveTask(activity: AnnotationDialog)
            : StaticAsyncTask<AnnotationDialog, String, Void, String?>(activity) {
            override fun AnnotationDialog.background(vararg params: String): String? {
                val accountController = controller.accountController(form.account)
                return accountController.taskAnnotate(form.uuid!!, params[0])
                val accountController = controller.accountController(data.account)
                return accountController.taskAnnotate(data.uuid, params[0])
            }

            override fun AnnotationDialog.finish(result: String?) {

M app/src/main/res/values/strings.xml => app/src/main/res/values/strings.xml +3 -0
@@ 91,6 91,9 @@
    <string name="edit_task_success">Task edited</string>
    <string name="add_task_success">Task added</string>

    <!-- Form -->
    <string name="form_discard_confirm">There are some changes, discard them?</string>

    <!-- Custom command dialog -->
    <string name="command_prompt_hint">Arguments</string>
    <string name="run">Run</string>