Skip to content

Instantly share code, notes, and snippets.

View dev-niiaddy's full-sized avatar

Addy Godwin Nii dev-niiaddy

View GitHub Profile
@dev-niiaddy
dev-niiaddy / QRPainter.kt
Last active May 13, 2024 10:47
A function to encode text as a QR code using BitmapPainter and zxing-core for use in jetpack compose with Image composable.
@Composable
fun rememberQrBitmapPainter(
content: String,
size: Dp = 150.dp,
padding: Dp = 0.dp
): BitmapPainter {
val density = LocalDensity.current
val sizePx = with(density) { size.roundToPx() }
val paddingPx = with(density) { padding.roundToPx() }
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>()
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 -> {