Icon list optimization

This commit is contained in:
J-Jamet
2021-03-13 14:05:44 +01:00
parent 7c0b925c96
commit faa39190fc
6 changed files with 27 additions and 22 deletions

View File

@@ -31,8 +31,10 @@ class IconCustomFragment : IconFragment<IconImageCustom>() {
return R.layout.fragment_icon_grid
}
override fun defineIconList(): List<IconImageCustom> {
return mDatabase?.getCustomIconList() ?: listOf()
override fun defineIconList() {
mDatabase?.doForEachCustomIcons { customIcon ->
iconPickerAdapter.addIcon(customIcon, false)
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

View File

@@ -51,7 +51,7 @@ abstract class IconFragment<T: IconImageDraw> : StylishFragment(),
abstract fun retrieveMainLayoutId(): Int
abstract fun defineIconList(): List<T>
abstract fun defineIconList()
override fun onAttach(context: Context) {
super.onAttach(context)
@@ -69,7 +69,8 @@ abstract class IconFragment<T: IconImageDraw> : StylishFragment(),
CoroutineScope(Dispatchers.IO).launch {
val populateList = launch {
iconPickerAdapter.setList(defineIconList())
iconPickerAdapter.clear()
defineIconList()
}
withContext(Dispatchers.Main) {
populateList.join()

View File

@@ -29,8 +29,10 @@ class IconStandardFragment : IconFragment<IconImageStandard>() {
return R.layout.fragment_icon_grid
}
override fun defineIconList(): List<IconImageStandard> {
return mDatabase?.getStandardIconList() ?: listOf()
override fun defineIconList() {
mDatabase?.doForEachStandardIcons { standardIcon ->
iconPickerAdapter.addIcon(standardIcon, false)
}
}
override fun onIconClickListener(icon: IconImageStandard) {

View File

@@ -23,12 +23,14 @@ class IconPickerAdapter<I: IconImageDraw>(val context: Context, private val tint
val lastPosition: Int
get() = iconList.lastIndex
fun addIcon(icon: I) {
fun addIcon(icon: I, notify: Boolean = true) {
if (!iconList.contains(icon)) {
iconList.add(icon)
if (notify) {
notifyItemInserted(iconList.indexOf(icon))
}
}
}
fun updateIcon(icon: I) {
val index = iconList.indexOf(icon)
@@ -74,6 +76,10 @@ class IconPickerAdapter<I: IconImageDraw>(val context: Context, private val tint
return iconList.filter { it.selected }
}
fun clear() {
iconList.clear()
}
fun setList(icons: List<I>) {
iconList.clear()
icons.forEach { iconImage ->

View File

@@ -103,8 +103,8 @@ class Database {
return mDatabaseKDB?.iconsManager ?: mDatabaseKDBX?.iconsManager ?: IconsManager()
}
fun getStandardIconList(): List<IconImageStandard> {
return iconsManager.getStandardIconList()
fun doForEachStandardIcons(action: (IconImageStandard) -> Unit) {
return iconsManager.doForEachStandardIcon(action)
}
fun getStandardIcon(iconId: Int): IconImageStandard {
@@ -114,8 +114,8 @@ class Database {
val allowCustomIcons: Boolean
get() = mDatabaseKDBX != null
fun getCustomIconList(): List<IconImageCustom> {
return iconsManager.getCustomIconList()
fun doForEachCustomIcons(action: (IconImageCustom) -> Unit) {
return iconsManager.doForEachCustomIcon(action)
}
fun getCustomIcon(iconId: UUID): IconImageCustom {

View File

@@ -39,8 +39,10 @@ class IconsManager {
return standardCache[searchIconId]
}
fun getStandardIconList(): List<IconImageStandard> {
return standardCache
fun doForEachStandardIcon(action: (IconImageStandard) -> Unit) {
standardCache.forEach { icon ->
action.invoke(icon)
}
}
/*
@@ -73,14 +75,6 @@ class IconsManager {
}
}
fun getCustomIconList(): List<IconImageCustom> {
val list = ArrayList<IconImageCustom>()
customCache.doForEachBinary { key, binary ->
list.add(IconImageCustom(key, binary))
}
return list
}
fun doForEachCustomIcon(action: (IconImageCustom) -> Unit) {
customCache.doForEachBinary { key, binary ->
action.invoke(IconImageCustom(key, binary))