forked from omni/OmniCards
feat: added editing cards feature
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
android:screenOrientation="portrait"
|
||||
android:exported="false" />
|
||||
<activity
|
||||
android:name=".AddCardActivity"
|
||||
android:name=".EditCardActivity"
|
||||
android:screenOrientation="portrait"
|
||||
android:exported="false" />
|
||||
<activity
|
||||
|
||||
@@ -3,6 +3,11 @@ package ru.omni_devel.cards
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
enum class EditCardAction {
|
||||
ADD,
|
||||
EDIT,
|
||||
}
|
||||
|
||||
@Serializable
|
||||
class CardInfo (
|
||||
val id: Int,
|
||||
|
||||
@@ -33,6 +33,20 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
||||
db.close()
|
||||
}
|
||||
|
||||
fun editCard(id: Int, name: String, codeValue: String, codeType: BarcodeFormat) {
|
||||
val values = ContentValues()
|
||||
|
||||
values.put("name", name)
|
||||
values.put("codeValue", codeValue)
|
||||
values.put("codeType", codeType.name)
|
||||
|
||||
val db = this.writableDatabase
|
||||
|
||||
db.update("cards", values, "id = ?", arrayOf(id.toString()))
|
||||
|
||||
db.close()
|
||||
}
|
||||
|
||||
fun getCards(): List<CardInfo> {
|
||||
val db = this.readableDatabase
|
||||
|
||||
|
||||
+49
-3
@@ -1,10 +1,13 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import android.widget.Spinner
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
@@ -13,7 +16,7 @@ import com.google.zxing.BarcodeFormat
|
||||
import com.journeyapps.barcodescanner.ScanContract
|
||||
import com.journeyapps.barcodescanner.ScanOptions
|
||||
|
||||
class AddCardActivity : AppCompatActivity() {
|
||||
class EditCardActivity : AppCompatActivity() {
|
||||
private lateinit var db: DbHelper
|
||||
|
||||
private val barcodeLauncher = registerForActivityResult(ScanContract()) { result ->
|
||||
@@ -27,15 +30,19 @@ class AddCardActivity : AppCompatActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContentView(R.layout.activity_add_card)
|
||||
setContentView(R.layout.activity_edit_card)
|
||||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
|
||||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
||||
insets
|
||||
}
|
||||
|
||||
val actionName = intent.getStringExtra("action") ?: EditCardAction.ADD.name
|
||||
val action = EditCardAction.valueOf(actionName)
|
||||
|
||||
db = DbHelper(this, null)
|
||||
|
||||
val actionLabel: TextView = findViewById(R.id.editCardActionLabel)
|
||||
val cardNameInput: EditText = findViewById(R.id.cardNameInput)
|
||||
val cardValueInput: EditText = findViewById(R.id.cardValueInput)
|
||||
val scanCodeButton: Button = findViewById(R.id.scanCodeButton)
|
||||
@@ -46,7 +53,6 @@ class AddCardActivity : AppCompatActivity() {
|
||||
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, options)
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||
cardCodeTypeSelector.adapter = adapter
|
||||
setCodeType("QR_CODE")
|
||||
|
||||
scanCodeButton.setOnClickListener {
|
||||
val options = ScanOptions()
|
||||
@@ -61,10 +67,50 @@ class AddCardActivity : AppCompatActivity() {
|
||||
val cardValue = cardValueInput.text.toString()
|
||||
val cardCodeType = BarcodeFormat.valueOf(cardCodeTypeSelector.selectedItem.toString())
|
||||
|
||||
when (action) {
|
||||
EditCardAction.ADD -> {
|
||||
db.addCard(cardName, cardValue, cardCodeType)
|
||||
|
||||
finish()
|
||||
}
|
||||
EditCardAction.EDIT -> {
|
||||
val cardId = intent.getIntExtra("cardId", -1)
|
||||
|
||||
db.editCard(cardId, cardName, cardValue, cardCodeType)
|
||||
|
||||
val intent = Intent(this, ShowCardActivity::class.java)
|
||||
|
||||
intent.putExtra("cardId", cardId)
|
||||
|
||||
startActivity(intent)
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
when (action) {
|
||||
EditCardAction.ADD -> {
|
||||
setCodeType("QR_CODE")
|
||||
|
||||
actionLabel.text = "Adding card"
|
||||
}
|
||||
EditCardAction.EDIT -> {
|
||||
val cardId = intent.getIntExtra("cardId", -1)
|
||||
|
||||
val cardInfo = db.getCard(cardId)
|
||||
|
||||
if (cardInfo == null) {
|
||||
Toast.makeText(this, "cardInfo is null", Toast.LENGTH_LONG).show()
|
||||
return
|
||||
}
|
||||
|
||||
actionLabel.text = "Editing card"
|
||||
|
||||
cardNameInput.setText(cardInfo.name)
|
||||
cardValueInput.setText(cardInfo.codeValue)
|
||||
setCodeType(cardInfo.codeType.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setCodeType(codeTypeString: String) {
|
||||
@@ -14,7 +14,6 @@ import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import com.google.android.material.button.MaterialButton
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.w3c.dom.Text
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
private lateinit var cardsList: LinearLayout
|
||||
@@ -37,7 +36,11 @@ class MainActivity : AppCompatActivity() {
|
||||
val syncWithWearOSButton: Button = findViewById(R.id.syncWithWearOSButton)
|
||||
|
||||
addCardButton.setOnClickListener {
|
||||
startActivity(Intent(this, AddCardActivity::class.java))
|
||||
val intent = Intent(this, EditCardActivity::class.java)
|
||||
|
||||
intent.putExtra("action", EditCardAction.ADD.name)
|
||||
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
syncWithWearOSButton.setOnClickListener {
|
||||
@@ -101,10 +104,21 @@ class MainActivity : AppCompatActivity() {
|
||||
button.setOnLongClickListener { view ->
|
||||
val popup = PopupMenu(this, view)
|
||||
|
||||
popup.menu.add("Edit")
|
||||
popup.menu.add("Remove")
|
||||
|
||||
popup.setOnMenuItemClickListener { menuItem ->
|
||||
when (menuItem.title) {
|
||||
"Edit" -> {
|
||||
val intent = Intent(this, EditCardActivity::class.java)
|
||||
|
||||
intent.putExtra("action", EditCardAction.EDIT.name)
|
||||
intent.putExtra("cardId", card.id)
|
||||
|
||||
startActivity(intent)
|
||||
|
||||
true
|
||||
}
|
||||
"Remove" -> {
|
||||
db.removeCard(card.id)
|
||||
renderCards()
|
||||
|
||||
@@ -46,12 +46,11 @@ class ShowCardActivity : AppCompatActivity() {
|
||||
|
||||
if (cardInfo == null) {
|
||||
Toast.makeText(this, "cardInfo is null", Toast.LENGTH_LONG).show()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
val cardCodeView: ImageView = findViewById(R.id.cardCodeView)
|
||||
val cardNameElement: TextView = findViewById(R.id.cardName)
|
||||
val cardNameElement: TextView = findViewById(R.id.editCardActionLabel)
|
||||
|
||||
cardNameElement.text = cardInfo.name
|
||||
|
||||
|
||||
+3
-3
@@ -5,7 +5,7 @@
|
||||
android:id="@+id/main"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".AddCardActivity">
|
||||
tools:context=".EditCardActivity">
|
||||
|
||||
<androidx.appcompat.widget.Toolbar
|
||||
android:id="@+id/toolbar2"
|
||||
@@ -19,11 +19,11 @@
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/cardName"
|
||||
android:id="@+id/editCardActionLabel"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="16dp"
|
||||
android:text="Adding card"
|
||||
android:text="Action"
|
||||
android:textColor="@color/text_primary"
|
||||
android:textSize="24sp"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/toolbar2"
|
||||
@@ -5,7 +5,7 @@
|
||||
android:id="@+id/main"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".AddCardActivity">
|
||||
tools:context=".EditCardActivity">
|
||||
|
||||
<androidx.appcompat.widget.Toolbar
|
||||
android:id="@+id/toolbar2"
|
||||
@@ -19,7 +19,7 @@
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/cardName"
|
||||
android:id="@+id/editCardActionLabel"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="16dp"
|
||||
|
||||
Reference in New Issue
Block a user