From f18d7ad1e0173e3e284ea81a4838011069be1026 Mon Sep 17 00:00:00 2001 From: leca Date: Sun, 17 Nov 2024 19:26:25 +0300 Subject: [PATCH] performance fixes --- .../fragments/CategoriesFragment.kt | 88 ++++++++++++------- .../fragments/ShelfFragment.kt | 4 +- .../fragments/StorageFragment.kt | 18 ++-- .../views/AbstractProductView.kt | 10 +-- 4 files changed, 67 insertions(+), 53 deletions(-) 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 4128ca1..d3165ab 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/CategoriesFragment.kt @@ -18,11 +18,13 @@ import org.foxarmy.barcodescannerforemployees.database.CategoryDAO import org.foxarmy.barcodescannerforemployees.database.DBStorageController import org.foxarmy.barcodescannerforemployees.getPreferences import org.foxarmy.barcodescannerforemployees.views.CategoryView +import kotlin.concurrent.thread class CategoriesFragment : Fragment() { private lateinit var sharedPreferences: SharedPreferences private lateinit var categoryDAO: CategoryDAO + private var updateInProgress = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -51,56 +53,74 @@ class CategoriesFragment : Fragment() { } fun removeSelected() { - val layout = view?.findViewById(R.id.categoriesLayout) + thread { + val layout = view?.findViewById(R.id.categoriesLayout) - 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() + 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() + 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 } - - deleted = true } - } - if (!deleted) { - Toast.makeText(requireContext(), getString(R.string.nothing_to_delete), Toast.LENGTH_SHORT).show() + if (!deleted) { + activity!!.runOnUiThread { + Toast.makeText(requireContext(), getString(R.string.nothing_to_delete), Toast.LENGTH_SHORT).show() + } + } + updateContent() } - updateContent() } fun updateSelected() { - val layout = view?.findViewById(R.id.categoriesLayout) - for (view: CategoryView in layout?.children!!.iterator() as Iterator) { - if (view.isCategorySelected) { - val addCategoryIntent = Intent(context, AddCategoryActivity::class.java) - val extras = Bundle() - extras.putParcelable("category", view.category) - addCategoryIntent.putExtras(extras) - ContextCompat.startActivity(context!!, addCategoryIntent, extras) + thread { + val layout = view?.findViewById(R.id.categoriesLayout) + for (view: CategoryView in layout?.children!!.iterator() as Iterator) { + if (view.isCategorySelected) { + val addCategoryIntent = Intent(context, AddCategoryActivity::class.java) + val extras = Bundle() + extras.putParcelable("category", view.category) + addCategoryIntent.putExtras(extras) + activity!!.runOnUiThread { + ContextCompat.startActivity(context!!, addCategoryIntent, extras) + } + } } } } fun updateContent() { - prepareDatabaseConnection() - val layout = view?.findViewById(R.id.categoriesLayout) - layout?.removeAllViews() + thread { + if (updateInProgress) return@thread + updateInProgress = true + prepareDatabaseConnection() + val layout = view?.findViewById(R.id.categoriesLayout) + activity!!.runOnUiThread { + layout?.removeAllViews() + } - val categories = categoryDAO.getAllCategories() + val categories = categoryDAO.getAllCategories() - for (category in categories) { - val categoryView = CategoryView(requireActivity(), requireContext(), category) - layout?.addView(categoryView) + for (category in categories) { + val categoryView = CategoryView(requireActivity(), requireContext(), category) + activity!!.runOnUiThread { + layout?.addView(categoryView) + } + } + + updateInProgress = false } } 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 24bc463..6d6027b 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/ShelfFragment.kt @@ -114,11 +114,12 @@ class ShelfFragment : Fragment() { } fun updateContent() { - prepareDatabaseConnection() thread { if (updateInProgress) return@thread updateInProgress = true + prepareDatabaseConnection() val grv = view?.findViewById(R.id.contentGridLayout) + activity!!.runOnUiThread { grv?.removeAllViews() } @@ -136,7 +137,6 @@ class ShelfFragment : Fragment() { } } updateInProgress = false - } } 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 ca9908d..3a96a07 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -17,6 +17,7 @@ import org.foxarmy.barcodescannerforemployees.activities.FindBarcodelessAbstract import org.foxarmy.barcodescannerforemployees.database.AbstractProductDAO import org.foxarmy.barcodescannerforemployees.database.DBStorageController import org.foxarmy.barcodescannerforemployees.databinding.FragmentStorageBinding +import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct import org.foxarmy.barcodescannerforemployees.generateThumbnailForImage import org.foxarmy.barcodescannerforemployees.getPreferences import org.foxarmy.barcodescannerforemployees.views.AbstractProductView @@ -31,6 +32,7 @@ class StorageFragment : Fragment() { private lateinit var sharedPreferences: SharedPreferences private lateinit var abstractProductDAO: AbstractProductDAO + private var updateInProcess = false override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -134,10 +136,14 @@ class StorageFragment : Fragment() { } fun updateContent() { - prepareDatabaseConnection() thread { + if (updateInProcess) return@thread + updateInProcess = true + prepareDatabaseConnection() + val grv = binding.contentGridLayout - activity!!.runOnUiThread{ + + activity!!.runOnUiThread { grv.removeAllViews() } @@ -165,14 +171,10 @@ class StorageFragment : Fragment() { grv.addView(abstractProductView) } } - }.join() + updateInProcess = false + } } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - - updateContent() - } fun filterByCategory(id: Int) { filterBy = "category" diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt index 55f632f..3f5b54f 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt @@ -14,8 +14,6 @@ import android.widget.Toast import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat.startActivity -import androidx.security.crypto.EncryptedSharedPreferences -import androidx.security.crypto.MasterKeys import org.foxarmy.barcodescannerforemployees.* import org.foxarmy.barcodescannerforemployees.activities.FullscreenActivity import org.foxarmy.barcodescannerforemployees.activities.MainActivity @@ -55,13 +53,7 @@ class AbstractProductView : LinearLayout { val inflater: LayoutInflater = activity.layoutInflater inflater.inflate(R.layout.abstract_product_view, this) - sharedPreferences = EncryptedSharedPreferences.create( - "sensitive", - MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC), - context, - EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, - EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM - ) + sharedPreferences = getPreferences(context) val dbHelper = DBStorageController(context, sharedPreferences.getString("currentGroup", "database")!!) categoryDAO = CategoryDAO(dbHelper)