dialog on unknown barcode on adding product

This commit is contained in:
leca 2024-10-16 01:57:53 +03:00
parent 224887422c
commit fd7feb72eb
2 changed files with 47 additions and 29 deletions

View File

@ -49,6 +49,8 @@ class AddAbstractProductActivity : AppCompatActivity() {
barcode = abstractProduct!!.barcode barcode = abstractProduct!!.barcode
} }
picturesPath = File(filesDir, "pictures") picturesPath = File(filesDir, "pictures")
val thumbnailsDir = File(cacheDir, "thumbnails") val thumbnailsDir = File(cacheDir, "thumbnails")
thumbnailsDir.mkdirs() thumbnailsDir.mkdirs()
@ -67,6 +69,10 @@ class AddAbstractProductActivity : AppCompatActivity() {
fillupCategorySpinner() fillupCategorySpinner()
if (abstractProduct?.name == "" && abstractProduct?.barcode != "") {
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,27 +136,8 @@ class AddAbstractProductActivity : AppCompatActivity() {
scanner.startScan() scanner.startScan()
.addOnSuccessListener { barcode -> .addOnSuccessListener { barcode ->
this.barcode = barcode.rawValue.toString() this.barcode = barcode.rawValue.toString()
barcodeText.setText(this.barcode)
val requester = Requester("https://ean-online.ru", "match.php")
requester.request(this, barcode.rawValue!!.toString())
var abstractProduct: AbstractProduct
thread { performRequest(this.barcode)
// Я сам в ахуях какой это костыль, пока хз как фиксить, потом придумаю :))
while (requester.response == "") { }
if (requester.response == "Not found 404") {
runOnUiThread {
Toast.makeText(this, "Product not found. Please, try again or type manually", Toast.LENGTH_LONG).show()
}
return@thread
}
abstractProduct = Parser().parse(requester.response)
requester.response = ""
runOnUiThread {
productNameText.text = abstractProduct.name
netWeightText.text = abstractProduct.netWeight.toString()
}
}
} }
.addOnFailureListener { e -> .addOnFailureListener { e ->
Toast.makeText( Toast.makeText(
@ -162,6 +149,30 @@ class AddAbstractProductActivity : AppCompatActivity() {
} }
} }
fun performRequest(barcode: String) {
barcodeText.setText(this.barcode)
val requester = Requester("https://ean-online.ru", "match.php")
requester.request(this, barcode)
var abstractProduct: AbstractProduct
thread {
// Я сам в ахуях какой это костыль, пока хз как фиксить, потом придумаю :))
while (requester.response == "") { }
if (requester.response == "Not found 404") {
runOnUiThread {
Toast.makeText(this, "Product not found. Please, try again or type manually", Toast.LENGTH_LONG).show()
}
return@thread
}
abstractProduct = Parser().parse(requester.response)
requester.response = ""
runOnUiThread {
productNameText.text = abstractProduct.name
netWeightText.text = abstractProduct.netWeight.toString()
}
}
}
fun fillupCategorySpinner() { fun fillupCategorySpinner() {
val db = DBStorageController(this).readableDatabase val db = DBStorageController(this).readableDatabase

View File

@ -1,13 +1,18 @@
package org.foxarmy.barcodescannerforemployees.activities package org.foxarmy.barcodescannerforemployees.activities
import android.app.DatePickerDialog import android.app.DatePickerDialog
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import android.widget.* import android.widget.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
import org.foxarmy.barcodescannerforemployees.dataclasses.Product import org.foxarmy.barcodescannerforemployees.dataclasses.Product
import org.foxarmy.barcodescannerforemployees.views.AbstractProductView import org.foxarmy.barcodescannerforemployees.views.AbstractProductView
import java.util.* import java.util.*
@ -93,14 +98,6 @@ class AddProductActivity : AppCompatActivity() {
expiryDateOverShelfLife = false expiryDateOverShelfLife = false
} }
// shelfLifeTextEdit.addTextChangedListener {
// if (shelfLifeTextEdit.text.toString() == "") {
// return@addTextChangedListener
// }
//
// evaluateDateOfExpiry()
// }
dateOfProductionSelectButton.setOnClickListener { dateOfProductionSelectButton.setOnClickListener {
val c = Calendar.getInstance() val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR) val year = c.get(Calendar.YEAR)
@ -180,7 +177,17 @@ class AddProductActivity : AppCompatActivity() {
fun findAndDisplayAbstractProductByBarcode(barcode: String) { fun findAndDisplayAbstractProductByBarcode(barcode: String) {
val abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, barcode) val abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, barcode)
if (abstractProduct == null) { if (abstractProduct == null) {
Toast.makeText(this, "No product found", Toast.LENGTH_SHORT).show() AlertDialog.Builder(this).setMessage("Product with such barcode not found. Try scanning with better lightning or add new abstract product")
.setPositiveButton("Add") { _: DialogInterface, _: Int ->
val addAbstractProductIntent = Intent(this, AddAbstractProductActivity::class.java)
val extras = Bundle()
extras.putParcelable("abstractProduct", AbstractProduct(0, barcode, "", 0.0, "", 0))
addAbstractProductIntent.putExtras(extras)
ContextCompat.startActivity(this, addAbstractProductIntent, extras)
}
.setNegativeButton("Try again") {_: DialogInterface, _: Int ->
}.show()
return return
} }
abstractProductView.abstractProduct = abstractProduct abstractProductView.abstractProduct = abstractProduct