From c9769223cb014f588d93c1a73da157010e68a1c8 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 25 Dec 2018 22:32:03 -0500 Subject: Add coroutines to FrostRequestService --- .../frost/services/FrostRequestService.kt | 40 +++++++++------------- 1 file changed, 17 insertions(+), 23 deletions(-) (limited to 'app') 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? = 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 } -- cgit v1.2.3