diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt index 4d30b3d..d389a30 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt @@ -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() } fun eraseAbstractProduct(db: SQLiteDatabase, id: Int, context: Context) { - val projection = arrayOf( + val projectionForAbstractProduct = arrayOf( AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME ) - val selection = "${BaseColumns._ID} = ?" - val selectionArgs = arrayOf(id.toString()) - val cursor = db.query( + val selectionForAbstractProduct = "${BaseColumns._ID} = ?" + val selectionArgsForAbstractProduct = arrayOf(id.toString()) + val cursorForAbstractProduct = db.query( AbstractProductContract.AbstractProductEntry.TABLE_NAME, - projection, - selection, - selectionArgs, + projectionForAbstractProduct, + selectionForAbstractProduct, + selectionArgsForAbstractProduct, null, null, null ) var imageHash: String = "" - with (cursor) { + with (cursorForAbstractProduct) { while(moveToNext()) { val productImageHash = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME)) imageHash = productImageHash @@ -165,9 +165,26 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE File(picturesDir, "$imageHash.png").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) } + fun eraseProduct(db:SQLiteDatabase, id: Int) { + db.delete(ProductContract.ProductEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null) + } + fun insertNewProduct(db: SQLiteDatabase, product: Product) { val values = ContentValues().apply { put(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID, product.abstractProductId) 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 65643a5..9260a17 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt @@ -101,8 +101,15 @@ class MainActivity : AppCompatActivity() { when (fragment::class.simpleName.toString()) { "StorageFragment" -> { - val storageFragment = fragment as StorageFragment - storageFragment.removeSelected() + 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 + storageFragment.removeSelected() + } + .setNegativeButton("No") { _: DialogInterface, _: Int -> + + }.show() } "CategoriesFragment" -> { diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt index 979bf6a..1a1562d 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt @@ -12,6 +12,7 @@ import org.foxarmy.barcodescannerforemployees.ProductContract import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.dataclasses.Product import org.foxarmy.barcodescannerforemployees.views.ProductView +import kotlin.concurrent.thread class ShelfFragment : Fragment() { override fun onCreateView( @@ -35,36 +36,44 @@ class ShelfFragment : Fragment() { } fun updateContent() { - val grv = view?.findViewById(R.id.contentGridLayout) - grv?.removeAllViews() + thread { + val grv = view?.findViewById(R.id.contentGridLayout) + activity!!.runOnUiThread { + grv?.removeAllViews() + } - val db = DBStorageController(requireContext()).readableDatabase - val projection = arrayOf( - BaseColumns._ID, - ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID, - ProductContract.ProductEntry.AMOUNT, - ProductContract.ProductEntry.DATE_OF_PRODUCTION, - ProductContract.ProductEntry.EXPIRY_DATE, - ) + val db = DBStorageController(requireContext()).readableDatabase + val projection = arrayOf( + BaseColumns._ID, + ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID, + ProductContract.ProductEntry.AMOUNT, + ProductContract.ProductEntry.DATE_OF_PRODUCTION, + 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) { - while(moveToNext()) { - val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID)) - val abstractProductId = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID)) - val amount = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.AMOUNT)) - val dateOfProduction = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.DATE_OF_PRODUCTION)) - val dateOfExpiry = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.EXPIRY_DATE)) + with(cursor) { + while (moveToNext()) { + val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID)) + val abstractProductId = + getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID)) + val amount = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.AMOUNT)) + 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( - requireActivity(), - requireContext(), - product - ) - grv?.addView(productView) + val productView = ProductView( + requireActivity(), + requireContext(), + product + ) + activity!!.runOnUiThread { + grv?.addView(productView) + } + } } } } 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 16b054d..7107c07 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -19,6 +19,7 @@ import org.foxarmy.barcodescannerforemployees.activities.AddAbstractProductActiv import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct import org.foxarmy.barcodescannerforemployees.generateThumbnailForImage import org.foxarmy.barcodescannerforemployees.views.AbstractProductView +import kotlin.concurrent.thread class StorageFragment : Fragment() { @@ -36,22 +37,28 @@ class StorageFragment : Fragment() { } fun removeSelected() { - val grv = view?.findViewById(R.id.contentGridLayout) + thread { + val grv = view?.findViewById(R.id.contentGridLayout) - val db = DBStorageController(requireContext()) - var deleted = false - for (view: AbstractProductView in grv?.children!!.iterator() as Iterator) { - view.findViewById(R.id.productPicture).setImageURI(null) - if (view.isProductSelected) { - db.eraseAbstractProduct(db.writableDatabase, view.abstractProduct.id, requireContext()) - deleted = true + val db = DBStorageController(requireContext()) + var deleted = false + for (view: AbstractProductView in grv?.children!!.iterator() as Iterator) { + activity!!.runOnUiThread { + view.findViewById(R.id.productPicture).setImageURI(null) + } + if (view.isProductSelected) { + db.eraseAbstractProduct(db.writableDatabase, view.abstractProduct.id, requireContext()) + deleted = true + } } - } - if (!deleted) { - Toast.makeText(requireContext(), "Nothing to delete", Toast.LENGTH_SHORT).show() + if (!deleted) { + activity!!.runOnUiThread { + Toast.makeText(requireContext(), "Nothing to delete", Toast.LENGTH_SHORT).show() + } + } + updateContent() } - updateContent() } fun updateSelected() { @@ -66,44 +73,49 @@ class StorageFragment : Fragment() { ContextCompat.startActivity(requireContext(), addProductIntent, extras) } } - } fun updateContent() { + thread { + val grv = view?.findViewById(R.id.contentGridLayout) + activity!!.runOnUiThread{ + grv?.removeAllViews() + } - val grv = view?.findViewById(R.id.contentGridLayout) - grv?.removeAllViews() + val db = DBStorageController(requireContext()).readableDatabase + 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 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) - 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) { - 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) - val product = AbstractProduct(productId, barcode, productName, netWeight, productImageHash, category) + generateThumbnailForImage(context!!, productImageHash) - generateThumbnailForImage(context!!, productImageHash) - - val abstractProduct = AbstractProductView( - requireActivity(), - requireContext(), - product - ) - grv?.addView(abstractProduct) + val abstractProduct = AbstractProductView( + requireActivity(), + requireContext(), + product + ) + activity!!.runOnUiThread{ + grv?.addView(abstractProduct) + } + } } } } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt index ffafb0e..bdc31c1 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt @@ -67,7 +67,6 @@ class ProductView: LinearLayout { productLifeSpan.text = "${product.dateOfProduction}-${product.dateOfExpiry}" this.background = ContextCompat.getDrawable(context, if (isProductSelected) R.drawable.outline_selected else R.drawable.outline) - findViewById(R.id.productLayout).setOnLongClickListener { isProductSelected = !isProductSelected