From c112eec868e87cec3b1950951248ac2e84cd70f4 Mon Sep 17 00:00:00 2001 From: leca Date: Fri, 18 Oct 2024 14:54:58 +0300 Subject: [PATCH] adding units to abstract product --- .../DBStorageController.kt | 20 +++++++++++++------ .../barcodescannerforemployees/Parser.kt | 20 +++++++++---------- .../activities/AddProductActivity.kt | 2 +- .../dataclasses/AbstractProduct.kt | 8 ++++++-- .../fragments/StorageFragment.kt | 6 ++++-- app/src/main/res/values/units.xml | 8 ++++++++ 6 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 app/src/main/res/values/units.xml diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt index 5c9b450..00fbcd3 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt @@ -18,6 +18,7 @@ object AbstractProductContract { const val PRODUCT_NET_WEIGHT = "net_weight" const val IMAGE_FILENAME = "image_filename" const val CATEGORY = "category" + const val UNIT = "unit" } } @@ -45,7 +46,8 @@ const val SQL_CREATE_ABSTRACT_PRODUCTS_TABLE = "${AbstractProductContract.AbstractProductEntry.PRODUCT_NAME} TEXT," + "${AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT} REAL," + "${AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME} TEXT," + - "${AbstractProductContract.AbstractProductEntry.CATEGORY} INTEGER)" + "${AbstractProductContract.AbstractProductEntry.CATEGORY} INTEGER," + + "${AbstractProductContract.AbstractProductEntry.UNIT} INTEGER)" const val SQL_CREATE_PRODUCTS_TABLE = "CREATE TABLE ${ProductContract.ProductEntry.TABLE_NAME} (" + @@ -109,6 +111,7 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE AbstractProductContract.AbstractProductEntry.PRODUCT_NAME, AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME, AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT, + AbstractProductContract.AbstractProductEntry.UNIT ) val selection = "${AbstractProductContract.AbstractProductEntry.CATEGORY} = ?" @@ -123,8 +126,9 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE val abstractProductName = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME)) val abstractProductNetWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT)) val abstractProductImageHash = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME)) + val abstractProductUnit = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT)) - val abstractProduct = AbstractProduct(abstractProductId, abstractProductBarcode, abstractProductName, abstractProductNetWeight, abstractProductImageHash, category = id) + val abstractProduct = AbstractProduct(abstractProductId, abstractProductBarcode, abstractProductName, abstractProductNetWeight, abstractProductImageHash, category = id, abstractProductUnit) result.add(abstractProduct) } @@ -203,7 +207,8 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE AbstractProductContract.AbstractProductEntry.PRODUCT_NAME, AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME, AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT, - AbstractProductContract.AbstractProductEntry.CATEGORY + AbstractProductContract.AbstractProductEntry.CATEGORY, + AbstractProductContract.AbstractProductEntry.UNIT ) val selection = "${AbstractProductContract.AbstractProductEntry.BARCODE} = ?" @@ -218,8 +223,9 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE val imageFilename = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME)) val netWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT)) val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY)) + val unit = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT)) - abstractProduct = AbstractProduct(id, barcode, productName, netWeight, imageFilename, category) + abstractProduct = AbstractProduct(id, barcode, productName, netWeight, imageFilename, category, unit) } } @@ -234,7 +240,8 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT, AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME, AbstractProductContract.AbstractProductEntry.CATEGORY, - AbstractProductContract.AbstractProductEntry.BARCODE + AbstractProductContract.AbstractProductEntry.BARCODE, + AbstractProductContract.AbstractProductEntry.UNIT ) val selection = "${BaseColumns._ID} = ?" @@ -250,8 +257,9 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE val netWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT)) val imageHash = getString((getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME))) val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY)) + val unit = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT)) - abstractProduct = AbstractProduct(productId, barcode, name, netWeight, imageHash, category) + abstractProduct = AbstractProduct(productId, barcode, name, netWeight, imageHash, category, unit) } } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt index aa0f51f..461629c 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt @@ -4,7 +4,7 @@ import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct class Parser constructor() { - fun findNetWeightInText(text: String, regexes: List): Pair { + fun findNetWeightInText(text: String, regexes: List): Triple { var text = text var netWeight = 0.0 for (regex in regexes.iterator()) { @@ -13,21 +13,21 @@ class Parser constructor() { val found = foundByRegex.groupValues[0] text = text.replace(found, "") netWeight = stripNetWeight(found) - when (found.lowercase().strip()) { - "кг" -> netWeight *= 1000 - "мл" -> netWeight /= 1000 - } - return Pair(text, netWeight) +// when (found.lowercase().strip()) { +// "кг" -> netWeight *= 1000 +// "мл" -> netWeight /= 1000 +// } + return Triple(text, netWeight, found) } } - return Pair("", 0.0) + return Triple("", 0.0, "") } fun parse(text: String): AbstractProduct { - val (name, netWeight) = findNetWeightInText( + val (name, netWeight, unit) = findNetWeightInText( text, listOf( - Regex("[0-9]+,?[0-9]* \\s*[лЛ]"), + Regex("[0-9]+,?[0-9]*\\s*[лЛ]"), Regex("[0-9]+,?[0-9]*\\s*((мл)|(МЛ)|(Мл))"), Regex("[0-9]+,?[0-9]*\\s*((кг)|(Кг))"), Regex("[0-9]+,?[0-9]*\\s*[гГ]"), @@ -35,6 +35,6 @@ class Parser constructor() { ) ) - return AbstractProduct(0, "", name, netWeight, "", 0) + return AbstractProduct(0, "", name, netWeight, "", 0, unit) } } \ No newline at end of file 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 36836f9..329118e 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt @@ -73,7 +73,7 @@ class AddProductActivity : AppCompatActivity() { update() } else { product = Product(0, 0, 0, 0, 0) - abstractProduct = AbstractProduct(0, "", "", 0.0, "", 0) + abstractProduct = AbstractProduct(0, "", "", 0.0, "", 0, 0) } scanButton.setOnClickListener { diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/dataclasses/AbstractProduct.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/dataclasses/AbstractProduct.kt index 8089189..1f0ed4a 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/dataclasses/AbstractProduct.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/dataclasses/AbstractProduct.kt @@ -10,14 +10,16 @@ class AbstractProduct() : Parcelable { var netWeight: Double = 0.0 var imageHash: String = "" var category: Int = 0 + var unit: Int = 0 - constructor(id: Int, barcode: String, name: String ,netWeight: Double, imageHash: String, category: Int) : this() { + constructor(id: Int, barcode: String, name: String, netWeight: Double, imageHash: String, category: Int, unit: Int) : this() { this.id = id this.barcode = barcode this.name = name this.netWeight = netWeight this.imageHash = imageHash - this.category= category + this.category = category + this.unit = unit } constructor(parcel: Parcel) : this() { @@ -27,6 +29,7 @@ class AbstractProduct() : Parcelable { netWeight = parcel.readDouble() imageHash = parcel.readString()!! category = parcel.readInt() + unit = parcel.readInt() } override fun writeToParcel(parcel: Parcel, flags: Int) { @@ -36,6 +39,7 @@ class AbstractProduct() : Parcelable { parcel.writeDouble(netWeight) parcel.writeString(imageHash) parcel.writeInt(category) + parcel.writeInt(unit) } override fun describeContents(): Int { diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt index e97184b..95ea038 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -116,7 +116,8 @@ class StorageFragment : Fragment() { AbstractProductContract.AbstractProductEntry.PRODUCT_NAME, AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT, AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME, - AbstractProductContract.AbstractProductEntry.CATEGORY + AbstractProductContract.AbstractProductEntry.CATEGORY, + AbstractProductContract.AbstractProductEntry.UNIT ) var orderBy: String = "" @@ -140,8 +141,9 @@ class StorageFragment : Fragment() { val netWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT)) val productImageHash = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME)) val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY)) + val unit = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT)) - val product = AbstractProduct(productId, barcode, productName, netWeight, productImageHash, category) + val product = AbstractProduct(productId, barcode, productName, netWeight, productImageHash, category, unit) generateThumbnailForImage(context!!, productImageHash) diff --git a/app/src/main/res/values/units.xml b/app/src/main/res/values/units.xml new file mode 100644 index 0000000..62aa378 --- /dev/null +++ b/app/src/main/res/values/units.xml @@ -0,0 +1,8 @@ + + + g + kg + l + ml + pc + \ No newline at end of file