create a new background thread only when needed

This commit is contained in:
tibbi
2019-08-19 22:38:59 +02:00
parent ec7621b988
commit b71768a0dc
8 changed files with 34 additions and 30 deletions

View File

@ -9,6 +9,7 @@ import android.net.Uri
import android.widget.RemoteViews
import com.simplemobiletools.commons.extensions.getLaunchIntent
import com.simplemobiletools.commons.extensions.setBackgroundColor
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.activities.SplashActivity
import com.simplemobiletools.notes.pro.extensions.config
@ -26,7 +27,7 @@ class MyWidgetProvider : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
Thread {
ensureBackgroundThread {
context.widgetsDB.getWidgets().forEach {
val views = RemoteViews(context.packageName, R.layout.widget)
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor)
@ -46,15 +47,15 @@ class MyWidgetProvider : AppWidgetProvider() {
appWidgetManager.updateAppWidget(it.widgetId, views)
appWidgetManager.notifyAppWidgetViewDataChanged(it.widgetId, R.id.notes_widget_listview)
}
}.start()
}
}
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
super.onDeleted(context, appWidgetIds)
Thread {
ensureBackgroundThread {
appWidgetIds.forEach {
context.widgetsDB.deleteWidgetId(it)
}
}.start()
}
}
}

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.notes.pro.helpers
import android.content.Context
import android.os.Handler
import android.os.Looper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.notesDB
@ -11,7 +12,7 @@ import java.io.File
class NotesHelper(val context: Context) {
fun getNotes(callback: (notes: ArrayList<Note>) -> Unit) {
Thread {
ensureBackgroundThread {
// make sure the initial note has enough time to be precreated
if (context.config.appRunCount <= 1) {
context.notesDB.getNotes()
@ -39,33 +40,33 @@ class NotesHelper(val context: Context) {
Handler(Looper.getMainLooper()).post {
callback(notes)
}
}.start()
}
}
fun getNoteWithId(id: Long, callback: (note: Note?) -> Unit) {
Thread {
ensureBackgroundThread {
val note = context.notesDB.getNoteWithId(id)
Handler(Looper.getMainLooper()).post {
callback(note)
}
}.start()
}
}
fun getNoteIdWithPath(path: String, callback: (id: Long?) -> Unit) {
Thread {
ensureBackgroundThread {
val id = context.notesDB.getNoteIdWithPath(path)
Handler(Looper.getMainLooper()).post {
callback(id)
}
}.start()
}
}
fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) {
Thread {
ensureBackgroundThread {
val noteId = context.notesDB.insertOrUpdate(note)
Handler(Looper.getMainLooper()).post {
callback?.invoke(noteId)
}
}.start()
}
}
}