From 7cc04679671af4c14ffa7dc71923fcb2ef3436a4 Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 11 Nov 2024 03:05:06 +0300 Subject: [PATCH] info about deleting an item is sent to the server now --- .../foxarmy/barcodescannerforemployees/Net.kt | 57 +++++++++++++++++++ .../FindBarcodelessAbstractProduct.kt | 2 +- .../database/AbstractProductDAO.kt | 4 +- .../fragments/CategoriesFragment.kt | 12 ++++ .../fragments/ShelfFragment.kt | 12 ++++ .../fragments/StorageFragment.kt | 13 ++++- 6 files changed, 96 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt index 02069a2..6778609 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Net.kt @@ -573,4 +573,61 @@ class Net { } }.join() } + + fun deleteCategory(groupId: Int, localId: Int): Response { + lateinit var response: Response + + thread { + val client = OkHttpClient() + + val request = Request.Builder() + .url("https://$server/api/category/$groupId/$localId") + .delete() + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + + response = client.newCall(request).execute() + }.join() + + return response + } + + fun deleteAbstractProduct(groupId: Int, localId: Int): Response { + lateinit var response: Response + + thread { + val client = OkHttpClient() + + val request = Request.Builder() + .url("https://$server/api/abstractproduct/$groupId/$localId") + .delete() + .addHeader("Authorization", "Bearer $token") + .addHeader("accept-language", language) + .build() + + response = client.newCall(request).execute() + }.join() + + return response + } + + fun deleteProduct(groupId: Int, localId: Int): Response { + lateinit var response: Response + + thread { + val client = OkHttpClient() + + val request = Request.Builder() + .url("https://$server/api/product/$groupId/$localId") + .delete() + .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/FindBarcodelessAbstractProduct.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/FindBarcodelessAbstractProduct.kt index 38c199e..32ab155 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/FindBarcodelessAbstractProduct.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/FindBarcodelessAbstractProduct.kt @@ -18,7 +18,7 @@ class FindBarcodelessAbstractProduct() : AppCompatActivity() { binding = ActivityFindBarcodelessAbstractProductBinding.inflate(layoutInflater) val ft = supportFragmentManager.beginTransaction() - val fragment = StorageFragment.newInstance("barcodeless", arrayOf("")) + val fragment = StorageFragment.newInstance("barcodeless", arrayOf("", " ")) ft.replace(R.id.content, fragment) ft.commit() 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 b2c8094..9e23898 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/database/AbstractProductDAO.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/database/AbstractProductDAO.kt @@ -308,8 +308,8 @@ class AbstractProductDAO(private val dbHelper: DBStorageController) { } "barcodeless" -> { - selection = "${AbstractProductContract.AbstractProductEntry.BARCODE} = '' " - selectionArgs = null + selection = "${AbstractProductContract.AbstractProductEntry.BARCODE} = ? OR ${AbstractProductContract.AbstractProductEntry.BARCODE} = ?" + selectionArgs = filter } } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt index 687550d..afaaaef 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt @@ -13,6 +13,7 @@ import androidx.core.view.children import androidx.fragment.app.Fragment import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKeys +import org.foxarmy.barcodescannerforemployees.Net import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.activities.AddCategoryActivity import org.foxarmy.barcodescannerforemployees.database.CategoryDAO @@ -58,7 +59,18 @@ class CategoriesFragment : Fragment() { var deleted = false for (view: CategoryView in layout?.children!!.iterator() as Iterator) { if (view.isCategorySelected) { + val net = Net() + net.token = sharedPreferences.getString("token", "")!! + net.server = sharedPreferences.getString("server", "")!! + net.language = sharedPreferences.getString("language", "en-US")!! + val currentGroup = sharedPreferences.getString("currentGroup", "")!!.toInt() + categoryDAO.eraseCategory(view.category.id, requireContext()) + val response = net.deleteCategory(currentGroup, view.category.id) + activity!!.runOnUiThread{ + Toast.makeText(context, response.body!!.string(), Toast.LENGTH_SHORT).show() + } + deleted = true } } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt index 5aab4fc..703bcf6 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt @@ -16,6 +16,7 @@ import androidx.fragment.app.Fragment import androidx.gridlayout.widget.GridLayout import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKeys +import org.foxarmy.barcodescannerforemployees.Net import org.foxarmy.barcodescannerforemployees.database.DBStorageController import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.activities.AddProductActivity @@ -151,7 +152,18 @@ class ShelfFragment : Fragment() { view.findViewById(R.id.productPicture).setImageURI(null) } if (view.isProductSelected) { + val net = Net() + net.token = sharedPreferences.getString("token", "")!! + net.server = sharedPreferences.getString("server", "")!! + net.language = sharedPreferences.getString("language", "en-US")!! + val currentGroup = sharedPreferences.getString("currentGroup", "")!!.toInt() + productDAO.eraseProduct(view.product.id) + val response = net.deleteProduct(currentGroup, view.product.id) + activity!!.runOnUiThread{ + Toast.makeText(context, response.body!!.string(), Toast.LENGTH_SHORT).show() + } + deleted = true } } 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 cdae2ec..31d86d5 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -12,6 +12,7 @@ import androidx.core.view.children import androidx.fragment.app.Fragment import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKeys +import org.foxarmy.barcodescannerforemployees.Net import org.foxarmy.barcodescannerforemployees.database.DBStorageController import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.activities.AddAbstractProductActivity @@ -95,7 +96,18 @@ class StorageFragment : Fragment() { view.findViewById(R.id.productPicture).setImageURI(null) } if (view.isProductSelected) { + val net = Net() + net.token = sharedPreferences.getString("token", "")!! + net.server = sharedPreferences.getString("server", "")!! + net.language = sharedPreferences.getString("language", "en-US")!! + val currentGroup = sharedPreferences.getString("currentGroup", "")!!.toInt() + abstractProductDAO.eraseAbstractProduct(view.abstractProduct.id, requireContext()) + val response = net.deleteAbstractProduct(currentGroup, view.abstractProduct.id) + activity!!.runOnUiThread{ + Toast.makeText(context, response.body!!.string(), Toast.LENGTH_SHORT).show() + } + deleted = true } } @@ -148,7 +160,6 @@ class StorageFragment : Fragment() { } abstractProductView.findViewById(R.id.productNameView).setOnClickListener { (activity as FindBarcodelessAbstractProduct).selected(abstractProductView.abstractProduct) - } }