From fd7feb72eb42fecd32b295d179711fa4cae050a2 Mon Sep 17 00:00:00 2001 From: leca Date: Wed, 16 Oct 2024 01:57:53 +0300 Subject: [PATCH] dialog on unknown barcode on adding product --- .../activities/AddAbstractProductActivity.kt | 51 +++++++++++-------- .../activities/AddProductActivity.kt | 25 +++++---- 2 files changed, 47 insertions(+), 29 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 c525e3e..9999719 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt @@ -49,6 +49,8 @@ class AddAbstractProductActivity : AppCompatActivity() { barcode = abstractProduct!!.barcode } + + picturesPath = File(filesDir, "pictures") val thumbnailsDir = File(cacheDir, "thumbnails") thumbnailsDir.mkdirs() @@ -67,6 +69,10 @@ class AddAbstractProductActivity : AppCompatActivity() { fillupCategorySpinner() + if (abstractProduct?.name == "" && abstractProduct?.barcode != "") { + performRequest(abstractProduct?.barcode!!) + } + if (abstractProduct != null) { val imageThumbnailUri = getImageUri(this, File(thumbnailsDir, "${abstractProduct!!.imageHash}.webp")) pictureFile = File(picturesPath, "${abstractProduct!!.imageHash}.png]") @@ -130,27 +136,8 @@ class AddAbstractProductActivity : AppCompatActivity() { scanner.startScan() .addOnSuccessListener { barcode -> 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 { - // Я сам в ахуях какой это костыль, пока хз как фиксить, потом придумаю :)) - 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() - } - } + performRequest(this.barcode) } .addOnFailureListener { e -> 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() { val db = DBStorageController(this).readableDatabase 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 c0f7098..96a4595 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,18 @@ package org.foxarmy.barcodescannerforemployees.activities import android.app.DatePickerDialog +import android.content.DialogInterface +import android.content.Intent import android.os.Bundle import android.view.View import android.widget.* +import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat import com.google.mlkit.vision.codescanner.GmsBarcodeScanning import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R +import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct import org.foxarmy.barcodescannerforemployees.dataclasses.Product import org.foxarmy.barcodescannerforemployees.views.AbstractProductView import java.util.* @@ -93,14 +98,6 @@ class AddProductActivity : AppCompatActivity() { expiryDateOverShelfLife = false } -// shelfLifeTextEdit.addTextChangedListener { -// if (shelfLifeTextEdit.text.toString() == "") { -// return@addTextChangedListener -// } -// -// evaluateDateOfExpiry() -// } - dateOfProductionSelectButton.setOnClickListener { val c = Calendar.getInstance() val year = c.get(Calendar.YEAR) @@ -180,7 +177,17 @@ class AddProductActivity : AppCompatActivity() { fun findAndDisplayAbstractProductByBarcode(barcode: String) { val abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, barcode) 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 } abstractProductView.abstractProduct = abstractProduct