Fixed parsing, working on adding products
This commit is contained in:
		@@ -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<Regex>): Pair<String, Double> {
 | 
			
		||||
        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)
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -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>): 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
 | 
			
		||||
 
 | 
			
		||||
@@ -23,5 +23,33 @@
 | 
			
		||||
                app:layout_constraintStart_toStartOf="parent"
 | 
			
		||||
                app:layout_constraintEnd_toEndOf="parent"
 | 
			
		||||
                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.core.widget.NestedScrollView>
 | 
			
		||||
@@ -26,4 +26,5 @@
 | 
			
		||||
    <string name="update">update</string>
 | 
			
		||||
    <string name="delete">delete</string>
 | 
			
		||||
    <string name="category">Category</string>
 | 
			
		||||
    <string name="date_of_production">Date of production</string>
 | 
			
		||||
</resources>
 | 
			
		||||
		Reference in New Issue
	
	Block a user