diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt index 9d80c7c..04c6882 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AbstractProduct.kt @@ -1,5 +1,11 @@ package org.foxarmy.barcodescannerforemployees -class AbstractProduct constructor(name:String, netWeight: Double, imageFile: String, type: Int) { +class AbstractProduct( + public var id: Int, + public var name: String, + public var netWeight: Double, + public var imageHash: String, + public var category: Int +) { } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt index d502d02..1bb1e92 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/DBStorageController.kt @@ -4,6 +4,8 @@ import android.content.Context import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import android.provider.BaseColumns +import android.util.Log +import java.io.File object ProductContract { object ProductEntry : BaseColumns { @@ -57,8 +59,44 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE TODO("Not yet implemented") } + public fun eraseAbstractProduct(db: SQLiteDatabase, id: Int, context: Context) { + val projection = arrayOf(BaseColumns._ID, + ProductContract.ProductEntry.IMAGE_FILENAME + ) + +// val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, null, , null, null, null) + val selection = "${BaseColumns._ID} = ?" + val selectionArgs = arrayOf(id.toString()) + val cursor = db.query( + ProductContract.ProductEntry.TABLE_NAME, + projection, + selection, + selectionArgs, + null, + null, + null + ) +// val cursor = db.rawQuery("SELECT image_filename FROM ${ProductContract.ProductEntry.TABLE_NAME} WHERE ${BaseColumns._ID}=$id", null) + var imageHash: String = "" + + with (cursor) { + while(moveToNext()) { + val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID)) + val productImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME)) + imageHash = productImageHash + } + } + Log.d("QWERTYUIOP", "Deleting image with hash: $imageHash and $id") + val picturesDir = File(context.filesDir, "pictures") + val thumbnailsDir = File(context.cacheDir, "thumbnails") + + File(picturesDir, "$imageHash.png").delete() + File(thumbnailsDir, "$imageHash.webp").delete() + Log.d("QWERTYUIOP", BaseColumns._ID + "=" + id) + db.delete(ProductContract.ProductEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null) + } + companion object { - // If you change the database schema, you must increment the database version. const val DATABASE_VERSION = 1 const val DATABASE_NAME = "database.db" } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/StorageActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/StorageActivity.kt index add21f8..738fdee 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/StorageActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/StorageActivity.kt @@ -2,16 +2,22 @@ package org.foxarmy.barcodescannerforemployees.activities import android.content.Intent import android.os.Bundle +import android.util.Log import android.view.Menu import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat +import androidx.core.view.children +import androidx.gridlayout.widget.GridLayout import androidx.navigation.findNavController import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.navigateUp import androidx.navigation.ui.setupActionBarWithNavController +import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.databinding.ActivityStorageBinding +import org.foxarmy.barcodescannerforemployees.fragments.StorageFragment +import org.foxarmy.barcodescannerforemployees.views.AbstractProductView class StorageActivity : AppCompatActivity() { @@ -47,6 +53,21 @@ class StorageActivity : AppCompatActivity() { R.id.action_settings -> { true } + R.id.action_delete -> { + // if there's no selected items, write a toast about it, otherwise delete those items. + val storageFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_content_storage)?.childFragmentManager!!.fragments[0] as StorageFragment + val amountOfViews = storageFragment.view?.findViewById(R.id.contentGridLayout)?.childCount + + for (view: AbstractProductView in storageFragment.view?.findViewById(R.id.contentGridLayout)?.children!!.iterator() as Iterator) { + val db = DBStorageController(this) + if (view.isProductSelected) { + db.eraseAbstractProduct(db.writableDatabase , view.product.id, this) + Log.d("QWERTYUIOP", "Removing ${view.product.id}") + } + } + storageFragment.updateContent() + true + } else -> super.onOptionsItemSelected(item) } } 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 4e28dc1..76c4d82 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -2,7 +2,6 @@ package org.foxarmy.barcodescannerforemployees.fragments import android.os.Bundle import android.provider.BaseColumns -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -18,6 +17,8 @@ import org.foxarmy.barcodescannerforemployees.views.AbstractProductView */ class StorageFragment : Fragment() { + public var test_value = 1 + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } @@ -33,10 +34,10 @@ class StorageFragment : Fragment() { override fun onResume() { super.onResume() - refillWithContent() + updateContent() } - public fun refillWithContent() { + public fun updateContent() { val grv = getView()?.findViewById(R.id.contentGridLayout) grv?.removeAllViews() @@ -52,26 +53,21 @@ class StorageFragment : Fragment() { with (cursor) { while(moveToNext()) { + val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID)) val productName = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NAME)) val netWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT)) val productImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME)) + val product = AbstractProduct(productId, productName, netWeight, productImageHash, 1) + generateThumbnailForImage(context!!, productImageHash) val abstractProduct = AbstractProductView( requireActivity(), requireContext(), - productImageHash, - productName, - netWeight, - 1 + product ) grv?.addView(abstractProduct) - - abstractProduct.setOnClickListener { - Log.d("QWERTYUIOP", "Clicked view") - } -// getView()?.findViewById(R.id.contentGridLayout)? } } } @@ -79,6 +75,6 @@ class StorageFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - refillWithContent() + updateContent() } } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt index 4885c17..33234a2 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt @@ -4,7 +4,6 @@ import android.app.Activity import android.content.Context import android.content.Intent import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.widget.ImageView import android.widget.LinearLayout @@ -12,6 +11,7 @@ import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat.startActivity +import org.foxarmy.barcodescannerforemployees.AbstractProduct import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity import org.foxarmy.barcodescannerforemployees.getImageUri @@ -24,9 +24,12 @@ class AbstractProductView: LinearLayout { private var netWeightField: TextView private var categoryField: TextView private var unitField: TextView + public var product: AbstractProduct var isProductSelected = false - constructor(activity: Activity, context: Context, productImageHash: String, productName: String, netWeight: Double, category: Int) : super(context) { + constructor(activity: Activity, context: Context, product: AbstractProduct) : super(context) { + + this.product = product val inflater:LayoutInflater = activity.layoutInflater inflater.inflate(R.layout.abstract_product_view, this) @@ -40,14 +43,13 @@ class AbstractProductView: LinearLayout { val thumbnailsDir = File(context.cacheDir, "thumbnails") thumbnailsDir.mkdirs() - val imageUri = getImageUri(activity, File(thumbnailsDir, "$productImageHash.webp")) + val imageUri = getImageUri(activity, File(thumbnailsDir, "${product.imageHash}.webp")) productPicture.setImageURI(imageUri) productPicture.rotation = 90f productPicture.setOnClickListener { - Log.d("QWERTYUIOP", productImageHash) val fullscreenIntent = Intent(activity, FullscreenActivity::class.java) val extras = Bundle() - extras.putString("imagehash", productImageHash) + extras.putString("imagehash", product.imageHash) fullscreenIntent.putExtras(extras) startActivity(context, fullscreenIntent, extras) } @@ -56,8 +58,8 @@ class AbstractProductView: LinearLayout { } - productNameField.text = productName - netWeightField.text = netWeight.toString() + productNameField.text = product.name + netWeightField.text = product.netWeight.toString() //TODO: category and units diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index 92e986b..538a82a 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -6,5 +6,5 @@ android:title="@string/action_settings" android:orderInCategory="100" app:showAsAction="never"/> - + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5fb787a..7425902 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -53,4 +53,5 @@ libero vel nunc consequat, quis tincidunt nisl eleifend. Cras bibendum enim a justo luctus vestibulum. Fusce dictum libero quis erat maximus, vitae volutpat diam dignissim. + Delete item(s)... \ No newline at end of file