mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-05-08 02:49:00 +02:00
require just a context to NotesHelper, not whole activity
This commit is contained in:
parent
6264edcdca
commit
b3203cb822
@ -1,26 +1,28 @@
|
|||||||
package com.simplemobiletools.notes.pro.helpers
|
package com.simplemobiletools.notes.pro.helpers
|
||||||
|
|
||||||
import android.app.Activity
|
import android.content.Context
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
import com.simplemobiletools.notes.pro.R
|
import com.simplemobiletools.notes.pro.R
|
||||||
import com.simplemobiletools.notes.pro.extensions.config
|
import com.simplemobiletools.notes.pro.extensions.config
|
||||||
import com.simplemobiletools.notes.pro.extensions.notesDB
|
import com.simplemobiletools.notes.pro.extensions.notesDB
|
||||||
import com.simplemobiletools.notes.pro.models.Note
|
import com.simplemobiletools.notes.pro.models.Note
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class NotesHelper(val activity: Activity) {
|
class NotesHelper(val context: Context) {
|
||||||
fun getNotes(callback: (notes: ArrayList<Note>) -> Unit) {
|
fun getNotes(callback: (notes: ArrayList<Note>) -> Unit) {
|
||||||
Thread {
|
Thread {
|
||||||
// make sure the initial note has enough time to be precreated
|
// make sure the initial note has enough time to be precreated
|
||||||
if (activity.config.appRunCount <= 1) {
|
if (context.config.appRunCount <= 1) {
|
||||||
activity.notesDB.getNotes()
|
context.notesDB.getNotes()
|
||||||
Thread.sleep(200)
|
Thread.sleep(200)
|
||||||
}
|
}
|
||||||
|
|
||||||
val notes = activity.notesDB.getNotes() as ArrayList<Note>
|
val notes = context.notesDB.getNotes() as ArrayList<Note>
|
||||||
val notesToDelete = ArrayList<Note>(notes.size)
|
val notesToDelete = ArrayList<Note>(notes.size)
|
||||||
notes.forEach {
|
notes.forEach {
|
||||||
if (it.path.isNotEmpty() && !File(it.path).exists()) {
|
if (it.path.isNotEmpty() && !File(it.path).exists()) {
|
||||||
activity.notesDB.deleteNote(it)
|
context.notesDB.deleteNote(it)
|
||||||
notesToDelete.add(it)
|
notesToDelete.add(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -28,13 +30,13 @@ class NotesHelper(val activity: Activity) {
|
|||||||
notes.removeAll(notesToDelete)
|
notes.removeAll(notesToDelete)
|
||||||
|
|
||||||
if (notes.isEmpty()) {
|
if (notes.isEmpty()) {
|
||||||
val generalNote = activity.resources.getString(R.string.general_note)
|
val generalNote = context.resources.getString(R.string.general_note)
|
||||||
val note = Note(null, generalNote, "", TYPE_TEXT)
|
val note = Note(null, generalNote, "", TYPE_TEXT)
|
||||||
activity.notesDB.insertOrUpdate(note)
|
context.notesDB.insertOrUpdate(note)
|
||||||
notes.add(note)
|
notes.add(note)
|
||||||
}
|
}
|
||||||
|
|
||||||
activity.runOnUiThread {
|
Handler(Looper.getMainLooper()).post {
|
||||||
callback(notes)
|
callback(notes)
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
@ -42,8 +44,8 @@ class NotesHelper(val activity: Activity) {
|
|||||||
|
|
||||||
fun getNoteWithId(id: Long, callback: (note: Note?) -> Unit) {
|
fun getNoteWithId(id: Long, callback: (note: Note?) -> Unit) {
|
||||||
Thread {
|
Thread {
|
||||||
val note = activity.notesDB.getNoteWithId(id)
|
val note = context.notesDB.getNoteWithId(id)
|
||||||
activity.runOnUiThread {
|
Handler(Looper.getMainLooper()).post {
|
||||||
callback(note)
|
callback(note)
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
@ -51,8 +53,8 @@ class NotesHelper(val activity: Activity) {
|
|||||||
|
|
||||||
fun getNoteIdWithPath(path: String, callback: (id: Long?) -> Unit) {
|
fun getNoteIdWithPath(path: String, callback: (id: Long?) -> Unit) {
|
||||||
Thread {
|
Thread {
|
||||||
val id = activity.notesDB.getNoteIdWithPath(path)
|
val id = context.notesDB.getNoteIdWithPath(path)
|
||||||
activity.runOnUiThread {
|
Handler(Looper.getMainLooper()).post {
|
||||||
callback(id)
|
callback(id)
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
@ -60,8 +62,8 @@ class NotesHelper(val activity: Activity) {
|
|||||||
|
|
||||||
fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) {
|
fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) {
|
||||||
Thread {
|
Thread {
|
||||||
val noteId = activity.notesDB.insertOrUpdate(note)
|
val noteId = context.notesDB.insertOrUpdate(note)
|
||||||
activity.runOnUiThread {
|
Handler(Looper.getMainLooper()).post {
|
||||||
callback?.invoke(noteId)
|
callback?.invoke(noteId)
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user