forked from omni/OmniCards
feat: added code scanning
This commit is contained in:
@@ -13,12 +13,15 @@
|
|||||||
android:theme="@style/Base.Theme.OmniCards">
|
android:theme="@style/Base.Theme.OmniCards">
|
||||||
<activity
|
<activity
|
||||||
android:name=".ShowCardActivity"
|
android:name=".ShowCardActivity"
|
||||||
|
android:screenOrientation="portrait"
|
||||||
android:exported="false" />
|
android:exported="false" />
|
||||||
<activity
|
<activity
|
||||||
android:name=".AddCardActivity"
|
android:name=".AddCardActivity"
|
||||||
|
android:screenOrientation="portrait"
|
||||||
android:exported="false" />
|
android:exported="false" />
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
|
android:screenOrientation="portrait"
|
||||||
android:exported="true">
|
android:exported="true">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|||||||
@@ -10,10 +10,25 @@ import androidx.appcompat.app.AppCompatActivity
|
|||||||
import androidx.core.view.ViewCompat
|
import androidx.core.view.ViewCompat
|
||||||
import androidx.core.view.WindowInsetsCompat
|
import androidx.core.view.WindowInsetsCompat
|
||||||
import com.google.zxing.BarcodeFormat
|
import com.google.zxing.BarcodeFormat
|
||||||
|
import com.journeyapps.barcodescanner.ScanContract
|
||||||
|
import com.journeyapps.barcodescanner.ScanOptions
|
||||||
|
|
||||||
class AddCardActivity : AppCompatActivity() {
|
class AddCardActivity : AppCompatActivity() {
|
||||||
private lateinit var db: DbHelper
|
private lateinit var db: DbHelper
|
||||||
|
|
||||||
|
private val barcodeLauncher = registerForActivityResult(ScanContract()) { result ->
|
||||||
|
if (result.contents != null) {
|
||||||
|
val codeTypes = BarcodeFormat.entries.map { it.name }
|
||||||
|
val codeTypeIndex = codeTypes.indexOf(result.formatName)
|
||||||
|
|
||||||
|
if (codeTypeIndex >= 0) {
|
||||||
|
findViewById<Spinner>(R.id.cardCodeTypeSelector).setSelection(codeTypeIndex)
|
||||||
|
}
|
||||||
|
|
||||||
|
findViewById<EditText>(R.id.cardValueInput).setText(result.contents)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
@@ -28,6 +43,7 @@ class AddCardActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
val cardNameInput: EditText = findViewById(R.id.cardNameInput)
|
val cardNameInput: EditText = findViewById(R.id.cardNameInput)
|
||||||
val cardValueInput: EditText = findViewById(R.id.cardValueInput)
|
val cardValueInput: EditText = findViewById(R.id.cardValueInput)
|
||||||
|
val scanCodeButton: Button = findViewById(R.id.scanCodeButton)
|
||||||
val cardCodeTypeSelector: Spinner = findViewById(R.id.cardCodeTypeSelector)
|
val cardCodeTypeSelector: Spinner = findViewById(R.id.cardCodeTypeSelector)
|
||||||
val saveCardButton: Button = findViewById(R.id.saveCardButton)
|
val saveCardButton: Button = findViewById(R.id.saveCardButton)
|
||||||
|
|
||||||
@@ -36,6 +52,14 @@ class AddCardActivity : AppCompatActivity() {
|
|||||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||||
cardCodeTypeSelector.adapter = adapter
|
cardCodeTypeSelector.adapter = adapter
|
||||||
|
|
||||||
|
scanCodeButton.setOnClickListener {
|
||||||
|
val options = ScanOptions()
|
||||||
|
options.setPrompt("Scan code")
|
||||||
|
options.setBeepEnabled(false)
|
||||||
|
options.setOrientationLocked(false)
|
||||||
|
barcodeLauncher.launch(options)
|
||||||
|
}
|
||||||
|
|
||||||
saveCardButton.setOnClickListener {
|
saveCardButton.setOnClickListener {
|
||||||
val cardName = cardNameInput.text.toString()
|
val cardName = cardNameInput.text.toString()
|
||||||
val cardValue = cardValueInput.text.toString()
|
val cardValue = cardValueInput.text.toString()
|
||||||
|
|||||||
@@ -50,13 +50,26 @@
|
|||||||
android:hint="Card name"
|
android:hint="Card name"
|
||||||
android:inputType="text" />
|
android:inputType="text" />
|
||||||
|
|
||||||
<EditText
|
<LinearLayout
|
||||||
android:id="@+id/cardValueInput"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="46dp"
|
android:layout_height="wrap_content"
|
||||||
android:ems="10"
|
android:orientation="horizontal">
|
||||||
android:hint="Card value"
|
|
||||||
android:inputType="text" />
|
<EditText
|
||||||
|
android:id="@+id/cardValueInput"
|
||||||
|
android:layout_width="259dp"
|
||||||
|
android:layout_height="46dp"
|
||||||
|
android:ems="10"
|
||||||
|
android:hint="Card value"
|
||||||
|
android:inputType="text" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/scanCodeButton"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="38dp"
|
||||||
|
android:text="Scan" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
<Spinner
|
<Spinner
|
||||||
android:id="@+id/cardCodeTypeSelector"
|
android:id="@+id/cardCodeTypeSelector"
|
||||||
|
|||||||
Reference in New Issue
Block a user