Android Networking: Beyond the Basics

Sep 8 2022 · Kotlin 1.7.10, Android 12, Android Studio Chipmunk

Part 2: Retrofit With Kotlin Coroutines

11. Challenge: Coroutines

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 10. Include Built-in Retrofit Support for Coroutines Next episode: 12. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Since you’re now a networking pro, there’s not much else to do but practice using Kotlin Coroutines, for other requests. In this challenge, all you have to do is change the getTasks, completeTask, addTask and getUserProfile calls, to use the built-in support from Retrofit.

Demo

Start by opening the RemoteApiService.kt file. Change the following calls to be suspendable, and remove the call generic return type:

@GET("/api/note")
suspend fun getNotes(): GetTasksResponse

@GET("/api/user/profile")
suspend fun getMyProfile(): UserProfileResponse

@POST("/api/note/complete")
suspend fun completeTask(@Query("id") noteId: String): CompleteNoteResponse

@POST("/api/note")
suspend fun addTask(@Body request: AddTaskRequest): Task
suspend fun getTasks(): Result<List<Task>> = try {
  val data = apiService.getNotes().notes

  Success(data.filter { !it.isCompleted })
} catch (error: Throwable) {
  Failure(error)
}

suspend fun completeTask(taskId: String): Result<String> = try {
  val response = apiService.completeTask(taskId)

  Success(response.message!!)
} catch (error: Throwable) {
  Failure(error)
}

suspend fun addTask(addTaskRequest: AddTaskRequest): Result<Task> = try {
  val task = apiService.addTask(addTaskRequest)

  Success(task)
} catch (error: Throwable) {
  Failure(error)
}

suspend fun getUserProfile(): Result<UserProfile> = try {
  val notesResult = getTasks()

  if (notesResult is Failure) {
    Failure(notesResult.error)
  } else {
    val notes = notesResult as Success
    val data = apiService.getMyProfile()

    if (data.name == null || data.email == null) {
      Failure(NullPointerException("No data available!"))
    } else {
      Success(UserProfile(data.email, data.name, notes.data.size))
    }
  }
} catch (error: Throwable) {
  Failure(error)
}
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
  val result = remoteApi.getUserProfile()
  withContext(Dispatchers.Main) {
    if (result is Success) {
...
}      

viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
  val result = remoteApi.getTasks()
  withContext(Dispatchers.Main) {
    if (result is Success) {
...
}

viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
  val result = remoteApi.completeTask(taskId)

  withContext(Dispatchers.Main) {
    if (result is Success) {
...
}

viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
  val result = remoteApi.addTask(AddTaskRequest(title, content, priority))
  withContext(Dispatchers.Main) {
    if (result is Success) {
...
}