added barcode to abstractproduct, working on adding products
This commit is contained in:
		@@ -45,7 +45,7 @@
 | 
			
		||||
 | 
			
		||||
        <provider
 | 
			
		||||
                android:name="androidx.core.content.FileProvider"
 | 
			
		||||
                android:authorities="com.google.firebase.components.activities.MainActivity.provider;com.google.firebase.components.activities.FullscreenActivity.provider;com.google.firebase.components.activities.AddAbstractProductActivity.provider"
 | 
			
		||||
                android:authorities="com.google.firebase.components.activities.MainActivity.provider;com.google.firebase.components.activities.FullscreenActivity.provider;com.google.firebase.components.activities.AddAbstractProductActivity.provider;com.google.firebase.components.activities.AddProductActivity.provider"
 | 
			
		||||
                android:exported="false"
 | 
			
		||||
                android:grantUriPermissions="true">
 | 
			
		||||
            <meta-data
 | 
			
		||||
 
 | 
			
		||||
@@ -166,6 +166,36 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
 | 
			
		||||
        db.delete(AbstractProductContract.AbstractProductEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun findAbstractProductByBarcode (db: SQLiteDatabase, barcode: String) : AbstractProduct? {
 | 
			
		||||
        var abstractProduct: AbstractProduct? = null
 | 
			
		||||
        val projection = arrayOf(
 | 
			
		||||
            BaseColumns._ID,
 | 
			
		||||
            AbstractProductContract.AbstractProductEntry.PRODUCT_NAME,
 | 
			
		||||
            AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME,
 | 
			
		||||
            AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT,
 | 
			
		||||
            AbstractProductContract.AbstractProductEntry.CATEGORY
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        val selection = "${AbstractProductContract.AbstractProductEntry.BARCODE} = ?"
 | 
			
		||||
        val selectionArgs = arrayOf(barcode)
 | 
			
		||||
 | 
			
		||||
        val cursor = db.query(AbstractProductContract.AbstractProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null)
 | 
			
		||||
 | 
			
		||||
        with(cursor) {
 | 
			
		||||
            while(moveToNext()) {
 | 
			
		||||
                val id = getInt(getColumnIndexOrThrow(BaseColumns._ID))
 | 
			
		||||
                val productName = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME))
 | 
			
		||||
                val imageFilename = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME))
 | 
			
		||||
                val netWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT))
 | 
			
		||||
                val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY))
 | 
			
		||||
 | 
			
		||||
                abstractProduct = AbstractProduct(id, barcode, productName, netWeight, imageFilename, category)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return abstractProduct
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    companion object {
 | 
			
		||||
        const val DATABASE_VERSION = 1
 | 
			
		||||
        const val DATABASE_NAME = "database.db"
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ package org.foxarmy.barcodescannerforemployees
 | 
			
		||||
 | 
			
		||||
import android.app.Activity
 | 
			
		||||
import android.content.Context
 | 
			
		||||
import android.content.ContextWrapper
 | 
			
		||||
import android.graphics.Bitmap
 | 
			
		||||
import android.graphics.BitmapFactory
 | 
			
		||||
import android.net.Uri
 | 
			
		||||
@@ -56,4 +57,30 @@ fun removeSubstringsFromString(text: String, toRemove: Array<String>): String {
 | 
			
		||||
    return result
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fun String.utf8(): String = URLEncoder.encode(this, "UTF-8")
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
@@ -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<ConstraintLayout>(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()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -11,7 +11,7 @@
 | 
			
		||||
            android:background="#00FFFEFE" android:clickable="true">
 | 
			
		||||
        <ImageView
 | 
			
		||||
                android:layout_width="match_parent"
 | 
			
		||||
                android:layout_height="200dp" app:srcCompat="@android:drawable/ic_btn_speak_now"
 | 
			
		||||
                android:layout_height="200dp" app:srcCompat="@android:drawable/ic_menu_gallery"
 | 
			
		||||
                android:id="@+id/productPicture"
 | 
			
		||||
                app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
 | 
			
		||||
                app:layout_constraintEnd_toEndOf="parent"/>
 | 
			
		||||
@@ -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"/>
 | 
			
		||||
    </androidx.constraintlayout.widget.ConstraintLayout>
 | 
			
		||||
 | 
			
		||||
</LinearLayout>
 | 
			
		||||
@@ -9,12 +9,19 @@
 | 
			
		||||
    <androidx.constraintlayout.widget.ConstraintLayout
 | 
			
		||||
            android:layout_width="wrap_content"
 | 
			
		||||
            android:layout_height="wrap_content"
 | 
			
		||||
            android:padding="16dp">
 | 
			
		||||
        <Spinner
 | 
			
		||||
            android:layout_width="400dp"
 | 
			
		||||
            android:layout_height="50dp" android:id="@+id/abstractProductSelect"
 | 
			
		||||
            app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="12dp"
 | 
			
		||||
            app:layout_constraintStart_toStartOf="parent"
 | 
			
		||||
            app:layout_constraintEnd_toEndOf="parent"/>
 | 
			
		||||
            android:padding="16dp" android:layout_gravity="center_horizontal" android:id="@+id/addProductConstraintLayout">
 | 
			
		||||
        <org.foxarmy.barcodescannerforemployees.views.AbstractProductView
 | 
			
		||||
                android:layout_width="300dp"
 | 
			
		||||
                android:layout_height="300dp" android:id="@+id/abstractProductView"
 | 
			
		||||
                app:layout_constraintTop_toTopOf="parent"
 | 
			
		||||
                android:layout_marginTop="32dp" app:layout_constraintStart_toStartOf="parent"
 | 
			
		||||
                app:layout_constraintEnd_toEndOf="parent"/>
 | 
			
		||||
        <Button
 | 
			
		||||
                android:text="@string/scan_label"
 | 
			
		||||
                android:layout_width="wrap_content"
 | 
			
		||||
                android:layout_height="wrap_content" android:id="@+id/scanButton" android:layout_weight="1"
 | 
			
		||||
                app:layout_constraintStart_toStartOf="parent"
 | 
			
		||||
                app:layout_constraintEnd_toEndOf="parent"
 | 
			
		||||
                app:layout_constraintTop_toBottomOf="@+id/abstractProductView" android:layout_marginTop="16dp"/>
 | 
			
		||||
    </androidx.constraintlayout.widget.ConstraintLayout>
 | 
			
		||||
</androidx.core.widget.NestedScrollView>
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
<resources>
 | 
			
		||||
    <string name="app_name">BarcodeScannerForEmployees</string>
 | 
			
		||||
    <string name="action_settings">Settings</string>
 | 
			
		||||
    <!-- Strings used for fragments for navigation -->
 | 
			
		||||
    <string name="first_fragment_label">Add new product</string>
 | 
			
		||||
    <string name="second_fragment_label">Products</string>
 | 
			
		||||
    <string name="scan_label">Scan</string>
 | 
			
		||||
@@ -13,8 +12,8 @@
 | 
			
		||||
    <string name="productName">Product name</string>
 | 
			
		||||
    <string name="productType">Product type</string>
 | 
			
		||||
    <string name="imageOfAProduct">Image of a product</string>
 | 
			
		||||
    <string name="sample_product_name">Уззкое название товара</string>
 | 
			
		||||
    <string name="sample_product_net_weight">1998</string>
 | 
			
		||||
    <string name="sample_product_name">Sample product name</string>
 | 
			
		||||
    <string name="sample_product_net_weight">100</string>
 | 
			
		||||
    <string name="sample_unit">g</string>
 | 
			
		||||
    <string name="sample_category">Sample category</string>
 | 
			
		||||
    <string name="title_activity_fullscreen">FullscreenActivity</string>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user