Skip to content

Commit 6682139

Browse files
committed
moving another function in Room
1 parent 43896a9 commit 6682139

File tree

6 files changed

+72
-69
lines changed

6 files changed

+72
-69
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class MainActivity : SimpleActivity() {
300300
}
301301

302302
private fun displayNewNoteDialog(value: String = "") {
303-
NewNoteDialog(this, dbHelper) {
303+
NewNoteDialog(this) {
304304
val newNote = Note(null, it, value, TYPE_NOTE)
305305
addNewNote(newNote)
306306
}
@@ -355,7 +355,7 @@ class MainActivity : SimpleActivity() {
355355
toast(R.string.invalid_file_format)
356356
} else if (file.length() > 10 * 1000 * 1000) {
357357
toast(R.string.file_too_large)
358-
} else if (checkTitle && dbHelper.doesNoteTitleExist(path.getFilenameFromPath())) {
358+
} else if (checkTitle && mNotes.any { it.title.equals(path.getFilenameFromPath(), true) }) {
359359
toast(R.string.title_taken)
360360
} else {
361361
onChecksPassed(file)
@@ -386,7 +386,7 @@ class MainActivity : SimpleActivity() {
386386
private fun openPath(path: String) {
387387
openFile(path, false) {
388388
var title = path.getFilenameFromPath()
389-
if (dbHelper.doesNoteTitleExist(title)) {
389+
if (mNotes.any { it.title.equals(title, true) }) {
390390
title += " (file)"
391391
}
392392

app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.simplemobiletools.commons.extensions.isMediaFile
88
import com.simplemobiletools.commons.extensions.setupDialogStuff
99
import com.simplemobiletools.notes.pro.R
1010
import com.simplemobiletools.notes.pro.activities.SimpleActivity
11-
import com.simplemobiletools.notes.pro.extensions.dbHelper
11+
import com.simplemobiletools.notes.pro.extensions.notesDB
1212
import com.simplemobiletools.notes.pro.helpers.NotesHelper
1313
import com.simplemobiletools.notes.pro.helpers.TYPE_NOTE
1414
import com.simplemobiletools.notes.pro.models.Note
@@ -30,7 +30,9 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal
3030
activity.setupDialogStuff(view, this, R.string.import_folder) {
3131
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
3232
val updateFilesOnEdit = view.open_file_type.checkedRadioButtonId == R.id.open_file_update_file
33-
saveFolder(updateFilesOnEdit)
33+
Thread {
34+
saveFolder(updateFilesOnEdit)
35+
}.start()
3436
}
3537
}
3638
}
@@ -44,7 +46,7 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal
4446
file.isDirectory -> false
4547
filename.isMediaFile() -> false
4648
file.length() > 10 * 1000 * 1000 -> false
47-
activity.dbHelper.doesNoteTitleExist(filename) -> false
49+
activity.notesDB.getNoteIdWithTitle(filename) != null -> false
4850
else -> true
4951
}
5052
}.forEach {
@@ -61,8 +63,10 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal
6163
}
6264
}
6365

64-
callback()
65-
dialog.dismiss()
66+
activity.runOnUiThread {
67+
callback()
68+
dialog.dismiss()
69+
}
6670
}
6771

6872
private fun saveNote(title: String, value: String, path: String) {

app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/NewNoteDialog.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import com.simplemobiletools.commons.extensions.showKeyboard
88
import com.simplemobiletools.commons.extensions.toast
99
import com.simplemobiletools.commons.extensions.value
1010
import com.simplemobiletools.notes.pro.R
11-
import com.simplemobiletools.notes.pro.helpers.DBHelper
11+
import com.simplemobiletools.notes.pro.extensions.notesDB
1212
import kotlinx.android.synthetic.main.dialog_new_note.view.*
1313

14-
class NewNoteDialog(val activity: Activity, val db: DBHelper, callback: (title: String) -> Unit) {
14+
class NewNoteDialog(val activity: Activity, callback: (title: String) -> Unit) {
1515
init {
1616
val view = activity.layoutInflater.inflate(R.layout.dialog_new_note, null)
1717

@@ -23,14 +23,16 @@ class NewNoteDialog(val activity: Activity, val db: DBHelper, callback: (title:
2323
showKeyboard(view.note_name)
2424
getButton(BUTTON_POSITIVE).setOnClickListener {
2525
val title = view.note_name.value
26-
when {
27-
title.isEmpty() -> activity.toast(R.string.no_title)
28-
db.doesNoteTitleExist(title) -> activity.toast(R.string.title_taken)
29-
else -> {
30-
callback(title)
31-
dismiss()
26+
Thread {
27+
when {
28+
title.isEmpty() -> activity.toast(R.string.no_title)
29+
activity.notesDB.getNoteIdWithTitle(title) != null -> activity.toast(R.string.title_taken)
30+
else -> {
31+
callback(title)
32+
dismiss()
33+
}
3234
}
33-
}
35+
}.start()
3436
}
3537
}
3638
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/RenameNoteDialog.kt

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import androidx.appcompat.app.AlertDialog
55
import com.simplemobiletools.commons.extensions.*
66
import com.simplemobiletools.notes.pro.R
77
import com.simplemobiletools.notes.pro.activities.SimpleActivity
8-
import com.simplemobiletools.notes.pro.extensions.dbHelper
9-
import com.simplemobiletools.notes.pro.helpers.NotesHelper
8+
import com.simplemobiletools.notes.pro.extensions.notesDB
109
import com.simplemobiletools.notes.pro.models.Note
1110
import kotlinx.android.synthetic.main.dialog_new_note.view.*
1211
import java.io.File
1312

14-
class RenameNoteDialog(val activity: SimpleActivity, val note: Note, callback: (note: Note) -> Unit) {
13+
class RenameNoteDialog(val activity: SimpleActivity, val note: Note, val callback: (note: Note) -> Unit) {
1514

1615
init {
1716
val view = activity.layoutInflater.inflate(R.layout.dialog_rename_note, null)
@@ -25,47 +24,55 @@ class RenameNoteDialog(val activity: SimpleActivity, val note: Note, callback: (
2524
showKeyboard(view.note_name)
2625
getButton(BUTTON_POSITIVE).setOnClickListener {
2726
val title = view.note_name.value
28-
when {
29-
title.isEmpty() -> activity.toast(R.string.no_title)
30-
activity.dbHelper.doesNoteTitleExist(title) -> activity.toast(R.string.title_taken)
31-
else -> {
32-
note.title = title
33-
val path = note.path
34-
if (path.isEmpty()) {
35-
NotesHelper(activity).insertOrUpdateNote(note) {
36-
dismiss()
37-
callback(note)
38-
}
39-
} else {
40-
if (title.isEmpty()) {
41-
activity.toast(R.string.filename_cannot_be_empty)
42-
return@setOnClickListener
43-
}
27+
Thread {
28+
newTitleConfirmed(title, this)
29+
}.start()
30+
}
31+
}
32+
}
33+
}
4434

45-
val file = File(path)
46-
val newFile = File(file.parent, title)
47-
if (!newFile.name.isAValidFilename()) {
48-
activity.toast(R.string.invalid_name)
49-
return@setOnClickListener
50-
}
35+
private fun newTitleConfirmed(title: String, dialog: AlertDialog) {
36+
when {
37+
title.isEmpty() -> activity.toast(R.string.no_title)
38+
activity.notesDB.getNoteIdWithTitle(title) != null -> activity.toast(R.string.title_taken)
39+
else -> {
40+
note.title = title
41+
val path = note.path
42+
if (path.isEmpty()) {
43+
activity.notesDB.insertOrUpdate(note)
44+
activity.runOnUiThread {
45+
dialog.dismiss()
46+
callback(note)
47+
}
48+
} else {
49+
if (title.isEmpty()) {
50+
activity.toast(R.string.filename_cannot_be_empty)
51+
return
52+
}
53+
54+
val file = File(path)
55+
val newFile = File(file.parent, title)
56+
if (!newFile.name.isAValidFilename()) {
57+
activity.toast(R.string.invalid_name)
58+
return
59+
}
5160

52-
activity.renameFile(file.absolutePath, newFile.absolutePath) {
53-
if (it) {
54-
note.path = newFile.absolutePath
55-
NotesHelper(activity).insertOrUpdateNote(note) {
56-
dismiss()
57-
callback(note)
58-
}
59-
} else {
60-
activity.toast(R.string.rename_file_error)
61-
return@renameFile
62-
}
63-
}
64-
}
65-
}
61+
activity.renameFile(file.absolutePath, newFile.absolutePath) {
62+
if (it) {
63+
note.path = newFile.absolutePath
64+
activity.notesDB.insertOrUpdate(note)
65+
activity.runOnUiThread {
66+
dialog.dismiss()
67+
callback(note)
6668
}
69+
} else {
70+
activity.toast(R.string.rename_file_error)
71+
return@renameFile
6772
}
6873
}
6974
}
75+
}
76+
}
7077
}
7178
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/DBHelper.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,6 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
7878
mDb.delete(WIDGETS_TABLE_NAME, "$COL_NOTE_ID = $id", null)
7979
}
8080

81-
fun doesNoteTitleExist(title: String): Boolean {
82-
val cols = arrayOf(COL_ID)
83-
val selection = "$COL_TITLE = ? COLLATE NOCASE"
84-
val selectionArgs = arrayOf(title)
85-
var cursor: Cursor? = null
86-
try {
87-
cursor = mDb.query(NOTES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
88-
return cursor.count == 1
89-
} finally {
90-
cursor?.close()
91-
}
92-
}
93-
9481
fun getWidgets(): ArrayList<Widget> {
9582
val widgets = ArrayList<Widget>()
9683
val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID)

app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/NotesDao.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ interface NotesDao {
1414
@Query("SELECT id FROM notes WHERE path = :path")
1515
fun getNoteIdWithPath(path: String): Long?
1616

17+
@Query("SELECT id FROM notes WHERE title = :title COLLATE NOCASE")
18+
fun getNoteIdWithTitle(title: String): Long?
19+
1720
@Insert(onConflict = OnConflictStrategy.REPLACE)
1821
fun insertOrUpdate(note: Note): Long
1922

0 commit comments

Comments
 (0)