|
1 | 1 | package com.simplemobiletools.notes.pro.helpers |
2 | 2 |
|
3 | | -import android.app.Activity |
| 3 | +import android.content.Context |
| 4 | +import android.os.Handler |
| 5 | +import android.os.Looper |
4 | 6 | import com.simplemobiletools.notes.pro.R |
5 | 7 | import com.simplemobiletools.notes.pro.extensions.config |
6 | 8 | import com.simplemobiletools.notes.pro.extensions.notesDB |
7 | 9 | import com.simplemobiletools.notes.pro.models.Note |
8 | 10 | import java.io.File |
9 | 11 |
|
10 | | -class NotesHelper(val activity: Activity) { |
| 12 | +class NotesHelper(val context: Context) { |
11 | 13 | fun getNotes(callback: (notes: ArrayList<Note>) -> Unit) { |
12 | 14 | Thread { |
13 | 15 | // 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() |
16 | 18 | Thread.sleep(200) |
17 | 19 | } |
18 | 20 |
|
19 | | - val notes = activity.notesDB.getNotes() as ArrayList<Note> |
| 21 | + val notes = context.notesDB.getNotes() as ArrayList<Note> |
20 | 22 | val notesToDelete = ArrayList<Note>(notes.size) |
21 | 23 | notes.forEach { |
22 | 24 | if (it.path.isNotEmpty() && !File(it.path).exists()) { |
23 | | - activity.notesDB.deleteNote(it) |
| 25 | + context.notesDB.deleteNote(it) |
24 | 26 | notesToDelete.add(it) |
25 | 27 | } |
26 | 28 | } |
27 | 29 |
|
28 | 30 | notes.removeAll(notesToDelete) |
29 | 31 |
|
30 | 32 | if (notes.isEmpty()) { |
31 | | - val generalNote = activity.resources.getString(R.string.general_note) |
| 33 | + val generalNote = context.resources.getString(R.string.general_note) |
32 | 34 | val note = Note(null, generalNote, "", TYPE_TEXT) |
33 | | - activity.notesDB.insertOrUpdate(note) |
| 35 | + context.notesDB.insertOrUpdate(note) |
34 | 36 | notes.add(note) |
35 | 37 | } |
36 | 38 |
|
37 | | - activity.runOnUiThread { |
| 39 | + Handler(Looper.getMainLooper()).post { |
38 | 40 | callback(notes) |
39 | 41 | } |
40 | 42 | }.start() |
41 | 43 | } |
42 | 44 |
|
43 | 45 | fun getNoteWithId(id: Long, callback: (note: Note?) -> Unit) { |
44 | 46 | Thread { |
45 | | - val note = activity.notesDB.getNoteWithId(id) |
46 | | - activity.runOnUiThread { |
| 47 | + val note = context.notesDB.getNoteWithId(id) |
| 48 | + Handler(Looper.getMainLooper()).post { |
47 | 49 | callback(note) |
48 | 50 | } |
49 | 51 | }.start() |
50 | 52 | } |
51 | 53 |
|
52 | 54 | fun getNoteIdWithPath(path: String, callback: (id: Long?) -> Unit) { |
53 | 55 | Thread { |
54 | | - val id = activity.notesDB.getNoteIdWithPath(path) |
55 | | - activity.runOnUiThread { |
| 56 | + val id = context.notesDB.getNoteIdWithPath(path) |
| 57 | + Handler(Looper.getMainLooper()).post { |
56 | 58 | callback(id) |
57 | 59 | } |
58 | 60 | }.start() |
59 | 61 | } |
60 | 62 |
|
61 | 63 | fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) { |
62 | 64 | Thread { |
63 | | - val noteId = activity.notesDB.insertOrUpdate(note) |
64 | | - activity.runOnUiThread { |
| 65 | + val noteId = context.notesDB.insertOrUpdate(note) |
| 66 | + Handler(Looper.getMainLooper()).post { |
65 | 67 | callback?.invoke(noteId) |
66 | 68 | } |
67 | 69 | }.start() |
|
0 commit comments