aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-25 22:32:03 -0500
committerAllan Wang <me@allanwang.ca>2018-12-25 22:32:03 -0500
commitc9769223cb014f588d93c1a73da157010e68a1c8 (patch)
tree8b361170882f4a5a9400375f9f4ec2f68a4f5c1a
parentd1ed8498e56b4580f27af8142e3e401e2841872e (diff)
downloadfrost-c9769223cb014f588d93c1a73da157010e68a1c8.tar.gz
frost-c9769223cb014f588d93c1a73da157010e68a1c8.tar.bz2
frost-c9769223cb014f588d93c1a73da157010e68a1c8.zip
Add coroutines to FrostRequestService
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/services/FrostRequestService.kt40
1 files changed, 17 insertions, 23 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostRequestService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostRequestService.kt
index d41f0b3c..989f1b24 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostRequestService.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostRequestService.kt
@@ -19,7 +19,6 @@ package com.pitchedapps.frost.services
import android.app.job.JobInfo
import android.app.job.JobParameters
import android.app.job.JobScheduler
-import android.app.job.JobService
import android.content.ComponentName
import android.content.Context
import android.content.Intent
@@ -32,8 +31,8 @@ import com.pitchedapps.frost.utils.EnumBundle
import com.pitchedapps.frost.utils.EnumBundleCompanion
import com.pitchedapps.frost.utils.EnumCompanion
import com.pitchedapps.frost.utils.L
-import org.jetbrains.anko.doAsync
-import java.util.concurrent.Future
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
/**
* Created by Allan Wang on 28/12/17.
@@ -159,17 +158,10 @@ object FrostRunnable {
}
}
-class FrostRequestService : JobService() {
-
- var future: Future<Unit>? = null
-
- override fun onStopJob(params: JobParameters?): Boolean {
- future?.cancel(true)
- future = null
- return false
- }
+class FrostRequestService : BaseJobService() {
override fun onStartJob(params: JobParameters?): Boolean {
+ super.onStartJob(params)
val bundle = params?.extras
if (bundle == null) {
L.eThrow("Launched ${this::class.java.simpleName} without param data")
@@ -185,18 +177,20 @@ class FrostRequestService : JobService() {
L.eThrow("Launched ${this::class.java.simpleName} without command")
return false
}
- future = doAsync {
- val now = System.currentTimeMillis()
- var failed = true
- cookie.fbRequest {
- L.d { "Requesting frost service for ${command.name}" }
- command.invoke(this, bundle)
- failed = false
- }
- L.d {
- "${if (failed) "Failed" else "Finished"} frost service for ${command.name} in ${System.currentTimeMillis() - now} ms"
+ launch(Dispatchers.IO) {
+ try {
+ var failed = true
+ cookie.fbRequest {
+ L.d { "Requesting frost service for ${command.name}" }
+ command.invoke(this, bundle)
+ failed = false
+ }
+ L.d {
+ "${if (failed) "Failed" else "Finished"} frost service for ${command.name} in ${System.currentTimeMillis() - startTime} ms"
+ }
+ } finally {
+ jobFinished(params, false)
}
- jobFinished(params, false)
}
return true
}