ability to remove a category

This commit is contained in:
leca 2024-10-09 03:28:29 +03:00
parent fbfe68786e
commit 068cd7846b
2 changed files with 58 additions and 2 deletions

View File

@ -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<AbstractProduct> {
var result = mutableListOf<AbstractProduct>()
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
)

View File

@ -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()
}
}
}