diff --git a/app/src/main/java/com/kanchanpal/newsfeed/api/BaseDataSource.kt b/app/src/main/java/com/kanchanpal/newsfeed/api/BaseDataSource.kt index b63b91e..4e2ae80 100644 --- a/app/src/main/java/com/kanchanpal/newsfeed/api/BaseDataSource.kt +++ b/app/src/main/java/com/kanchanpal/newsfeed/api/BaseDataSource.kt @@ -15,7 +15,7 @@ abstract class BaseDataSource { val response = call() if (response.isSuccessful) { val body = response.body() - if (body != null) return Result.success(body) + if (body != null) return Result.Success(body) } return error(" ${response.code()} ${response.message()}") } catch (e: Exception) { @@ -28,7 +28,7 @@ abstract class BaseDataSource { /** We can deserialize error model (in case we get error msg from server) * and pass the message */ Timber.e(message) - return Result.error("Network call has failed for a following reason: $message") + return Result.Error("Network call has failed for a following reason: $message") } } diff --git a/app/src/main/java/com/kanchanpal/newsfeed/data/Result.kt b/app/src/main/java/com/kanchanpal/newsfeed/data/Result.kt index 2e87a87..b4b6d2d 100644 --- a/app/src/main/java/com/kanchanpal/newsfeed/data/Result.kt +++ b/app/src/main/java/com/kanchanpal/newsfeed/data/Result.kt @@ -6,19 +6,8 @@ package com.kanchanpal.newsfeed.data * Result is usually created by the Repository classes where they return * `LiveData>` to pass back the latest data to the UI with its fetch status. */ -data class Result(val status: Status, val data: T?, val message: String?) { - enum class Status { - SUCCESS, ERROR, LOADING - } - companion object { - fun success(data: T): Result { - return Result(Status.SUCCESS, data, null) - } - fun error(message: String, data: T? = null): Result { - return Result(Status.ERROR, data, message) - } - fun loading(data: T? = null): Result { - return Result(Status.LOADING, data, null) - } - } + +sealed class Result { + data class Success(val data : T) : Result() + data class Error(val message: String? = "Unknown error", val data: T? = null): Result() } diff --git a/app/src/main/java/com/kanchanpal/newsfeed/data/newsSet/NewsPageDataSource.kt b/app/src/main/java/com/kanchanpal/newsfeed/data/newsSet/NewsPageDataSource.kt index 453a39d..9d01132 100644 --- a/app/src/main/java/com/kanchanpal/newsfeed/data/newsSet/NewsPageDataSource.kt +++ b/app/src/main/java/com/kanchanpal/newsfeed/data/newsSet/NewsPageDataSource.kt @@ -26,14 +26,14 @@ class NewsPageDataSource @Inject constructor( callback: LoadInitialCallback ) { networkState.postValue(NetworkState.LOADING) - fetchData(1, params.requestedLoadSize) { + fetchData(page = 1, pageSize = params.requestedLoadSize) { callback.onResult(it, null, 2) } } override fun loadAfter(params: LoadParams, callback: LoadCallback) { networkState.postValue(NetworkState.LOADING) val page = params.key - fetchData(page, params.requestedLoadSize) { + fetchData(page = page, pageSize = params.requestedLoadSize) { callback.onResult(it, page + 1) } } @@ -47,15 +47,17 @@ class NewsPageDataSource @Inject constructor( private fun fetchData(page: Int, pageSize: Int, callback: (List) -> Unit) { coroutineScope.launch(getJobErrorHandler()) { - val response = remoteDataSource.fetchNewsList(apiKey, page, pageSize) - if (response.status == Result.Status.SUCCESS) { - val results = response.data?.articles ?: emptyList() - newsDao.insertAll(results) - callback(results) - networkState.postValue(NetworkState.LOADED) - } else if (response.status == Result.Status.ERROR) { - networkState.postValue(NetworkState.error(response.message ?: "Unknown error")) - postError(response.message ?: "Unknown error") + when (val response = remoteDataSource.fetchNewsList(apiKey, page, pageSize)) { + is Result.Error -> { + networkState.postValue(NetworkState.error(response.message ?: "Unknown error")) + postError(response.message) + } + is Result.Success -> { + val results = response.data.articles + newsDao.insertAll(results) + callback(results) + networkState.postValue(NetworkState.LOADED) + } } } } @@ -64,7 +66,7 @@ class NewsPageDataSource @Inject constructor( postError(e.message ?: e.toString()) } - private fun postError(message: String) { + private fun postError(message: String?) { Timber.e("An error happened: $message") } }