From e025a27c57309a9d7c1a6f150a3cbe87385cde17 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 21 Nov 2016 20:38:07 +0100 Subject: [PATCH] move the new note dialog in separate file --- .../notes/activities/MainActivity.kt | 38 ++++-------------- .../notes/dialogs/NewNoteDialog.kt | 39 +++++++++++++++++++ 2 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/notes/dialogs/NewNoteDialog.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt index 2eec3089..73160e42 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt @@ -10,14 +10,13 @@ import android.util.TypedValue import android.view.Menu import android.view.MenuItem import android.view.View -import android.view.WindowManager import android.view.inputmethod.InputMethodManager -import android.widget.EditText import com.simplemobiletools.notes.MyWidgetProvider import com.simplemobiletools.notes.R import com.simplemobiletools.notes.TYPE_NOTE import com.simplemobiletools.notes.Utils import com.simplemobiletools.notes.databases.DBHelper +import com.simplemobiletools.notes.dialogs.NewNoteDialog import com.simplemobiletools.notes.dialogs.OpenNoteDialog import com.simplemobiletools.notes.dialogs.WidgetNoteDialog import com.simplemobiletools.notes.extensions.toast @@ -130,34 +129,13 @@ class MainActivity : SimpleActivity(), OpenNoteDialog.OpenNoteListener { } fun displayNewNoteDialog() { - val newNoteView = layoutInflater.inflate(R.layout.new_note, null) - - AlertDialog.Builder(this).apply { - setTitle(resources.getString(R.string.new_note)) - setView(newNoteView) - setPositiveButton(R.string.ok, null) - setNegativeButton(R.string.cancel, null) - create().apply { - window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) - show() - getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { - val titleET = newNoteView.findViewById(R.id.note_name) as EditText - val title = titleET.value - if (title.isEmpty()) { - toast(R.string.no_title) - } else if (mDb.doesTitleExist(title)) { - toast(R.string.title_taken) - } else { - saveText() - val newNote = Note(0, title, "", TYPE_NOTE) - val id = mDb.insertNote(newNote) - updateSelectedNote(id) - dismiss() - mNotes = mDb.getNotes() - invalidateOptionsMenu() - } - } - } + NewNoteDialog(this, mDb) { + saveText() + val newNote = Note(0, it, "", TYPE_NOTE) + val id = mDb.insertNote(newNote) + updateSelectedNote(id) + mNotes = mDb.getNotes() + invalidateOptionsMenu() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/dialogs/NewNoteDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/dialogs/NewNoteDialog.kt new file mode 100644 index 00000000..d7745f8b --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/dialogs/NewNoteDialog.kt @@ -0,0 +1,39 @@ +package com.simplemobiletools.notes.dialogs + +import android.app.Activity +import android.app.AlertDialog +import android.view.WindowManager +import com.simplemobiletools.notes.R +import com.simplemobiletools.notes.databases.DBHelper +import com.simplemobiletools.notes.extensions.toast +import com.simplemobiletools.notes.extensions.value +import kotlinx.android.synthetic.main.new_note.view.* + +class NewNoteDialog(val activity: Activity, val db: DBHelper, callback: (title: String) -> Unit) { + + init { + val view = activity.layoutInflater.inflate(R.layout.new_note, null) + + AlertDialog.Builder(activity).apply { + setTitle(activity.resources.getString(R.string.new_note)) + setView(view) + setPositiveButton(R.string.ok, null) + setNegativeButton(R.string.cancel, null) + create().apply { + window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) + show() + getButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE).setOnClickListener { + val title = view.note_name.value + if (title.isEmpty()) { + activity.toast(R.string.no_title) + } else if (db.doesTitleExist(title)) { + activity.toast(R.string.title_taken) + } else { + callback.invoke(title) + dismiss() + } + } + } + } + } +}