performance fixes

This commit is contained in:
leca 2024-11-17 19:26:25 +03:00
parent 1d7050c687
commit f18d7ad1e0
4 changed files with 67 additions and 53 deletions

View File

@ -18,11 +18,13 @@ import org.foxarmy.barcodescannerforemployees.database.CategoryDAO
import org.foxarmy.barcodescannerforemployees.database.DBStorageController import org.foxarmy.barcodescannerforemployees.database.DBStorageController
import org.foxarmy.barcodescannerforemployees.getPreferences import org.foxarmy.barcodescannerforemployees.getPreferences
import org.foxarmy.barcodescannerforemployees.views.CategoryView import org.foxarmy.barcodescannerforemployees.views.CategoryView
import kotlin.concurrent.thread
class CategoriesFragment : Fragment() { class CategoriesFragment : Fragment() {
private lateinit var sharedPreferences: SharedPreferences private lateinit var sharedPreferences: SharedPreferences
private lateinit var categoryDAO: CategoryDAO private lateinit var categoryDAO: CategoryDAO
private var updateInProgress = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -51,56 +53,74 @@ class CategoriesFragment : Fragment() {
} }
fun removeSelected() { fun removeSelected() {
val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout) thread {
val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)
var deleted = false var deleted = false
for (view: CategoryView in layout?.children!!.iterator() as Iterator<CategoryView>) { for (view: CategoryView in layout?.children!!.iterator() as Iterator<CategoryView>) {
if (view.isCategorySelected) { if (view.isCategorySelected) {
val net = Net() val net = Net()
net.token = sharedPreferences.getString("token", "")!! net.token = sharedPreferences.getString("token", "")!!
net.server = sharedPreferences.getString("server", "")!! net.server = sharedPreferences.getString("server", "")!!
net.language = sharedPreferences.getString("language", "en-US")!! net.language = sharedPreferences.getString("language", "en-US")!!
val currentGroup = sharedPreferences.getString("currentGroup", "")!!.toInt() val currentGroup = sharedPreferences.getString("currentGroup", "")!!.toInt()
categoryDAO.eraseCategory(view.category.id, requireContext()) categoryDAO.eraseCategory(view.category.id, requireContext())
val response = net.deleteCategory(currentGroup, view.category.id) val response = net.deleteCategory(currentGroup, view.category.id)
activity!!.runOnUiThread{ activity!!.runOnUiThread{
Toast.makeText(context, response.body!!.string(), Toast.LENGTH_SHORT).show() Toast.makeText(context, response.body!!.string(), Toast.LENGTH_SHORT).show()
}
deleted = true
} }
deleted = true
} }
}
if (!deleted) { if (!deleted) {
Toast.makeText(requireContext(), getString(R.string.nothing_to_delete), Toast.LENGTH_SHORT).show() activity!!.runOnUiThread {
Toast.makeText(requireContext(), getString(R.string.nothing_to_delete), Toast.LENGTH_SHORT).show()
}
}
updateContent()
} }
updateContent()
} }
fun updateSelected() { fun updateSelected() {
val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout) thread {
for (view: CategoryView in layout?.children!!.iterator() as Iterator<CategoryView>) { val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)
if (view.isCategorySelected) { for (view: CategoryView in layout?.children!!.iterator() as Iterator<CategoryView>) {
val addCategoryIntent = Intent(context, AddCategoryActivity::class.java) if (view.isCategorySelected) {
val extras = Bundle() val addCategoryIntent = Intent(context, AddCategoryActivity::class.java)
extras.putParcelable("category", view.category) val extras = Bundle()
addCategoryIntent.putExtras(extras) extras.putParcelable("category", view.category)
ContextCompat.startActivity(context!!, addCategoryIntent, extras) addCategoryIntent.putExtras(extras)
activity!!.runOnUiThread {
ContextCompat.startActivity(context!!, addCategoryIntent, extras)
}
}
} }
} }
} }
fun updateContent() { fun updateContent() {
prepareDatabaseConnection() thread {
val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout) if (updateInProgress) return@thread
layout?.removeAllViews() updateInProgress = true
prepareDatabaseConnection()
val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)
activity!!.runOnUiThread {
layout?.removeAllViews()
}
val categories = categoryDAO.getAllCategories() val categories = categoryDAO.getAllCategories()
for (category in categories) { for (category in categories) {
val categoryView = CategoryView(requireActivity(), requireContext(), category) val categoryView = CategoryView(requireActivity(), requireContext(), category)
layout?.addView(categoryView) activity!!.runOnUiThread {
layout?.addView(categoryView)
}
}
updateInProgress = false
} }
} }

