Skip to content

Instantly share code, notes, and snippets.

View RoRoche's full-sized avatar

Romain Rochegude RoRoche

View GitHub Profile
GetReposManagedRestRequest(
workManager = workManager,
baseUrl = "https://api.github.com/",
user = "RoRoche"
).liveData().observe(
lifecycleOwner,
Observer { workInfo ->
if (workInfo != null) {
// check with workInfo.state and deal with it
}
import androidx.work.*
class GetReposManagedRestRequest(origin: ManagedRestRequest) : ManagedRestRequest.Wrap(origin) {
constructor(
workManager: WorkManager,
worker: OneTimeWorkRequest
) : this(
SimpleOneTimeWorkRequest(workManager, worker)
)
import androidx.work.OneTimeWorkRequest
import androidx.work.Operation
import androidx.work.WorkManager
class SimpleOneTimeWorkRequest(
private val workManager: WorkManager,
private val worker: OneTimeWorkRequest,
private val operation: Operation
) : ManagedRestRequest {
import androidx.lifecycle.LiveData
import androidx.work.Operation
import androidx.work.WorkInfo
interface ManagedRestRequest {
fun operation(): Operation
fun liveData(): LiveData<WorkInfo>
fun cancel(): Operation
abstract class Wrap protected constructor(
@RoRoche
RoRoche / GetReposWorker.kt
Last active November 21, 2019 17:23
GetReposWorker
import android.content.Context
import androidx.work.Worker
import androidx.work.WorkerParameters
import okhttp3.OkHttpClient
class GetReposWorker(
appContext: Context,
workerParams: WorkerParameters
) : Worker(appContext, workerParams) {
override fun doWork(): Result {
implementation group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.2.14'
implementation group: 'io.reactivex.rxjava2', name: 'rxandroid', version: '2.1.1'
implementation group: 'io.reactivex.rxjava2', name: 'rxkotlin', version: '2.4.0'
RxGetReposRequest(
restRequest = WithBodyRestRequest(
SuccessfulRestRequest(
LoggableRequest(
GetReposRequest(
client = client,
baseUrl = "https://api.github.com/",
user = "RoRoche"
)
)
import io.reactivex.Single
import okhttp3.OkHttpClient
class RxGetReposRequest(
restRequest: RestRequest
) : RxRestRequest.WrapSyncRequest<List<Repo>>(restRequest) {
override fun single(): Single<List<Repo>> {
return Single.create { emitter ->
try {
val repos = JsonRepos(restRequest)
@RoRoche
RoRoche / RxRestRequest.kt
Created November 21, 2019 16:28
RxRestRequest.kt
import io.reactivex.Single
interface RxRestRequest<T> {
fun single(): Single<T>
abstract class WrapSyncRequest<T>(protected val restRequest: RestRequest) : RxRestRequest<T>
}
class NoBodyRestResponseException : RuntimeException()