Skip to content

Commit

Permalink
Merge pull request #493 from code-payments/chore/clean-up-access-key-gen
Browse files Browse the repository at this point in the history
Chore/clean up access key gen
  • Loading branch information
bmc08gt authored Aug 6, 2024
2 parents dbbde28 + db7664c commit d240be7
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 71 deletions.
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ dependencies {
implementation(Libs.hilt_nav_compose)
implementation(Libs.lib_phone_number_port)
implementation(Libs.mp_android_chart)
implementation(Libs.qr_generator)
implementation(Libs.zxing)
implementation(Libs.mixpanel)

Expand Down
113 changes: 60 additions & 53 deletions app/src/main/java/com/getcode/util/QRCodeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.getcode.util

import android.graphics.Bitmap
import android.graphics.Color
import androidmads.library.qrgenearator.QRGContents
import androidmads.library.qrgenearator.QRGEncoder
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
Expand All @@ -15,25 +13,21 @@ import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.getcode.theme.Transparent
import com.getcode.theme.White
import com.getcode.ui.utils.toAGColor
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.WriterException
import com.google.zxing.qrcode.QRCodeWriter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

fun generateQrCode(url: String, size: Int? = null): Bitmap? {
val qrgEncoder = if (size == null) {
QRGEncoder(url, QRGContents.Type.TEXT)
} else {
QRGEncoder(url, null, QRGContents.Type.TEXT, size)
}
qrgEncoder.colorBlack = White.toAGColor()
qrgEncoder.colorWhite = Transparent.toAGColor()
return qrgEncoder.bitmap
fun generateQrCode(url: String, size: Int): Bitmap? {
return generateQr(
url = url,
size = size,
padding = 0,
contentColor = Color.WHITE,
spaceColor = Color.TRANSPARENT
)
}

@Composable
Expand All @@ -47,7 +41,6 @@ fun rememberQrBitmapPainter(
val sizePx = with(density) { size.roundToPx() }
val paddingPx = with(density) { padding.roundToPx() }


var bitmap by remember(content) {
mutableStateOf<Bitmap?>(null)
}
Expand All @@ -56,45 +49,11 @@ fun rememberQrBitmapPainter(
if (bitmap != null) return@LaunchedEffect

launch(Dispatchers.IO) {
val qrCodeWriter = QRCodeWriter()

val encodeHints = mutableMapOf<EncodeHintType, Any?>()
.apply {
this[EncodeHintType.MARGIN] = paddingPx
}

val bitmapMatrix = try {
qrCodeWriter.encode(
content, BarcodeFormat.QR_CODE,
sizePx, sizePx, encodeHints
)
} catch (ex: WriterException) {
null
}

val matrixWidth = bitmapMatrix?.width ?: sizePx
val matrixHeight = bitmapMatrix?.height ?: sizePx

val newBitmap = Bitmap.createBitmap(
bitmapMatrix?.width ?: sizePx,
bitmapMatrix?.height ?: sizePx,
Bitmap.Config.ARGB_8888,
bitmap = generateQr(
url = content,
size = sizePx,
padding = paddingPx,
)

val pixels = IntArray(matrixWidth * matrixHeight)

for (x in 0 until matrixWidth) {
for (y in 0 until matrixHeight) {
val shouldColorPixel = bitmapMatrix?.get(x, y) ?: false
val pixelColor = if (shouldColorPixel) Color.BLACK else Color.WHITE

pixels[y * matrixWidth + x] = pixelColor
}
}

newBitmap.setPixels(pixels, 0, matrixWidth, 0, 0, matrixWidth, matrixHeight)

bitmap = newBitmap
}
}

Expand All @@ -107,3 +66,51 @@ fun rememberQrBitmapPainter(
BitmapPainter(currentBitmap.asImageBitmap())
}
}

private fun generateQr(
url: String,
size: Int,
padding: Int,
contentColor: Int = Color.BLACK,
spaceColor: Int = Color.WHITE,
): Bitmap {
val qrCodeWriter = QRCodeWriter()

val encodeHints = mutableMapOf<EncodeHintType, Any?>()
.apply {
this[EncodeHintType.MARGIN] = padding
}

val bitmapMatrix = try {
qrCodeWriter.encode(
url, BarcodeFormat.QR_CODE,
size, size, encodeHints
)
} catch (ex: WriterException) {
null
}

val matrixWidth = bitmapMatrix?.width ?: size
val matrixHeight = bitmapMatrix?.height ?: size

val newBitmap = Bitmap.createBitmap(
bitmapMatrix?.width ?: size,
bitmapMatrix?.height ?: size,
Bitmap.Config.ARGB_8888,
)

val pixels = IntArray(matrixWidth * matrixHeight)

for (x in 0 until matrixWidth) {
for (y in 0 until matrixHeight) {
val shouldColorPixel = bitmapMatrix?.get(x, y) ?: false
val pixelColor = if (shouldColorPixel) contentColor else spaceColor

pixels[y * matrixWidth + x] = pixelColor
}
}

newBitmap.setPixels(pixels, 0, matrixWidth, 0, 0, matrixWidth, matrixHeight)

return newBitmap
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import android.annotation.SuppressLint
import android.os.Build
import com.getcode.analytics.Action
import com.getcode.analytics.ActionSource
import com.getcode.analytics.AnalyticsManager
import com.getcode.analytics.AnalyticsService
import com.getcode.manager.AuthManager
import com.getcode.media.MediaScanner
import com.getcode.manager.MnemonicManager
import com.getcode.media.MediaScanner
import com.getcode.navigation.core.CodeNavigator
import com.getcode.navigation.screens.CodeLoginPermission
import com.getcode.navigation.screens.HomeScreen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@ import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Typeface
import android.os.Environment
import androidmads.library.qrgenearator.QRGContents
import androidmads.library.qrgenearator.QRGEncoder
import androidx.core.graphics.applyCanvas
import androidx.core.graphics.drawable.toBitmap
import androidx.lifecycle.viewModelScope
import com.getcode.R
import com.getcode.media.MediaScanner
import com.getcode.manager.MnemonicManager
import com.getcode.manager.SessionManager
import com.getcode.manager.TopBarManager
import com.getcode.media.MediaScanner
import com.getcode.network.repository.TransactionRepository
import com.getcode.network.repository.TransactionRepository.DeniedReason
import com.getcode.network.repository.decodeBase64
import com.getcode.theme.Alert
import com.getcode.theme.R as themeR
import com.getcode.theme.Brand
import com.getcode.theme.Transparent
import com.getcode.theme.White
import com.getcode.ui.utils.toAGColor
import com.getcode.util.generateQrCode
Expand All @@ -41,6 +37,7 @@ import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import com.getcode.theme.R as themeR


data class AccessKeyUiModel(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.getcode.view.main.account

import android.annotation.SuppressLint
import androidx.lifecycle.viewModelScope
import com.getcode.media.MediaScanner
import com.getcode.manager.MnemonicManager
import com.getcode.navigation.core.CodeNavigator
Expand All @@ -10,7 +11,11 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Completable
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds


@HiltViewModel
Expand All @@ -20,23 +25,36 @@ class AccountAccessKeyViewModel @Inject constructor(
mediaScanner: MediaScanner,
) : BaseAccessKeyViewModel(resources, mnemonicManager, mediaScanner) {
@SuppressLint("CheckResult")
fun onSubmit(navigator: CodeNavigator) {
fun onSubmit() {
Completable.create {
val result = saveBitmapToFile()
if (result) it.onComplete() else it.onError(IllegalStateException())
}
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.computation())
.doOnSubscribe {
uiFlow.value = uiFlow.value.copy(isLoading = true, isEnabled = false)
uiFlow.update {
it.copy(isLoading = true, isEnabled = false)
}
}
.doOnComplete {
uiFlow.value =
uiFlow.value.copy(isLoading = false, isEnabled = false, isSuccess = true)
viewModelScope.launch {
uiFlow.update {
it.copy(isLoading = false, isEnabled = false, isSuccess = true)
}
// wait 2s and reset button state
delay(2.seconds)

uiFlow.update {
it.copy(isSuccess = false, isEnabled = true)
}

}
}
.doOnError {
uiFlow.value =
uiFlow.value.copy(isLoading = false, isEnabled = true, isSuccess = false)
uiFlow.update {
it.copy(isLoading = false, isEnabled = true, isSuccess = false)
}
}
.subscribe({}, ::onSubmitError)
}
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/getcode/view/main/account/BackupKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import com.getcode.util.launchAppSettings
fun BackupKey(
viewModel: AccountAccessKeyViewModel,
) {
val navigator = LocalCodeNavigator.current
val dataState by viewModel.uiFlow.collectAsState()

val context = LocalContext.current
Expand All @@ -83,7 +82,7 @@ fun BackupKey(
val launcher = getPermissionLauncher(onPermissionResult)

if (isExportSeedRequested && isStoragePermissionGranted) {
viewModel.onSubmit(navigator)
viewModel.onSubmit()
isExportSeedRequested = false
}

Expand Down
2 changes: 0 additions & 2 deletions buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ object Versions {
const val lib_phone_number_port: String = "8.12.43"
const val lib_phone_number_google: String = "8.12.54"
const val hilt_nav_compose: String = "1.1.0-alpha02"
const val qr_generator: String = "1.0.1"
const val zxing: String = "3.3.2"

const val androidx_test_runner = "1.4.0"
Expand Down Expand Up @@ -224,7 +223,6 @@ object Libs {
const val lib_phone_number_port = "io.michaelrocks:libphonenumber-android:${Versions.lib_phone_number_port}"
const val lib_phone_number_google = "com.googlecode.libphonenumber:libphonenumber:${Versions.lib_phone_number_google}"
const val hilt_nav_compose = "androidx.hilt:hilt-navigation-compose:1.1.0-alpha01"
const val qr_generator = "com.github.androidmads:QRGenerator:${Versions.qr_generator}"
const val zxing = "com.google.zxing:core:${Versions.zxing}"

const val retrofit = "com.squareup.retrofit2:retrofit:${Versions.retrofit}"
Expand Down

0 comments on commit d240be7

Please sign in to comment.