fixed using network in offline mode

This commit is contained in:
leca 2024-11-14 13:32:56 +03:00
parent 2676d8083e
commit 2ebcfff51a
2 changed files with 29 additions and 16 deletions

View File

@ -33,6 +33,7 @@ import java.io.FileOutputStream
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.StandardCopyOption import java.nio.file.StandardCopyOption
import kotlin.concurrent.thread import kotlin.concurrent.thread
import kotlin.math.abs
class AddAbstractProductActivity : AppCompatActivity() { class AddAbstractProductActivity : AppCompatActivity() {
private lateinit var imageView: ImageView private lateinit var imageView: ImageView
@ -128,6 +129,9 @@ class AddAbstractProductActivity : AppCompatActivity() {
netWeightText.text = abstractProduct!!.netWeight.toString() netWeightText.text = abstractProduct!!.netWeight.toString()
categorySpinner.setSelection(abstractProduct!!.category - 1) categorySpinner.setSelection(abstractProduct!!.category - 1)
unitTypeSpinner.setSelection(abstractProduct!!.unit) unitTypeSpinner.setSelection(abstractProduct!!.unit)
if (abstractProduct!!.barcode == "" || abstractProduct!!.barcode == " ") {
noBarcodeCheckBox.isChecked = true
}
} }
saveButton.setOnClickListener { saveButton.setOnClickListener {
@ -151,19 +155,25 @@ 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()
return@setOnClickListener return@setOnClickListener
} }
val currentGroup: Int
val net = Net() val currentGroupString = sharedPreferences.getString("currentGroup", "offline")!!
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()
lateinit var response: Response 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( abstractProduct = AbstractProduct(
if(abstractProduct == null) 0 else abstractProduct!!.id, if (abstractProduct == null) 0 else abstractProduct!!.id,
if (noBarcodeCheckBox.isChecked) "" else barcode, if (noBarcodeCheckBox.isChecked) "" else barcode,
productName, productName,
netWeight.toString().toDouble(), netWeight.toString().toDouble(),
@ -171,16 +181,18 @@ class AddAbstractProductActivity : AppCompatActivity() {
categorySpinner.selectedItemPosition + 1, categorySpinner.selectedItemPosition + 1,
unitTypeSpinner.selectedItemPosition unitTypeSpinner.selectedItemPosition
) )
val pictureFile = File(File(filesDir, "pictures"), "${abstractProduct!!.imageHash}.png") val pictureFile = File(File(filesDir, "pictures"), "${abstractProduct!!.imageHash}.png")
if (action == "update") { if (action == "update") {
DAO.updateAbstractProduct(abstractProduct!!) 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") { } else if (action == "new" || action == "new_from_barcode") {
abstractProduct!!.id = DAO.addAbstractProduct(abstractProduct!!).toInt() 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() finish()
} }
@ -257,7 +269,8 @@ class AddAbstractProductActivity : AppCompatActivity() {
fun fillupCategorySpinner() { 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 } 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 orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
val matrix = Matrix() val matrix = Matrix()
when(orientation){ when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> matrix.postRotate(90F) ExifInterface.ORIENTATION_ROTATE_90 -> matrix.postRotate(90F)
ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180F) ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180F)
ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270F) ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270F)

View File

@ -37,6 +37,8 @@ class StorageFragment : Fragment() {
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
binding = FragmentStorageBinding.inflate(inflater)
sharedPreferences = EncryptedSharedPreferences.create( sharedPreferences = EncryptedSharedPreferences.create(
"sensitive", "sensitive",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC), MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
@ -47,8 +49,6 @@ class StorageFragment : Fragment() {
prepareDatabaseConnection() prepareDatabaseConnection()
binding = FragmentStorageBinding.inflate(inflater)
fillUpSortBySpinner() fillUpSortBySpinner()
binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {