Make Task.doInBackground a suspend function

This commit is contained in:
Alinson S. Xavier 2026-04-10 15:14:14 -05:00
parent 499146d102
commit 2e58d60fab
14 changed files with 28 additions and 25 deletions

View File

@ -95,6 +95,8 @@ android {
mokkery {
defaultMockMode.set(dev.mokkery.MockMode.autofill)
stubs.allowClassInheritance.set(true)
stubs.allowConcreteClassInstantiation.set(true)
}
dependencies {

View File

@ -19,6 +19,7 @@
package org.isoron.uhabits.tasks
import android.os.AsyncTask
import kotlinx.coroutines.runBlocking
import org.isoron.uhabits.core.tasks.Task
import org.isoron.uhabits.core.tasks.TaskRunner
import java.util.HashMap
@ -58,7 +59,7 @@ class AndroidTaskRunner : TaskRunner {
@Deprecated("Deprecated in Java")
override fun doInBackground(vararg params: Void?): Void? {
if (isCancelled) return null
task.doInBackground()
runBlocking { task.doInBackground() }
return null
}

View File

@ -35,7 +35,7 @@ class ExportDBTask(
private val listener: Listener
) : Task {
private var filename: String? = null
override fun doInBackground() {
override suspend fun doInBackground() {
filename = null
filename = try {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)

View File

@ -19,7 +19,6 @@
package org.isoron.uhabits.tasks
import android.util.Log
import kotlinx.coroutines.runBlocking
import org.isoron.platform.io.UserFile
import org.isoron.platform.io.begin
import org.isoron.platform.io.commit
@ -36,18 +35,16 @@ class ImportDataTask(
) : Task {
private var result = 0
private val modelFactory: SQLModelFactory = modelFactory as SQLModelFactory
override fun doInBackground() {
override suspend fun doInBackground() {
modelFactory.database.begin()
try {
runBlocking {
if (importer.canHandle(file)) {
importer.importHabitsFromFile(file)
result = SUCCESS
modelFactory.database.commit()
} else {
result = NOT_RECOGNIZED
modelFactory.database.commit()
}
if (importer.canHandle(file)) {
importer.importHabitsFromFile(file)
result = SUCCESS
modelFactory.database.commit()
} else {
result = NOT_RECOGNIZED
modelFactory.database.commit()
}
} catch (e: Exception) {
result = FAILED

View File

@ -33,7 +33,7 @@ open class CommandRunner(
open fun run(command: Command) {
taskRunner.execute(
object : Task {
override fun doInBackground() {
override suspend fun doInBackground() {
command.run()
}
override fun onPostExecute() {

View File

@ -197,7 +197,9 @@ abstract class HabitList : Iterable<Habit> {
if (habit.isNumerical) {
val s = habit.targetValue.toString()
if ('.' !in s) "$s.0" else s
} else "",
} else {
""
},
habit.isArchived.toString()
)
sb.append(csvLine(cols))

View File

@ -19,7 +19,6 @@
package org.isoron.uhabits.core.tasks
import org.isoron.platform.io.UserFile
import org.isoron.platform.runSuspend
import org.isoron.platform.time.getToday
import org.isoron.uhabits.core.io.HabitsCSVExporter
import org.isoron.uhabits.core.models.Habit
@ -32,13 +31,13 @@ class ExportCSVTask(
private val listener: ExportCSVListener
) : Task {
private var archiveFilename: String? = null
override fun doInBackground() {
override suspend fun doInBackground() {
try {
val exporter = HabitsCSVExporter(habitList, selectedHabits)
val bytes = runSuspend { exporter.writeArchive() }
val bytes = exporter.writeArchive()
val date = getToday().toCSVString()
val zipFile = outputDir.resolve("Loop Habits CSV $date.zip")
runSuspend { zipFile.writeBytes(bytes) }
zipFile.writeBytes(bytes)
archiveFilename = zipFile.pathString
} catch (e: Exception) {
e.printStackTrace()

View File

@ -18,6 +18,8 @@
*/
package org.isoron.uhabits.core.tasks
import org.isoron.platform.runSuspend
class SingleThreadTaskRunner : TaskRunner {
override val activeTaskCount: Int
get() = 0
@ -32,7 +34,7 @@ class SingleThreadTaskRunner : TaskRunner {
if (!task.isCanceled()) {
task.onAttached(this)
task.onPreExecute()
task.doInBackground()
runSuspend { task.doInBackground() }
task.onPostExecute()
}
for (l in listeners) l.onTaskFinished(task)

View File

@ -21,7 +21,7 @@ package org.isoron.uhabits.core.tasks
fun interface Task {
fun cancel() {}
fun isCanceled() = false
fun doInBackground()
suspend fun doInBackground()
fun onAttached(runner: TaskRunner) {}
fun onPostExecute() {}
fun onPreExecute() {}

View File

@ -113,7 +113,7 @@ open class NotificationTray(
private val date: LocalDate = data.date
private val reminderTime: Long = data.reminderTime
override fun doInBackground() {
override suspend fun doInBackground() {
isCompleted = habit.isCompletedToday()
}

View File

@ -292,7 +292,7 @@ class HabitCardListCache(
}
@Synchronized
override fun doInBackground() {
override suspend fun doInBackground() {
newData.fetchHabits()
newData.copyScoresFrom(data)
newData.copyCheckmarksFrom(data)

View File

@ -19,8 +19,8 @@
package org.isoron.uhabits.core.tasks
import dev.mokkery.mock
import dev.mokkery.verify
import dev.mokkery.verify.VerifyMode.Companion.order
import dev.mokkery.verifySuspend
import org.isoron.uhabits.core.BaseUnitTest
import kotlin.test.BeforeTest
import kotlin.test.Test
@ -38,7 +38,7 @@ class SingleThreadTaskRunnerTest : BaseUnitTest() {
@Test
fun test() {
runner.execute(task)
verify(order) {
verifySuspend(order) {
task.onAttached(runner)
task.onPreExecute()
task.doInBackground()