added ability to delete abstract products

This commit is contained in:
leca 2024-10-06 01:12:47 +03:00
parent 609544c387
commit 9d52a81923
7 changed files with 87 additions and 23 deletions

View File

@ -1,5 +1,11 @@
package org.foxarmy.barcodescannerforemployees 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
) {
} }

View File

@ -4,6 +4,8 @@ import android.content.Context
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteOpenHelper
import android.provider.BaseColumns import android.provider.BaseColumns
import android.util.Log
import java.io.File
object ProductContract { object ProductContract {
object ProductEntry : BaseColumns { object ProductEntry : BaseColumns {
@ -57,8 +59,44 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
TODO("Not yet implemented") 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 { companion object {
// If you change the database schema, you must increment the database version.
const val DATABASE_VERSION = 1 const val DATABASE_VERSION = 1
const val DATABASE_NAME = "database.db" const val DATABASE_NAME = "database.db"
} }

View File

@ -2,16 +2,22 @@ package org.foxarmy.barcodescannerforemployees.activities
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.children
import androidx.gridlayout.widget.GridLayout
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController import androidx.navigation.ui.setupActionBarWithNavController
import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.databinding.ActivityStorageBinding import org.foxarmy.barcodescannerforemployees.databinding.ActivityStorageBinding
import org.foxarmy.barcodescannerforemployees.fragments.StorageFragment
import org.foxarmy.barcodescannerforemployees.views.AbstractProductView
class StorageActivity : AppCompatActivity() { class StorageActivity : AppCompatActivity() {
@ -47,6 +53,21 @@ class StorageActivity : AppCompatActivity() {
R.id.action_settings -> { R.id.action_settings -> {
true 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<GridLayout>(R.id.contentGridLayout)?.childCount
for (view: AbstractProductView in storageFragment.view?.findViewById<GridLayout>(R.id.contentGridLayout)?.children!!.iterator() as Iterator<AbstractProductView>) {
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) else -> super.onOptionsItemSelected(item)
} }
} }

View File

@ -2,7 +2,6 @@ package org.foxarmy.barcodescannerforemployees.fragments
import android.os.Bundle import android.os.Bundle
import android.provider.BaseColumns import android.provider.BaseColumns
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -18,6 +17,8 @@ import org.foxarmy.barcodescannerforemployees.views.AbstractProductView
*/ */
class StorageFragment : Fragment() { class StorageFragment : Fragment() {
public var test_value = 1
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
} }
@ -33,10 +34,10 @@ class StorageFragment : Fragment() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
refillWithContent() updateContent()
} }
public fun refillWithContent() { public fun updateContent() {
val grv = getView()?.findViewById<GridLayout>(R.id.contentGridLayout) val grv = getView()?.findViewById<GridLayout>(R.id.contentGridLayout)
grv?.removeAllViews() grv?.removeAllViews()
@ -52,26 +53,21 @@ class StorageFragment : Fragment() {
with (cursor) { with (cursor) {
while(moveToNext()) { while(moveToNext()) {
val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val productName = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NAME)) val productName = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NAME))
val netWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT)) val netWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT))
val productImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME)) val productImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME))
val product = AbstractProduct(productId, productName, netWeight, productImageHash, 1)
generateThumbnailForImage(context!!, productImageHash) generateThumbnailForImage(context!!, productImageHash)
val abstractProduct = AbstractProductView( val abstractProduct = AbstractProductView(
requireActivity(), requireActivity(),
requireContext(), requireContext(),
productImageHash, product
productName,
netWeight,
1
) )
grv?.addView(abstractProduct) grv?.addView(abstractProduct)
abstractProduct.setOnClickListener {
Log.d("QWERTYUIOP", "Clicked view")
}
// getView()?.findViewById<GridView>(R.id.contentGridLayout)?
} }
} }
} }
@ -79,6 +75,6 @@ class StorageFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
refillWithContent() updateContent()
} }
} }

View File

@ -4,7 +4,6 @@ import android.app.Activity
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.widget.ImageView import android.widget.ImageView
import android.widget.LinearLayout import android.widget.LinearLayout
@ -12,6 +11,7 @@ import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.startActivity import androidx.core.content.ContextCompat.startActivity
import org.foxarmy.barcodescannerforemployees.AbstractProduct
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity
import org.foxarmy.barcodescannerforemployees.getImageUri import org.foxarmy.barcodescannerforemployees.getImageUri
@ -24,9 +24,12 @@ class AbstractProductView: LinearLayout {
private var netWeightField: TextView private var netWeightField: TextView
private var categoryField: TextView private var categoryField: TextView
private var unitField: TextView private var unitField: TextView
public var product: AbstractProduct
var isProductSelected = false 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 val inflater:LayoutInflater = activity.layoutInflater
inflater.inflate(R.layout.abstract_product_view, this) inflater.inflate(R.layout.abstract_product_view, this)
@ -40,14 +43,13 @@ class AbstractProductView: LinearLayout {
val thumbnailsDir = File(context.cacheDir, "thumbnails") val thumbnailsDir = File(context.cacheDir, "thumbnails")
thumbnailsDir.mkdirs() thumbnailsDir.mkdirs()
val imageUri = getImageUri(activity, File(thumbnailsDir, "$productImageHash.webp")) val imageUri = getImageUri(activity, File(thumbnailsDir, "${product.imageHash}.webp"))
productPicture.setImageURI(imageUri) productPicture.setImageURI(imageUri)
productPicture.rotation = 90f productPicture.rotation = 90f
productPicture.setOnClickListener { productPicture.setOnClickListener {
Log.d("QWERTYUIOP", productImageHash)
val fullscreenIntent = Intent(activity, FullscreenActivity::class.java) val fullscreenIntent = Intent(activity, FullscreenActivity::class.java)
val extras = Bundle() val extras = Bundle()
extras.putString("imagehash", productImageHash) extras.putString("imagehash", product.imageHash)
fullscreenIntent.putExtras(extras) fullscreenIntent.putExtras(extras)
startActivity(context, fullscreenIntent, extras) startActivity(context, fullscreenIntent, extras)
} }
@ -56,8 +58,8 @@ class AbstractProductView: LinearLayout {
} }
productNameField.text = productName productNameField.text = product.name
netWeightField.text = netWeight.toString() netWeightField.text = product.netWeight.toString()
//TODO: category and units //TODO: category and units

View File

@ -6,5 +6,5 @@
android:title="@string/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" android:orderInCategory="100"
app:showAsAction="never"/> app:showAsAction="never"/>
<item android:title="Delete item..." android:id="@+id/action_delete"/> <item android:id="@+id/action_delete" android:title="@string/delete_menu"/>
</menu> </menu>

View File

@ -53,4 +53,5 @@
libero vel nunc consequat, quis tincidunt nisl eleifend. Cras bibendum enim a justo luctus vestibulum. Fusce 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. dictum libero quis erat maximus, vitae volutpat diam dignissim.
</string> </string>
<string name="delete_menu">Delete item(s)...</string>
</resources> </resources>