Skip to content

Commit

Permalink
Replaced Enum for Result with sealed class
Browse files Browse the repository at this point in the history
  • Loading branch information
kanch231004 committed Aug 20, 2021
1 parent 40d1539 commit 09914d2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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")
}
}

19 changes: 4 additions & 15 deletions app/src/main/java/com/kanchanpal/newsfeed/data/Result.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,8 @@ package com.kanchanpal.newsfeed.data
* Result is usually created by the Repository classes where they return
* `LiveData<Result<T>>` to pass back the latest data to the UI with its fetch status.
*/
data class Result<out T>(val status: Status, val data: T?, val message: String?) {
enum class Status {
SUCCESS, ERROR, LOADING
}
companion object {
fun <T> success(data: T): Result<T> {
return Result(Status.SUCCESS, data, null)
}
fun <T> error(message: String, data: T? = null): Result<T> {
return Result(Status.ERROR, data, message)
}
fun <T> loading(data: T? = null): Result<T> {
return Result(Status.LOADING, data, null)
}
}

sealed class Result<out T> {
data class Success<out T>(val data : T) : Result<T>()
data class Error<out T>(val message: String? = "Unknown error", val data: T? = null): Result<T>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class NewsPageDataSource @Inject constructor(
callback: LoadInitialCallback<Int, NewsListModel>
) {
networkState.postValue(NetworkState.LOADING)
fetchData(1, params.requestedLoadSize) {
fetchData(page = 1, pageSize = params.requestedLoadSize) {
callback.onResult(it, null, 2)
}
}
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, NewsListModel>) {
networkState.postValue(NetworkState.LOADING)
val page = params.key
fetchData(page, params.requestedLoadSize) {
fetchData(page = page, pageSize = params.requestedLoadSize) {
callback.onResult(it, page + 1)
}
}
Expand All @@ -47,15 +47,17 @@ class NewsPageDataSource @Inject constructor(

private fun fetchData(page: Int, pageSize: Int, callback: (List<NewsListModel>) -> 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)
}
}
}
}
Expand All @@ -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")
}
}

0 comments on commit 09914d2

Please sign in to comment.