refactored for work with multiple databases. Didn't test yet

This commit is contained in:
leca 2024-11-05 04:13:15 +03:00
parent 4041e63a4b
commit 8adbe65aa0
13 changed files with 577 additions and 362 deletions

View File

@ -6,7 +6,11 @@ import android.database.DatabaseUtils
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.widget.Toast
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
import org.foxarmy.barcodescannerforemployees.dataclasses.Category
import org.foxarmy.barcodescannerforemployees.dataclasses.Product import org.foxarmy.barcodescannerforemployees.dataclasses.Product
import java.io.File import java.io.File
@ -62,9 +66,16 @@ const val SQL_CREATE_CATEGORIES_TABLE =
"${BaseColumns._ID} INTEGER PRIMARY KEY," + "${BaseColumns._ID} INTEGER PRIMARY KEY," +
"${CategoriesContract.CategoryEntry.CATEGORY_NAME} TEXT)" "${CategoriesContract.CategoryEntry.CATEGORY_NAME} TEXT)"
class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { class DBStorageController(context: Context, dbname: String) :
SQLiteOpenHelper(context, dbname, null, DATABASE_VERSION) {
private lateinit var dbname: String
private lateinit var db: SQLiteDatabase
override fun onCreate(db: SQLiteDatabase) { override fun onCreate(db: SQLiteDatabase) {
this.db = db
DATABASE_NAME = "$dbname.db"
db.execSQL(SQL_CREATE_ABSTRACT_PRODUCTS_TABLE) db.execSQL(SQL_CREATE_ABSTRACT_PRODUCTS_TABLE)
db.execSQL(SQL_CREATE_PRODUCTS_TABLE) db.execSQL(SQL_CREATE_PRODUCTS_TABLE)
db.execSQL(SQL_CREATE_CATEGORIES_TABLE) db.execSQL(SQL_CREATE_CATEGORIES_TABLE)
@ -74,24 +85,32 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
TODO("Not yet implemented") TODO("Not yet implemented")
} }
fun eraseCategory (db: SQLiteDatabase, id: Int, context: Context) { fun eraseCategory(id: Int, context: Context) {
val productsInCategory = getAllAbstractProductInCategory(db, id) val productsInCategory = getAllAbstractProductInCategory(id)
for (product in productsInCategory.iterator()) { for (product in productsInCategory.iterator()) {
eraseAbstractProduct(db, product.id, context) eraseAbstractProduct(product.id, context)
} }
db.delete(CategoriesContract.CategoryEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null) db.delete(CategoriesContract.CategoryEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null)
} }
fun getCategoryNameById(db: SQLiteDatabase, id: Int) : String { fun getCategoryNameById(id: Int): String {
var result = "" var result = ""
val projection = arrayOf(CategoriesContract.CategoryEntry.CATEGORY_NAME) val projection = arrayOf(CategoriesContract.CategoryEntry.CATEGORY_NAME)
val selection = "${BaseColumns._ID} = ?" val selection = "${BaseColumns._ID} = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(id.toString())
val cursor = db.query(CategoriesContract.CategoryEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null) val cursor = db.query(
CategoriesContract.CategoryEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
null
)
with(cursor) { with(cursor) {
while (moveToNext()) { while (moveToNext()) {
@ -102,7 +121,7 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
return result return result
} }
fun getAllAbstractProductInCategory(db: SQLiteDatabase, id: Int) : List<AbstractProduct> { fun getAllAbstractProductInCategory(id: Int): List<AbstractProduct> {
var result = mutableListOf<AbstractProduct>() var result = mutableListOf<AbstractProduct>()
val projection = arrayOf( val projection = arrayOf(
@ -117,18 +136,39 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
val selection = "${AbstractProductContract.AbstractProductEntry.CATEGORY} = ?" val selection = "${AbstractProductContract.AbstractProductEntry.CATEGORY} = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(id.toString())
val cursor = db.query(AbstractProductContract.AbstractProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null) val cursor = db.query(
AbstractProductContract.AbstractProductEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
null
)
with(cursor) { with(cursor) {
while (moveToNext()) { while (moveToNext()) {
val abstractProductId = getInt(getColumnIndexOrThrow(BaseColumns._ID)) val abstractProductId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val abstractProductBarcode = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.BARCODE)) val abstractProductBarcode =
val abstractProductName = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME)) getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.BARCODE))
val abstractProductNetWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT)) val abstractProductName =
val abstractProductImageHash = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME)) getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME))
val abstractProductUnit = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT)) val abstractProductNetWeight =
getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT))
val abstractProductImageHash =
getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME))
val abstractProductUnit =
getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT))
val abstractProduct = AbstractProduct(abstractProductId, abstractProductBarcode, abstractProductName, abstractProductNetWeight, abstractProductImageHash, category = id, abstractProductUnit) val abstractProduct = AbstractProduct(
abstractProductId,
abstractProductBarcode,
abstractProductName,
abstractProductNetWeight,
abstractProductImageHash,
category = id,
abstractProductUnit
)
result.add(abstractProduct) result.add(abstractProduct)
} }
@ -137,10 +177,15 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
return result return result
} }
fun getAmountOfAbstractProductsInCategory(db:SQLiteDatabase, id: Int) : Int { fun getAmountOfAbstractProductsInCategory(id: Int): Int {
return DatabaseUtils.longForQuery(db, "SELECT COUNT(*) FROM ${AbstractProductContract.AbstractProductEntry.TABLE_NAME} WHERE ${AbstractProductContract.AbstractProductEntry.CATEGORY} = ?", arrayOf(id.toString())).toInt() return DatabaseUtils.longForQuery(
db,
"SELECT COUNT(*) FROM ${AbstractProductContract.AbstractProductEntry.TABLE_NAME} WHERE ${AbstractProductContract.AbstractProductEntry.CATEGORY} = ?",
arrayOf(id.toString())
).toInt()
} }
fun eraseAbstractProduct(db: SQLiteDatabase, id: Int, context: Context) {
fun eraseAbstractProduct(id: Int, context: Context) {
val projectionForAbstractProduct = arrayOf( val projectionForAbstractProduct = arrayOf(
AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME
) )
@ -160,7 +205,8 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
with(cursorForAbstractProduct) { with(cursorForAbstractProduct) {
while (moveToNext()) { while (moveToNext()) {
val productImageHash = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME)) val productImageHash =
getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME))
imageHash = productImageHash imageHash = productImageHash
} }
} }
@ -174,22 +220,30 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
val selectionForProducts = "${ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID} = ?" val selectionForProducts = "${ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID} = ?"
val selectionArgsForProducts = arrayOf(id.toString()) val selectionArgsForProducts = arrayOf(id.toString())
val cursorForProducts = db.query(ProductContract.ProductEntry.TABLE_NAME, projectionForProducts, selectionForProducts, selectionArgsForProducts, null, null, null) val cursorForProducts = db.query(
ProductContract.ProductEntry.TABLE_NAME,
projectionForProducts,
selectionForProducts,
selectionArgsForProducts,
null,
null,
null
)
with(cursorForProducts) { with(cursorForProducts) {
while (moveToNext()) { while (moveToNext()) {
eraseProduct(db, getInt(getColumnIndexOrThrow(BaseColumns._ID))) eraseProduct(getInt(getColumnIndexOrThrow(BaseColumns._ID)))
} }
} }
db.delete(AbstractProductContract.AbstractProductEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null) db.delete(AbstractProductContract.AbstractProductEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null)
} }
fun eraseProduct(db:SQLiteDatabase, id: Int) { fun eraseProduct(id: Int) {
db.delete(ProductContract.ProductEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null) db.delete(ProductContract.ProductEntry.TABLE_NAME, BaseColumns._ID + "=" + id, null)
} }
fun insertNewProduct(db: SQLiteDatabase, product: Product) { fun insertNewProduct(product: Product) {
val values = ContentValues().apply { val values = ContentValues().apply {
put(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID, product.abstractProductId) put(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID, product.abstractProductId)
put(ProductContract.ProductEntry.AMOUNT, product.amount) put(ProductContract.ProductEntry.AMOUNT, product.amount)
@ -200,7 +254,7 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
db.insert(ProductContract.ProductEntry.TABLE_NAME, null, values) db.insert(ProductContract.ProductEntry.TABLE_NAME, null, values)
} }
fun findAmountOfProductsWithExpiryDate(db: SQLiteDatabase, date: Long): Int { fun findAmountOfProductsWithExpiryDate(date: Long): Int {
var amount = 0 var amount = 0
val projection = arrayOf( val projection = arrayOf(
@ -212,7 +266,8 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
val selection = "${ProductContract.ProductEntry.EXPIRY_DATE} = ?" val selection = "${ProductContract.ProductEntry.EXPIRY_DATE} = ?"
val selectionArgs = arrayOf(date.toString()) val selectionArgs = arrayOf(date.toString())
val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null) val cursor =
db.query(ProductContract.ProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null)
with(cursor) { with(cursor) {
while (moveToNext()) { while (moveToNext()) {
@ -223,7 +278,7 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
return amount return amount
} }
fun findAllExpiryDates(db:SQLiteDatabase): Set<Long> { fun findAllExpiryDates(): Set<Long> {
val dates: MutableSet<Long> = mutableSetOf<Long>() val dates: MutableSet<Long> = mutableSetOf<Long>()
val projection = arrayOf( val projection = arrayOf(
@ -243,7 +298,7 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
return dates return dates
} }
fun findAbstractProductByBarcode (db: SQLiteDatabase, barcode: String) : AbstractProduct? { fun findAbstractProductByBarcode(barcode: String): AbstractProduct? {
var abstractProduct: AbstractProduct? = null var abstractProduct: AbstractProduct? = null
val projection = arrayOf( val projection = arrayOf(
BaseColumns._ID, BaseColumns._ID,
@ -257,14 +312,25 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
val selection = "${AbstractProductContract.AbstractProductEntry.BARCODE} = ?" val selection = "${AbstractProductContract.AbstractProductEntry.BARCODE} = ?"
val selectionArgs = arrayOf(barcode) val selectionArgs = arrayOf(barcode)
val cursor = db.query(AbstractProductContract.AbstractProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null) val cursor = db.query(
AbstractProductContract.AbstractProductEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
null
)
with(cursor) { with(cursor) {
while (moveToNext()) { while (moveToNext()) {
val id = getInt(getColumnIndexOrThrow(BaseColumns._ID)) val id = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val productName = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME)) val productName =
val imageFilename = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME)) getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME))
val netWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT)) val imageFilename =
getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME))
val netWeight =
getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT))
val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY)) val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY))
val unit = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT)) val unit = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT))
@ -275,7 +341,7 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
return abstractProduct return abstractProduct
} }
fun findAbstractProductById(db: SQLiteDatabase, id: Int): AbstractProduct? { fun findAbstractProductById(id: Int): AbstractProduct? {
var abstractProduct: AbstractProduct? = null var abstractProduct: AbstractProduct? = null
val projection = arrayOf( val projection = arrayOf(
BaseColumns._ID, BaseColumns._ID,
@ -290,15 +356,25 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
val selection = "${BaseColumns._ID} = ?" val selection = "${BaseColumns._ID} = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(id.toString())
val cursor = db.query(AbstractProductContract.AbstractProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null) val cursor = db.query(
AbstractProductContract.AbstractProductEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
null
)
with(cursor) { with(cursor) {
while (moveToNext()) { while (moveToNext()) {
val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID)) val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val barcode = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.BARCODE)) val barcode = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.BARCODE))
val name = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME)) val name = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME))
val netWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT)) val netWeight =
val imageHash = getString((getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME))) getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT))
val imageHash =
getString((getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME)))
val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY)) val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY))
val unit = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT)) val unit = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT))
@ -309,18 +385,246 @@ class DBStorageController(context: Context) : SQLiteOpenHelper(context, DATABASE
return abstractProduct return abstractProduct
} }
fun updateProduct(db: SQLiteDatabase, product: Product) { fun updateProduct(product: Product) {
val values = ContentValues().apply { val values = ContentValues().apply {
put(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID, product.abstractProductId) put(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID, product.abstractProductId)
put(ProductContract.ProductEntry.AMOUNT, product.amount) put(ProductContract.ProductEntry.AMOUNT, product.amount)
put(ProductContract.ProductEntry.DATE_OF_PRODUCTION, product.dateOfProduction) put(ProductContract.ProductEntry.DATE_OF_PRODUCTION, product.dateOfProduction)
put(ProductContract.ProductEntry.EXPIRY_DATE, product.dateOfExpiry) put(ProductContract.ProductEntry.EXPIRY_DATE, product.dateOfExpiry)
} }
db.update(ProductContract.ProductEntry.TABLE_NAME, values, "${BaseColumns._ID} = ?", arrayOf(product.id.toString())) db.update(
ProductContract.ProductEntry.TABLE_NAME,
values,
"${BaseColumns._ID} = ?",
arrayOf(product.id.toString())
)
}
fun addAbstractProduct(abstractProduct: AbstractProduct, context: Context) {
val values = ContentValues().apply {
put(AbstractProductContract.AbstractProductEntry.BARCODE, abstractProduct.barcode)
put(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME, abstractProduct.name)
put(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT, abstractProduct.netWeight.toString())
put(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME, abstractProduct.imageHash)
put(AbstractProductContract.AbstractProductEntry.CATEGORY, abstractProduct.category)
put(AbstractProductContract.AbstractProductEntry.UNIT, abstractProduct.unit)
}
val id = db.insert(AbstractProductContract.AbstractProductEntry.TABLE_NAME, null, values)
val n = Net()
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
masterKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
n.language = sharedPreferences.getString("language", "en-US")!!
n.server = sharedPreferences.getString("server", "")!!
n.token = sharedPreferences.getString("token", "")!!
val pictureFile = File(File(context.filesDir, "pictures"), "${abstractProduct.imageHash}.png")
val response = n.uploadAbstractProduct(1, abstractProduct, pictureFile);
Toast.makeText(context, response.body!!.string(), Toast.LENGTH_LONG).show()
}
fun updateAbstractProduct(abstractProduct: AbstractProduct, context: Context) {
val values = ContentValues().apply {
put(AbstractProductContract.AbstractProductEntry.BARCODE, abstractProduct.barcode)
put(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME, abstractProduct.name)
put(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT, abstractProduct.netWeight.toString())
put(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME, abstractProduct.imageHash)
put(AbstractProductContract.AbstractProductEntry.CATEGORY, abstractProduct.category)
put(AbstractProductContract.AbstractProductEntry.UNIT, abstractProduct.unit)
}
db.update(
AbstractProductContract.AbstractProductEntry.TABLE_NAME,
values,
"${BaseColumns._ID} = ?",
arrayOf(abstractProduct.id.toString())
)
}
fun getAllCategories(): List<Category> {
val categories = mutableListOf<Category>()
val projection = arrayOf(
CategoriesContract.CategoryEntry.CATEGORY_NAME
)
val cursor = db.query(
CategoriesContract.CategoryEntry.TABLE_NAME,
projection,
null,
null,
null,
null,
BaseColumns._ID + " ASC"
)
with(cursor) {
while (moveToNext()) {
val category = Category(
getInt(getColumnIndexOrThrow(BaseColumns._ID)),
getString(getColumnIndexOrThrow(CategoriesContract.CategoryEntry.CATEGORY_NAME))
)
categories.add(category)
}
}
return categories
}
fun addCategory(category: Category) {
val values = ContentValues().apply {
put(CategoriesContract.CategoryEntry.CATEGORY_NAME, category.name)
}
db.insert(CategoriesContract.CategoryEntry.TABLE_NAME, null, values)
}
fun updateCategory(category: Category) {
val values = ContentValues().apply {
put(CategoriesContract.CategoryEntry.CATEGORY_NAME, category.name)
}
db.update(CategoriesContract.CategoryEntry.TABLE_NAME, values, "${BaseColumns._ID} = ?", arrayOf(category.id.toString()))
}
fun getSortedListOfProducts(selectedSort: Int, filterBy: String, filter: String): List<Product> {
val products = mutableListOf<Product>()
val projection = arrayOf(
BaseColumns._ID,
ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID,
ProductContract.ProductEntry.AMOUNT,
ProductContract.ProductEntry.DATE_OF_PRODUCTION,
ProductContract.ProductEntry.EXPIRY_DATE,
)
var orderBy: String = ""
when (selectedSort) {
0 -> {
orderBy =
"(SELECT ${AbstractProductContract.AbstractProductEntry.PRODUCT_NAME} FROM ${AbstractProductContract.AbstractProductEntry.TABLE_NAME} WHERE ${BaseColumns._ID} = ${ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID}) ASC"
}
1 -> {
orderBy =
"(SELECT ${AbstractProductContract.AbstractProductEntry.CATEGORY} FROM ${AbstractProductContract.AbstractProductEntry.TABLE_NAME} WHERE ${BaseColumns._ID} = ${ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID}) ASC"
}
3 -> {
orderBy = "${ProductContract.ProductEntry.DATE_OF_PRODUCTION} ASC"
}
4 -> {
orderBy = "${ProductContract.ProductEntry.EXPIRY_DATE} ASC"
}
}
var selection: String? = null
var selectionArgs: Array<String>? = null
when (filterBy) {
"expiryDate" -> {
selection = "${ProductContract.ProductEntry.EXPIRY_DATE} = ?"
selectionArgs = arrayOf(filter)
}
"" -> {}
}
val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, orderBy)
with(cursor) {
while (moveToNext()) {
val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val abstractProductId =
getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID))
val amount = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.AMOUNT))
val dateOfProduction =
getLong(getColumnIndexOrThrow(ProductContract.ProductEntry.DATE_OF_PRODUCTION))
val dateOfExpiry = getLong(getColumnIndexOrThrow(ProductContract.ProductEntry.EXPIRY_DATE))
val product = Product(productId, abstractProductId, amount, dateOfProduction, dateOfExpiry)
if (selectedSort == 2) { //freshness
products.add(product)
}
}
}
products.sortWith(compareByDescending { it.freshness })
return products
}
fun getSortedListOfAbstractProducts(selectedSort: Int, filterBy: String, filter: Array<String>): List<AbstractProduct> {
val abstractProducts = mutableListOf<AbstractProduct>()
val projection = arrayOf(
BaseColumns._ID,
AbstractProductContract.AbstractProductEntry.BARCODE,
AbstractProductContract.AbstractProductEntry.PRODUCT_NAME,
AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT,
AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME,
AbstractProductContract.AbstractProductEntry.CATEGORY,
AbstractProductContract.AbstractProductEntry.UNIT
)
var orderBy: String = ""
when(selectedSort) {
0 -> {
orderBy = "${AbstractProductContract.AbstractProductEntry.PRODUCT_NAME} ASC"
}
1 -> {
orderBy = "${AbstractProductContract.AbstractProductEntry.CATEGORY} ASC"
}
}
var selection = ""
var selectionArgs: Array<String>? = null
when (filterBy) {
"category" -> {
selection = "${AbstractProductContract.AbstractProductEntry.CATEGORY} = ?"
selectionArgs = filter
}
"barcodeless" -> {
selection = "${AbstractProductContract.AbstractProductEntry.BARCODE} = '' "
selectionArgs = null
}
}
val cursor = db.query(AbstractProductContract.AbstractProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, orderBy)
with (cursor) {
while(moveToNext()) {
val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val barcode = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.BARCODE))
val productName = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME))
val netWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT))
val productImageHash = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME))
val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY))
val unit = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT))
val product = AbstractProduct(productId, barcode, productName, netWeight, productImageHash, category, unit)
abstractProducts.add(product)
}
}
return abstractProducts
} }
companion object { companion object {
const val DATABASE_VERSION = 1 const val DATABASE_VERSION = 1
const val DATABASE_NAME = "database.db" var DATABASE_NAME = "database.db"
} }
} }

