From 231419eb7714e5ac59fd37677444d15659b8cc9f Mon Sep 17 00:00:00 2001 From: leca Date: Tue, 15 Oct 2024 13:26:04 +0300 Subject: [PATCH] ability to delete a product --- .../activities/MainActivity.kt | 4 +++ .../fragments/ShelfFragment.kt | 29 +++++++++++++++++++ .../views/ProductView.kt | 1 + app/src/main/res/drawable/outline.xml | 2 +- .../main/res/drawable/outline_selected.xml | 2 +- 5 files changed, 36 insertions(+), 2 deletions(-) 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 9260a17..ed89b8f 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt @@ -123,6 +123,10 @@ class MainActivity : AppCompatActivity() { }.show() } + "ShelfFragment" -> { + val shelfFragment = fragment as ShelfFragment + shelfFragment.removeSelected() + } } true } 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 1a1562d..5829bb1 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt @@ -5,6 +5,9 @@ import android.provider.BaseColumns import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageView +import android.widget.Toast +import androidx.core.view.children import androidx.fragment.app.Fragment import androidx.gridlayout.widget.GridLayout import org.foxarmy.barcodescannerforemployees.DBStorageController @@ -77,4 +80,30 @@ class ShelfFragment : Fragment() { } } } + + fun removeSelected() { + thread { + val grv = view?.findViewById(R.id.contentGridLayout) + + val db = DBStorageController(requireContext()) + var deleted = false + for (view: ProductView 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()) + db.eraseProduct(db.writableDatabase, view.product.id) + deleted = true + } + } + + if (!deleted) { + activity!!.runOnUiThread { + Toast.makeText(requireContext(), "Nothing to delete", Toast.LENGTH_SHORT).show() + } + } + updateContent() + } + } } \ No newline at end of file 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 7af5ee9..ffa0609 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/ProductView.kt @@ -61,6 +61,7 @@ class ProductView: LinearLayout { productLifeSpan = findViewById(R.id.dateSpan) findViewById(R.id.productLayout).setOnLongClickListener { + isProductSelected = !isProductSelected updateStroke() true } diff --git a/app/src/main/res/drawable/outline.xml b/app/src/main/res/drawable/outline.xml index b00970f..c65da40 100644 --- a/app/src/main/res/drawable/outline.xml +++ b/app/src/main/res/drawable/outline.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/src/main/res/drawable/outline_selected.xml b/app/src/main/res/drawable/outline_selected.xml index 2ffc4b5..28fafaf 100644 --- a/app/src/main/res/drawable/outline_selected.xml +++ b/app/src/main/res/drawable/outline_selected.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file