From 2ebcfff51ad62e962aa57c2f2552dd7a53e205fa Mon Sep 17 00:00:00 2001 From: leca Date: Thu, 14 Nov 2024 13:32:56 +0300 Subject: [PATCH] fixed using network in offline mode --- .../activities/AddAbstractProductActivity.kt | 41 ++++++++++++------- .../fragments/StorageFragment.kt | 4 +- 2 files changed, 29 insertions(+), 16 deletions(-) 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 17d6ee0..fd3ee93 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt @@ -33,6 +33,7 @@ import java.io.FileOutputStream import java.nio.file.Files import java.nio.file.StandardCopyOption import kotlin.concurrent.thread +import kotlin.math.abs class AddAbstractProductActivity : AppCompatActivity() { private lateinit var imageView: ImageView @@ -128,6 +129,9 @@ class AddAbstractProductActivity : AppCompatActivity() { netWeightText.text = abstractProduct!!.netWeight.toString() categorySpinner.setSelection(abstractProduct!!.category - 1) unitTypeSpinner.setSelection(abstractProduct!!.unit) + if (abstractProduct!!.barcode == "" || abstractProduct!!.barcode == " ") { + noBarcodeCheckBox.isChecked = true + } } saveButton.setOnClickListener { @@ -151,19 +155,25 @@ class AddAbstractProductActivity : AppCompatActivity() { Toast.makeText(this, getString(R.string.product_net_weight_request), Toast.LENGTH_SHORT).show() return@setOnClickListener } - - val net = Net() - - net.token = sharedPreferences.getString("token", "")!! - net.server = sharedPreferences.getString("server", "")!! - net.language = sharedPreferences.getString("language", "en-US")!! - - val currentGroup: Int = if (sharedPreferences.getString("currentGroup", "offline")!! == "offline") 0 else sharedPreferences.getString("currentGroup", "")!!.toInt() + val currentGroup: Int + val currentGroupString = sharedPreferences.getString("currentGroup", "offline")!! lateinit var response: Response + val net = Net() + + if (currentGroupString != "offline") { + currentGroup = currentGroupString.toInt() + + net.token = sharedPreferences.getString("token", "")!! + net.server = sharedPreferences.getString("server", "")!! + net.language = sharedPreferences.getString("language", "en-US")!! + + } else { + currentGroup = 0 + } abstractProduct = AbstractProduct( - if(abstractProduct == null) 0 else abstractProduct!!.id, + if (abstractProduct == null) 0 else abstractProduct!!.id, if (noBarcodeCheckBox.isChecked) "" else barcode, productName, netWeight.toString().toDouble(), @@ -171,16 +181,18 @@ class AddAbstractProductActivity : AppCompatActivity() { categorySpinner.selectedItemPosition + 1, unitTypeSpinner.selectedItemPosition ) + val pictureFile = File(File(filesDir, "pictures"), "${abstractProduct!!.imageHash}.png") if (action == "update") { DAO.updateAbstractProduct(abstractProduct!!) - response = net.updateAbstractProduct(currentGroup, abstractProduct!!, pictureFile) + if (currentGroup > 0) response = net.updateAbstractProduct(currentGroup, abstractProduct!!, pictureFile) } else if (action == "new" || action == "new_from_barcode") { abstractProduct!!.id = DAO.addAbstractProduct(abstractProduct!!).toInt() - response = net.uploadAbstractProduct(currentGroup, abstractProduct!!, pictureFile); + if (currentGroup > 0) response = + net.uploadAbstractProduct(currentGroup, abstractProduct!!, pictureFile) } - Toast.makeText(this, response.body!!.string(), Toast.LENGTH_LONG).show() + if (currentGroup > 0) Toast.makeText(this, response.body!!.string(), Toast.LENGTH_LONG).show() finish() } @@ -257,7 +269,8 @@ class AddAbstractProductActivity : AppCompatActivity() { fun fillupCategorySpinner() { - val categoriesDAO = CategoryDAO(DBStorageController(this, sharedPreferences.getString("currentGroup", "database")!!)) + val categoriesDAO = + CategoryDAO(DBStorageController(this, sharedPreferences.getString("currentGroup", "database")!!)) val categories = categoriesDAO.getAllCategories().map { category -> category.name } @@ -280,7 +293,7 @@ class AddAbstractProductActivity : AppCompatActivity() { val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) val matrix = Matrix() - when(orientation){ + when (orientation) { ExifInterface.ORIENTATION_ROTATE_90 -> matrix.postRotate(90F) ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180F) ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270F) 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 baa2f26..071359d 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -37,6 +37,8 @@ class StorageFragment : Fragment() { inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { + binding = FragmentStorageBinding.inflate(inflater) + sharedPreferences = EncryptedSharedPreferences.create( "sensitive", MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC), @@ -47,8 +49,6 @@ class StorageFragment : Fragment() { prepareDatabaseConnection() - binding = FragmentStorageBinding.inflate(inflater) - fillUpSortBySpinner() binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {