From 6ce23c2081551cfe2b7312c05ea89c307b528a13 Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 19 Oct 2024 23:09:50 +0300 Subject: [PATCH] handling non-existend abstract products --- .../activities/AddAbstractProductActivity.kt | 24 +++++++++----- .../activities/AddProductActivity.kt | 31 ++++++++++++++++--- app/src/main/res/values-ru/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt index a9e4000..1d0a553 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt @@ -45,6 +45,7 @@ class AddAbstractProductActivity : AppCompatActivity() { private lateinit var pictureFile: File private lateinit var picturesPath: File private var barcode: String = "" + private var updatingExisting = false private var scanningBarcode = false @@ -55,9 +56,6 @@ class AddAbstractProductActivity : AppCompatActivity() { val extras = intent.extras abstractProduct = extras!!.get("abstractProduct") as AbstractProduct? - if (abstractProduct != null) { - barcode = abstractProduct!!.barcode - } picturesPath = File(filesDir, "pictures") val thumbnailsDir = File(cacheDir, "thumbnails") @@ -83,10 +81,20 @@ class AddAbstractProductActivity : AppCompatActivity() { this.barcode = barcodeText.text.toString() } - if (abstractProduct?.name == "" && abstractProduct?.barcode != "") { - performRequest(abstractProduct?.barcode!!) + if (abstractProduct != null) { + barcode = abstractProduct!!.barcode + updatingExisting = false + performRequest(abstractProduct!!.barcode) + } else { + updatingExisting = true } + +// if (abstractProduct?.name == "" && abstractProduct?.barcode != "") { +// updatingExisting = true +// performRequest(abstractProduct?.barcode!!) +// } + if (abstractProduct != null) { val imageThumbnailUri = getImageUri(this, File(thumbnailsDir, "${abstractProduct!!.imageHash}.webp")) pictureFile = File(picturesPath, "${abstractProduct!!.imageHash}.png]") @@ -130,15 +138,15 @@ class AddAbstractProductActivity : AppCompatActivity() { put(AbstractProductContract.AbstractProductEntry.UNIT, unitTypeSpinner.selectedItemPosition) } - if (abstractProduct == null) { - db.insert(AbstractProductContract.AbstractProductEntry.TABLE_NAME, null, values) - } else { + if (updatingExisting) { db.update( AbstractProductContract.AbstractProductEntry.TABLE_NAME, values, "${BaseColumns._ID} = ?", arrayOf(abstractProduct!!.id.toString()) ) + } else { + db.insert(AbstractProductContract.AbstractProductEntry.TABLE_NAME, null, values) } finish() diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt index 773f0ac..f1a0e01 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt @@ -1,13 +1,16 @@ package org.foxarmy.barcodescannerforemployees.activities import android.app.DatePickerDialog +import android.content.Intent import android.os.Build import android.os.Bundle import android.view.View import android.widget.* import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.RequiresApi +import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat import androidx.core.widget.addTextChangedListener import com.journeyapps.barcodescanner.ScanContract import com.journeyapps.barcodescanner.ScanIntentResult @@ -206,9 +209,24 @@ class AddProductActivity : AppCompatActivity() { product!!.dateOfExpiry = c.timeInMillis / 1000 } - private fun displayAbstractProduct(abstractProduct: AbstractProduct) { - abstractProductView.abstractProduct = abstractProduct - abstractProductView.update() + private fun displayAbstractProduct(abstractProduct: AbstractProduct?, scannedBarcode: String) { + if (abstractProduct == null) { + AlertDialog.Builder(this) + .setMessage(getString(R.string.abstract_product_does_not_exist)) + .setPositiveButton(R.string.yes) { _, _ -> + val addAbstractProductIntent = Intent(this, AddAbstractProductActivity::class.java) + val extras = Bundle() + extras.putParcelable("abstractProduct", AbstractProduct(0, scannedBarcode, "", 0.0, "", 0, 0)) + addAbstractProductIntent.putExtras(extras) + ContextCompat.startActivity(this, addAbstractProductIntent, extras) + } + .setNegativeButton(R.string.no) {_, _ -> + + }.show() + } else { + abstractProductView.abstractProduct = abstractProduct + abstractProductView.update() + } } @RequiresApi(Build.VERSION_CODES.R) @@ -230,8 +248,11 @@ class AddProductActivity : AppCompatActivity() { } else { val scannedBarcode = result.contents abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, scannedBarcode) - displayAbstractProduct(abstractProduct!!) - product?.abstractProductId = abstractProduct!!.id + + displayAbstractProduct(abstractProduct, scannedBarcode) + if (abstractProduct != null) { + product?.abstractProductId = abstractProduct!!.id + } } } } diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index fe5cac0..3d39ce8 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -77,4 +77,5 @@ Имя Категория + Абстрактный продукт с таким штрихкодом не существует. Хотите его добавить?. \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3c11ee2..8900d29 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -75,4 +75,5 @@ Expired Barcode Scan + Abstract product with such barcode does not exist. Do you want to add one? \ No newline at end of file