|
17 | 17 |
|
18 | 18 | package com.example.android.devbyteviewer.work
|
19 | 19 |
|
20 |
| -// TODO (01) Create the RefreshDataWorker class, extend it from CoroutineWorker, and |
21 |
| -// pass in a Context and WorkerParams. |
| 20 | +import android.content.Context |
| 21 | +import androidx.work.CoroutineWorker |
| 22 | +import androidx.work.WorkerParameters |
| 23 | +import com.example.android.devbyteviewer.database.getDatabase |
| 24 | +import com.example.android.devbyteviewer.repository.VideosRepository |
| 25 | +import retrofit2.HttpException |
22 | 26 |
|
23 |
| -// TODO (02) Override the required doWork() method, and create variables for the |
24 |
| -// database and the repository. |
25 |
| - |
26 |
| -// TODO (03) Inside doWork(), in a try-catch block, refresh the videos, and |
27 |
| -// use Payload() to return SUCCESS or RETRY result. |
| 27 | +class RefreshDataWorker(appContext: Context, params: WorkerParameters): |
| 28 | + CoroutineWorker(appContext, params) { |
| 29 | + /** |
| 30 | + * A coroutine-friendly method to do your work. |
| 31 | + */ |
| 32 | + override suspend fun doWork(): Payload { |
| 33 | + val database = getDatabase(applicationContext) |
| 34 | + val repository = VideosRepository(database) |
| 35 | + return try { |
| 36 | + repository.refreshVideos() |
| 37 | + Payload(Result.SUCCESS) |
| 38 | + } catch (e: HttpException) { |
| 39 | + Payload(Result.RETRY) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
0 commit comments