Skip to content

Commit b3203cb

Browse files
committed
require just a context to NotesHelper, not whole activity
1 parent 6264edc commit b3203cb

File tree

1 file changed

+17
-15
lines changed
  • app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers

1 file changed

+17
-15
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,69 @@
11
package com.simplemobiletools.notes.pro.helpers
22

3-
import android.app.Activity
3+
import android.content.Context
4+
import android.os.Handler
5+
import android.os.Looper
46
import com.simplemobiletools.notes.pro.R
57
import com.simplemobiletools.notes.pro.extensions.config
68
import com.simplemobiletools.notes.pro.extensions.notesDB
79
import com.simplemobiletools.notes.pro.models.Note
810
import java.io.File
911

10-
class NotesHelper(val activity: Activity) {
12+
class NotesHelper(val context: Context) {
1113
fun getNotes(callback: (notes: ArrayList<Note>) -> Unit) {
1214
Thread {
1315
// make sure the initial note has enough time to be precreated
14-
if (activity.config.appRunCount <= 1) {
15-
activity.notesDB.getNotes()
16+
if (context.config.appRunCount <= 1) {
17+
context.notesDB.getNotes()
1618
Thread.sleep(200)
1719
}
1820

19-
val notes = activity.notesDB.getNotes() as ArrayList<Note>
21+
val notes = context.notesDB.getNotes() as ArrayList<Note>
2022
val notesToDelete = ArrayList<Note>(notes.size)
2123
notes.forEach {
2224
if (it.path.isNotEmpty() && !File(it.path).exists()) {
23-
activity.notesDB.deleteNote(it)
25+
context.notesDB.deleteNote(it)
2426
notesToDelete.add(it)
2527
}
2628
}
2729

2830
notes.removeAll(notesToDelete)
2931

3032
if (notes.isEmpty()) {
31-
val generalNote = activity.resources.getString(R.string.general_note)
33+
val generalNote = context.resources.getString(R.string.general_note)
3234
val note = Note(null, generalNote, "", TYPE_TEXT)
33-
activity.notesDB.insertOrUpdate(note)
35+
context.notesDB.insertOrUpdate(note)
3436
notes.add(note)
3537
}
3638

37-
activity.runOnUiThread {
39+
Handler(Looper.getMainLooper()).post {
3840
callback(notes)
3941
}
4042
}.start()
4143
}
4244

4345
fun getNoteWithId(id: Long, callback: (note: Note?) -> Unit) {
4446
Thread {
45-
val note = activity.notesDB.getNoteWithId(id)
46-
activity.runOnUiThread {
47+
val note = context.notesDB.getNoteWithId(id)
48+
Handler(Looper.getMainLooper()).post {
4749
callback(note)
4850
}
4951
}.start()
5052
}
5153

5254
fun getNoteIdWithPath(path: String, callback: (id: Long?) -> Unit) {
5355
Thread {
54-
val id = activity.notesDB.getNoteIdWithPath(path)
55-
activity.runOnUiThread {
56+
val id = context.notesDB.getNoteIdWithPath(path)
57+
Handler(Looper.getMainLooper()).post {
5658
callback(id)
5759
}
5860
}.start()
5961
}
6062

6163
fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) {
6264
Thread {
63-
val noteId = activity.notesDB.insertOrUpdate(note)
64-
activity.runOnUiThread {
65+
val noteId = context.notesDB.insertOrUpdate(note)
66+
Handler(Looper.getMainLooper()).post {
6567
callback?.invoke(noteId)
6668
}
6769
}.start()

0 commit comments

Comments
 (0)