require just a context to NotesHelper, not whole activity

This commit is contained in:
tibbi 2018-12-09 21:30:46 +01:00
parent 6264edcdca
commit b3203cb822

View File

@ -1,26 +1,28 @@
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.extensions.config
import com.simplemobiletools.notes.pro.extensions.notesDB
import com.simplemobiletools.notes.pro.models.Note
import java.io.File
class NotesHelper(val activity: Activity) {
class NotesHelper(val context: Context) {
fun getNotes(callback: (notes: ArrayList<Note>) -> Unit) {
Thread {
// make sure the initial note has enough time to be precreated
if (activity.config.appRunCount <= 1) {
activity.notesDB.getNotes()
if (context.config.appRunCount <= 1) {
context.notesDB.getNotes()
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)
notes.forEach {
if (it.path.isNotEmpty() && !File(it.path).exists()) {
activity.notesDB.deleteNote(it)
context.notesDB.deleteNote(it)
notesToDelete.add(it)
}
}
@ -28,13 +30,13 @@ class NotesHelper(val activity: Activity) {
notes.removeAll(notesToDelete)
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)
activity.notesDB.insertOrUpdate(note)
context.notesDB.insertOrUpdate(note)
notes.add(note)
}
activity.runOnUiThread {
Handler(Looper.getMainLooper()).post {
callback(notes)
}
}.start()
@ -42,8 +44,8 @@ class NotesHelper(val activity: Activity) {
fun getNoteWithId(id: Long, callback: (note: Note?) -> Unit) {
Thread {
val note = activity.notesDB.getNoteWithId(id)
activity.runOnUiThread {
val note = context.notesDB.getNoteWithId(id)
Handler(Looper.getMainLooper()).post {
callback(note)
}
}.start()
@ -51,8 +53,8 @@ class NotesHelper(val activity: Activity) {
fun getNoteIdWithPath(path: String, callback: (id: Long?) -> Unit) {
Thread {
val id = activity.notesDB.getNoteIdWithPath(path)
activity.runOnUiThread {
val id = context.notesDB.getNoteIdWithPath(path)
Handler(Looper.getMainLooper()).post {
callback(id)
}
}.start()
@ -60,8 +62,8 @@ class NotesHelper(val activity: Activity) {
fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) {
Thread {
val noteId = activity.notesDB.insertOrUpdate(note)
activity.runOnUiThread {
val noteId = context.notesDB.insertOrUpdate(note)
Handler(Looper.getMainLooper()).post {
callback?.invoke(noteId)
}
}.start()