From a6ab2e305f09ede4ce5f6f136e3a03c58b624531 Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 12 Oct 2024 14:42:11 +0300 Subject: [PATCH] added barcode to abstractproduct, working on adding products --- app/src/main/AndroidManifest.xml | 2 +- .../DBStorageController.kt | 30 ++++++++++ .../barcodescannerforemployees/Utils.kt | 29 ++++++++- .../activities/AddProductActivity.kt | 40 ++++++++++++- .../views/AbstractProductView.kt | 60 ++++++++++++------- .../main/res/layout/abstract_product_view.xml | 5 +- .../main/res/layout/fragment_add_product.xml | 21 ++++--- app/src/main/res/values/strings.xml | 5 +- 8 files changed, 156 insertions(+), 36 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d0e1812..529a2f4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -45,7 +45,7 @@ ): String { return result } -fun String.utf8(): String = URLEncoder.encode(this, "UTF-8") \ No newline at end of file +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 + } else if (context is ContextWrapper) { + return if (context is Activity) { + context + } else { + getActivity(context.baseContext) + } + } + + return null +} \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt index ee819b7..3310da0 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt @@ -1,18 +1,56 @@ package org.foxarmy.barcodescannerforemployees.activities import android.os.Bundle +import android.widget.Button +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import com.google.mlkit.vision.codescanner.GmsBarcodeScanning +import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.dataclasses.Product +import org.foxarmy.barcodescannerforemployees.views.AbstractProductView class AddProductActivity : AppCompatActivity() { + + private lateinit var scanButton: Button + private lateinit var abstractProductView: AbstractProductView + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_add_product) + setContentView(R.layout.fragment_add_product) val extras = intent.extras val product = extras!!.get("product") as Product? + + scanButton = findViewById(R.id.scanButton) + abstractProductView = findViewById(R.id.abstractProductView) + /* + scanButton = findViewById(R.id.scanButton) + abstractProductView = AbstractProductView(this, this, AbstractProduct()) + findViewById(R.id.addProductConstraintLayout).addView(abstractProductView) + */ + + scanButton.setOnClickListener { + val scanner = GmsBarcodeScanning.getClient(this) + scanner.startScan() + .addOnSuccessListener { barcode -> + findAndDisplayAbstractProductByBarcode(barcode.rawValue.toString()) + } + .addOnFailureListener { + Toast.makeText(this, "Cannot scan barcode", Toast.LENGTH_SHORT).show() + } + } + } + + fun findAndDisplayAbstractProductByBarcode(barcode: String) { + val abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, barcode) + if (abstractProduct == null) { + Toast.makeText(this, "No product found", Toast.LENGTH_SHORT).show() + return + } + abstractProductView.abstractProduct = abstractProduct + abstractProductView.update() } } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt index 435cbc2..814c5ec 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt @@ -4,6 +4,7 @@ import android.app.Activity import android.content.Context import android.content.Intent import android.os.Bundle +import android.util.AttributeSet import android.view.LayoutInflater import android.widget.ImageView import android.widget.LinearLayout @@ -15,26 +16,37 @@ import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R 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 { - private var productLayout: ConstraintLayout - private var productPicture: ImageView - private var productNameField: TextView - private var netWeightField: TextView - private var categoryField: TextView - private var unitField: TextView - var abstractProduct: AbstractProduct + private var productLayout: ConstraintLayout? = null + private var productPicture: ImageView? = null + private var productNameField: TextView? = null + private var netWeightField: TextView? = null + private var categoryField: TextView? = null + private var unitField: TextView? = null + var abstractProduct: AbstractProduct = AbstractProduct() var isProductSelected = false + private lateinit var activity: Activity + + constructor(context: Context, a: AttributeSet) : super(context, a) { + activity = getActivity(context)!! + val inflater:LayoutInflater = activity.layoutInflater + inflater.inflate(R.layout.abstract_product_view, this) + } constructor(activity: Activity, context: Context, abstractProduct: AbstractProduct) : super(context) { - this.abstractProduct = abstractProduct - + this.activity = activity val inflater:LayoutInflater = activity.layoutInflater inflater.inflate(R.layout.abstract_product_view, this) + update() + } + + fun init () { productLayout = findViewById(R.id.productLayout) productPicture = findViewById(R.id.productPicture) productNameField = findViewById(R.id.productNameView) @@ -42,12 +54,8 @@ class AbstractProductView: LinearLayout { categoryField = findViewById(R.id.categoryView) unitField = findViewById(R.id.unitView) - val thumbnailsDir = File(context.cacheDir, "thumbnails") - thumbnailsDir.mkdirs() - val imageUri = getImageUri(activity, File(thumbnailsDir, "${abstractProduct.imageHash}.webp")) - productPicture.setImageURI(imageUri) - productPicture.rotation = 90f - productPicture.setOnClickListener { + + productPicture!!.setOnClickListener { val fullscreenIntent = Intent(activity, FullscreenActivity::class.java) val extras = Bundle() extras.putString("imagehash", abstractProduct.imageHash) @@ -55,20 +63,30 @@ class AbstractProductView: LinearLayout { startActivity(context, fullscreenIntent, extras) } - productLayout.setOnClickListener { + productLayout!!.setOnClickListener { } - productNameField.text = abstractProduct.name - netWeightField.text = abstractProduct.netWeight.toString() - categoryField.text = DBStorageController(context).getCategoryNameById(DBStorageController(context).readableDatabase, abstractProduct.category) - //TODO: units - productLayout.setOnLongClickListener { + productLayout!!.setOnLongClickListener { isProductSelected = !isProductSelected this.background = ContextCompat.getDrawable(context, if (isProductSelected) R.drawable.outline_selected else R.drawable.outline) true } } + + fun update() { + init() + + val thumbnailsDir = File(context.cacheDir, "thumbnails") + thumbnailsDir.mkdirs() + val imageUri = getImageUri(activity, File(thumbnailsDir, "${abstractProduct.imageHash}.webp")) + productPicture!!.setImageURI(imageUri) + productPicture!!.rotation = 90f + + productNameField!!.text = abstractProduct.name + netWeightField!!.text = abstractProduct.netWeight.toString() + categoryField!!.text = DBStorageController(context).getCategoryNameById(DBStorageController(context).readableDatabase, abstractProduct.category) + } } \ No newline at end of file diff --git a/app/src/main/res/layout/abstract_product_view.xml b/app/src/main/res/layout/abstract_product_view.xml index e7c669c..2660ad5 100644 --- a/app/src/main/res/layout/abstract_product_view.xml +++ b/app/src/main/res/layout/abstract_product_view.xml @@ -11,7 +11,7 @@ android:background="#00FFFEFE" android:clickable="true"> @@ -45,7 +45,8 @@ android:layout_height="wrap_content" android:id="@+id/categoryView" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="25dp" android:fontFamily="monospace" android:textSize="10sp" - app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="20dp"/> + app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="20dp" + app:layout_constraintTop_toBottomOf="@+id/productNameView" android:layout_marginTop="10dp"/> \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_add_product.xml b/app/src/main/res/layout/fragment_add_product.xml index 127294b..d855d8e 100644 --- a/app/src/main/res/layout/fragment_add_product.xml +++ b/app/src/main/res/layout/fragment_add_product.xml @@ -9,12 +9,19 @@ - + android:padding="16dp" android:layout_gravity="center_horizontal" android:id="@+id/addProductConstraintLayout"> + +