From 068cd7846bed2fd608fa7515b40651cac52fb0ba Mon Sep 17 00:00:00 2001 From: leca Date: Wed, 9 Oct 2024 03:28:29 +0300 Subject: [PATCH] ability to remove a category --- .../DBStorageController.kt | 46 ++++++++++++++++++- .../views/CategoryView.kt | 14 +++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt index 6cb2eab..393960b 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt @@ -62,7 +62,51 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE TODO("Not yet implemented") } - public fun eraseAbstractProduct(db: SQLiteDatabase, id: Int, context: Context) { + fun eraseCategory (db: SQLiteDatabase, id: Int, context: Context) { + val productsInCategory = getAllAbstractProductInCategory(db, id) + + for (product in productsInCategory.iterator()) { + eraseAbstractProduct(db, product.id, context) + } + + db.delete(CategoriesContract.CategoryEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null) + } + + fun getAllAbstractProductInCategory(db: SQLiteDatabase, id: Int) : List { + + var result = mutableListOf() + val projection = arrayOf( + BaseColumns._ID, + ProductContract.ProductEntry.PRODUCT_NAME, + ProductContract.ProductEntry.IMAGE_FILENAME, + ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, + ) + + val selection = "${ProductContract.ProductEntry.CATEGORY} = ?" + val selectionArgs = arrayOf(id.toString()) + + val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null) + + with(cursor) { + while (moveToNext()) { + val abstractProductId = getInt(getColumnIndexOrThrow(BaseColumns._ID)) + val abstractProductName = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NAME)) + val abstractProductNetWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT)) + val abstractProductImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME)) + + val abstractProduct = AbstractProduct( + abstractProductId, abstractProductName, abstractProductNetWeight, abstractProductImageHash, + categoryName = "" + ) + + result.add(abstractProduct) + } + } + + return result + } + + fun eraseAbstractProduct(db: SQLiteDatabase, id: Int, context: Context) { val projection = arrayOf( ProductContract.ProductEntry.IMAGE_FILENAME ) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt index 57e2d12..09e350c 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/CategoryView.kt @@ -1,7 +1,9 @@ package org.foxarmy.barcodescannerforemployees.views import android.app.Activity +import android.app.AlertDialog import android.content.Context +import android.content.DialogInterface import android.content.Intent import android.os.Bundle import android.view.LayoutInflater @@ -10,6 +12,7 @@ import android.widget.LinearLayout import android.widget.TextView import androidx.core.content.ContextCompat import org.foxarmy.barcodescannerforemployees.Category +import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.activities.AddCategoryActivity @@ -41,7 +44,16 @@ class CategoryView : LinearLayout { } deleteButton.setOnClickListener { - TODO("implement delete button") + val builder = AlertDialog.Builder(context) + .setMessage("Deleting this category will also delete ALL the products, that belong to that category. Do you want to proceed?") + .setPositiveButton("Yes") { _: DialogInterface, _: Int -> + val db = DBStorageController(context).writableDatabase + DBStorageController(context).eraseCategory(db, category.id, context) + + } + .setNegativeButton("No") { _: DialogInterface, _: Int -> + + }.show() } } } \ No newline at end of file