handling non-existend abstract products

This commit is contained in:
leca 2024-10-19 23:09:50 +03:00
parent 8a018903f0
commit 6ce23c2081
4 changed files with 44 additions and 13 deletions

View File

@ -45,6 +45,7 @@ class AddAbstractProductActivity : AppCompatActivity() {
private lateinit var pictureFile: File private lateinit var pictureFile: File
private lateinit var picturesPath: File private lateinit var picturesPath: File
private var barcode: String = "" private var barcode: String = ""
private var updatingExisting = false
private var scanningBarcode = false private var scanningBarcode = false
@ -55,9 +56,6 @@ class AddAbstractProductActivity : AppCompatActivity() {
val extras = intent.extras val extras = intent.extras
abstractProduct = extras!!.get("abstractProduct") as AbstractProduct? abstractProduct = extras!!.get("abstractProduct") as AbstractProduct?
if (abstractProduct != null) {
barcode = abstractProduct!!.barcode
}
picturesPath = File(filesDir, "pictures") picturesPath = File(filesDir, "pictures")
val thumbnailsDir = File(cacheDir, "thumbnails") val thumbnailsDir = File(cacheDir, "thumbnails")
@ -83,10 +81,20 @@ class AddAbstractProductActivity : AppCompatActivity() {
this.barcode = barcodeText.text.toString() this.barcode = barcodeText.text.toString()
} }
if (abstractProduct?.name == "" && abstractProduct?.barcode != "") { if (abstractProduct != null) {
performRequest(abstractProduct?.barcode!!) barcode = abstractProduct!!.barcode
updatingExisting = false
performRequest(abstractProduct!!.barcode)
} else {
updatingExisting = true
} }
// if (abstractProduct?.name == "" && abstractProduct?.barcode != "") {
// updatingExisting = true
// performRequest(abstractProduct?.barcode!!)
// }
if (abstractProduct != null) { if (abstractProduct != null) {
val imageThumbnailUri = getImageUri(this, File(thumbnailsDir, "${abstractProduct!!.imageHash}.webp")) val imageThumbnailUri = getImageUri(this, File(thumbnailsDir, "${abstractProduct!!.imageHash}.webp"))
pictureFile = File(picturesPath, "${abstractProduct!!.imageHash}.png]") pictureFile = File(picturesPath, "${abstractProduct!!.imageHash}.png]")
@ -130,15 +138,15 @@ class AddAbstractProductActivity : AppCompatActivity() {
put(AbstractProductContract.AbstractProductEntry.UNIT, unitTypeSpinner.selectedItemPosition) put(AbstractProductContract.AbstractProductEntry.UNIT, unitTypeSpinner.selectedItemPosition)
} }
if (abstractProduct == null) { if (updatingExisting) {
db.insert(AbstractProductContract.AbstractProductEntry.TABLE_NAME, null, values)
} else {
db.update( db.update(
AbstractProductContract.AbstractProductEntry.TABLE_NAME, AbstractProductContract.AbstractProductEntry.TABLE_NAME,
values, values,
"${BaseColumns._ID} = ?", "${BaseColumns._ID} = ?",
arrayOf(abstractProduct!!.id.toString()) arrayOf(abstractProduct!!.id.toString())
) )
} else {
db.insert(AbstractProductContract.AbstractProductEntry.TABLE_NAME, null, values)
} }
finish() finish()

View File

@ -1,13 +1,16 @@
package org.foxarmy.barcodescannerforemployees.activities package org.foxarmy.barcodescannerforemployees.activities
import android.app.DatePickerDialog import android.app.DatePickerDialog
import android.content.Intent
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import android.widget.* import android.widget.*
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener import androidx.core.widget.addTextChangedListener
import com.journeyapps.barcodescanner.ScanContract import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanIntentResult import com.journeyapps.barcodescanner.ScanIntentResult
@ -206,10 +209,25 @@ class AddProductActivity : AppCompatActivity() {
product!!.dateOfExpiry = c.timeInMillis / 1000 product!!.dateOfExpiry = c.timeInMillis / 1000
} }
private fun displayAbstractProduct(abstractProduct: AbstractProduct) { 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.abstractProduct = abstractProduct
abstractProductView.update() abstractProductView.update()
} }
}
@RequiresApi(Build.VERSION_CODES.R) @RequiresApi(Build.VERSION_CODES.R)
val requestPermissionLauncher = val requestPermissionLauncher =
@ -230,11 +248,14 @@ class AddProductActivity : AppCompatActivity() {
} else { } else {
val scannedBarcode = result.contents val scannedBarcode = result.contents
abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, scannedBarcode) abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, scannedBarcode)
displayAbstractProduct(abstractProduct!!)
displayAbstractProduct(abstractProduct, scannedBarcode)
if (abstractProduct != null) {
product?.abstractProductId = abstractProduct!!.id product?.abstractProductId = abstractProduct!!.id
} }
} }
} }
}
private fun prepareBarcodeScanner() { private fun prepareBarcodeScanner() {
val options = ScanOptions() val options = ScanOptions()

View File

@ -77,4 +77,5 @@
<item>Имя</item> <item>Имя</item>
<item>Категория</item> <item>Категория</item>
</string-array> </string-array>
<string name="abstract_product_does_not_exist">Абстрактный продукт с таким штрихкодом не существует. Хотите его добавить?. </string>
</resources> </resources>

View File

@ -75,4 +75,5 @@
<string name="expired">Expired</string> <string name="expired">Expired</string>
<string name="barcode">Barcode</string> <string name="barcode">Barcode</string>
<string name="scan_label">Scan</string> <string name="scan_label">Scan</string>
<string name="abstract_product_does_not_exist">Abstract product with such barcode does not exist. Do you want to add one?</string>
</resources> </resources>