Last active
October 12, 2021 13:21
-
-
Save virtual-addy/b47cea179c0fb432946976efa9bd2630 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class ApiResult<out T : Any> { | |
data class Success<out T : Any>(val response: T) : ApiResult<T>() | |
data class HttpError(val code: Int?, val message: String?) : ApiResult<Nothing>() | |
data class GenericError(val error: Exception) : ApiResult<Nothing>() | |
object InProgress : ApiResult<Nothing>() | |
object NoInternet : ApiResult<Nothing>() | |
// Override for quick logging | |
override fun toString(): String { | |
return when (this) { | |
is Success<*> -> "Success [data=$response]" | |
is HttpError -> "Http Error [httpCode=$code]" | |
is GenericError -> "Error [error=${error.localizedMessage}]" | |
is NoInternet -> "No Internet" | |
is InProgress -> "In progress" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun syncVerifyOtp( | |
verifyRequest: VerifyRequest | |
): ResultWrapper<VerifyResponse> { | |
return requester.makeRequestToApi { | |
apiService.verifyPhoneNumber(verifyRequest) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun CoroutineScope.launchOnIO( | |
block: suspend CoroutineScope.() -> Unit | |
) { | |
this.launch(Dispatchers.IO) { | |
block() | |
} | |
} | |
fun CoroutineScope.onMain(block: () -> Unit) { | |
this.launch (Dispatchers.Main){ | |
block() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun <T : Any> makeRequestToApi( | |
call: suspend () -> T, | |
): ApiResult<T> { | |
return try { | |
val data = call.invoke() | |
ApiResult.Success(data) | |
} catch (throwable: Exception) { | |
return when (throwable) { | |
is HttpException -> { | |
ApiResult.HttpError(throwable.code(), parseErrorMessage(throwable)) | |
} | |
is IOException -> ApiResult.NoInternet | |
else -> ApiResult.GenericError(throwable) | |
} | |
} | |
} | |
fun parseErrorMessage(httpException: HttpException): String { | |
val errorBody = httpException.response()?.errorBody()?.string() | |
return try { | |
val messageObject = Gson().fromJson(errorBody, JsonObject::class.java) | |
messageObject.get("message").asString | |
} catch (ex: Exception) { | |
"" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias ResultWrapper<T> = ApiResult<DataResponse<T>> | |
data class DataResponse<T>( | |
@SerializedName("data") | |
val data: T, | |
@SerializedName("developer_message") | |
val developerMessage: String?, | |
@SerializedName("error") | |
val error: Boolean = false, | |
@SerializedName("message") | |
val message: String?, | |
@SerializedName("status") | |
val status: Int = 0 | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lifecycleScope.launchOnIO { | |
val verifyResult = authApi.syncVerifyOtp(verifyRequest) | |
// request has completed with result | |
reqDialog.dismiss() | |
when (verifyResult) { | |
is ApiResult.Success -> { | |
// switch back to main thread from IO | |
onMain { | |
completeVerification(verifyResult.response.data) | |
} | |
} | |
is ApiResult.HttpError -> { | |
// switch to main thread scope to show error message | |
onMain { | |
val errorMessage = verifyResult.errorResponse?.developerMessage | |
val errorDialog = buildErrorDialog(errorMessage) | |
errorDialog.show() | |
} | |
} | |
else -> { | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun changePIN( | |
phoneNumber: String, | |
updatePinReq: UpdatePinReq | |
): LiveData<ApiResult<Any>> { | |
return liveData(Dispatchers.IO) { | |
emit(ApiResult.InProgress) | |
emit(appRepository.changePassword(phoneNumber, updatePinReq)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment