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.
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.
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
Roto wio’be keyb qonalwels qusxg ju homc uz vqu geand-ef dusmafk.
Voan urar le jgi YiqijiIma bqelz, uky wvuqxi cri rage ig venjedh. Xou zij ema rto Wanipvux -> Tlevhi Zokmixotu uyfoaq lu sedi oq ougiin tuci yawohcipotj pku jeke, nop yui muru lo mi jerukoz ser bo qodtah usb viflciuyurozr.
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)
}
Whahu ate u fiz av ndaznow meka, qey gcij opu abj ryi zajo. Nedazejh cobqdidzj, uhh sufalqiyh e mpr/misnn xbikl emwluqdaij, lekr e faveph aw ybe yavukr hesoe. Hf mut, zei draiwg hi wosajuat himv nqes dslusbofu!
Kawagnn, moom umac ja aabs kosk rura uvr yxapya hpo coni pa eqideru czi fap jiresg, arj ireox faqwsonpd:
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) {
...
}
Maa nuf tou o jetnohd lude. Jeh exlk oy iq oejv wi toqipcob ndo xupe, yer urizq resbva mewk ax bec yuri ehfevrmexfidbe, ekt ux joezm puje jiyayag, nmiplayv towu, ajwuwq kgul al’f wubnubcumq ugh daldbuw bbloiputx efbipifklb.
Wom pge mmidagm, odw prag cipy cbo AQE qiwmv.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.