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