From 8496a6f2f4578cbcd62bd918987e79c07f4a960e Mon Sep 17 00:00:00 2001 From: leca Date: Tue, 17 Sep 2024 02:20:27 +0300 Subject: [PATCH] saving images and abstract product view --- .../AbstractProduct.kt | 5 + .../AbstractProductView.kt | 176 ++++++++++++++++++ .../AddProductFragment.kt | 30 ++- .../MainActivity.kt | 33 ++-- .../MainScreenFragment.kt | 10 +- .../barcodescannerforemployees/Parser.kt | 2 +- .../barcodescannerforemployees/Product.kt | 7 - .../StorageFragment.kt | 15 ++ app/src/main/res/layout/activity_main.xml | 5 + app/src/main/res/layout/content_main.xml | 2 +- app/src/main/res/layout/fragment_second.xml | 23 +-- app/src/main/res/layout/fragment_storage.xml | 2 +- .../layout/sample_abstract_product_view.xml | 26 +++ app/src/main/res/values-night/styles.xml | 6 + .../values/attrs_abstract_product_view.xml | 8 + app/src/main/res/values/colors.xml | 4 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 6 + 18 files changed, 306 insertions(+), 57 deletions(-) create mode 100644 app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt create mode 100644 app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt delete mode 100644 app/src/main/java/org/foxarmy/barcodescannerforemployees/Product.kt create mode 100644 app/src/main/res/layout/sample_abstract_product_view.xml create mode 100644 app/src/main/res/values-night/styles.xml create mode 100644 app/src/main/res/values/attrs_abstract_product_view.xml create mode 100644 app/src/main/res/values/styles.xml diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt new file mode 100644 index 0000000..9d80c7c --- /dev/null +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt @@ -0,0 +1,5 @@ +package org.foxarmy.barcodescannerforemployees + +class AbstractProduct constructor(name:String, netWeight: Double, imageFile: String, type: Int) { + +} \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt new file mode 100644 index 0000000..db8f058 --- /dev/null +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt @@ -0,0 +1,176 @@ +package org.foxarmy.barcodescannerforemployees + +import android.content.Context +import android.widget.ImageView +import android.widget.LinearLayout +import android.widget.TextView + +/** + * TODO: document your custom view class. + */ +class AbstractProductView: LinearLayout { + private val productImage = ImageView(context) + private val productNameField = TextView(context) + private val netWeightField = TextView(context) + private val categoryField = TextView(context) + + + constructor(context: Context, productImageFile: String, productName: String, netWeight: Double, category: Int) : super(context) { +// this.layerType = +// setLayerType() + this.orientation = LinearLayout.VERTICAL +// productImage.set + productNameField.setText(productName) + netWeightField.setText(netWeight.toString()) + + addView(productNameField) + addView(netWeightField) + +// if(findViewById(R.id.productLayout) == null) { +// Toast.makeText(context, "AAAAAAAAAAA", Toast.LENGTH_LONG).show() +// } + +// findViewById(R.id.productLayout).addView(productNameField) +// (requestLayout() as RelativeLayout).addView(productNameField) + + } +} + +//class AbstractProductView : View { +// +// private var _exampleString: String? = null // TODO: use a default from R.string... +// private var _exampleColor: Int = Color.RED // TODO: use a default from R.color... +// private var _exampleDimension: Float = 0f // TODO: use a default from R.dimen... +// +// private lateinit var textPaint: TextPaint +// private var textWidth: Float = 0f +// private var textHeight: Float = 0f +// +// /** +// * The text to draw +// */ +// var exampleString: String? +// get() = _exampleString +// set(value) { +// _exampleString = value +// invalidateTextPaintAndMeasurements() +// } +// +// /** +// * The font color +// */ +// var exampleColor: Int +// get() = _exampleColor +// set(value) { +// _exampleColor = value +// invalidateTextPaintAndMeasurements() +// } +// +// /** +// * In the example view, this dimension is the font size. +// */ +// var exampleDimension: Float +// get() = _exampleDimension +// set(value) { +// _exampleDimension = value +// invalidateTextPaintAndMeasurements() +// } +// +// /** +// * In the example view, this drawable is drawn above the text. +// */ +// var exampleDrawable: Drawable? = null +// +// constructor(context: Context) : super(context) { +// init(null, 0) +// } +// +// constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { +// init(attrs, 0) +// } +// +// constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) { +// init(attrs, defStyle) +// } +// +// private fun init(attrs: AttributeSet?, defStyle: Int) { +// // Load attributes +// val a = context.obtainStyledAttributes( +// attrs, R.styleable.AbstractProductView, defStyle, 0 +// ) +// +// _exampleString = a.getString( +// R.styleable.AbstractProductView_exampleString +// ) +// _exampleColor = a.getColor( +// R.styleable.AbstractProductView_exampleColor, +// exampleColor +// ) +// // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with +// // values that should fall on pixel boundaries. +// _exampleDimension = a.getDimension( +// R.styleable.AbstractProductView_exampleDimension, +// exampleDimension +// ) +// +// if (a.hasValue(R.styleable.AbstractProductView_exampleDrawable)) { +// exampleDrawable = a.getDrawable( +// R.styleable.AbstractProductView_exampleDrawable +// ) +// exampleDrawable?.callback = this +// } +// +// a.recycle() +// +// // Set up a default TextPaint object +// textPaint = TextPaint().apply { +// flags = Paint.ANTI_ALIAS_FLAG +// textAlign = Paint.Align.LEFT +// } +// +// // Update TextPaint and text measurements from attributes +// invalidateTextPaintAndMeasurements() +// } +// +// private fun invalidateTextPaintAndMeasurements() { +// textPaint.let { +// it.textSize = exampleDimension +// it.color = exampleColor +// textWidth = it.measureText(exampleString) +// textHeight = it.fontMetrics.bottom +// } +// } +// +// override fun onDraw(canvas: Canvas) { +// super.onDraw(canvas) +// +// // TODO: consider storing these as member variables to reduce +// // allocations per draw cycle. +// val paddingLeft = paddingLeft +// val paddingTop = paddingTop +// val paddingRight = paddingRight +// val paddingBottom = paddingBottom +// +// val contentWidth = width - paddingLeft - paddingRight +// val contentHeight = height - paddingTop - paddingBottom +// +// exampleString?.let { +// // Draw the text. +// canvas.drawText( +// it, +// paddingLeft + (contentWidth - textWidth) / 2, +// paddingTop + (contentHeight + textHeight) / 2, +// textPaint +// ) +// } +// +// // Draw the example drawable on top of the text. +// exampleDrawable?.let { +// it.setBounds( +// paddingLeft, paddingTop, +// paddingLeft + contentWidth, paddingTop + contentHeight +// ) +// it.draw(canvas) +// } +// } +//} \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt index f3b7fab..cec75ec 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt @@ -16,12 +16,23 @@ import com.google.mlkit.vision.barcode.common.Barcode import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions import com.google.mlkit.vision.codescanner.GmsBarcodeScanning import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBinding +import java.io.File +import java.io.FileOutputStream +import java.nio.ByteBuffer +import java.security.MessageDigest + + +@OptIn(ExperimentalStdlibApi::class) +fun String.md5(): String { + val md = MessageDigest.getInstance("MD5") + val digest = md.digest(this.toByteArray()) + return digest.toHexString() +} /** * A simple [Fragment] subclass as the default destination in the navigation. */ - class AddProductFragment : Fragment() { private var _binding: AddProductFragmentBinding? = null @@ -45,7 +56,18 @@ class AddProductFragment : Fragment() { when (requestCode) { 200 -> { if (resultCode == Activity.RESULT_OK && data != null) { - binding.imageView.setImageBitmap(data.extras?.get("data") as Bitmap) + val picture = (data.extras?.get("data") as Bitmap) + binding.imageView.setImageBitmap(picture) + val picturesPath = context?.filesDir + val dir = File(picturesPath, "pictures") + dir.mkdirs() + val filename = picture.toString().md5() + val file = File(dir, filename) + FileOutputStream(file).use { + val b = ByteBuffer.allocate(picture.byteCount) + picture.copyPixelsToBuffer(b) + it.write(b.array()) + } } } } @@ -69,6 +91,10 @@ class AddProductFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + binding.saveButton.setOnClickListener { + + } + binding.takePictureButton.setOnClickListener { requestPermissionLauncher.launch(android.Manifest.permission.CAMERA) } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainActivity.kt index 21a23e7..529d6dd 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainActivity.kt @@ -1,7 +1,6 @@ package org.foxarmy.barcodescannerforemployees import android.os.Bundle -import android.provider.BaseColumns import android.view.Menu import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity @@ -30,22 +29,22 @@ class MainActivity : AppCompatActivity() { // // val netRowId = db?.insert(ProductContract.ProductEntry.TABLE_NAME, null, values) - val db2 = dbHelper.readableDatabase - - val projection = arrayOf(BaseColumns._ID, ProductContract.ProductEntry.PRODUCT_NAME, ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, ProductContract.ProductEntry.IMAGE_FILENAME) - - val selection = "${ProductContract.ProductEntry.PRODUCT_NAME} = ?" - val selectionArgs = arrayOf("Имя продукта") - - val cursor = db2.query( - ProductContract.ProductEntry.TABLE_NAME, - null, - selection, - selectionArgs, - null, - null, - null - ) +// val db2 = dbHelper.readableDatabase +// +// val projection = arrayOf(BaseColumns._ID, ProductContract.ProductEntry.PRODUCT_NAME, ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, ProductContract.ProductEntry.IMAGE_FILENAME) +// +// val selection = "${ProductContract.ProductEntry.PRODUCT_NAME} = ?" +// val selectionArgs = arrayOf("Имя продукта") +// +// val cursor = db2.query( +// ProductContract.ProductEntry.TABLE_NAME, +// null, +// selection, +// selectionArgs, +// null, +// null, +// null +// ) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainScreenFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainScreenFragment.kt index a6e63fc..86e590c 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainScreenFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/MainScreenFragment.kt @@ -1,11 +1,10 @@ package org.foxarmy.barcodescannerforemployees import android.os.Bundle -import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.navigation.fragment.findNavController +import androidx.fragment.app.Fragment import org.foxarmy.barcodescannerforemployees.databinding.FragmentSecondBinding /** @@ -23,7 +22,6 @@ class MainScreenFragment : Fragment() { inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { - _binding = FragmentSecondBinding.inflate(inflater, container, false) return binding.root @@ -32,9 +30,9 @@ class MainScreenFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - binding.buttonSecond.setOnClickListener { - findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment) - } +// binding..setOnClickListener { +// findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment) +// } } override fun onDestroyView() { diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt index d30dc4a..1d7958b 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Parser.kt @@ -5,7 +5,7 @@ class Parser constructor(payloadStartRegex: String, payloadEndRegex: String, pay val payloadEndRegex: String = payloadEndRegex val payloadRegex: String = payloadRegex - fun parse(text: String): MutableList { + fun parse(text: String): MutableList { val payloadStart = Regex(payloadStartRegex) val payloadEnd = Regex(payloadEndRegex) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Product.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Product.kt deleted file mode 100644 index df834e0..0000000 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Product.kt +++ /dev/null @@ -1,7 +0,0 @@ -package org.foxarmy.barcodescannerforemployees - -class Product constructor(name:String, netWeight: Double) { - var name: String = name - var netWeight: Double = netWeight - var imageFile: String = "" -} \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt index b550406..c8f6efd 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt @@ -4,6 +4,7 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.constraintlayout.widget.ConstraintLayout import androidx.fragment.app.Fragment /** @@ -24,4 +25,18 @@ class StorageFragment : Fragment() { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_storage, container, false) } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + getView()?.findViewById(R.id.storageLayout)?.addView( + AbstractProductView( + requireContext(), + productImageFile = "test1", + productName = "test2", + netWeight = 1.1, + category = 1 + ) + ) + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c2a2fe6..304b809 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -8,6 +8,11 @@ android:fitsSystemWindows="true" tools:context=".MainActivity"> + + + + app:layout_behavior="@string/appbar_scrolling_view_behavior" android:id="@+id/storageLayout"> -