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,6 +53,7 @@ class CategoriesFragment : Fragment() {
} }
fun removeSelected() { fun removeSelected() {
thread {
val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout) val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)
var deleted = false var deleted = false
@ -73,12 +76,16 @@ class CategoriesFragment : Fragment() {
} }
if (!deleted) { if (!deleted) {
activity!!.runOnUiThread {
Toast.makeText(requireContext(), getString(R.string.nothing_to_delete), Toast.LENGTH_SHORT).show() Toast.makeText(requireContext(), getString(R.string.nothing_to_delete), Toast.LENGTH_SHORT).show()
} }
}
updateContent() updateContent()
} }
}
fun updateSelected() { fun updateSelected() {
thread {
val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout) val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)
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) {
@ -86,24 +93,37 @@ class CategoriesFragment : Fragment() {
val extras = Bundle() val extras = Bundle()
extras.putParcelable("category", view.category) extras.putParcelable("category", view.category)
addCategoryIntent.putExtras(extras) addCategoryIntent.putExtras(extras)
activity!!.runOnUiThread {
ContextCompat.startActivity(context!!, addCategoryIntent, extras) ContextCompat.startActivity(context!!, addCategoryIntent, extras)
} }
} }
} }
}
}
fun updateContent() { fun updateContent() {
thread {
if (updateInProgress) return@thread
updateInProgress = true
prepareDatabaseConnection() prepareDatabaseConnection()
val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout) val layout = view?.findViewById<LinearLayout>(R.id.categoriesLayout)
activity!!.runOnUiThread {
layout?.removeAllViews() 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)
activity!!.runOnUiThread {
layout?.addView(categoryView) layout?.addView(categoryView)
} }
} }
updateInProgress = false
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)

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,9 +136,13 @@ 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)