From efcd9361b28f96b7d38b16ae745c6689f041eb80 Mon Sep 17 00:00:00 2001 From: leca Date: Sun, 13 Oct 2024 02:12:01 +0300 Subject: [PATCH] Fixed parsing, working on adding products --- .../barcodescannerforemployees/Parser.kt | 70 +++++++------------ .../barcodescannerforemployees/Utils.kt | 14 ---- .../main/res/layout/fragment_add_product.xml | 28 ++++++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 55 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt index 50492f9..aa0f51f 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt @@ -3,55 +3,37 @@ package org.foxarmy.barcodescannerforemployees import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct class Parser constructor() { - fun parse(text: String): AbstractProduct { + + fun findNetWeightInText(text: String, regexes: List): Pair { var text = text - - var name = "" var netWeight = 0.0 - - //Find volume in liters - val litersRegex = Regex("[0-9+],[0-9*]\\s*[лЛ]") - val foundLiters = litersRegex.find(text) - if (foundLiters != null) { - text = text.replace(foundLiters.groupValues[0], "") - netWeight = stripNetWeight(foundLiters.groupValues[0]) - } else { // not found liters. Maybe milliliters? - val millilitersRegex = Regex("[0-9+],[0-9*]\\s*((мл)|(МЛ)|(Мл))") - val foundMilliliters = millilitersRegex.find(text) - netWeight = if (foundMilliliters != null) { - text = text.replace(foundMilliliters.groupValues[0], "") - stripNetWeight(foundMilliliters.groupValues[0]) / 1000 // Found milliliters, convert to liters - } else { - 0.0 // Nothing found + for (regex in regexes.iterator()) { + val foundByRegex = regex.find(text) + if (foundByRegex != null) { + val found = foundByRegex.groupValues[0] + text = text.replace(found, "") + netWeight = stripNetWeight(found) + when (found.lowercase().strip()) { + "кг" -> netWeight *= 1000 + "мл" -> netWeight /= 1000 + } + return Pair(text, netWeight) } } + return Pair("", 0.0) + } - val kilogramRegex = Regex("[0-9+],[0-9*]\\s*((кг)|(Кг))") - val foundKilograms = kilogramRegex.find(text) - if (foundKilograms != null) { - text = text.replace(foundKilograms.groupValues[0], "") - netWeight = stripNetWeight(foundKilograms.groupValues[0]) * 1000 - } else { // Not found kilograms, maybe we could find grams? - val gramsRegex = Regex("[0-9+],[0-9*]\\s*[гГ]") - val foundGrams = gramsRegex.find(text) - netWeight = if (foundGrams != null) { - text = text.replace(foundGrams.groupValues[0], "") - stripNetWeight(foundGrams.groupValues[0]) - } else { - 0.0 // Nothing found - } - } - - val piecesRegex = Regex("[0-9+],*[0-9*]\\s*((шт)|(Шт))") - val foundPieces = piecesRegex.find(text) - if (foundPieces != null) { - text = text.replace(foundPieces.groupValues[0], "") - netWeight = stripNetWeight(foundPieces.groupValues[0]) - } else { - netWeight = 0.0 - } - - name = text + fun parse(text: String): AbstractProduct { + val (name, netWeight) = 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*((шт)|(Шт))") + ) + ) return AbstractProduct(0, "", name, netWeight, "", 0) } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt index a5a9880..206dc9d 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt @@ -7,7 +7,6 @@ import android.graphics.Bitmap import android.graphics.BitmapFactory import android.net.Uri import android.os.Build -import android.util.Log import androidx.annotation.RequiresApi import androidx.core.content.FileProvider import androidx.core.graphics.scale @@ -53,24 +52,11 @@ fun removeSubstringsFromString(text: String, toRemove: Array): String { for (candidate in toRemove.iterator()) { result = result.replace(candidate, "") } - Log.d("QWERTYUIOP", result) return result } fun String.utf8(): String = URLEncoder.encode(this, "UTF-8") -//fun getActivity(context: Context?): Activity? { -// if (context == null) { -// return null -// } else if (context is ContextWrapper) { -// if (context is Activity) -// return context -// else -// return getActivity(context as ContextWrapper)!!.baseContext as Activity -// } -// return null -//} - fun getActivity(context: Context?): Activity? { if (context == null) { return null diff --git a/app/src/main/res/layout/fragment_add_product.xml b/app/src/main/res/layout/fragment_add_product.xml index d855d8e..03009c9 100644 --- a/app/src/main/res/layout/fragment_add_product.xml +++ b/app/src/main/res/layout/fragment_add_product.xml @@ -23,5 +23,33 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/abstractProductView" android:layout_marginTop="16dp"/> + +