Skip to content

Commit

Permalink
Use suspend fun in Retrofit
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedeltaher committed Jan 5, 2020
1 parent 28104bc commit b9ef257
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/task/data/DataRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import javax.inject.Inject
class DataRepository @Inject
constructor(private val remoteRepository: RemoteRepository, private val localRepository: LocalRepository) : DataSource {

override fun requestNews(): Resource<NewsModel> {
override suspend fun requestNews(): Resource<NewsModel> {
return remoteRepository.requestNews()
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/task/data/DataSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import com.task.data.remote.dto.NewsModel
*/

interface DataSource {
fun requestNews(): Resource<NewsModel>
suspend fun requestNews(): Resource<NewsModel>
}
14 changes: 7 additions & 7 deletions app/src/main/java/com/task/data/remote/RemoteRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.task.data.remote.service.NewsService
import com.task.utils.Constants
import com.task.utils.Constants.INSTANCE.ERROR_UNDEFINED
import com.task.utils.Network.Utils.isConnected
import retrofit2.Call
import retrofit2.Response
import java.io.IOException
import javax.inject.Inject

Expand All @@ -22,9 +22,9 @@ import javax.inject.Inject
class RemoteRepository @Inject
constructor(private val serviceGenerator: ServiceGenerator) : RemoteSource {

override fun requestNews(): Resource<NewsModel> {
override suspend fun requestNews(): Resource<NewsModel> {
val newsService = serviceGenerator.createService(NewsService::class.java, Constants.BASE_URL)
return when (val response = processCall(newsService.fetchNews(), false)) {
return when (val response = processCall(newsService::fetchNews, false)) {
is Data -> {
Resource.Success(data = response.data as NewsModel)
}
Expand All @@ -34,13 +34,13 @@ constructor(private val serviceGenerator: ServiceGenerator) : RemoteSource {
}
}

private fun processCall(call: Call<*>, isVoid: Boolean): Any {
private suspend fun processCall(responseCall: suspend () -> Response<*>, isVoid: Boolean): Any {
if (!isConnected(App.context)) {
return Error(code = NO_INTERNET_CONNECTION, description = ErrorsMap[NO_INTERNET_CONNECTION] ?: "")
return Error(code = NO_INTERNET_CONNECTION, description = ErrorsMap[NO_INTERNET_CONNECTION]
?: "")
}
try {
val response = call.execute()
?: return Data(Error(NETWORK_ERROR, ERROR_UNDEFINED))
val response = responseCall.invoke()
val responseCode = response.code()
/**
* isVoid is for APIs which reply only with code without any body, such as some Apis
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/task/data/remote/RemoteSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import com.task.data.remote.dto.NewsModel
*/

internal interface RemoteSource {
fun requestNews(): Resource<NewsModel>
suspend fun requestNews(): Resource<NewsModel>
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/task/data/remote/service/NewsService.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.task.data.remote.service

import com.task.data.remote.dto.NewsModel
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET

/**
Expand All @@ -10,5 +10,5 @@ import retrofit2.http.GET

interface NewsService {
@GET("topstories/v2/home.json?api-key=4rfwOLzLTWd1a5xixcPjwddAhw3p0eiF")
fun fetchNews(): Call<NewsModel>
suspend fun fetchNews(): Response<NewsModel>
}
1 change: 0 additions & 1 deletion app/src/main/java/com/task/usecase/NewsUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.task.data.Resource
import com.task.data.remote.Error
import com.task.data.remote.dto.NewsItem
import com.task.data.remote.dto.NewsModel
import com.task.ui.base.listeners.BaseCallback
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down

0 comments on commit b9ef257

Please sign in to comment.