mirror of
				https://github.com/SimpleMobileTools/Simple-Notes.git
				synced 2025-06-05 17:00:23 +02:00 
			
		
		
		
	handle note updating and inserting
This commit is contained in:
		| @@ -27,7 +27,10 @@ import com.simplemobiletools.notes.pro.R | ||||
| import com.simplemobiletools.notes.pro.adapters.NotesPagerAdapter | ||||
| import com.simplemobiletools.notes.pro.databases.NotesDatabase | ||||
| import com.simplemobiletools.notes.pro.dialogs.* | ||||
| import com.simplemobiletools.notes.pro.extensions.* | ||||
| import com.simplemobiletools.notes.pro.extensions.config | ||||
| import com.simplemobiletools.notes.pro.extensions.dbHelper | ||||
| import com.simplemobiletools.notes.pro.extensions.getTextSize | ||||
| import com.simplemobiletools.notes.pro.extensions.updateWidgets | ||||
| import com.simplemobiletools.notes.pro.helpers.MIME_TEXT_PLAIN | ||||
| import com.simplemobiletools.notes.pro.helpers.NotesHelper | ||||
| import com.simplemobiletools.notes.pro.helpers.OPEN_NOTE_ID | ||||
| @@ -304,19 +307,16 @@ class MainActivity : SimpleActivity() { | ||||
|     } | ||||
|  | ||||
|     private fun addNewNote(note: Note) { | ||||
|         Thread { | ||||
|             val id = notesDB.insertOrUpdate(note) | ||||
|             mNotes = notesDB.getNotes().toMutableList() as ArrayList<Note> | ||||
|         NotesHelper(this).insertOrUpdateNote(note) { | ||||
|             val newNoteId = it | ||||
|             showSaveButton = false | ||||
|             runOnUiThread { | ||||
|                 invalidateOptionsMenu() | ||||
|                 initViewPager() | ||||
|                 updateSelectedNote(id) | ||||
|                 view_pager.onGlobalLayout { | ||||
|                     mAdapter?.focusEditText(getNoteIndexWithId(id)) | ||||
|                 } | ||||
|             invalidateOptionsMenu() | ||||
|             initViewPager() | ||||
|             updateSelectedNote(newNoteId) | ||||
|             view_pager.onGlobalLayout { | ||||
|                 mAdapter?.focusEditText(getNoteIndexWithId(newNoteId)) | ||||
|             } | ||||
|         }.start() | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private fun launchAbout() { | ||||
| @@ -455,7 +455,7 @@ class MainActivity : SimpleActivity() { | ||||
|                         mCurrentNote.value = "" | ||||
|                     } | ||||
|  | ||||
|                     dbHelper.updateNote(mCurrentNote) | ||||
|                     NotesHelper(this).insertOrUpdateNote(mCurrentNote) | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|   | ||||
| @@ -9,7 +9,7 @@ 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 | ||||
| import kotlinx.android.synthetic.main.dialog_import_folder.view.* | ||||
| @@ -67,8 +67,6 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal | ||||
|  | ||||
|     private fun saveNote(title: String, value: String, path: String) { | ||||
|         val note = Note(null, title, value, TYPE_NOTE, path) | ||||
|         Thread { | ||||
|             activity.notesDB.insertOrUpdate(note) | ||||
|         } | ||||
|         NotesHelper(activity).insertOrUpdateNote(note) | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -6,6 +6,7 @@ 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.models.Note | ||||
| import kotlinx.android.synthetic.main.dialog_new_note.view.* | ||||
| import java.io.File | ||||
| @@ -31,7 +32,10 @@ class RenameNoteDialog(val activity: SimpleActivity, val note: Note, callback: ( | ||||
|                                     note.title = title | ||||
|                                     val path = note.path | ||||
|                                     if (path.isEmpty()) { | ||||
|                                         activity.dbHelper.updateNote(note) | ||||
|                                         NotesHelper(activity).insertOrUpdateNote(note) { | ||||
|                                             dismiss() | ||||
|                                             callback(note) | ||||
|                                         } | ||||
|                                     } else { | ||||
|                                         if (title.isEmpty()) { | ||||
|                                             activity.toast(R.string.filename_cannot_be_empty) | ||||
| @@ -48,16 +52,16 @@ class RenameNoteDialog(val activity: SimpleActivity, val note: Note, callback: ( | ||||
|                                         activity.renameFile(file.absolutePath, newFile.absolutePath) { | ||||
|                                             if (it) { | ||||
|                                                 note.path = newFile.absolutePath | ||||
|                                                 activity.dbHelper.updateNote(note) | ||||
|                                                 NotesHelper(activity).insertOrUpdateNote(note) { | ||||
|                                                     dismiss() | ||||
|                                                     callback(note) | ||||
|                                                 } | ||||
|                                             } else { | ||||
|                                                 activity.toast(R.string.rename_file_error) | ||||
|                                                 return@renameFile | ||||
|                                             } | ||||
|                                         } | ||||
|                                     } | ||||
|  | ||||
|                                     dismiss() | ||||
|                                     callback(note) | ||||
|                                 } | ||||
|                             } | ||||
|                         } | ||||
|   | ||||
| @@ -188,8 +188,9 @@ class NoteFragment : androidx.fragment.app.Fragment() { | ||||
|  | ||||
|     private fun saveNoteValue(note: Note) { | ||||
|         if (note.path.isEmpty()) { | ||||
|             db.updateNote(note) | ||||
|             (activity as MainActivity).noteSavedSuccessfully(note.title) | ||||
|             NotesHelper(activity!!).insertOrUpdateNote(note) { | ||||
|                 (activity as MainActivity).noteSavedSuccessfully(note.title) | ||||
|             } | ||||
|         } else { | ||||
|             val currentText = getCurrentNoteViewText() | ||||
|             if (currentText != null) { | ||||
|   | ||||
| @@ -108,13 +108,6 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe | ||||
|         return 0 | ||||
|     } | ||||
|  | ||||
|     fun updateNote(note: Note) { | ||||
|         val values = fillNoteContentValues(note) | ||||
|         val selection = "$COL_ID = ?" | ||||
|         val selectionArgs = arrayOf(note.id.toString()) | ||||
|         mDb.update(NOTES_TABLE_NAME, values, selection, selectionArgs) | ||||
|     } | ||||
|  | ||||
|     fun getWidgets(): ArrayList<Widget> { | ||||
|         val widgets = ArrayList<Widget>() | ||||
|         val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID) | ||||
|   | ||||
| @@ -41,4 +41,13 @@ class NotesHelper(val activity: Activity) { | ||||
|             } | ||||
|         }.start() | ||||
|     } | ||||
|  | ||||
|     fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) { | ||||
|         Thread { | ||||
|             val noteId = activity.notesDB.insertOrUpdate(note) | ||||
|             activity.runOnUiThread { | ||||
|                 callback?.invoke(noteId) | ||||
|             } | ||||
|         }.start() | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user