Skip to content

Commit 66d75b9

Browse files
committed
use some extension functions
1 parent 4f81180 commit 66d75b9

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import com.simplemobiletools.notes.Utils
1616
import com.simplemobiletools.notes.databases.DBHelper
1717
import com.simplemobiletools.notes.dialogs.OpenNoteDialog
1818
import com.simplemobiletools.notes.dialogs.WidgetNoteDialog
19+
import com.simplemobiletools.notes.extensions.toast
20+
import com.simplemobiletools.notes.extensions.value
1921
import com.simplemobiletools.notes.models.Note
2022
import kotlinx.android.synthetic.main.activity_main.*
2123

@@ -127,11 +129,11 @@ class MainActivity : SimpleActivity(), OpenNoteDialog.OpenNoteListener {
127129
show()
128130
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
129131
val titleET = newNoteView.findViewById(R.id.note_name) as EditText
130-
val title = titleET.text.toString().trim { it <= ' ' }
132+
val title = titleET.value
131133
if (title.isEmpty()) {
132-
Utils.showToast(applicationContext, R.string.no_title)
134+
toast(R.string.no_title)
133135
} else if (mDb.doesTitleExist(title)) {
134-
Utils.showToast(applicationContext, R.string.title_taken)
136+
toast(R.string.title_taken)
135137
} else {
136138
saveText()
137139
val newNote = Note(0, title, "")
@@ -177,10 +179,10 @@ class MainActivity : SimpleActivity(), OpenNoteDialog.OpenNoteListener {
177179
if (mCurrentNote == null)
178180
return
179181

180-
val newText = getCurrentNoteValue()
182+
val newText = notes_view.value
181183
val oldText = mCurrentNote!!.value
182184
if (newText != oldText) {
183-
Utils.showToast(applicationContext, R.string.note_saved)
185+
toast(R.string.note_saved)
184186
mCurrentNote!!.value = newText
185187
mDb.updateNote(mCurrentNote!!)
186188
}
@@ -190,9 +192,9 @@ class MainActivity : SimpleActivity(), OpenNoteDialog.OpenNoteListener {
190192
}
191193

192194
private fun shareText() {
193-
val text = getCurrentNoteValue()
195+
val text = notes_view.value
194196
if (text.isEmpty()) {
195-
Utils.showToast(applicationContext, R.string.cannot_share_empty_text)
197+
toast(R.string.cannot_share_empty_text)
196198
return
197199
}
198200

@@ -207,16 +209,10 @@ class MainActivity : SimpleActivity(), OpenNoteDialog.OpenNoteListener {
207209
}
208210
}
209211

210-
private fun getCurrentNoteValue(): String {
211-
return notes_view.text.toString().trim()
212-
}
213-
214212
private fun hideKeyboard() {
215213
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
216214
imm.hideSoftInputFromWindow(notes_view.windowToken, 0)
217215
}
218216

219-
override fun noteSelected(id: Int) {
220-
updateSelectedNote(id)
221-
}
217+
override fun noteSelected(id: Int) = updateSelectedNote(id)
222218
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.simplemobiletools.notes.extensions
2+
3+
import android.content.Context
4+
import android.widget.Toast
5+
6+
fun Context.toast(id: Int) = Toast.makeText(this, id, Toast.LENGTH_SHORT).show()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.simplemobiletools.notes.extensions
2+
3+
import android.widget.EditText
4+
5+
val EditText.value: String get() = text.toString().trim()

0 commit comments

Comments
 (0)