now deleting product together with abstract product

This commit is contained in:
leca 2024-10-15 02:39:23 +03:00
parent 5066ded9c7
commit f50e10e231
5 changed files with 121 additions and 77 deletions

View File

@ -137,24 +137,24 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
return DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM ${AbstractProductContract.AbstractProductEntry.TABLE_NAME} WHERE ${AbstractProductContract.AbstractProductEntry.CATEGORY} = ?", arrayOf(id.toString())).toInt() return DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM ${AbstractProductContract.AbstractProductEntry.TABLE_NAME} WHERE ${AbstractProductContract.AbstractProductEntry.CATEGORY} = ?", arrayOf(id.toString())).toInt()
} }
fun eraseAbstractProduct(db: SQLiteDatabase, id: Int, context: Context) { fun eraseAbstractProduct(db: SQLiteDatabase, id: Int, context: Context) {
val projection = arrayOf( val projectionForAbstractProduct = arrayOf(
AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME
) )
val selection = "${BaseColumns._ID} = ?" val selectionForAbstractProduct = "${BaseColumns._ID} = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgsForAbstractProduct = arrayOf(id.toString())
val cursor = db.query( val cursorForAbstractProduct = db.query(
AbstractProductContract.AbstractProductEntry.TABLE_NAME, AbstractProductContract.AbstractProductEntry.TABLE_NAME,
projection, projectionForAbstractProduct,
selection, selectionForAbstractProduct,
selectionArgs, selectionArgsForAbstractProduct,
null, null,
null, null,
null null
) )
var imageHash: String = "" var imageHash: String = ""
with (cursor) { with (cursorForAbstractProduct) {
while(moveToNext()) { while(moveToNext()) {
val productImageHash = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME)) val productImageHash = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME))
imageHash = productImageHash imageHash = productImageHash
@ -165,9 +165,26 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
File(picturesDir, "$imageHash.png").delete() File(picturesDir, "$imageHash.png").delete()
File(thumbnailsDir, "$imageHash.webp").delete() File(thumbnailsDir, "$imageHash.webp").delete()
val projectionForProducts = arrayOf(BaseColumns._ID)
val selectionForProducts = "${ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID} = ?"
val selectionArgsForProducts = arrayOf(id.toString())
val cursorForProducts = db.query(ProductContract.ProductEntry.TABLE_NAME, projectionForProducts, selectionForProducts, selectionArgsForProducts, null, null, null)
with (cursorForProducts) {
while(moveToNext()) {
eraseProduct(db, getInt(getColumnIndexOrThrow(BaseColumns._ID)))
}
}
db.delete(AbstractProductContract.AbstractProductEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null) db.delete(AbstractProductContract.AbstractProductEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null)
} }
fun eraseProduct(db:SQLiteDatabase, id: Int) {
db.delete(ProductContract.ProductEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null)
}
fun insertNewProduct(db: SQLiteDatabase, product: Product) { fun insertNewProduct(db: SQLiteDatabase, product: Product) {
val values = ContentValues().apply { val values = ContentValues().apply {
put(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID, product.abstractProductId) put(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID, product.abstractProductId)

View File

@ -101,8 +101,15 @@ class MainActivity : AppCompatActivity() {
when (fragment::class.simpleName.toString()) { when (fragment::class.simpleName.toString()) {
"StorageFragment" -> { "StorageFragment" -> {
val storageFragment = fragment as StorageFragment AlertDialog.Builder(this)
storageFragment.removeSelected() .setMessage("Deleting an abstract product will also delete ALL the products, that belong to it. Do you want to proceed?")
.setPositiveButton("Yes") { _: DialogInterface, _: Int ->
val storageFragment = fragment as StorageFragment
storageFragment.removeSelected()
}
.setNegativeButton("No") { _: DialogInterface, _: Int ->
}.show()
} }
"CategoriesFragment" -> { "CategoriesFragment" -> {

View File

@ -12,6 +12,7 @@ import org.foxarmy.barcodescannerforemployees.ProductContract
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.dataclasses.Product import org.foxarmy.barcodescannerforemployees.dataclasses.Product
import org.foxarmy.barcodescannerforemployees.views.ProductView import org.foxarmy.barcodescannerforemployees.views.ProductView
import kotlin.concurrent.thread
class ShelfFragment : Fragment() { class ShelfFragment : Fragment() {
override fun onCreateView( override fun onCreateView(
@ -35,36 +36,44 @@ class ShelfFragment : Fragment() {
} }
fun updateContent() { fun updateContent() {
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout) thread {
grv?.removeAllViews() val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout)
activity!!.runOnUiThread {
grv?.removeAllViews()
}
val db = DBStorageController(requireContext()).readableDatabase val db = DBStorageController(requireContext()).readableDatabase
val projection = arrayOf( val projection = arrayOf(
BaseColumns._ID, BaseColumns._ID,
ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID, ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID,
ProductContract.ProductEntry.AMOUNT, ProductContract.ProductEntry.AMOUNT,
ProductContract.ProductEntry.DATE_OF_PRODUCTION, ProductContract.ProductEntry.DATE_OF_PRODUCTION,
ProductContract.ProductEntry.EXPIRY_DATE, ProductContract.ProductEntry.EXPIRY_DATE,
) )
val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, null, null, null, null, null) val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, null, null, null, null, null)
with (cursor) { with(cursor) {
while(moveToNext()) { while (moveToNext()) {
val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID)) val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val abstractProductId = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID)) val abstractProductId =
val amount = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.AMOUNT)) getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID))
val dateOfProduction = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.DATE_OF_PRODUCTION)) val amount = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.AMOUNT))
val dateOfExpiry = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.EXPIRY_DATE)) val dateOfProduction =
getString(getColumnIndexOrThrow(ProductContract.ProductEntry.DATE_OF_PRODUCTION))
val dateOfExpiry = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.EXPIRY_DATE))
val product = Product(productId, abstractProductId, amount, dateOfProduction, dateOfExpiry) val product = Product(productId, abstractProductId, amount, dateOfProduction, dateOfExpiry)
val productView = ProductView( val productView = ProductView(
requireActivity(), requireActivity(),
requireContext(), requireContext(),
product product
) )
grv?.addView(productView) activity!!.runOnUiThread {
grv?.addView(productView)
}
}
} }
} }
} }

