adding units to abstract product

This commit is contained in:
leca 2024-10-18 14:54:58 +03:00
parent a2662bf55b
commit c112eec868
6 changed files with 43 additions and 21 deletions

View File

@ -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)
}
}

View File

@ -4,7 +4,7 @@ import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
class Parser constructor() {
fun findNetWeightInText(text: String, regexes: List<Regex>): Pair<String, Double> {
fun findNetWeightInText(text: String, regexes: List<Regex>): Triple<String, Double, String> {
var text = text
var netWeight = 0.0
for (regex in regexes.iterator()) {
@ -13,18 +13,18 @@ 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*[лЛ]"),
@ -35,6 +35,6 @@ class Parser constructor() {
)
)
return AbstractProduct(0, "", name, netWeight, "", 0)
return AbstractProduct(0, "", name, netWeight, "", 0, unit)
}
}

View File

@ -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 {

View File

@ -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.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 {

View File

@ -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)

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="gram">g</string>
<string name="kilogram">kg</string>
<string name="liter">l</string>
<string name="milliliter">ml</string>
<string name="pieces">pc</string>
</resources>