From 9ea70a30d8ee299b6c3ae3ae9d052ad160926e5a Mon Sep 17 00:00:00 2001 From: leca Date: Sun, 29 Sep 2024 03:35:34 +0300 Subject: [PATCH] First working viewgroup --- .../AbstractProductView.kt | 200 ++---------------- .../StorageFragment.kt | 2 +- .../main/res/layout/abstract_product_view.xml | 21 +- app/src/main/res/values/strings.xml | 1 + 4 files changed, 29 insertions(+), 195 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt index 9fd369a..61ecc22 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProductView.kt @@ -2,202 +2,38 @@ package org.foxarmy.barcodescannerforemployees import android.app.Activity import android.content.Context -import android.os.Bundle -import android.util.Log import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView -import androidx.core.net.toFile -import org.foxarmy.barcodescannerforemployees.databinding.AbstractProductViewBinding -import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBinding import java.io.File class AbstractProductView: LinearLayout { - private var _binding: AbstractProductViewBinding? = null - private val productImage = ImageView(context) - private val productNameField = TextView(context) - private val netWeightField = TextView(context) - private val categoryField = TextView(context) - - fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - - _binding = AbstractProductViewBinding.inflate(inflater, container, false) - } + private var productPicture: ImageView + private var productNameField: TextView + private var netWeightField: TextView + private var categoryField: TextView + private var unitField: TextView constructor(activity: Activity, context: Context, productImageFile: String, productName: String, netWeight: Double, category: Int) : super(context) { - this.orientation = VERTICAL + + val inflater:LayoutInflater = activity.layoutInflater + inflater.inflate(R.layout.abstract_product_view, this) + + productPicture = findViewById(R.id.productPicture) + productNameField = findViewById(R.id.productNameView) + netWeightField = findViewById(R.id.productNetWeightView) + categoryField = findViewById(R.id.categoryView) + unitField = findViewById(R.id.unitView) val picturesDir = File(context.filesDir, "pictures") picturesDir.mkdirs() val imageUri = getImageUri(activity, File(picturesDir, productImageFile)) - if (imageUri != null) { - Log.d("QWERTYUIOP", "хуй: ${imageUri.path.toString()}") - try { - Log.d("QWERTYUIOP", "Does exist: ${imageUri.toFile().absolutePath}") - } catch (e:Exception) { - Log.d("QWERTYUIOP", e.message.toString()) - } - } - productImage.setImageURI(imageUri) + + productPicture.setImageURI(imageUri) productNameField.text = productName netWeightField.text = 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) + //TODO: category and units } -} - -//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 +} \ 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 caab862..d4b845c 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt @@ -34,7 +34,7 @@ class StorageFragment : Fragment() { 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 selectionArgs = arrayOf("test") val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null) diff --git a/app/src/main/res/layout/abstract_product_view.xml b/app/src/main/res/layout/abstract_product_view.xml index 8a51305..04b20c7 100644 --- a/app/src/main/res/layout/abstract_product_view.xml +++ b/app/src/main/res/layout/abstract_product_view.xml @@ -1,19 +1,9 @@ - - - @@ -47,6 +37,13 @@ app:layout_constraintStart_toEndOf="@+id/productNetWeightView" app:layout_constraintTop_toBottomOf="@+id/productNameView" android:layout_marginTop="10dp" android:fontFamily="monospace" android:textSize="12sp" android:layout_marginStart="8dp"/> + - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 72f9bf7..0951f0e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,4 +16,5 @@ Уззкое название товара 1998 g + Sample category \ No newline at end of file