View File

@ -19,6 +19,7 @@ import org.foxarmy.barcodescannerforemployees.activities.AddAbstractProductActiv
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
import org.foxarmy.barcodescannerforemployees.generateThumbnailForImage import org.foxarmy.barcodescannerforemployees.generateThumbnailForImage
import org.foxarmy.barcodescannerforemployees.views.AbstractProductView import org.foxarmy.barcodescannerforemployees.views.AbstractProductView
import kotlin.concurrent.thread
class StorageFragment : Fragment() { class StorageFragment : Fragment() {
@ -36,22 +37,28 @@ class StorageFragment : Fragment() {
} }
fun removeSelected() { fun removeSelected() {
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout) thread {
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout)
val db = DBStorageController(requireContext()) val db = DBStorageController(requireContext())
var deleted = false var deleted = false
for (view: AbstractProductView in grv?.children!!.iterator() as Iterator<AbstractProductView>) { for (view: AbstractProductView in grv?.children!!.iterator() as Iterator<AbstractProductView>) {
view.findViewById<ImageView>(R.id.productPicture).setImageURI(null) activity!!.runOnUiThread {
if (view.isProductSelected) { view.findViewById<ImageView>(R.id.productPicture).setImageURI(null)
db.eraseAbstractProduct(db.writableDatabase, view.abstractProduct.id, requireContext()) }
deleted = true if (view.isProductSelected) {
db.eraseAbstractProduct(db.writableDatabase, view.abstractProduct.id, requireContext())
deleted = true
}
} }
}
if (!deleted) { if (!deleted) {
Toast.makeText(requireContext(), "Nothing to delete", Toast.LENGTH_SHORT).show() activity!!.runOnUiThread {
Toast.makeText(requireContext(), "Nothing to delete", Toast.LENGTH_SHORT).show()
}
}
updateContent()
} }
updateContent()
} }
fun updateSelected() { fun updateSelected() {
@ -66,44 +73,49 @@ class StorageFragment : Fragment() {
ContextCompat.startActivity(requireContext(), addProductIntent, extras) ContextCompat.startActivity(requireContext(), addProductIntent, extras)
} }
} }
} }
fun updateContent() { fun updateContent() {
thread {
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout)
activity!!.runOnUiThread{
grv?.removeAllViews()
}
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout) val db = DBStorageController(requireContext()).readableDatabase
grv?.removeAllViews() val projection = arrayOf(
BaseColumns._ID,
AbstractProductContract.AbstractProductEntry.BARCODE,
AbstractProductContract.AbstractProductEntry.PRODUCT_NAME,
AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT,
AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME,
AbstractProductContract.AbstractProductEntry.CATEGORY
)
val db = DBStorageController(requireContext()).readableDatabase val cursor = db.query(AbstractProductContract.AbstractProductEntry.TABLE_NAME, projection, null, null, null, null, null)
val projection = arrayOf(BaseColumns._ID,
AbstractProductContract.AbstractProductEntry.BARCODE,
AbstractProductContract.AbstractProductEntry.PRODUCT_NAME,
AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT,
AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME,
AbstractProductContract.AbstractProductEntry.CATEGORY
)
val cursor = db.query(AbstractProductContract.AbstractProductEntry.TABLE_NAME, projection, null, null, null, null, null) with (cursor) {
while(moveToNext()) {
val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val barcode = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.BARCODE))
val productName = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME))
val netWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT))
val productImageHash = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME))
val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY))
with (cursor) { val product = AbstractProduct(productId, barcode, productName, netWeight, productImageHash, category)
while(moveToNext()) {
val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val barcode = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.BARCODE))
val productName = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME))
val netWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT))
val productImageHash = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME))
val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY))
val product = AbstractProduct(productId, barcode, productName, netWeight, productImageHash, category) generateThumbnailForImage(context!!, productImageHash)
generateThumbnailForImage(context!!, productImageHash) val abstractProduct = AbstractProductView(
requireActivity(),
val abstractProduct = AbstractProductView( requireContext(),
requireActivity(), product
requireContext(), )
product activity!!.runOnUiThread{
) grv?.addView(abstractProduct)
grv?.addView(abstractProduct) }
}
} }
} }
} }

View File

@ -67,7 +67,6 @@ class ProductView: LinearLayout {
productLifeSpan.text = "${product.dateOfProduction}-${product.dateOfExpiry}" productLifeSpan.text = "${product.dateOfProduction}-${product.dateOfExpiry}"
this.background = ContextCompat.getDrawable(context, if (isProductSelected) R.drawable.outline_selected else R.drawable.outline) this.background = ContextCompat.getDrawable(context, if (isProductSelected) R.drawable.outline_selected else R.drawable.outline)
findViewById<ConstraintLayout>(R.id.productLayout).setOnLongClickListener { findViewById<ConstraintLayout>(R.id.productLayout).setOnLongClickListener {
isProductSelected = !isProductSelected isProductSelected = !isProductSelected