From 67911bbd592a022b0d408b6b8621b57fa163833a Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 3 Sep 2022 22:53:43 +0200 Subject: [PATCH] split widgets per apps --- .../launcher/adapters/WidgetsAdapter.kt | 53 ++++++++++++++----- .../launcher/fragments/WidgetsFragment.kt | 43 +++++++++++---- .../launcher/helpers/Constants.kt | 4 ++ .../launcher/models/AppMetadata.kt | 5 -- .../launcher/models/AppWidget.kt | 3 +- .../launcher/models/WidgetsListItem.kt | 3 ++ .../launcher/models/WidgetsListItemsHolder.kt | 3 ++ .../launcher/models/WidgetsListSection.kt | 5 ++ .../layout/item_widget_list_items_holder.xml | 8 +++ ...t_app.xml => item_widget_list_section.xml} | 0 10 files changed, 98 insertions(+), 29 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt delete mode 100644 app/src/main/kotlin/com/simplemobiletools/launcher/models/AppMetadata.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListItem.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListItemsHolder.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListSection.kt create mode 100644 app/src/main/res/layout/item_widget_list_items_holder.xml rename app/src/main/res/layout/{item_widget_app.xml => item_widget_list_section.xml} (100%) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/WidgetsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/WidgetsAdapter.kt index 59b32cf..0924e26 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/WidgetsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/WidgetsAdapter.kt @@ -7,38 +7,63 @@ import androidx.recyclerview.widget.RecyclerView import com.simplemobiletools.commons.extensions.getProperTextColor import com.simplemobiletools.launcher.R import com.simplemobiletools.launcher.activities.SimpleActivity -import com.simplemobiletools.launcher.models.AppWidget -import kotlinx.android.synthetic.main.item_widget_app.view.* +import com.simplemobiletools.launcher.helpers.WIDGET_LIST_ITEMS_HOLDER +import com.simplemobiletools.launcher.helpers.WIDGET_LIST_SECTION +import com.simplemobiletools.launcher.models.WidgetsListItem +import com.simplemobiletools.launcher.models.WidgetsListItemsHolder +import com.simplemobiletools.launcher.models.WidgetsListSection +import kotlinx.android.synthetic.main.item_widget_list_section.view.* class WidgetsAdapter( val activity: SimpleActivity, - val appWidgets: ArrayList, - val itemClick: (Any) -> Unit + val widgetListItems: ArrayList ) : RecyclerView.Adapter() { private var textColor = activity.getProperTextColor() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val view = LayoutInflater.from(parent.context).inflate(R.layout.item_widget_app, parent, false) + val layoutId = when (viewType) { + WIDGET_LIST_SECTION -> R.layout.item_widget_list_section + else -> R.layout.item_widget_list_items_holder + } + + val view = LayoutInflater.from(parent.context).inflate(layoutId, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { - holder.bindView(appWidgets[position]) + val widgetListItem = widgetListItems[position] + holder.bindView(widgetListItems[position]) { itemView, layoutPosition -> + when (widgetListItem) { + is WidgetsListSection -> setupListSection(itemView, widgetListItem) + is WidgetsListItemsHolder -> setupListItemsHolder(itemView, widgetListItem) + } + } } - override fun getItemCount() = appWidgets.size + override fun getItemCount() = widgetListItems.size + + override fun getItemViewType(position: Int) = when { + widgetListItems[position] is WidgetsListSection -> WIDGET_LIST_SECTION + else -> WIDGET_LIST_ITEMS_HOLDER + } + + private fun setupListSection(view: View, section: WidgetsListSection) { + view.apply { + widget_app_title.text = section.appTitle + widget_app_title.setTextColor(textColor) + + widget_app_icon.setImageDrawable(section.appIcon) + } + } + + private fun setupListItemsHolder(view: View, listItem: WidgetsListItem) {} inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { - fun bindView(appWidget: AppWidget): View { + fun bindView(widgetListItem: WidgetsListItem, callback: (itemView: View, adapterPosition: Int) -> Unit) { itemView.apply { - widget_app_title.text = appWidget.appTitle - widget_app_title.setTextColor(textColor) - - widget_app_icon.setImageDrawable(appWidget.appIcon) + callback(this, adapterPosition) } - - return itemView } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt index 1bfe7c3..4de5fe1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt @@ -13,8 +13,10 @@ import com.simplemobiletools.commons.helpers.isRPlus import com.simplemobiletools.launcher.R import com.simplemobiletools.launcher.activities.MainActivity import com.simplemobiletools.launcher.adapters.WidgetsAdapter -import com.simplemobiletools.launcher.models.AppMetadata import com.simplemobiletools.launcher.models.AppWidget +import com.simplemobiletools.launcher.models.WidgetsListItem +import com.simplemobiletools.launcher.models.WidgetsListItemsHolder +import com.simplemobiletools.launcher.models.WidgetsListSection import kotlinx.android.synthetic.main.widgets_fragment.view.* class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment(context, attributeSet) { @@ -29,7 +31,7 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment widgets_list.scrollToPosition(0) setupViews() - val appWidgets = (widgets_list.adapter as WidgetsAdapter).appWidgets + val appWidgets = (widgets_list.adapter as WidgetsAdapter).widgetListItems setupAdapter(appWidgets) } @@ -51,15 +53,38 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment } appWidgets = appWidgets.sortedWith(compareBy({ it.appTitle }, { it.widgetTitle })).toMutableList() as ArrayList - setupAdapter(appWidgets) + splitWidgetsByApps(appWidgets) } } - private fun setupAdapter(appWidgets: ArrayList) { - activity?.runOnUiThread { - WidgetsAdapter(activity!!, appWidgets) { + private fun splitWidgetsByApps(appWidgets: ArrayList) { + var currentAppPackageName = "" + val widgetListItems = ArrayList() + var currentAppWidgets = ArrayList() + appWidgets.forEach { appWidget -> + if (appWidget.appPackageName != currentAppPackageName) { + if (widgetListItems.isNotEmpty()) { + widgetListItems.add(WidgetsListItemsHolder(currentAppWidgets)) + currentAppWidgets = ArrayList() + } - }.apply { + widgetListItems.add(WidgetsListSection(appWidget.appTitle, appWidget.appIcon)) + } + + currentAppWidgets.add(appWidget) + currentAppPackageName = appWidget.appPackageName + } + + if (widgetListItems.isNotEmpty()) { + widgetListItems.add(WidgetsListItemsHolder(currentAppWidgets)) + } + + setupAdapter(widgetListItems) + } + + private fun setupAdapter(widgetsListItems: ArrayList) { + activity?.runOnUiThread { + WidgetsAdapter(activity!!, widgetsListItems).apply { widgets_list.adapter = this } } @@ -100,7 +125,7 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment widgets_fastscroller.setPadding(leftListPadding, 0, rightListPadding, 0) } - private fun getAppMetadataFromPackage(packageName: String): AppMetadata? { + private fun getAppMetadataFromPackage(packageName: String): WidgetsListSection? { try { val appInfo = activity!!.packageManager.getApplicationInfo(packageName, 0) val appTitle = activity!!.packageManager.getApplicationLabel(appInfo).toString() @@ -114,7 +139,7 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment } if (appTitle.isNotEmpty()) { - return AppMetadata(appTitle, appIcon) + return WidgetsListSection(appTitle, appIcon) } } catch (ignored: Exception) { } catch (error: Error) { diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt new file mode 100644 index 0000000..f4b51b3 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/helpers/Constants.kt @@ -0,0 +1,4 @@ +package com.simplemobiletools.launcher.helpers + +const val WIDGET_LIST_SECTION = 0 +const val WIDGET_LIST_ITEMS_HOLDER = 1 diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppMetadata.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppMetadata.kt deleted file mode 100644 index d3a7ee6..0000000 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppMetadata.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.simplemobiletools.launcher.models - -import android.graphics.drawable.Drawable - -data class AppMetadata(var appTitle: String, var appIcon: Drawable) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppWidget.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppWidget.kt index 2e114c4..5147571 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppWidget.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppWidget.kt @@ -2,4 +2,5 @@ package com.simplemobiletools.launcher.models import android.graphics.drawable.Drawable -data class AppWidget(var appPackageName: String, var appTitle: String, val appIcon: Drawable, val widgetTitle: String, var width: Int, val height: Int) +data class AppWidget(var appPackageName: String, var appTitle: String, val appIcon: Drawable, val widgetTitle: String, var width: Int, val height: Int) : + WidgetsListItem() diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListItem.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListItem.kt new file mode 100644 index 0000000..0ee25de --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListItem.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.launcher.models + +open class WidgetsListItem diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListItemsHolder.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListItemsHolder.kt new file mode 100644 index 0000000..aa4b4bd --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListItemsHolder.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.launcher.models + +data class WidgetsListItemsHolder(val widgets: ArrayList) : WidgetsListItem() diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListSection.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListSection.kt new file mode 100644 index 0000000..112df21 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/models/WidgetsListSection.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.launcher.models + +import android.graphics.drawable.Drawable + +data class WidgetsListSection(var appTitle: String, var appIcon: Drawable) : WidgetsListItem() diff --git a/app/src/main/res/layout/item_widget_list_items_holder.xml b/app/src/main/res/layout/item_widget_list_items_holder.xml new file mode 100644 index 0000000..a194410 --- /dev/null +++ b/app/src/main/res/layout/item_widget_list_items_holder.xml @@ -0,0 +1,8 @@ + + + + diff --git a/app/src/main/res/layout/item_widget_app.xml b/app/src/main/res/layout/item_widget_list_section.xml similarity index 100% rename from app/src/main/res/layout/item_widget_app.xml rename to app/src/main/res/layout/item_widget_list_section.xml