View File

@ -1,11 +1,10 @@
package org.foxarmy.barcodescannerforemployees.activities package org.foxarmy.barcodescannerforemployees.activities
import android.content.ContentValues
import android.content.DialogInterface import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.provider.BaseColumns
import android.util.Log import android.util.Log
import android.widget.* import android.widget.*
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
@ -49,11 +48,23 @@ class AddAbstractProductActivity : AppCompatActivity() {
private var scanningBarcode = false private var scanningBarcode = false
private lateinit var db: DBStorageController
private lateinit var sharedPreferences: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_add_abstract_product) setContentView(R.layout.fragment_add_abstract_product)
sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
db = DBStorageController(this, sharedPreferences.getString("currentGroup", "database")!!)
picturesPath = File(filesDir, "pictures") picturesPath = File(filesDir, "pictures")
val thumbnailsDir = File(cacheDir, "thumbnails") val thumbnailsDir = File(cacheDir, "thumbnails")
@ -92,6 +103,7 @@ class AddAbstractProductActivity : AppCompatActivity() {
"update" -> { "update" -> {
abstractProduct = extras.get("abstractProduct") as AbstractProduct? abstractProduct = extras.get("abstractProduct") as AbstractProduct?
} }
"new_from_barcode" -> { "new_from_barcode" -> {
abstractProduct = extras.get("abstractProduct") as AbstractProduct? abstractProduct = extras.get("abstractProduct") as AbstractProduct?
barcode = abstractProduct!!.barcode barcode = abstractProduct!!.barcode
@ -131,43 +143,10 @@ class AddAbstractProductActivity : AppCompatActivity() {
Toast.makeText(this, getString(R.string.product_net_weight_request), Toast.LENGTH_SHORT).show() Toast.makeText(this, getString(R.string.product_net_weight_request), Toast.LENGTH_SHORT).show()
} }
val db = DBStorageController(this).writableDatabase
val values = ContentValues().apply {
put(AbstractProductContract.AbstractProductEntry.BARCODE, barcode)
put(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME, productName)
put(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT, netWeight.toString())
put(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME, pictureFile.nameWithoutExtension)
put(AbstractProductContract.AbstractProductEntry.CATEGORY, categorySpinner.selectedItemPosition)
put(AbstractProductContract.AbstractProductEntry.UNIT, unitTypeSpinner.selectedItemPosition)
}
if (action == "update") { if (action == "update") {
db.update( db.updateAbstractProduct(abstractProduct!!, this)
AbstractProductContract.AbstractProductEntry.TABLE_NAME,
values,
"${BaseColumns._ID} = ?",
arrayOf(abstractProduct!!.id.toString())
)
} else if (action == "new" || action == "new_from_barcode") { } else if (action == "new" || action == "new_from_barcode") {
val id = db.insert(AbstractProductContract.AbstractProductEntry.TABLE_NAME, null, values) db.addAbstractProduct(abstractProduct!!, this)
val n = Net()
val abstractProduct = AbstractProduct(id.toInt(), barcode, productName, netWeight.toString().toDouble(), pictureFile.nameWithoutExtension, categorySpinner.selectedItemPosition, unitTypeSpinner.selectedItemPosition)
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
masterKeyAlias,
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
n.language = sharedPreferences.getString("language", "en-US")!!
n.server = sharedPreferences.getString("server", "")!!
n.token = sharedPreferences.getString("token", "")!!
val response = n.uploadAbstractProduct(1, abstractProduct, File(pictureFile.absolutePath));
Toast.makeText(this, response.body!!.string(), Toast.LENGTH_LONG).show()
} }
finish() finish()
} }
@ -190,11 +169,7 @@ class AddAbstractProductActivity : AppCompatActivity() {
var abstractProduct: AbstractProduct var abstractProduct: AbstractProduct
if (DBStorageController(this).findAbstractProductByBarcode( if (db.findAbstractProductByBarcode(this.barcode) != null) {
DBStorageController(this).readableDatabase,
this.barcode
) != null
) {
AlertDialog.Builder(this) AlertDialog.Builder(this)
.setMessage(getString(R.string.abstract_product_already_exists)) .setMessage(getString(R.string.abstract_product_already_exists))
.setPositiveButton(getString(R.string.quit)) { _: DialogInterface, _: Int -> .setPositiveButton(getString(R.string.quit)) { _: DialogInterface, _: Int ->
@ -203,10 +178,8 @@ class AddAbstractProductActivity : AppCompatActivity() {
.setNegativeButton(getString(R.string.edit_existing)) { _: DialogInterface, _: Int -> .setNegativeButton(getString(R.string.edit_existing)) { _: DialogInterface, _: Int ->
val addProductIntent = Intent(this, AddAbstractProductActivity::class.java) val addProductIntent = Intent(this, AddAbstractProductActivity::class.java)
val extras = Bundle() val extras = Bundle()
val existingAbstractProduct = DBStorageController(this).findAbstractProductByBarcode( val existingAbstractProduct = db.findAbstractProductByBarcode(this.barcode)
DBStorageController(this).readableDatabase,
this.barcode
)
extras.putParcelable("abstractProduct", existingAbstractProduct) extras.putParcelable("abstractProduct", existingAbstractProduct)
addProductIntent.putExtras(extras) addProductIntent.putExtras(extras)
ContextCompat.startActivity(this, addProductIntent, extras) ContextCompat.startActivity(this, addProductIntent, extras)
@ -243,40 +216,17 @@ class AddAbstractProductActivity : AppCompatActivity() {
getString(R.string.pieces) getString(R.string.pieces)
) )
val arrayAdapter = val arrayAdapter =
ArrayAdapter<String>(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, units) ArrayAdapter(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, units)
arrayAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item) arrayAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item)
unitTypeSpinner.adapter = arrayAdapter unitTypeSpinner.adapter = arrayAdapter
} }
fun fillupCategorySpinner() { fun fillupCategorySpinner() {
val db = DBStorageController(this).readableDatabase val categories = db.getAllCategories().map { category -> category.name }
val categories = mutableListOf("")
val projection = arrayOf(
CategoriesContract.CategoryEntry.CATEGORY_NAME
)
val cursor = db.query(
CategoriesContract.CategoryEntry.TABLE_NAME,
projection,
null,
null,
null,
null,
BaseColumns._ID + " ASC"
)
with(cursor) {
while (moveToNext()) {
categories.add(getString(getColumnIndexOrThrow(CategoriesContract.CategoryEntry.CATEGORY_NAME)))
}
}
val arrayAdapter = val arrayAdapter =
ArrayAdapter<String>(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, categories) ArrayAdapter(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, categories)
arrayAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item) arrayAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item)
categorySpinner.adapter = arrayAdapter categorySpinner.adapter = arrayAdapter
} }
@ -322,7 +272,8 @@ class AddAbstractProductActivity : AppCompatActivity() {
getPicture() getPicture()
} }
} else { } else {
Toast.makeText(this, getString(R.string.camera_permission_for_picture_request), Toast.LENGTH_LONG).show() Toast.makeText(this, getString(R.string.camera_permission_for_picture_request), Toast.LENGTH_LONG)
.show()
} }
} }
@ -339,6 +290,12 @@ class AddAbstractProductActivity : AppCompatActivity() {
} }
} }
override fun onDestroy() {
super.onDestroy()
db.close()
}
private fun prepareBarcodeScanner() { private fun prepareBarcodeScanner() {
val options = ScanOptions() val options = ScanOptions()
options.setDesiredBarcodeFormats(ScanOptions.EAN_13) options.setDesiredBarcodeFormats(ScanOptions.EAN_13)

View File

@ -1,23 +1,37 @@
package org.foxarmy.barcodescannerforemployees.activities package org.foxarmy.barcodescannerforemployees.activities
import android.app.Activity import android.app.Activity
import android.content.ContentValues import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.provider.BaseColumns
import android.widget.Button import android.widget.Button
import android.widget.EditText import android.widget.EditText
import android.widget.Toast import android.widget.Toast
import org.foxarmy.barcodescannerforemployees.CategoriesContract import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.dataclasses.Category import org.foxarmy.barcodescannerforemployees.dataclasses.Category
class AddCategoryActivity : Activity() { class AddCategoryActivity : Activity() {
private lateinit var db: DBStorageController
private lateinit var sharedPreferences: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_category) setContentView(R.layout.activity_add_category)
sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
db = DBStorageController(this, sharedPreferences.getString("currentGroup", "database")!!)
val extras = intent.extras val extras = intent.extras
val category = extras!!.get("category") as Category? val category = extras!!.get("category") as Category?
@ -26,27 +40,24 @@ class AddCategoryActivity : Activity() {
categoryNameTextEdit.setText(category!!.name) categoryNameTextEdit.setText(category!!.name)
findViewById<Button>(R.id.saveButton).setOnClickListener { findViewById<Button>(R.id.saveButton).setOnClickListener {
val db = DBStorageController(this).writableDatabase
if (categoryNameTextEdit.text.toString() == "") { if (categoryNameTextEdit.text.toString() == "") {
Toast.makeText(this, getString(R.string.category_name_required), Toast.LENGTH_SHORT).show() Toast.makeText(this, getString(R.string.category_name_required), Toast.LENGTH_SHORT).show()
return@setOnClickListener return@setOnClickListener
} }
if (category.id == 0) { // Inserting new category if (category.id == 0) { // Inserting new category
val values = ContentValues().apply { db.addCategory(Category(0, categoryNameTextEdit.text.toString()))
put(CategoriesContract.CategoryEntry.CATEGORY_NAME, categoryNameTextEdit.text.toString())
}
db.insert(CategoriesContract.CategoryEntry.TABLE_NAME, null, values)
} else { // Updating existing category } else { // Updating existing category
val values = ContentValues().apply { db.updateCategory(category)
put(CategoriesContract.CategoryEntry.CATEGORY_NAME, categoryNameTextEdit.text.toString())
}
db.update(CategoriesContract.CategoryEntry.TABLE_NAME, values, "${BaseColumns._ID} = ?", arrayOf(category.id.toString()))
} }
finish() finish()
} }
} }
override fun onDestroy() {
super.onDestroy()
db.close()
}
} }

