mirror of
https://github.com/SimpleMobileTools/Simple-Launcher.git
synced 2025-02-27 08:47:38 +01:00
avoid refreshing Widgets fragment if nothing changed
This commit is contained in:
parent
4e295deb3e
commit
0e9d77541d
@ -22,7 +22,7 @@ import kotlinx.android.synthetic.main.item_widget_preview.view.*
|
||||
|
||||
class WidgetsAdapter(
|
||||
val activity: SimpleActivity,
|
||||
val widgetListItems: ArrayList<WidgetsListItem>,
|
||||
var widgetListItems: ArrayList<WidgetsListItem>,
|
||||
val widgetsFragmentListener: WidgetsFragmentListener
|
||||
) : RecyclerView.Adapter<WidgetsAdapter.ViewHolder>() {
|
||||
|
||||
@ -113,6 +113,15 @@ class WidgetsAdapter(
|
||||
}
|
||||
}
|
||||
|
||||
fun updateItems(newItems: ArrayList<WidgetsListItem>) {
|
||||
val oldSum = widgetListItems.sumOf { it.getHashToCompare() }
|
||||
val newSum = newItems.sumOf { it.getHashToCompare() }
|
||||
if (oldSum != newSum) {
|
||||
widgetListItems = newItems
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
|
||||
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||||
fun bindView(widgetListItem: WidgetsListItem, callback: (itemView: View, adapterPosition: Int) -> Unit) {
|
||||
itemView.apply {
|
||||
|
@ -150,8 +150,13 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
|
||||
|
||||
private fun setupAdapter(widgetsListItems: ArrayList<WidgetsListItem>) {
|
||||
activity?.runOnUiThread {
|
||||
WidgetsAdapter(activity!!, widgetsListItems, this).apply {
|
||||
widgets_list.adapter = this
|
||||
val currAdapter = widgets_list.adapter
|
||||
if (currAdapter == null) {
|
||||
WidgetsAdapter(activity!!, widgetsListItems, this).apply {
|
||||
widgets_list.adapter = this
|
||||
}
|
||||
} else {
|
||||
(currAdapter as WidgetsAdapter).updateItems(widgetsListItems)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -215,20 +220,20 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
|
||||
}
|
||||
|
||||
override fun onWidgetLongPressed(appWidget: AppWidget) {
|
||||
val gridItem =
|
||||
HomeScreenGridItem(
|
||||
null,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
appWidget.widthCells,
|
||||
appWidget.heightCells,
|
||||
appWidget.appPackageName,
|
||||
"",
|
||||
ITEM_TYPE_WIDGET,
|
||||
appWidget.widgetPreviewImage
|
||||
)
|
||||
val gridItem = HomeScreenGridItem(
|
||||
null,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
appWidget.widthCells,
|
||||
appWidget.heightCells,
|
||||
appWidget.appPackageName,
|
||||
"",
|
||||
ITEM_TYPE_WIDGET,
|
||||
appWidget.widgetPreviewImage
|
||||
)
|
||||
|
||||
activity?.widgetLongPressedOnList(gridItem)
|
||||
ignoreTouches = true
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ data class AppLauncher(
|
||||
|
||||
fun getHashToCompare() = getStringToCompare().hashCode()
|
||||
|
||||
fun getStringToCompare(): String {
|
||||
private fun getStringToCompare(): String {
|
||||
return copy(id = null, drawable = null).toString()
|
||||
}
|
||||
|
||||
|
@ -5,10 +5,16 @@ import android.graphics.drawable.Drawable
|
||||
data class AppWidget(
|
||||
var appPackageName: String,
|
||||
var appTitle: String,
|
||||
val appIcon: Drawable,
|
||||
val appIcon: Drawable?,
|
||||
val widgetTitle: String,
|
||||
val widgetPreviewImage: Drawable?,
|
||||
var widthCells: Int,
|
||||
val heightCells: Int,
|
||||
val isShortcut: Boolean
|
||||
) : WidgetsListItem()
|
||||
) : WidgetsListItem() {
|
||||
override fun getHashToCompare() = getStringToCompare().hashCode()
|
||||
|
||||
private fun getStringToCompare(): String {
|
||||
return copy(appIcon = null, widgetPreviewImage = null).toString()
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
package com.simplemobiletools.launcher.models
|
||||
|
||||
open class WidgetsListItem
|
||||
abstract class WidgetsListItem {
|
||||
abstract fun getHashToCompare(): Int
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
package com.simplemobiletools.launcher.models
|
||||
|
||||
data class WidgetsListItemsHolder(val widgets: ArrayList<AppWidget>) : WidgetsListItem()
|
||||
open class WidgetsListItemsHolder(val widgets: ArrayList<AppWidget>) : WidgetsListItem() {
|
||||
override fun getHashToCompare() = widgets.sumOf { it.getHashToCompare() }
|
||||
}
|
||||
|
@ -2,4 +2,10 @@ package com.simplemobiletools.launcher.models
|
||||
|
||||
import android.graphics.drawable.Drawable
|
||||
|
||||
data class WidgetsListSection(var appTitle: String, var appIcon: Drawable) : WidgetsListItem()
|
||||
data class WidgetsListSection(var appTitle: String, var appIcon: Drawable?) : WidgetsListItem() {
|
||||
override fun getHashToCompare() = getStringToCompare().hashCode()
|
||||
|
||||
private fun getStringToCompare(): String {
|
||||
return copy(appIcon = null).toString()
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user