| 1 |
package io.oxygenhub.android.alerts |
| 2 |
|
| 3 |
import android.app.job.JobParameters |
| 4 |
import android.app.job.JobService |
| 5 |
import io.oxygenhub.android.OxygenHubApp |
| 6 |
import io.oxygenhub.android.api.HubException |
| 7 |
import kotlinx.coroutines.CoroutineScope |
| 8 |
import kotlinx.coroutines.Dispatchers |
| 9 |
import kotlinx.coroutines.SupervisorJob |
| 10 |
import kotlinx.coroutines.cancel |
| 11 |
import kotlinx.coroutines.launch |
| 12 |
|
| 13 |
/** The periodic check behind [LandAlerts]: what the account can land now. */ |
| 14 |
class LandCheckJob : JobService() { |
| 15 |
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) |
| 16 |
|
| 17 |
override fun onStartJob(params: JobParameters): Boolean { |
| 18 |
// Signed out: nothing to ask, and the next sign-in finds the alerts as they were. |
| 19 |
val api = (application as OxygenHubApp).container.sessions.api() ?: return false |
| 20 |
scope.launch { |
| 21 |
try { |
| 22 |
LandAlerts.check(this@LandCheckJob, api.userProposals(landable = true).landable) |
| 23 |
} catch (_: HubException) { |
| 24 |
// Unreachable or refused: the next period asks again. |
| 25 |
} |
| 26 |
jobFinished(params, false) |
| 27 |
} |
| 28 |
return true |
| 29 |
} |
| 30 |
|
| 31 |
override fun onStopJob(params: JobParameters): Boolean = false |
| 32 |
|
| 33 |
override fun onDestroy() { |
| 34 |
scope.cancel() |
| 35 |
super.onDestroy() |
| 36 |
} |
| 37 |
} |