View File

@ -2,6 +2,7 @@ package org.foxarmy.barcodescannerforemployees.activities
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
@ -12,6 +13,8 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener import androidx.core.widget.addTextChangedListener
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import com.journeyapps.barcodescanner.ScanContract import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanIntentResult import com.journeyapps.barcodescanner.ScanIntentResult
import com.journeyapps.barcodescanner.ScanOptions import com.journeyapps.barcodescanner.ScanOptions
@ -45,11 +48,24 @@ class AddProductActivity : AppCompatActivity() {
private var product: Product? = null private var product: Product? = null
private var abstractProduct: AbstractProduct? = null private var abstractProduct: AbstractProduct? = null
private lateinit var db: DBStorageController
private lateinit var sharedPreferences: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_add_product) setContentView(R.layout.fragment_add_product)
sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
db = DBStorageController(this, sharedPreferences.getString("currentGroup", "database")!!)
scanButton = findViewById(R.id.scanButton) scanButton = findViewById(R.id.scanButton)
noBarcodeButton = findViewById(R.id.noBarcodeButton) noBarcodeButton = findViewById(R.id.noBarcodeButton)
abstractProductView = findViewById(R.id.abstractProductView) abstractProductView = findViewById(R.id.abstractProductView)
@ -70,7 +86,7 @@ class AddProductActivity : AppCompatActivity() {
product = extras!!.get("product") as Product? product = extras!!.get("product") as Product?
if (product != null) { if (product != null) {
abstractProduct = DBStorageController(this).findAbstractProductById(DBStorageController(this).readableDatabase, product!!.abstractProductId) abstractProduct = db.findAbstractProductById(product!!.abstractProductId)
abstractProductView.abstractProduct = abstractProduct!! abstractProductView.abstractProduct = abstractProduct!!
expiryDateRadioButton.isSelected = true expiryDateRadioButton.isSelected = true
shelfLifeRadioButton.isSelected = false shelfLifeRadioButton.isSelected = false
@ -148,9 +164,9 @@ class AddProductActivity : AppCompatActivity() {
} }
if (updatingExistentProduct) { if (updatingExistentProduct) {
DBStorageController(this).updateProduct(DBStorageController(this).writableDatabase, product!!) db.updateProduct(product!!)
} else { } else {
DBStorageController(this).insertNewProduct(DBStorageController(this).writableDatabase, product!!) db.insertNewProduct(product!!)
} }
finish() finish()
@ -245,7 +261,7 @@ class AddProductActivity : AppCompatActivity() {
Toast.makeText(this, getString(R.string.cancelled), Toast.LENGTH_SHORT).show() Toast.makeText(this, getString(R.string.cancelled), Toast.LENGTH_SHORT).show()
} else { } else {
val scannedBarcode = result.contents val scannedBarcode = result.contents
abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, scannedBarcode) abstractProduct = db.findAbstractProductByBarcode(scannedBarcode)
displayAbstractProduct(abstractProduct, scannedBarcode) displayAbstractProduct(abstractProduct, scannedBarcode)
if (abstractProduct != null) { if (abstractProduct != null) {
@ -266,4 +282,10 @@ class AddProductActivity : AppCompatActivity() {
scanLauncher.launch(options) scanLauncher.launch(options)
} }
override fun onDestroy() {
super.onDestroy()
db.close()
}
} }

View File

@ -2,10 +2,13 @@ package org.foxarmy.barcodescannerforemployees.activities
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.widget.LinearLayout import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.databinding.ActivityExpiryCalendarBinding import org.foxarmy.barcodescannerforemployees.databinding.ActivityExpiryCalendarBinding
@ -14,11 +17,24 @@ import org.foxarmy.barcodescannerforemployees.views.ExpiryGroupView
class ExpiryCalendarActivity : AppCompatActivity() { class ExpiryCalendarActivity : AppCompatActivity() {
private lateinit var binding: ActivityExpiryCalendarBinding private lateinit var binding: ActivityExpiryCalendarBinding
private lateinit var db: DBStorageController
private lateinit var sharedPreferences: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_expiry_dates) setContentView(R.layout.fragment_expiry_dates)
sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
db = DBStorageController(this, sharedPreferences.getString("currentGroup", "database")!!)
fillUp() fillUp()
} }
@ -31,7 +47,7 @@ class ExpiryCalendarActivity : AppCompatActivity() {
} }
private fun fillUp() { private fun fillUp() {
val dates = DBStorageController(this).findAllExpiryDates(DBStorageController(this).readableDatabase) val dates = db.findAllExpiryDates()
val container = findViewById<LinearLayout>(R.id.datesLinearLayout) val container = findViewById<LinearLayout>(R.id.datesLinearLayout)

View File

@ -19,11 +19,9 @@ class GroupActivity : AppCompatActivity() {
binding = ActivityGroupBinding.inflate(layoutInflater) binding = ActivityGroupBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val sharedPreferences = EncryptedSharedPreferences.create( val sharedPreferences = EncryptedSharedPreferences.create(
"sensitive", "sensitive",
masterKeyAlias, MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
applicationContext, applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM

View File

@ -25,11 +25,9 @@ class LoginActivity : AppCompatActivity() {
fillUpLanguagesSpinner() fillUpLanguagesSpinner()
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val sharedPreferences = EncryptedSharedPreferences.create( val sharedPreferences = EncryptedSharedPreferences.create(
"sensitive", "sensitive",
masterKeyAlias, MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
applicationContext, applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM

View File

@ -56,11 +56,9 @@ class ManageGroupActivity : AppCompatActivity(){
} }
fun amIAnAdminIn(groupId: Int): Boolean { fun amIAnAdminIn(groupId: Int): Boolean {
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val sharedPreferences = EncryptedSharedPreferences.create( val sharedPreferences = EncryptedSharedPreferences.create(
"sensitive", "sensitive",
masterKeyAlias, MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
applicationContext, applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM

View File

@ -22,11 +22,9 @@ class MyGroupsActivity : AppCompatActivity(){
binding = ActivityMyGroupsBinding.inflate(layoutInflater) binding = ActivityMyGroupsBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val sharedPreferences = EncryptedSharedPreferences.create( val sharedPreferences = EncryptedSharedPreferences.create(
"sensitive", "sensitive",
masterKeyAlias, MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
applicationContext, applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM

View File

@ -2,15 +2,26 @@ package org.foxarmy.barcodescannerforemployees.activities
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.util.Log
import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys import androidx.security.crypto.MasterKeys
class NavigatorActivity : Activity() { class NavigatorActivity : Activity() {
private lateinit var sharedPreferences: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
var intent = Intent(this, MainActivity::class.java) var intent = Intent(this, MainActivity::class.java)
if (!isInGroup()) { if (!isInGroup()) {
@ -27,33 +38,10 @@ class NavigatorActivity : Activity() {
} }
private fun isAuthenticated(): Boolean { private fun isAuthenticated(): Boolean {
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) return sharedPreferences.getString("token", "") != ""
val sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
masterKeyAlias,
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
val jwt = sharedPreferences.getString("token", "")
return jwt != ""
} }
private fun isInGroup(): Boolean { private fun isInGroup(): Boolean {
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) return sharedPreferences.getStringSet("groups", emptySet())!!.isNotEmpty()
val sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
masterKeyAlias,
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
val groups = sharedPreferences.getStringSet("groups", emptySet())
Log.d("QWERTYUIOP", groups.toString())
return groups!!.isNotEmpty()
} }
} }

View File

@ -1,8 +1,8 @@
package org.foxarmy.barcodescannerforemployees.fragments package org.foxarmy.barcodescannerforemployees.fragments
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.provider.BaseColumns
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -11,14 +11,32 @@ import android.widget.Toast
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.children import androidx.core.view.children
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import org.foxarmy.barcodescannerforemployees.CategoriesContract import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.activities.AddCategoryActivity import org.foxarmy.barcodescannerforemployees.activities.AddCategoryActivity
import org.foxarmy.barcodescannerforemployees.dataclasses.Category
import org.foxarmy.barcodescannerforemployees.views.CategoryView import org.foxarmy.barcodescannerforemployees.views.CategoryView
class CategoriesFragment : Fragment() { class CategoriesFragment : Fragment() {
private lateinit var sharedPreferences: SharedPreferences
private lateinit var db: DBStorageController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
requireContext(),
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
db = DBStorageController(requireContext(), sharedPreferences.getString("currentGroup", "database")!!)
}
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
@ -35,11 +53,10 @@ class CategoriesFragment : Fragment() {
fun removeSelected() { fun removeSelected() {
val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout) val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)
val db = DBStorageController(requireContext())
var deleted = false var deleted = false
for (view: CategoryView in layout?.children!!.iterator() as Iterator<CategoryView>) { for (view: CategoryView in layout?.children!!.iterator() as Iterator<CategoryView>) {
if (view.isCategorySelected) { if (view.isCategorySelected) {
db.eraseCategory(db.writableDatabase, view.category.id, requireContext()) db.eraseCategory(view.category.id, requireContext())
deleted = true deleted = true
} }
} }
@ -67,31 +84,23 @@ class CategoriesFragment : Fragment() {
val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout) val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)
layout?.removeAllViews() layout?.removeAllViews()
val db = DBStorageController(requireContext()).readableDatabase val categories = db.getAllCategories()
val projection = arrayOf(
BaseColumns._ID,
CategoriesContract.CategoryEntry.CATEGORY_NAME
)
val cursor = db.query(CategoriesContract.CategoryEntry.TABLE_NAME, projection, null, null, null, null, null)
with (cursor) {
while(moveToNext()) {
val categoryId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val categoryName = getString(getColumnIndexOrThrow(CategoriesContract.CategoryEntry.CATEGORY_NAME))
val category = Category(categoryId, categoryName)
for (category in categories) {
val categoryView = CategoryView(requireActivity(), requireContext(), category) val categoryView = CategoryView(requireActivity(), requireContext(), category)
layout?.addView(categoryView) layout?.addView(categoryView)
} }
} }
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
updateContent() updateContent()
} }
override fun onDestroy() {
super.onDestroy()
db.close()
}
} }

View File

@ -1,9 +1,8 @@
package org.foxarmy.barcodescannerforemployees.fragments package org.foxarmy.barcodescannerforemployees.fragments
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.provider.BaseColumns
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -15,13 +14,12 @@ import androidx.core.content.ContextCompat
import androidx.core.view.children import androidx.core.view.children
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.gridlayout.widget.GridLayout import androidx.gridlayout.widget.GridLayout
import org.foxarmy.barcodescannerforemployees.AbstractProductContract import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.ProductContract
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.activities.AddProductActivity import org.foxarmy.barcodescannerforemployees.activities.AddProductActivity
import org.foxarmy.barcodescannerforemployees.databinding.FragmentShelfBinding import org.foxarmy.barcodescannerforemployees.databinding.FragmentShelfBinding
import org.foxarmy.barcodescannerforemployees.dataclasses.Product
import org.foxarmy.barcodescannerforemployees.views.ProductView import org.foxarmy.barcodescannerforemployees.views.ProductView
import kotlin.concurrent.thread import kotlin.concurrent.thread
@ -32,6 +30,23 @@ class ShelfFragment : Fragment() {
private var filterBy = "" private var filterBy = ""
private var filter = "" private var filter = ""
private lateinit var db: DBStorageController
private lateinit var sharedPreferences: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
requireContext(),
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
db = DBStorageController(requireContext(), sharedPreferences.getString("currentGroup", "database")!!)
}
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
@ -107,88 +122,7 @@ class ShelfFragment : Fragment() {
grv?.removeAllViews() grv?.removeAllViews()
} }
val db = DBStorageController(requireContext()).readableDatabase val products = db.getSortedListOfProducts(binding.spinner.selectedItemPosition, filterBy, filter)
val projection = arrayOf(
BaseColumns._ID,
ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID,
ProductContract.ProductEntry.AMOUNT,
ProductContract.ProductEntry.DATE_OF_PRODUCTION,
ProductContract.ProductEntry.EXPIRY_DATE,
)
var orderBy: String = ""
when (binding.spinner.selectedItemPosition) {
0 -> {
orderBy =
"(SELECT ${AbstractProductContract.AbstractProductEntry.PRODUCT_NAME} FROM ${AbstractProductContract.AbstractProductEntry.TABLE_NAME} WHERE ${BaseColumns._ID} = ${ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID}) ASC"
}
1 -> {
orderBy =
"(SELECT ${AbstractProductContract.AbstractProductEntry.CATEGORY} FROM ${AbstractProductContract.AbstractProductEntry.TABLE_NAME} WHERE ${BaseColumns._ID} = ${ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID}) ASC"
}
// Wow, I wrote this on first try but unfortunately SQLite can't do that :(
// I'll leave it here as a memory.
// "Freshness" -> {
// orderBy =
// "(SELECT ( (julianday(${ProductContract.ProductEntry.EXPIRY_DATE}) - julianday('now', 'localtime')) / (julianday(${ProductContract.ProductEntry.EXPIRY_DATE}) - julianday(${ProductContract.ProductEntry.DATE_OF_PRODUCTION})) )) ASC"
// }
3 -> {
orderBy = "${ProductContract.ProductEntry.DATE_OF_PRODUCTION} ASC"
}
4 -> {
orderBy = "${ProductContract.ProductEntry.EXPIRY_DATE} ASC"
}
}
var selection: String? = null
var selectionArgs: Array<String>? = null
when (filterBy) {
"expiryDate" -> {
selection = "${ProductContract.ProductEntry.EXPIRY_DATE} = ?"
selectionArgs = arrayOf(filter)
}
"" -> {}
}
val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, orderBy)
val products = mutableListOf<Product>()
with(cursor) {
while (moveToNext()) {
val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val abstractProductId =
getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.ABSTRACT_PRODUCT_ID))
val amount = getInt(getColumnIndexOrThrow(ProductContract.ProductEntry.AMOUNT))
val dateOfProduction =
getLong(getColumnIndexOrThrow(ProductContract.ProductEntry.DATE_OF_PRODUCTION))
val dateOfExpiry = getLong(getColumnIndexOrThrow(ProductContract.ProductEntry.EXPIRY_DATE))
val product = Product(productId, abstractProductId, amount, dateOfProduction, dateOfExpiry)
if (binding.spinner.selectedItemPosition == 2) { //freshness
products.add(product)
} else {
val productView = ProductView(
requireActivity(),
requireContext(),
product
)
activity!!.runOnUiThread {
grv?.addView(productView)
}
}
}
}
products.sortWith(compareByDescending { it.freshness })
for (product in products.iterator()) { for (product in products.iterator()) {
val productView = ProductView( val productView = ProductView(
@ -201,6 +135,7 @@ class ShelfFragment : Fragment() {
} }
} }
updateInProgress = false updateInProgress = false
} }
} }
@ -208,14 +143,13 @@ class ShelfFragment : Fragment() {
thread { thread {
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout) val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout)
val db = DBStorageController(requireContext())
var deleted = false var deleted = false
for (view: ProductView in grv?.children!!.iterator() as Iterator<ProductView>) { for (view: ProductView in grv?.children!!.iterator() as Iterator<ProductView>) {
activity!!.runOnUiThread { activity!!.runOnUiThread {
view.findViewById<ImageView>(R.id.productPicture).setImageURI(null) view.findViewById<ImageView>(R.id.productPicture).setImageURI(null)
} }
if (view.isProductSelected) { if (view.isProductSelected) {
db.eraseProduct(db.writableDatabase, view.product.id) db.eraseProduct(view.product.id)
deleted = true deleted = true
} }
} }
@ -229,6 +163,12 @@ class ShelfFragment : Fragment() {
} }
} }
override fun onDestroy() {
super.onDestroy()
db.close()
}
companion object { companion object {
fun newInstance(date: Long):ShelfFragment = ShelfFragment().apply { fun newInstance(date: Long):ShelfFragment = ShelfFragment().apply {

View File

@ -1,8 +1,8 @@
package org.foxarmy.barcodescannerforemployees.fragments package org.foxarmy.barcodescannerforemployees.fragments
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.provider.BaseColumns
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -10,13 +10,13 @@ import android.widget.*
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.children import androidx.core.view.children
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import org.foxarmy.barcodescannerforemployees.AbstractProductContract import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.activities.AddAbstractProductActivity import org.foxarmy.barcodescannerforemployees.activities.AddAbstractProductActivity
import org.foxarmy.barcodescannerforemployees.activities.FindBarcodelessAbstractProduct import org.foxarmy.barcodescannerforemployees.activities.FindBarcodelessAbstractProduct
import org.foxarmy.barcodescannerforemployees.databinding.FragmentStorageBinding import org.foxarmy.barcodescannerforemployees.databinding.FragmentStorageBinding
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
import org.foxarmy.barcodescannerforemployees.generateThumbnailForImage import org.foxarmy.barcodescannerforemployees.generateThumbnailForImage
import org.foxarmy.barcodescannerforemployees.views.AbstractProductView import org.foxarmy.barcodescannerforemployees.views.AbstractProductView
import kotlin.concurrent.thread import kotlin.concurrent.thread
@ -26,6 +26,24 @@ class StorageFragment : Fragment() {
private lateinit var binding: FragmentStorageBinding private lateinit var binding: FragmentStorageBinding
private var filterBy = "" private var filterBy = ""
private lateinit var filter: Array<String> private lateinit var filter: Array<String>
private lateinit var sharedPreferences: SharedPreferences
private lateinit var db: DBStorageController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedPreferences = EncryptedSharedPreferences.create(
"sensitive",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
requireContext(),
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
db = DBStorageController(requireContext(), sharedPreferences.getString("currentGroup", "database")!!)
}
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
@ -72,14 +90,13 @@ class StorageFragment : Fragment() {
thread { thread {
val grv = binding.contentGridLayout val grv = binding.contentGridLayout
val db = DBStorageController(requireContext())
var deleted = false var deleted = false
for (view: AbstractProductView in grv.children.iterator() as Iterator<AbstractProductView>) { for (view: AbstractProductView in grv.children.iterator() as Iterator<AbstractProductView>) {
activity!!.runOnUiThread { activity!!.runOnUiThread {
view.findViewById<ImageView>(R.id.productPicture).setImageURI(null) view.findViewById<ImageView>(R.id.productPicture).setImageURI(null)
} }
if (view.isProductSelected) { if (view.isProductSelected) {
db.eraseAbstractProduct(db.writableDatabase, view.abstractProduct.id, requireContext()) db.eraseAbstractProduct(view.abstractProduct.id, requireContext())
deleted = true deleted = true
} }
} }
@ -115,77 +132,29 @@ class StorageFragment : Fragment() {
grv.removeAllViews() grv.removeAllViews()
} }
val db = DBStorageController(requireContext()).readableDatabase val abstractProducts = db.getSortedListOfAbstractProducts(binding.spinner.selectedItemPosition, filterBy, filter)
val projection = arrayOf(
BaseColumns._ID,
AbstractProductContract.AbstractProductEntry.BARCODE,
AbstractProductContract.AbstractProductEntry.PRODUCT_NAME,
AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT,
AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME,
AbstractProductContract.AbstractProductEntry.CATEGORY,
AbstractProductContract.AbstractProductEntry.UNIT
)
var orderBy: String = "" for (abstractProduct in abstractProducts) {
when(binding.spinner.selectedItemPosition) { generateThumbnailForImage(context!!, abstractProduct.imageHash)
0 -> {
orderBy = "${AbstractProductContract.AbstractProductEntry.PRODUCT_NAME} ASC"
}
1 -> {
orderBy = "${AbstractProductContract.AbstractProductEntry.CATEGORY} ASC"
}
}
var selection = "" val abstractProductView = AbstractProductView(
var selectionArgs: Array<String>? = null
when (filterBy) {
"category" -> {
selection = "${AbstractProductContract.AbstractProductEntry.CATEGORY} = ?"
selectionArgs = filter
}
"barcodeless" -> {
selection = "${AbstractProductContract.AbstractProductEntry.BARCODE} = '' "
selectionArgs = null
}
}
val cursor = db.query(AbstractProductContract.AbstractProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, orderBy)
with (cursor) {
while(moveToNext()) {
val productId = getInt(getColumnIndexOrThrow(BaseColumns._ID))
val barcode = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.BARCODE))
val productName = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NAME))
val netWeight = getDouble(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.PRODUCT_NET_WEIGHT))
val productImageHash = getString(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.IMAGE_FILENAME))
val category = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.CATEGORY))
val unit = getInt(getColumnIndexOrThrow(AbstractProductContract.AbstractProductEntry.UNIT))
val product = AbstractProduct(productId, barcode, productName, netWeight, productImageHash, category, unit)
generateThumbnailForImage(context!!, productImageHash)
val abstractProduct = AbstractProductView(
requireActivity(), requireActivity(),
requireContext(), requireContext(),
product abstractProduct
) )
if (filterBy == "barcodeless") { if (filterBy == "barcodeless") {
abstractProduct.setOnClickListener { abstractProductView.setOnClickListener {
(activity as FindBarcodelessAbstractProduct).selected(abstractProduct.abstractProduct) (activity as FindBarcodelessAbstractProduct).selected(abstractProductView.abstractProduct)
} }
abstractProduct.findViewById<TextView>(R.id.productNameView).setOnClickListener { abstractProductView.findViewById<TextView>(R.id.productNameView).setOnClickListener {
(activity as FindBarcodelessAbstractProduct).selected(abstractProduct.abstractProduct) (activity as FindBarcodelessAbstractProduct).selected(abstractProductView.abstractProduct)
} }
} }
activity!!.runOnUiThread{ activity!!.runOnUiThread{
grv.addView(abstractProduct) grv.addView(abstractProductView)
}
} }
} }
}.join() }.join()
@ -202,6 +171,13 @@ class StorageFragment : Fragment() {
filter = arrayOf("$id") filter = arrayOf("$id")
updateContent() updateContent()
} }
override fun onDestroy() {
super.onDestroy()
db.close()
}
companion object { companion object {
fun newInstance(filterBy: String, filter: Array<String>):StorageFragment = StorageFragment().apply { fun newInstance(filterBy: String, filter: Array<String>):StorageFragment = StorageFragment().apply {