implemented units

This commit is contained in:
leca 2024-10-18 15:53:36 +03:00
parent e9ef4c44f9
commit 0fdcabca48
6 changed files with 50 additions and 19 deletions

View File

@ -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
@ -81,3 +80,14 @@ fun calculateProductFreshness(dateOfProduction: Long, dateOfExpiry: Long): Doubl
return lifeSpanLeft / productLifeSpan.toDouble()
}
fun getUnitNameById (context: Context, id: Int): String {
return when(id) {
0 -> { context.getString(R.string.kilogram) }
1 -> { context.getString(R.string.gram) }
2 -> { context.getString(R.string.liter) }
3 -> { context.getString(R.string.milliliter) }
4 -> { context.getString(R.string.pieces) }
else -> { "" }
}
}

View File

@ -34,6 +34,7 @@ class AddAbstractProductActivity : AppCompatActivity() {
private lateinit var barcodeText: EditText
private lateinit var productNameText: TextView
private lateinit var netWeightText: TextView
private lateinit var unitTypeSpinner: Spinner
private lateinit var categorySpinner: Spinner
@ -68,10 +69,12 @@ class AddAbstractProductActivity : AppCompatActivity() {
barcodeText = findViewById(R.id.barcodeTextEdit)
productNameText = findViewById(R.id.productName)
netWeightText = findViewById(R.id.netWeight)
unitTypeSpinner = findViewById(R.id.unitTypeSpinner)
categorySpinner = findViewById(R.id.categorySpinner)
fillupCategorySpinner()
fillupUnitsSpinner()
if (abstractProduct?.name == "" && abstractProduct?.barcode != "") {
performRequest(abstractProduct?.barcode!!)
@ -86,6 +89,7 @@ class AddAbstractProductActivity : AppCompatActivity() {
productNameText.text = abstractProduct!!.name
netWeightText.text = abstractProduct!!.netWeight.toString()
categorySpinner.setSelection(abstractProduct!!.category)
unitTypeSpinner.setSelection(abstractProduct!!.unit)
}
saveButton.setOnClickListener {
@ -116,6 +120,7 @@ class AddAbstractProductActivity : AppCompatActivity() {
put(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT, netWeight.toString())
put(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME, pictureFile.nameWithoutExtension)
put(AbstractProductContract.AbstractProductEntry.CATEGORY, categorySpinner.selectedItemPosition)
put(AbstractProductContract.AbstractProductEntry.UNIT, unitTypeSpinner.selectedItemPosition)
}
if (abstractProduct == null) {
@ -192,10 +197,26 @@ class AddAbstractProductActivity : AppCompatActivity() {
runOnUiThread {
productNameText.text = abstractProduct.name
netWeightText.text = abstractProduct.netWeight.toString()
unitTypeSpinner.setSelection(abstractProduct.unit)
}
}
}
private fun fillupUnitsSpinner() {
val units = listOf(
getString(R.string.kilogram),
getString(R.string.gram),
getString(R.string.liter),
getString(R.string.milliliter),
getString(R.string.pieces)
)
val arrayAdapter = ArrayAdapter<String>(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, units)
arrayAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item)
unitTypeSpinner.adapter = arrayAdapter
}
fun fillupCategorySpinner() {
val db = DBStorageController(this).readableDatabase

View File

@ -12,12 +12,9 @@ import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.startActivity
import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.*
import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
import org.foxarmy.barcodescannerforemployees.getActivity
import org.foxarmy.barcodescannerforemployees.getImageUri
import java.io.File
class AbstractProductView: LinearLayout {
@ -67,7 +64,7 @@ class AbstractProductView: LinearLayout {
}
//TODO: units
unitField?.text = getUnitNameById(context, abstractProduct.unit)
productLayout!!.setOnLongClickListener {
isProductSelected = !isProductSelected

View File

@ -40,17 +40,21 @@
android:visibility="visible" android:hint="@string/product_name_label" android:textColorHint="#737373"
android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/barcodeTextEdit"/>
<EditText
android:layout_width="350dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="text"
android:ems="10"
android:id="@+id/netWeight"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:visibility="visible" android:hint="@string/netWeight" android:textColorHint="#737373"
app:layout_constraintTop_toBottomOf="@+id/productName"/>
app:layout_constraintTop_toBottomOf="@+id/productName"
/>
<Spinner
android:layout_width="130dp"
android:layout_height="50dp" android:id="@+id/unitTypeSpinner"
app:layout_constraintStart_toEndOf="@+id/netWeight"
app:layout_constraintTop_toBottomOf="@+id/productName" app:layout_constraintEnd_toEndOf="parent"/>
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"

View File

@ -27,4 +27,11 @@
<string name="delete">delete</string>
<string name="category">Category</string>
<string name="date_of_production">Date of production</string>
<!-- Unit names -->
<string name="kilogram">kg</string>
<string name="gram">g</string>
<string name="liter">l</string>
<string name="milliliter">ml</string>
<string name="pieces">pc</string>
</resources>

View File

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