Skip to content

Commit

Permalink
Add handle error
Browse files Browse the repository at this point in the history
  • Loading branch information
odiapratama committed Jul 19, 2021
1 parent 7ec8060 commit 0f7b90e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/bedrest/app/data/model/ResultData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.bedrest.app.data.model
sealed class ResultData<out R> {

data class Success<out T>(val data: T) : ResultData<T>()
data class Error(val exception: Exception) : ResultData<Nothing>()
data class Error(val message: String?) : ResultData<Nothing>()
object Loading : ResultData<Nothing>()

var status = Status.LOADING
Expand All @@ -17,7 +17,7 @@ sealed class ResultData<out R> {
override fun toString(): String {
return when (this) {
is Success<*> -> "Success[data=$data]"
is Error -> "Error[exception=$exception]"
is Error -> "Error[exception=$message]"
Loading -> "Loading"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.bedrest.app.data.repository
import com.bedrest.app.data.model.Availability
import com.bedrest.app.data.model.ResultData
import com.bedrest.app.data.remote.AvailabilityApi
import com.bedrest.app.utils.ErrorUtils.getError
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject
Expand All @@ -15,13 +16,14 @@ class AvailabilityRepositoryImpl @Inject constructor(
province: String,
revalidate: Boolean
): ResultData<List<Availability>> {
val result: ResultData<List<Availability>>
var result: ResultData<List<Availability>>
withContext(Dispatchers.IO) {
result = try {
val response = availabilityApi.getHospitalAvailability(province, revalidate)
ResultData.Success(response.data)
if (response.status == 200) ResultData.Success(response.data ?: emptyList())
else ResultData.Error(response.error)
} catch (e: Exception) {
ResultData.Error(e)
ResultData.Error(e.getError())
}
}
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class AvailabilityActivity : BaseMapsActivity<ActivityAvailabilityBinding>() {
is ResultData.Error -> {
Toast.makeText(
this,
it.exception.localizedMessage,
it.message ?: getString(R.string.error_response),
Toast.LENGTH_SHORT
).show()
skeleton.showOriginal()
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/bedrest/app/utils/ErrorUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.bedrest.app.utils

import com.bedrest.app.App
import com.bedrest.app.R
import com.bedrest.app.data.model.BaseResponse
import com.bedrest.app.utils.StringUtils.stringSuspending
import com.google.gson.Gson
import retrofit2.HttpException

object ErrorUtils {

suspend fun Exception.getError(): String {
return when (this) {
is HttpException -> {
val errorResponse: BaseResponse<*> = Gson().fromJson(
response()?.errorBody()?.stringSuspending(),
BaseResponse::class.java
)
errorResponse.error
}
else -> App.INSTANCE.applicationContext.getString(R.string.error_response)
}
}
}
12 changes: 9 additions & 3 deletions app/src/main/java/com/bedrest/app/utils/StringUtils.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.bedrest.app.utils

import android.content.Context
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.ResponseBody
import java.util.*

object StringUtils {

fun String.convertKeyword() = replace("_", " ").replaceFirstChar { char ->
if (char.isLowerCase()) char.titlecase(Locale.getDefault())
else char.toString()
fun String.convertKeyword() = split("_").joinToString(" ") { char ->
char.replaceFirstChar { it.titlecase(Locale.getDefault()) }
}

fun String.toKeywordPattern() = lowercase(Locale.getDefault()).replace(" ", "_")
Expand All @@ -25,4 +27,8 @@ object StringUtils {
val lonPattern = Regex("\\s*[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)\$")
return matches(lonPattern)
}

@Suppress("BlockingMethodInNonBlockingContext")
suspend fun ResponseBody.stringSuspending(): String =
withContext(Dispatchers.IO) { string() }
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="detail_kasur">Detail Kasur</string>
<string name="prefix_last_update">Diperbaharui %s menit yang lalu</string>
<string name="cari_provinsi_disini">Cari provinsi disini</string>
<string name="error_response">Terjadi kesalahan pada server. Coba beberapa saat lagi!</string>
</resources>

0 comments on commit 0f7b90e

Please sign in to comment.