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 b725aabf..d962dbec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt @@ -16,6 +16,8 @@ import com.simplemobiletools.notes.Utils import com.simplemobiletools.notes.databases.DBHelper import com.simplemobiletools.notes.dialogs.OpenNoteDialog import com.simplemobiletools.notes.dialogs.WidgetNoteDialog +import com.simplemobiletools.notes.extensions.toast +import com.simplemobiletools.notes.extensions.value import com.simplemobiletools.notes.models.Note import kotlinx.android.synthetic.main.activity_main.* @@ -127,11 +129,11 @@ class MainActivity : SimpleActivity(), OpenNoteDialog.OpenNoteListener { show() getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { val titleET = newNoteView.findViewById(R.id.note_name) as EditText - val title = titleET.text.toString().trim { it <= ' ' } + val title = titleET.value if (title.isEmpty()) { - Utils.showToast(applicationContext, R.string.no_title) + toast(R.string.no_title) } else if (mDb.doesTitleExist(title)) { - Utils.showToast(applicationContext, R.string.title_taken) + toast(R.string.title_taken) } else { saveText() val newNote = Note(0, title, "") @@ -177,10 +179,10 @@ class MainActivity : SimpleActivity(), OpenNoteDialog.OpenNoteListener { if (mCurrentNote == null) return - val newText = getCurrentNoteValue() + val newText = notes_view.value val oldText = mCurrentNote!!.value if (newText != oldText) { - Utils.showToast(applicationContext, R.string.note_saved) + toast(R.string.note_saved) mCurrentNote!!.value = newText mDb.updateNote(mCurrentNote!!) } @@ -190,9 +192,9 @@ class MainActivity : SimpleActivity(), OpenNoteDialog.OpenNoteListener { } private fun shareText() { - val text = getCurrentNoteValue() + val text = notes_view.value if (text.isEmpty()) { - Utils.showToast(applicationContext, R.string.cannot_share_empty_text) + toast(R.string.cannot_share_empty_text) return } @@ -207,16 +209,10 @@ class MainActivity : SimpleActivity(), OpenNoteDialog.OpenNoteListener { } } - private fun getCurrentNoteValue(): String { - return notes_view.text.toString().trim() - } - private fun hideKeyboard() { val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(notes_view.windowToken, 0) } - override fun noteSelected(id: Int) { - updateSelectedNote(id) - } + override fun noteSelected(id: Int) = updateSelectedNote(id) } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/extensions/context.kt b/app/src/main/kotlin/com/simplemobiletools/notes/extensions/context.kt new file mode 100644 index 00000000..40761ec9 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/extensions/context.kt @@ -0,0 +1,6 @@ +package com.simplemobiletools.notes.extensions + +import android.content.Context +import android.widget.Toast + +fun Context.toast(id: Int) = Toast.makeText(this, id, Toast.LENGTH_SHORT).show() diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/extensions/editText.kt b/app/src/main/kotlin/com/simplemobiletools/notes/extensions/editText.kt new file mode 100644 index 00000000..79081004 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/extensions/editText.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.notes.extensions + +import android.widget.EditText + +val EditText.value: String get() = text.toString().trim()