View File

@ -114,11 +114,12 @@ class ShelfFragment : Fragment() {
} }
fun updateContent() { fun updateContent() {
prepareDatabaseConnection()
thread { thread {
if (updateInProgress) return@thread if (updateInProgress) return@thread
updateInProgress = true updateInProgress = true
prepareDatabaseConnection()
val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout) val grv = view?.findViewById<GridLayout>(R.id.contentGridLayout)
activity!!.runOnUiThread { activity!!.runOnUiThread {
grv?.removeAllViews() grv?.removeAllViews()
} }
@ -136,7 +137,6 @@ class ShelfFragment : Fragment() {
} }
} }
updateInProgress = false updateInProgress = false
} }
} }

View File

@ -17,6 +17,7 @@ import org.foxarmy.barcodescannerforemployees.activities.FindBarcodelessAbstract
import org.foxarmy.barcodescannerforemployees.database.AbstractProductDAO import org.foxarmy.barcodescannerforemployees.database.AbstractProductDAO
import org.foxarmy.barcodescannerforemployees.database.DBStorageController import org.foxarmy.barcodescannerforemployees.database.DBStorageController
import org.foxarmy.barcodescannerforemployees.databinding.FragmentStorageBinding import org.foxarmy.barcodescannerforemployees.databinding.FragmentStorageBinding
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
import org.foxarmy.barcodescannerforemployees.generateThumbnailForImage import org.foxarmy.barcodescannerforemployees.generateThumbnailForImage
import org.foxarmy.barcodescannerforemployees.getPreferences import org.foxarmy.barcodescannerforemployees.getPreferences
import org.foxarmy.barcodescannerforemployees.views.AbstractProductView import org.foxarmy.barcodescannerforemployees.views.AbstractProductView
@ -31,6 +32,7 @@ class StorageFragment : Fragment() {
private lateinit var sharedPreferences: SharedPreferences private lateinit var sharedPreferences: SharedPreferences
private lateinit var abstractProductDAO: AbstractProductDAO private lateinit var abstractProductDAO: AbstractProductDAO
private var updateInProcess = false
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
@ -134,10 +136,14 @@ class StorageFragment : Fragment() {
} }
fun updateContent() { fun updateContent() {
prepareDatabaseConnection()
thread { thread {
if (updateInProcess) return@thread
updateInProcess = true
prepareDatabaseConnection()
val grv = binding.contentGridLayout val grv = binding.contentGridLayout
activity!!.runOnUiThread{
activity!!.runOnUiThread {
grv.removeAllViews() grv.removeAllViews()
} }
@ -165,14 +171,10 @@ class StorageFragment : Fragment() {
grv.addView(abstractProductView) grv.addView(abstractProductView)
} }
} }
}.join() updateInProcess = false
}
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
updateContent()
}
fun filterByCategory(id: Int) { fun filterByCategory(id: Int) {
filterBy = "category" filterBy = "category"

View File

@ -14,8 +14,6 @@ import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.startActivity import androidx.core.content.ContextCompat.startActivity
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import org.foxarmy.barcodescannerforemployees.* import org.foxarmy.barcodescannerforemployees.*
import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity
import org.foxarmy.barcodescannerforemployees.activities.MainActivity import org.foxarmy.barcodescannerforemployees.activities.MainActivity
@ -55,13 +53,7 @@ class AbstractProductView : LinearLayout {
val inflater: LayoutInflater = activity.layoutInflater val inflater: LayoutInflater = activity.layoutInflater
inflater.inflate(R.layout.abstract_product_view, this) inflater.inflate(R.layout.abstract_product_view, this)
sharedPreferences = EncryptedSharedPreferences.create( sharedPreferences = getPreferences(context)
"sensitive",
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
val dbHelper = DBStorageController(context, sharedPreferences.getString("currentGroup", "database")!!) val dbHelper = DBStorageController(context, sharedPreferences.getString("currentGroup", "database")!!)
categoryDAO = CategoryDAO(dbHelper) categoryDAO = CategoryDAO(dbHelper)