First working viewgroup
This commit is contained in:
		@@ -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<ConstraintLayout>(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)
 | 
			
		||||
//        }
 | 
			
		||||
//    }
 | 
			
		||||
//}
 | 
			
		||||
}
 | 
			
		||||
@@ -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)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,19 +1,9 @@
 | 
			
		||||
<FrameLayout
 | 
			
		||||
<LinearLayout
 | 
			
		||||
        xmlns:android="http://schemas.android.com/apk/res/android"
 | 
			
		||||
        xmlns:app="http://schemas.android.com/apk/res-auto"
 | 
			
		||||
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="300dp"
 | 
			
		||||
        android:layout_height="300dp">
 | 
			
		||||
 | 
			
		||||
    <org.foxarmy.barcodescannerforemployees.AbstractProductView
 | 
			
		||||
            style="@style/Widget.Theme.BarcodeScannerForEmployees.MyView"
 | 
			
		||||
            android:layout_width="300dp"
 | 
			
		||||
            android:layout_height="300dp"
 | 
			
		||||
            android:paddingLeft="20dp"
 | 
			
		||||
            android:paddingBottom="40dp"
 | 
			
		||||
            app:exampleDimension="24sp"
 | 
			
		||||
            app:exampleString="Hello, AbstractProductView"
 | 
			
		||||
            app:exampleDrawable="@android:drawable/ic_menu_add">
 | 
			
		||||
    </org.foxarmy.barcodescannerforemployees.AbstractProductView>
 | 
			
		||||
    <androidx.constraintlayout.widget.ConstraintLayout
 | 
			
		||||
            android:layout_width="300dp"
 | 
			
		||||
            android:layout_height="300dp" android:id="@+id/productLayout">
 | 
			
		||||
@@ -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"/>
 | 
			
		||||
        <TextView
 | 
			
		||||
                android:text="@string/sample_category"
 | 
			
		||||
                android:layout_width="wrap_content"
 | 
			
		||||
                android:layout_height="wrap_content" android:id="@+id/categoryView"
 | 
			
		||||
                app:layout_constraintEnd_toEndOf="parent"
 | 
			
		||||
                android:layout_marginEnd="20dp" android:fontFamily="monospace" android:textSize="10sp"
 | 
			
		||||
                app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="20dp"/>
 | 
			
		||||
    </androidx.constraintlayout.widget.ConstraintLayout>
 | 
			
		||||
 | 
			
		||||
</FrameLayout>
 | 
			
		||||
</LinearLayout>
 | 
			
		||||
@@ -16,4 +16,5 @@
 | 
			
		||||
    <string name="sample_product_name">Уззкое название товара</string>
 | 
			
		||||
    <string name="sample_product_net_weight">1998</string>
 | 
			
		||||
    <string name="sample_unit">g</string>
 | 
			
		||||
    <string name="sample_category">Sample category</string>
 | 
			
		||||
</resources>
 | 
			
		||||
		Reference in New Issue
	
	Block a user