Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ dependencies {

implementation(libs.android.billing.client)

implementation(libs.zxing.core)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/com/creative/qrcodescanner/ui/QRApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.creative.qrcodescanner.R
import com.creative.qrcodescanner.ui.generate.GenerateQRScreenLayout
import com.creative.qrcodescanner.ui.history.HistoryScreenLayout
import com.creative.qrcodescanner.ui.main.MainScreenLayout
import com.creative.qrcodescanner.ui.main.MainViewModel
Expand Down Expand Up @@ -167,6 +168,9 @@ fun QRApp(vm: MainViewModel = hiltViewModel(),
composable(route = AppScreen.PREMIUM.value) {
PremiumScreenLayout(appNav = appNavHost)
}
composable(route = AppScreen.GENERATE.value) {
GenerateQRScreenLayout(appNav = appNavHost)
}
}
}

Expand All @@ -175,5 +179,6 @@ enum class AppScreen(val value: String) {
SETTING("setting"),
PREMIUM("premium"),
HISTORY("history"),
RESULT("result/{id}")
RESULT("result/{id}"),
GENERATE("generate")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.creative.qrcodescanner.ui.generate

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.windowInsetsBottomHeight
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavHostController
import com.creative.qrcodescanner.R
import com.creative.qrcodescanner.ui.nav.TopNavBar
import com.creative.qrcodescanner.ui.shadow

@Composable
fun GenerateQRScreenLayout(
appNav: NavHostController,
viewModel: GenerateQRViewModel = hiltViewModel()
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
var inputText by rememberSaveable { mutableStateOf("") }
val keyboardController = LocalSoftwareKeyboardController.current

BackHandler {
appNav.popBackStack()
}

Scaffold(
topBar = {
TopNavBar(titleResId = R.string.generate_qr_code) {
appNav.popBackStack()
}
},
bottomBar = {
Spacer(modifier = Modifier.windowInsetsBottomHeight(WindowInsets.navigationBars))
}
) { paddingValues ->
Column(
modifier = Modifier
.padding(paddingValues)
.fillMaxSize()
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
OutlinedTextField(
value = inputText,
onValueChange = { inputText = it },
label = { Text(text = stringResource(R.string.enter_text_to_generate_qr)) },
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight(),
maxLines = 5,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(onDone = {
keyboardController?.hide()
viewModel.generateQRCode(inputText)
})
)

TextButton(
onClick = {
keyboardController?.hide()
viewModel.generateQRCode(inputText)
},
modifier = Modifier
.fillMaxWidth()
.shadow(
MaterialTheme.colorScheme.primary.copy(alpha = 0.3f),
blurRadius = 8.dp, borderRadius = 8.dp, spread = 0.dp, offsetY = 0.dp, offsetX = 0.dp
)
.background(MaterialTheme.colorScheme.primary, shape = RoundedCornerShape(8.dp))
.clip(RoundedCornerShape(8.dp))
) {
Text(
text = stringResource(R.string.generate).uppercase(),
color = MaterialTheme.colorScheme.onPrimary
)
}

when (val state = uiState) {
is GenerateQRUIState.Success -> {
Image(
bitmap = state.bitmap.asImageBitmap(),
contentDescription = stringResource(R.string.qr_code_generated),
modifier = Modifier
.size(256.dp)
.shadow(
MaterialTheme.colorScheme.onBackground.copy(alpha = 0.1f),
blurRadius = 8.dp, borderRadius = 8.dp, spread = 0.dp, offsetY = 0.dp, offsetX = 0.dp
)
.background(Color.White, shape = RoundedCornerShape(8.dp))
.padding(8.dp)
)
}

is GenerateQRUIState.Error -> {
Text(
text = state.message,
color = MaterialTheme.colorScheme.error,
style = MaterialTheme.typography.bodyMedium
)
}

is GenerateQRUIState.Idle -> {
// No content to show yet
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.creative.qrcodescanner.ui.generate

import android.graphics.Bitmap
import android.graphics.Color
import androidx.lifecycle.ViewModel
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.qrcode.QRCodeWriter
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

sealed class GenerateQRUIState {
data object Idle : GenerateQRUIState()
data class Success(val bitmap: Bitmap) : GenerateQRUIState()
data class Error(val message: String) : GenerateQRUIState()
}

@HiltViewModel
class GenerateQRViewModel @Inject constructor() : ViewModel() {

private val _uiState: MutableStateFlow<GenerateQRUIState> = MutableStateFlow(GenerateQRUIState.Idle)
val uiState: StateFlow<GenerateQRUIState> = _uiState.asStateFlow()

fun generateQRCode(text: String, size: Int = 512) {
if (text.isBlank()) {
_uiState.value = GenerateQRUIState.Error("Please enter text")
return
}
try {
val hints = mapOf(
EncodeHintType.MARGIN to 1,
EncodeHintType.CHARACTER_SET to "UTF-8"
)
val bitMatrix = QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hints)
val pixels = IntArray(size * size)
for (y in 0 until size) {
for (x in 0 until size) {
pixels[y * size + x] = if (bitMatrix[x, y]) Color.BLACK else Color.WHITE
}
}
val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
bitmap.setPixels(pixels, 0, size, 0, 0, size, size)
_uiState.value = GenerateQRUIState.Success(bitmap)
} catch (e: Exception) {
_uiState.value = GenerateQRUIState.Error(e.message ?: "Failed to generate QR code")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.creative.qrcodescanner.ui.theme.QRCodeScannerTheme

@Composable
@Stable
fun BoxScope.FooterTools(navToHistory: () -> Unit, pickGallery: () -> Unit) {
fun BoxScope.FooterTools(navToHistory: () -> Unit, pickGallery: () -> Unit, navToGenerate: () -> Unit) {
Image(
painter = painterResource(id = R.drawable.add_photo_alternate),
contentDescription = stringResource(R.string.gallery_picker),
Expand All @@ -42,6 +42,21 @@ fun BoxScope.FooterTools(navToHistory: () -> Unit, pickGallery: () -> Unit) {
.padding(18.dp)
)

Image(
painter = painterResource(id = R.drawable.icon_qr_generate),
contentDescription = stringResource(R.string.generate_qr_button),
contentScale = ContentScale.Inside,
modifier = Modifier
.align(Alignment.BottomCenter)
.padding(16.dp)
.shadow(Color(0x901c1c1c).copy(alpha = 0.5f), blurRadius = 12.dp, borderRadius = 32.dp, spread = 0.dp, offsetY = 1.dp, offsetX = 1.dp)
.size(64.dp)
.background(color = Color(0x901c1c1c), shape = CircleShape)
.clip(CircleShape)
.clickable(onClick = navToGenerate)
.padding(18.dp)
)

Image(
painter = painterResource(id = R.drawable.history),
contentDescription = stringResource(R.string.history_picker),
Expand All @@ -63,7 +78,7 @@ fun BoxScope.FooterTools(navToHistory: () -> Unit, pickGallery: () -> Unit) {
fun FooterToolsPreview() {
QRCodeScannerTheme {
Box(modifier = Modifier.fillMaxWidth()) {
FooterTools(navToHistory = {}, pickGallery = {})
FooterTools(navToHistory = {}, pickGallery = {}, navToGenerate = {})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ fun MainScreenLayout(vm: MainViewModel, appNavHost: NavHostController) {
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)
)
}
},
navToGenerate = remember {
{
appNavHost.navigate(AppScreen.GENERATE.value)
}
})
}

Expand Down
30 changes: 30 additions & 0 deletions app/src/main/res/drawable/icon_qr_generate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFF"
android:pathData="M3,11V3H11V11H3ZM5,5V9H9V5H5Z"/>
<path
android:fillColor="#FFFFFF"
android:pathData="M3,21V13H11V21H3ZM5,15V19H9V15H5Z"/>
<path
android:fillColor="#FFFFFF"
android:pathData="M13,3V11H21V3H13ZM19,9H15V5H19V9Z"/>
<path
android:fillColor="#FFFFFF"
android:pathData="M18,13H21V16H18V13Z"/>
<path
android:fillColor="#FFFFFF"
android:pathData="M13,13H16V16H13V13Z"/>
<path
android:fillColor="#FFFFFF"
android:pathData="M16,16H18V18H16V16Z"/>
<path
android:fillColor="#FFFFFF"
android:pathData="M18,18H21V21H18V18Z"/>
<path
android:fillColor="#FFFFFF"
android:pathData="M13,18H16V21H13V18Z"/>
</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,10 @@
<string name="only_2_99_first_3_months">2.99$ First 3 Months</string>
<string name="processing_purchase">Processing purchase...</string>
<string name="billing_service_is_not_available_please_check_your_network_connection_and_try_again">Billing Service is not available, please check your network connection and try again.</string>
<string name="generate_qr_code">Generate QR Code</string>
<string name="generate_qr_button">Generate QR</string>
<string name="enter_text_to_generate_qr">Enter text to generate QR code</string>
<string name="generate">Generate</string>
<string name="qr_code_generated">QR Code Generated</string>
<string name="please_enter_text">Please enter text</string>
</resources>
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ datastore_version = "1.0.0"
billing_version = "6.1.0"
firebaseCrashlyticsBuildtools = "2.9.9"
ksp_version = "1.9.0-1.0.13"
zxing_version = "3.5.3"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
Expand Down Expand Up @@ -71,6 +72,7 @@ androidx-datastore-preferences = { group = "androidx.datastore", name = "datasto

android-billing-client = { group = "com.android.billingclient", name = "billing", version.ref = "billing_version" }
firebase-crashlytics-buildtools = { group = "com.google.firebase", name = "firebase-crashlytics-buildtools", version.ref = "firebaseCrashlyticsBuildtools" }
zxing-core = { group = "com.google.zxing", name = "core", version.ref = "zxing_version" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
Expand Down