move the new note dialog in separate file

This commit is contained in:
tibbi 2016-11-21 20:38:07 +01:00
parent f10cc1f3d4
commit e025a27c57
2 changed files with 47 additions and 30 deletions

View File

@ -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()
}
}

View File

@ -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()
}
}
}
}
}
}