diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt index 237b151..fc9c998 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt @@ -1,11 +1,51 @@ package org.foxarmy.barcodescannerforemployees -class AbstractProduct( - public var id: Int, - public var name: String, - public var netWeight: Double, - public var imageHash: String, - public var categoryName: String -) { +import android.os.Parcel +import android.os.Parcelable + +class AbstractProduct() : Parcelable { + var id: Int = 0 + var name: String = "" + var netWeight: Double = 0.0 + var imageHash: String = "" + var category: Int = 0 + + constructor(id: Int, name: String ,netWeight: Double, imageHash: String, category: Int) : this() { + this.id = id + this.name = name + this.netWeight = netWeight + this.imageHash = imageHash + this.category= category + } + + constructor(parcel: Parcel) : this() { + id = parcel.readInt() + name = parcel.readString()!! + netWeight = parcel.readDouble() + imageHash = parcel.readString()!! + category = parcel.readInt() + } + + override fun writeToParcel(parcel: Parcel, flags: Int) { + parcel.writeInt(id) + parcel.writeString(name) + parcel.writeDouble(netWeight) + parcel.writeString(imageHash) + parcel.writeInt(category) + } + + override fun describeContents(): Int { + return 0 + } + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): AbstractProduct { + return AbstractProduct(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt index 393960b..a612c42 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt @@ -72,6 +72,24 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE db.delete(CategoriesContract.CategoryEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null) } + fun getCategoryNameById(db: SQLiteDatabase, id: Int) : String { + var result = "" + + val projection = arrayOf(CategoriesContract.CategoryEntry.CATEGORY_NAME) + val selection = "${BaseColumns._ID} = ?" + val selectionArgs = arrayOf(id.toString()) + + val cursor = db.query(CategoriesContract.CategoryEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null) + + with (cursor) { + while (moveToNext()) { + result = getString(getColumnIndexOrThrow(CategoriesContract.CategoryEntry.CATEGORY_NAME)) + } + } + + return result + } + fun getAllAbstractProductInCategory(db: SQLiteDatabase, id: Int) : List { var result = mutableListOf() @@ -94,10 +112,7 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE val abstractProductNetWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT)) val abstractProductImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME)) - val abstractProduct = AbstractProduct( - abstractProductId, abstractProductName, abstractProductNetWeight, abstractProductImageHash, - categoryName = "" - ) + val abstractProduct = AbstractProduct(abstractProductId, abstractProductName, abstractProductNetWeight, abstractProductImageHash, category = id) result.add(abstractProduct) } 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 71d1c4b..f96faf7 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddProductActivity.kt @@ -31,6 +31,7 @@ class AddProductActivity : AppCompatActivity() { private lateinit var categorySpinner: Spinner + private var product: AbstractProduct? = null private lateinit var pictureFile: File private lateinit var picturesPath: File private lateinit var binding: ActivityAddProductBinding @@ -40,8 +41,12 @@ class AddProductActivity : AppCompatActivity() { setContentView(R.layout.fragment_add_product) + val extras = intent.extras + product = extras!!.get("updatingObject") as AbstractProduct? picturesPath = File(filesDir, "pictures") + val thumbnailsDir = File(cacheDir, "thumbnails") + thumbnailsDir.mkdirs() picturesPath.mkdirs() imageView = findViewById(R.id.imageView) @@ -56,10 +61,21 @@ class AddProductActivity : AppCompatActivity() { fillupCategorySpinner() + if (product != null) { + val imageThumbnailUri = getImageUri(this, File(thumbnailsDir, "${product!!.imageHash}.webp")) + pictureFile = File(picturesPath, "${product!!.imageHash}.png]") + imageView.setImageURI(imageThumbnailUri) + imageView.rotation = 90f + productNameText.text = product!!.name + netWeightText.text = product!!.netWeight.toString() + categorySpinner.setSelection(product!!.category) + } + saveButton.setOnClickListener { val productName = productNameText.text.toString() val netWeight = netWeightText.text - if (!this::pictureFile.isInitialized || !pictureFile.exists()) { + if (!this::pictureFile.isInitialized && !pictureFile.exists()) { + Toast.makeText(this, "Please, make a picture of a product!", Toast.LENGTH_SHORT).show() return@setOnClickListener } @@ -80,7 +96,11 @@ class AddProductActivity : AppCompatActivity() { put(ProductContract.ProductEntry.CATEGORY, categorySpinner.selectedItemPosition) } - db.insert(ProductContract.ProductEntry.TABLE_NAME, null, values) + if (product == null) { + db.insert(ProductContract.ProductEntry.TABLE_NAME, null, values) + } else { + db.update(ProductContract.ProductEntry.TABLE_NAME, values, "${BaseColumns._ID} = ?", arrayOf(product!!.id.toString())) + } finish() } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt index 596c1e2..5bb27d4 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt @@ -38,6 +38,10 @@ class MainActivity : AppCompatActivity() { "StorageFragment" -> { val addProductIntent = Intent(this, AddProductActivity::class.java) val extras = Bundle() + // I reuse the same stuff for editing and adding new product. + // if updatingObject == null, it means that we need to create new object + extras.putParcelable("updatingObject", null) + addProductIntent.putExtras(extras) ContextCompat.startActivity(this, addProductIntent, extras) } @@ -90,7 +94,7 @@ class MainActivity : AppCompatActivity() { } "CategoriesFragment" -> { - val builder = AlertDialog.Builder(this) + AlertDialog.Builder(this) .setMessage("Deleting a category will also delete ALL the products, that belong to that category. Do you want to proceed?") .setPositiveButton("Yes") { _: DialogInterface, _: Int -> val categoriesFragment = fragment as CategoriesFragment diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt index 2c4182b..dabb681 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -1,5 +1,6 @@ package org.foxarmy.barcodescannerforemployees.fragments +import android.content.Intent import android.os.Bundle import android.provider.BaseColumns import android.view.LayoutInflater @@ -7,10 +8,12 @@ import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.Toast +import androidx.core.content.ContextCompat import androidx.core.view.children import androidx.fragment.app.Fragment import androidx.gridlayout.widget.GridLayout import org.foxarmy.barcodescannerforemployees.* +import org.foxarmy.barcodescannerforemployees.activities.AddProductActivity import org.foxarmy.barcodescannerforemployees.views.AbstractProductView class StorageFragment : Fragment() { @@ -48,6 +51,17 @@ class StorageFragment : Fragment() { } fun updateSelected() { + val grv = view?.findViewById(R.id.contentGridLayout) + + for (view: AbstractProductView in grv?.children!!.iterator() as Iterator) { + if (view.isProductSelected) { + val addProductIntent = Intent(requireContext(), AddProductActivity::class.java) + val extras = Bundle() + extras.putParcelable("updatingObject", view.product) + addProductIntent.putExtras(extras) + ContextCompat.startActivity(requireContext(), addProductIntent, extras) + } + } } @@ -73,19 +87,8 @@ class StorageFragment : Fragment() { val netWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT)) val productImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME)) val category = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.CATEGORY)) - var categoryName = "" - val projection2 = arrayOf(CategoriesContract.CategoryEntry.CATEGORY_NAME) - val cursor2 = db.query(CategoriesContract.CategoryEntry.TABLE_NAME, projection2, "${BaseColumns._ID} = ?", - arrayOf(category.toString()), null, null, BaseColumns._ID+" ASC") - - with (cursor2) { - while (moveToNext()) { - categoryName = getString(getColumnIndexOrThrow(CategoriesContract.CategoryEntry.CATEGORY_NAME)) - } - } - - val product = AbstractProduct(productId, productName, netWeight, productImageHash, categoryName) + val product = AbstractProduct(productId, productName, netWeight, productImageHash, category) generateThumbnailForImage(context!!, productImageHash) 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 5f7d9ee..88e2557 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt @@ -12,6 +12,7 @@ import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat.startActivity import org.foxarmy.barcodescannerforemployees.AbstractProduct +import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity import org.foxarmy.barcodescannerforemployees.getImageUri @@ -58,9 +59,11 @@ class AbstractProductView: LinearLayout { } + + productNameField.text = product.name netWeightField.text = product.netWeight.toString() - categoryField.text = product.categoryName + categoryField.text = DBStorageController(context).getCategoryNameById(DBStorageController(context).readableDatabase, product.category) //TODO: units