Skip to content

Commit 93e8a8b

Browse files
author
Jill Heske
committed
Step.07-Solution-Create-a-Worker
1 parent 087d5c0 commit 93e8a8b

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

app/src/main/java/com/example/android/devbyteviewer/work/RefreshDataWork.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,26 @@
1717

1818
package com.example.android.devbyteviewer.work
1919

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
2226

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

Comments
 (0)