Skip to content

Instantly share code, notes, and snippets.

View Aldikitta's full-sized avatar
🏠
Working from home

Muh. Aldi Andi kitta Aldikitta

🏠
Working from home
View GitHub Profile
sealed interface SallyResponseResource<out T> {
data class Success<T>(val data: T) : SallyResponseResource<T>
data class Error(val exception: AppException, val errorCode: String? = null) :
SallyResponseResource<Nothing>
data class Loading(val status: Boolean) : SallyResponseResource<Nothing>
}
open class AppException(message: String? = null, cause: Throwable? = null) :
Throwable(message, cause)
class NetworkException(message: String? = null, cause: Throwable? = null) :
AppException(message, cause)
class ServerException(message: String? = null, cause: Throwable? = null) :
AppException(message, cause)
class ClientException(message: String? = null, cause: Throwable? = null) :
suspend fun <T> asSallyResponseResourceSuspend(apiCall: suspend () -> T): SallyResponseResource<T> {
return try {
SallyResponseResource.Loading(true)
val response = apiCall.invoke()
SallyResponseResource.Success(response)
} catch (error: Throwable) {
val exception = when (error) {
is HttpException -> {
when (error.code()) {
in 400..499 -> {
fun <T> Flow<T>.asSallyResponseResourceFlow(): Flow<SallyResponseResource<T>> {
return this
.map<T, SallyResponseResource<T>> {
SallyResponseResource.Success(it)
}
.onStart { emit(SallyResponseResource.Loading(true)) }
.onCompletion { emit(SallyResponseResource.Loading(false)) }
.catch { error ->
val exception = when (error) {
is HttpException -> {
const val CLIENT_ERROR = "Terjadi kesalahan, mohon periksa masukan anda"
const val SERVER_ERROR = "Terjadi kesalahan pada Server, coba lagi nanti"
const val NETWORK_ERROR = "Koneksi internet bermasalah, coba lagi nanti"
const val HTTP_UNKNOWN_ERROR = "HTTP Error tidak diketahui (exc: 4xx/5xx)"
const val UNKNOWN_ERROR = "Error tidak diketahui"
@GET("list/{id}")
suspend fun getDetailAgentVisitWithFullResponse(
@Header("Authorization") token: String,
@Path(value = "id", encoded = true) id: Int
): BaseResponse<GetAgentDetailResponse>
suspend fun getDetailAgentVisitFullResponse(
token: String,
id: Int
): SallyResponseResource<BaseResponse<GetAgentDetailResponse>> {
return asSallyResponseResourceSuspend {
agentVisitService.getDetailAgentVisitWithFullResponse(
token = token,
id = id
)
}
fun getDetailAgentVisitFullResponseFlow(
token: String,
id: Int
): Flow<BaseResponse<GetAgentDetailResponse>> {
return flow {
while (true) {
val getDetailAgent = agentVisitService.getDetailAgentVisitWithFullResponse(
token = token,
id = id
)
override suspend fun getDetailAgentVisitWithFullResponse(id: Int): SallyResponseResource<BaseResponse<GetAgentDetailResponse>> {
return agentVisitDataSource.getDetailAgentVisitFullResponse(
token = "",
id = id
)
}
override fun getDetailAgentVisitFullResponseFlow(id: Int): Flow<SallyResponseResource<BaseResponse<GetAgentDetailResponse>>> {
return agentVisitDataSource.getDetailAgentVisitFullResponseFlow(
token = "",
id = id
).asSallyResponseResourceFlow()
}