Fixed parsing, working on adding products

This commit is contained in:
leca 2024-10-13 02:12:01 +03:00
parent a6ab2e305f
commit efcd9361b2
4 changed files with 55 additions and 58 deletions

View File

@ -3,55 +3,37 @@ package org.foxarmy.barcodescannerforemployees
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
class Parser constructor() { class Parser constructor() {
fun parse(text: String): AbstractProduct {
fun findNetWeightInText(text: String, regexes: List<Regex>): Pair<String, Double> {
var text = text var text = text
var name = ""
var netWeight = 0.0 var netWeight = 0.0
for (regex in regexes.iterator()) {
//Find volume in liters val foundByRegex = regex.find(text)
val litersRegex = Regex("[0-9+],[0-9*]\\s*[лЛ]") if (foundByRegex != null) {
val foundLiters = litersRegex.find(text) val found = foundByRegex.groupValues[0]
if (foundLiters != null) { text = text.replace(found, "")
text = text.replace(foundLiters.groupValues[0], "") netWeight = stripNetWeight(found)
netWeight = stripNetWeight(foundLiters.groupValues[0]) when (found.lowercase().strip()) {
} else { // not found liters. Maybe milliliters? "кг" -> netWeight *= 1000
val millilitersRegex = Regex("[0-9+],[0-9*]\\s*((мл)|(МЛ)|(Мл))") "мл" -> netWeight /= 1000
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
} }
return Pair(text, netWeight)
}
}
return Pair("", 0.0)
} }
val kilogramRegex = Regex("[0-9+],[0-9*]\\s*((кг)|(Кг))") fun parse(text: String): AbstractProduct {
val foundKilograms = kilogramRegex.find(text) val (name, netWeight) = findNetWeightInText(
if (foundKilograms != null) { text,
text = text.replace(foundKilograms.groupValues[0], "") listOf(
netWeight = stripNetWeight(foundKilograms.groupValues[0]) * 1000 Regex("[0-9]+,?[0-9]* \\s*[лЛ]"),
} else { // Not found kilograms, maybe we could find grams? Regex("[0-9]+,?[0-9]*\\s*((мл)|(МЛ)|(Мл))"),
val gramsRegex = Regex("[0-9+],[0-9*]\\s*[гГ]") Regex("[0-9]+,?[0-9]*\\s*((кг)|(Кг))"),
val foundGrams = gramsRegex.find(text) Regex("[0-9]+,?[0-9]*\\s*[гГ]"),
netWeight = if (foundGrams != null) { Regex("[0-9]+,?[0-9*]\\s*((шт)|(Шт))")
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
return AbstractProduct(0, "", name, netWeight, "", 0) return AbstractProduct(0, "", name, netWeight, "", 0)
} }

View File

@ -7,7 +7,6 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.core.graphics.scale import androidx.core.graphics.scale
@ -53,24 +52,11 @@ fun removeSubstringsFromString(text: String, toRemove: Array<String>): String {
for (candidate in toRemove.iterator()) { for (candidate in toRemove.iterator()) {
result = result.replace(candidate, "") result = result.replace(candidate, "")
} }
Log.d("QWERTYUIOP", result)
return result return result
} }
fun String.utf8(): String = URLEncoder.encode(this, "UTF-8") 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? { fun getActivity(context: Context?): Activity? {
if (context == null) { if (context == null) {
return null return null

View File

@ -23,5 +23,33 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/abstractProductView" android:layout_marginTop="16dp"/> app:layout_constraintTop_toBottomOf="@+id/abstractProductView" android:layout_marginTop="16dp"/>
<TextView
android:text="Date of production:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/dateOfProductionTextView"
app:layout_constraintTop_toBottomOf="@+id/scanButton"
android:layout_marginTop="16dp" app:layout_constraintStart_toStartOf="parent"/>
<Button
android:text="Select"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/scanButton"
app:layout_constraintStart_toEndOf="@+id/dateOfProductionTextView"
app:layout_constraintEnd_toEndOf="parent"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
android:orientation="horizontal">
<RadioButton
android:text="Expiry date"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/ExpiryDateRadio"/>
<RadioButton
android:text="Shell life"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/shellLifeRadio"/>
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView> </androidx.core.widget.NestedScrollView>

View File

@ -26,4 +26,5 @@
<string name="update">update</string> <string name="update">update</string>
<string name="delete">delete</string> <string name="delete">delete</string>
<string name="category">Category</string> <string name="category">Category</string>
<string name="date_of_production">Date of production</string>
</resources> </resources>