handling non-existend abstract products
This commit is contained in:
parent
8a018903f0
commit
6ce23c2081
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,4 +77,5 @@
|
|||
<item>Имя</item>
|
||||
<item>Категория</item>
|
||||
</string-array>
|
||||
<string name="abstract_product_does_not_exist">Абстрактный продукт с таким штрихкодом не существует. Хотите его добавить?. </string>
|
||||
</resources>
|
|
@ -75,4 +75,5 @@
|
|||
<string name="expired">Expired</string>
|
||||
<string name="barcode">Barcode</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>
|
Loading…
Reference in New Issue