import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanOptions
import com.neco_desarrollo.qrcodescanerjetpack.data.MainDb
import com.neco_desarrollo.qrcodescanerjetpack.data.Product
import com.neco_desarrollo.qrcodescanerjetpack.ui.theme.QRcodeScanerJetpackTheme
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@Inject
lateinit var mainDb: MainDb
var counter = 0
private val scanLauncher = registerForActivityResult(
ScanContract()
) { result ->
if (result.contents == null) {
} else {
Toast.makeText(
this,
"Scan data: ${result.contents}", Toast.LENGTH_SHORT
).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val productStateList = mainDb.dao.getAllProducts()
.collectAsState(initial = emptyList())
val coroutineScope = rememberCoroutineScope()
QRcodeScanerJetpackTheme {
Column(
Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
LazyColumn(
modifier = Modifier
.padding(top = 15.dp)
.fillMaxWidth()
.fillMaxHeight(0.9f),
) {
items(productStateList.value) { product ->
Text(
modifier = Modifier.fillMaxWidth(),
text = product.name,
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(10.dp))
}
}
Button(onClick = {
coroutineScope.launch {
mainDb.dao.insertProduct(
Product(
null,
"Product ${counter++}",
"asldfkjasldkfasldkfj"
)
)
}
}) {
Text(text = "Create data")
}
}
}
}
}
private fun scan() {
val options = ScanOptions()
options.setDesiredBarcodeFormats(ScanOptions.QR_CODE)
options.setPrompt("Scan a barcode")
options.setCameraId(0)
options.setBeepEnabled(false)
options.setBarcodeImageEnabled(true)
scanLauncher.launch(options)
}
}
Hello, I admire your works and want to repeat one of them. I already have experience building metal detectors and am going to repeat your model Spirit Pi 4 Pro A because I liked it. You can modify the code a little and discriminate between metals with an inscription on the screen and a different sound for non-ferrous metal and ferrous metal