moving another function in Room

This commit is contained in:
tibbi 2018-11-07 20:39:17 +01:00
parent 43896a964a
commit 66821397e9
6 changed files with 75 additions and 72 deletions

View File

@ -300,7 +300,7 @@ class MainActivity : SimpleActivity() {
}
private fun displayNewNoteDialog(value: String = "") {
NewNoteDialog(this, dbHelper) {
NewNoteDialog(this) {
val newNote = Note(null, it, value, TYPE_NOTE)
addNewNote(newNote)
}
@ -355,7 +355,7 @@ class MainActivity : SimpleActivity() {
toast(R.string.invalid_file_format)
} else if (file.length() > 10 * 1000 * 1000) {
toast(R.string.file_too_large)
} else if (checkTitle && dbHelper.doesNoteTitleExist(path.getFilenameFromPath())) {
} else if (checkTitle && mNotes.any { it.title.equals(path.getFilenameFromPath(), true) }) {
toast(R.string.title_taken)
} else {
onChecksPassed(file)
@ -386,7 +386,7 @@ class MainActivity : SimpleActivity() {
private fun openPath(path: String) {
openFile(path, false) {
var title = path.getFilenameFromPath()
if (dbHelper.doesNoteTitleExist(title)) {
if (mNotes.any { it.title.equals(title, true) }) {
title += " (file)"
}

View File

@ -8,7 +8,7 @@ import com.simplemobiletools.commons.extensions.isMediaFile
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.activities.SimpleActivity
import com.simplemobiletools.notes.pro.extensions.dbHelper
import com.simplemobiletools.notes.pro.extensions.notesDB
import com.simplemobiletools.notes.pro.helpers.NotesHelper
import com.simplemobiletools.notes.pro.helpers.TYPE_NOTE
import com.simplemobiletools.notes.pro.models.Note
@ -30,7 +30,9 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal
activity.setupDialogStuff(view, this, R.string.import_folder) {
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val updateFilesOnEdit = view.open_file_type.checkedRadioButtonId == R.id.open_file_update_file
saveFolder(updateFilesOnEdit)
Thread {
saveFolder(updateFilesOnEdit)
}.start()
}
}
}
@ -44,7 +46,7 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal
file.isDirectory -> false
filename.isMediaFile() -> false
file.length() > 10 * 1000 * 1000 -> false
activity.dbHelper.doesNoteTitleExist(filename) -> false
activity.notesDB.getNoteIdWithTitle(filename) != null -> false
else -> true
}
}.forEach {
@ -61,8 +63,10 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal
}
}
callback()
dialog.dismiss()
activity.runOnUiThread {
callback()
dialog.dismiss()
}
}
private fun saveNote(title: String, value: String, path: String) {

View File

@ -8,10 +8,10 @@ import com.simplemobiletools.commons.extensions.showKeyboard
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.helpers.DBHelper
import com.simplemobiletools.notes.pro.extensions.notesDB
import kotlinx.android.synthetic.main.dialog_new_note.view.*
class NewNoteDialog(val activity: Activity, val db: DBHelper, callback: (title: String) -> Unit) {
class NewNoteDialog(val activity: Activity, callback: (title: String) -> Unit) {
init {
val view = activity.layoutInflater.inflate(R.layout.dialog_new_note, null)
@ -23,14 +23,16 @@ class NewNoteDialog(val activity: Activity, val db: DBHelper, callback: (title:
showKeyboard(view.note_name)
getButton(BUTTON_POSITIVE).setOnClickListener {
val title = view.note_name.value
when {
title.isEmpty() -> activity.toast(R.string.no_title)
db.doesNoteTitleExist(title) -> activity.toast(R.string.title_taken)
else -> {
callback(title)
dismiss()
Thread {
when {
title.isEmpty() -> activity.toast(R.string.no_title)
activity.notesDB.getNoteIdWithTitle(title) != null -> activity.toast(R.string.title_taken)
else -> {
callback(title)
dismiss()
}
}
}
}.start()
}
}
}

View File

@ -5,13 +5,12 @@ import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.activities.SimpleActivity
import com.simplemobiletools.notes.pro.extensions.dbHelper
import com.simplemobiletools.notes.pro.helpers.NotesHelper
import com.simplemobiletools.notes.pro.extensions.notesDB
import com.simplemobiletools.notes.pro.models.Note
import kotlinx.android.synthetic.main.dialog_new_note.view.*
import java.io.File
class RenameNoteDialog(val activity: SimpleActivity, val note: Note, callback: (note: Note) -> Unit) {
class RenameNoteDialog(val activity: SimpleActivity, val note: Note, val callback: (note: Note) -> Unit) {
init {
val view = activity.layoutInflater.inflate(R.layout.dialog_rename_note, null)
@ -25,47 +24,55 @@ class RenameNoteDialog(val activity: SimpleActivity, val note: Note, callback: (
showKeyboard(view.note_name)
getButton(BUTTON_POSITIVE).setOnClickListener {
val title = view.note_name.value
when {
title.isEmpty() -> activity.toast(R.string.no_title)
activity.dbHelper.doesNoteTitleExist(title) -> activity.toast(R.string.title_taken)
else -> {
note.title = title
val path = note.path
if (path.isEmpty()) {
NotesHelper(activity).insertOrUpdateNote(note) {
dismiss()
callback(note)
}
} else {
if (title.isEmpty()) {
activity.toast(R.string.filename_cannot_be_empty)
return@setOnClickListener
}
val file = File(path)
val newFile = File(file.parent, title)
if (!newFile.name.isAValidFilename()) {
activity.toast(R.string.invalid_name)
return@setOnClickListener
}
activity.renameFile(file.absolutePath, newFile.absolutePath) {
if (it) {
note.path = newFile.absolutePath
NotesHelper(activity).insertOrUpdateNote(note) {
dismiss()
callback(note)
}
} else {
activity.toast(R.string.rename_file_error)
return@renameFile
}
}
}
}
}
Thread {
newTitleConfirmed(title, this)
}.start()
}
}
}
}
private fun newTitleConfirmed(title: String, dialog: AlertDialog) {
when {
title.isEmpty() -> activity.toast(R.string.no_title)
activity.notesDB.getNoteIdWithTitle(title) != null -> activity.toast(R.string.title_taken)
else -> {
note.title = title
val path = note.path
if (path.isEmpty()) {
activity.notesDB.insertOrUpdate(note)
activity.runOnUiThread {
dialog.dismiss()
callback(note)
}
} else {
if (title.isEmpty()) {
activity.toast(R.string.filename_cannot_be_empty)
return
}
val file = File(path)
val newFile = File(file.parent, title)
if (!newFile.name.isAValidFilename()) {
activity.toast(R.string.invalid_name)
return
}
activity.renameFile(file.absolutePath, newFile.absolutePath) {
if (it) {
note.path = newFile.absolutePath
activity.notesDB.insertOrUpdate(note)
activity.runOnUiThread {
dialog.dismiss()
callback(note)
}
} else {
activity.toast(R.string.rename_file_error)
return@renameFile
}
}
}
}
}
}
}

View File

@ -78,19 +78,6 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
mDb.delete(WIDGETS_TABLE_NAME, "$COL_NOTE_ID = $id", null)
}
fun doesNoteTitleExist(title: String): Boolean {
val cols = arrayOf(COL_ID)
val selection = "$COL_TITLE = ? COLLATE NOCASE"
val selectionArgs = arrayOf(title)
var cursor: Cursor? = null
try {
cursor = mDb.query(NOTES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
return cursor.count == 1
} finally {
cursor?.close()
}
}
fun getWidgets(): ArrayList<Widget> {
val widgets = ArrayList<Widget>()
val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID)

View File

@ -14,6 +14,9 @@ interface NotesDao {
@Query("SELECT id FROM notes WHERE path = :path")
fun getNoteIdWithPath(path: String): Long?
@Query("SELECT id FROM notes WHERE title = :title COLLATE NOCASE")
fun getNoteIdWithTitle(title: String): Long?
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(note: Note): Long