a bit of remake, better UI and ability to update abstract products

This commit is contained in:
leca 2024-10-09 17:49:12 +03:00
parent a5a4c5db40
commit 332c7cd3fe
6 changed files with 112 additions and 27 deletions

View File

@ -1,11 +1,51 @@
package org.foxarmy.barcodescannerforemployees package org.foxarmy.barcodescannerforemployees
class AbstractProduct( import android.os.Parcel
public var id: Int, import android.os.Parcelable
public var name: String,
public var netWeight: Double, class AbstractProduct() : Parcelable {
public var imageHash: String, var id: Int = 0
public var categoryName: String 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<AbstractProduct> {
override fun createFromParcel(parcel: Parcel): AbstractProduct {
return AbstractProduct(parcel)
}
override fun newArray(size: Int): Array<AbstractProduct?> {
return arrayOfNulls(size)
}
}
} }

View File

@ -72,6 +72,24 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
db.delete(CategoriesContract.CategoryEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null) 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<AbstractProduct> { fun getAllAbstractProductInCategory(db: SQLiteDatabase, id: Int) : List<AbstractProduct> {
var result = mutableListOf<AbstractProduct>() var result = mutableListOf<AbstractProduct>()
@ -94,10 +112,7 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
val abstractProductNetWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT)) val abstractProductNetWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT))
val abstractProductImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME)) val abstractProductImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME))
val abstractProduct = AbstractProduct( val abstractProduct = AbstractProduct(abstractProductId, abstractProductName, abstractProductNetWeight, abstractProductImageHash, category = id)
abstractProductId, abstractProductName, abstractProductNetWeight, abstractProductImageHash,
categoryName = ""
)
result.add(abstractProduct) result.add(abstractProduct)
} }

View File

@ -31,6 +31,7 @@ class AddProductActivity : AppCompatActivity() {
private lateinit var categorySpinner: Spinner private lateinit var categorySpinner: Spinner
private var product: AbstractProduct? = null
private lateinit var pictureFile: File private lateinit var pictureFile: File
private lateinit var picturesPath: File private lateinit var picturesPath: File
private lateinit var binding: ActivityAddProductBinding private lateinit var binding: ActivityAddProductBinding
@ -40,8 +41,12 @@ class AddProductActivity : AppCompatActivity() {
setContentView(R.layout.fragment_add_product) setContentView(R.layout.fragment_add_product)
val extras = intent.extras
product = extras!!.get("updatingObject") as AbstractProduct?
picturesPath = File(filesDir, "pictures") picturesPath = File(filesDir, "pictures")
val thumbnailsDir = File(cacheDir, "thumbnails")
thumbnailsDir.mkdirs()
picturesPath.mkdirs() picturesPath.mkdirs()
imageView = findViewById(R.id.imageView) imageView = findViewById(R.id.imageView)
@ -56,10 +61,21 @@ class AddProductActivity : AppCompatActivity() {
fillupCategorySpinner() 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 { saveButton.setOnClickListener {
val productName = productNameText.text.toString() val productName = productNameText.text.toString()
val netWeight = netWeightText.text 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() Toast.makeText(this, "Please, make a picture of a product!", Toast.LENGTH_SHORT).show()
return@setOnClickListener return@setOnClickListener
} }
@ -80,7 +96,11 @@ class AddProductActivity : AppCompatActivity() {
put(ProductContract.ProductEntry.CATEGORY, categorySpinner.selectedItemPosition) 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() finish()
} }

View File

@ -38,6 +38,10 @@ class MainActivity : AppCompatActivity() {
"StorageFragment" -> { "StorageFragment" -> {
val addProductIntent = Intent(this, AddProductActivity::class.java) val addProductIntent = Intent(this, AddProductActivity::class.java)
val extras = Bundle() 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) ContextCompat.startActivity(this, addProductIntent, extras)
} }
@ -90,7 +94,7 @@ class MainActivity : AppCompatActivity() {
} }
"CategoriesFragment" -> { "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?") .setMessage("Deleting a category will also delete ALL the products, that belong to that category. Do you want to proceed?")
.setPositiveButton("Yes") { _: DialogInterface, _: Int -> .setPositiveButton("Yes") { _: DialogInterface, _: Int ->
val categoriesFragment = fragment as CategoriesFragment val categoriesFragment = fragment as CategoriesFragment

View File

@ -1,5 +1,6 @@
package org.foxarmy.barcodescannerforemployees.fragments package org.foxarmy.barcodescannerforemployees.fragments
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.provider.BaseColumns import android.provider.BaseColumns
import android.view.LayoutInflater import android.view.LayoutInflater
@ -7,10 +8,12 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.Toast import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.view.children import androidx.core.view.children
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.gridlayout.widget.GridLayout import androidx.gridlayout.widget.GridLayout
import org.foxarmy.barcodescannerforemployees.* import org.foxarmy.barcodescannerforemployees.*
import org.foxarmy.barcodescannerforemployees.activities.AddProductActivity
import org.foxarmy.barcodescannerforemployees.views.AbstractProductView import org.foxarmy.barcodescannerforemployees.views.AbstractProductView
class StorageFragment : Fragment() { class StorageFragment : Fragment() {
@ -48,6 +51,17 @@ class StorageFragment : Fragment() {
} }
fun updateSelected() { fun updateSelected() {
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout)
for (view: AbstractProductView in grv?.children!!.iterator() as Iterator<AbstractProductView>) {
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 netWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT))
val productImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME)) val productImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME))
val category = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.CATEGORY)) val category = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.CATEGORY))
var categoryName = ""
val projection2 = arrayOf(CategoriesContract.CategoryEntry.CATEGORY_NAME) val product = AbstractProduct(productId, productName, netWeight, productImageHash, category)
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)
generateThumbnailForImage(context!!, productImageHash) generateThumbnailForImage(context!!, productImageHash)

View File

@ -12,6 +12,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.startActivity import androidx.core.content.ContextCompat.startActivity
import org.foxarmy.barcodescannerforemployees.AbstractProduct import org.foxarmy.barcodescannerforemployees.AbstractProduct
import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity
import org.foxarmy.barcodescannerforemployees.getImageUri import org.foxarmy.barcodescannerforemployees.getImageUri
@ -58,9 +59,11 @@ class AbstractProductView: LinearLayout {
} }
productNameField.text = product.name productNameField.text = product.name
netWeightField.text = product.netWeight.toString() netWeightField.text = product.netWeight.toString()
categoryField.text = product.categoryName categoryField.text = DBStorageController(context).getCategoryNameById(DBStorageController(context).readableDatabase, product.category)
//TODO: units //TODO: units