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,9 +101,16 @@ class MainActivity : AppCompatActivity() {
when (fragment::class.simpleName.toString()) { when (fragment::class.simpleName.toString()) {
"StorageFragment" -> { "StorageFragment" -> {
AlertDialog.Builder(this)
.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 val storageFragment = fragment as StorageFragment
storageFragment.removeSelected() storageFragment.removeSelected()
} }
.setNegativeButton("No") { _: DialogInterface, _: Int ->
}.show()
}
"CategoriesFragment" -> { "CategoriesFragment" -> {
AlertDialog.Builder(this) AlertDialog.Builder(this)

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,8 +36,11 @@ class ShelfFragment : Fragment() {
} }
fun updateContent() { fun updateContent() {
thread {
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout) val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout)
activity!!.runOnUiThread {
grv?.removeAllViews() grv?.removeAllViews()
}
val db = DBStorageController(requireContext()).readableDatabase val db = DBStorageController(requireContext()).readableDatabase
val projection = arrayOf( val projection = arrayOf(
@ -49,12 +53,14 @@ class ShelfFragment : Fragment() {
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 =
getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID))
val amount = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.AMOUNT)) val amount = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.AMOUNT))
val dateOfProduction = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.DATE_OF_PRODUCTION)) val dateOfProduction =
getString(getColumnIndexOrThrow(ProductContract.ProductEntry.DATE_OF_PRODUCTION))
val dateOfExpiry = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.EXPIRY_DATE)) val dateOfExpiry = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.EXPIRY_DATE))
val product = Product(productId, abstractProductId, amount, dateOfProduction, dateOfExpiry) val product = Product(productId, abstractProductId, amount, dateOfProduction, dateOfExpiry)
@ -64,8 +70,11 @@ class ShelfFragment : Fragment() {
requireContext(), requireContext(),
product product
) )
activity!!.runOnUiThread {
grv?.addView(productView) 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,12 +37,15 @@ class StorageFragment : Fragment() {
} }
fun removeSelected() { fun removeSelected() {
thread {
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout) 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>) {
activity!!.runOnUiThread {
view.findViewById<ImageView>(R.id.productPicture).setImageURI(null) view.findViewById<ImageView>(R.id.productPicture).setImageURI(null)
}
if (view.isProductSelected) { if (view.isProductSelected) {
db.eraseAbstractProduct(db.writableDatabase, view.abstractProduct.id, requireContext()) db.eraseAbstractProduct(db.writableDatabase, view.abstractProduct.id, requireContext())
deleted = true deleted = true
@ -49,10 +53,13 @@ class StorageFragment : Fragment() {
} }
if (!deleted) { if (!deleted) {
activity!!.runOnUiThread {
Toast.makeText(requireContext(), "Nothing to delete", Toast.LENGTH_SHORT).show() Toast.makeText(requireContext(), "Nothing to delete", Toast.LENGTH_SHORT).show()
} }
}
updateContent() updateContent()
} }
}
fun updateSelected() { fun updateSelected() {
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout) val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout)
@ -66,16 +73,18 @@ 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) val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout)
activity!!.runOnUiThread{
grv?.removeAllViews() grv?.removeAllViews()
}
val db = DBStorageController(requireContext()).readableDatabase val db = DBStorageController(requireContext()).readableDatabase
val projection = arrayOf(BaseColumns._ID, val projection = arrayOf(
BaseColumns._ID,
AbstractProductContract.AbstractProductEntry.BARCODE, AbstractProductContract.AbstractProductEntry.BARCODE,
AbstractProductContract.AbstractProductEntry.PRODUCT_NAME, AbstractProductContract.AbstractProductEntry.PRODUCT_NAME,
AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT, AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT,
@ -103,10 +112,13 @@ class StorageFragment : Fragment() {
requireContext(), requireContext(),
product product
) )
activity!!.runOnUiThread{
grv?.addView(abstractProduct) grv?.addView(abstractProduct)
} }
} }
} }
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)

View File

@ -68,7 +68,6 @@ class ProductView: LinearLayout {
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
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)