From 94d309c49105665743d604d5e3335619613ed002 Mon Sep 17 00:00:00 2001 From: leca Date: Wed, 6 Nov 2024 04:21:12 +0300 Subject: [PATCH] updating abstract products are now uploaded --- .../foxarmy/barcodescannerforemployees/Net.kt | 35 +++++++++++++++++-- .../activities/AddAbstractProductActivity.kt | 27 +++++++------- .../database/AbstractProductDAO.kt | 2 +- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt index 3fc7d85..b1dee2d 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt @@ -116,7 +116,7 @@ class Net { return response } - fun changeUsername( newUsername: String): Response { + fun changeUsername(newUsername: String): Response { lateinit var response: Response @@ -257,7 +257,7 @@ class Net { return responseText } - fun getGroupName(id: Int) : String { + fun getGroupName(id: Int): String { lateinit var response: Response thread { @@ -352,4 +352,35 @@ class Net { return response } + + fun updateAbstractProduct(groupId: Int, abstractProduct: AbstractProduct, imageFile: File): Response { + lateinit var response: Response + + thread { + val client = OkHttpClient() + + val body = MultipartBody.Builder() + .setType("multipart/form-data".toMediaType()) + .addFormDataPart("file", imageFile.name, imageFile.asRequestBody("image/png".toMediaTypeOrNull())) + .addFormDataPart("groupId", groupId.toString()) + .addFormDataPart("localId", abstractProduct.id.toString()) + .addFormDataPart("barcode", abstractProduct.barcode) + .addFormDataPart("name", abstractProduct.name) + .addFormDataPart("net_weight", abstractProduct.netWeight.toString()) + .addFormDataPart("category", abstractProduct.category.toString()) + .addFormDataPart("unit", abstractProduct.unit.toString()) + .build() + + val request = Request.Builder() + .url("https://$server/api/abstractproduct/update") + .post(body) + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + + response = client.newCall(request).execute() + }.join() + + return response + } } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt index bf44ae3..6030b4d 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt @@ -157,22 +157,23 @@ class AddAbstractProductActivity : AppCompatActivity() { val currentGroup = sharedPreferences.getString("currentGroup", "")!! lateinit var response: Response + + abstractProduct = AbstractProduct( + if(abstractProduct == null) 0 else abstractProduct!!.id, + barcode, + productName, + netWeight.toString().toDouble(), + pictureFile.nameWithoutExtension, + categorySpinner.selectedItemPosition, + unitTypeSpinner.selectedItemPosition + ) + val pictureFile = File(File(filesDir, "pictures"), "${abstractProduct!!.imageHash}.png") + if (action == "update") { - DAO.updateAbstractProduct(abstractProduct!!, this) + DAO.updateAbstractProduct(abstractProduct!!) + response = net.updateAbstractProduct(currentGroup.toInt(), abstractProduct!!, pictureFile) } else if (action == "new" || action == "new_from_barcode") { - - abstractProduct = AbstractProduct( - 0, - barcode, - productName, - netWeight.toString().toDouble(), - pictureFile.nameWithoutExtension, - categorySpinner.selectedItemPosition, - unitTypeSpinner.selectedItemPosition - ) - abstractProduct!!.id = DAO.addAbstractProduct(abstractProduct!!).toInt() - val pictureFile = File(File(filesDir, "pictures"), "${abstractProduct!!.imageHash}.png") response = net.uploadAbstractProduct(currentGroup.toInt(), abstractProduct!!, pictureFile); } Toast.makeText(this, response.body!!.string(), Toast.LENGTH_LONG).show() diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/database/AbstractProductDAO.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/database/AbstractProductDAO.kt index e7463b1..5d7ada1 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/database/AbstractProductDAO.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/database/AbstractProductDAO.kt @@ -244,7 +244,7 @@ class AbstractProductDAO(private val dbHelper: DBStorageController) { return id } - fun updateAbstractProduct(abstractProduct: AbstractProduct, context: Context) { + fun updateAbstractProduct(abstractProduct: AbstractProduct) { val db = dbHelper.writableDatabase