split widgets per apps
This commit is contained in:
parent
92d0d8a958
commit
67911bbd59
10 changed files with 98 additions and 29 deletions
|
@ -7,38 +7,63 @@ import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.simplemobiletools.commons.extensions.getProperTextColor
|
import com.simplemobiletools.commons.extensions.getProperTextColor
|
||||||
import com.simplemobiletools.launcher.R
|
import com.simplemobiletools.launcher.R
|
||||||
import com.simplemobiletools.launcher.activities.SimpleActivity
|
import com.simplemobiletools.launcher.activities.SimpleActivity
|
||||||
import com.simplemobiletools.launcher.models.AppWidget
|
import com.simplemobiletools.launcher.helpers.WIDGET_LIST_ITEMS_HOLDER
|
||||||
import kotlinx.android.synthetic.main.item_widget_app.view.*
|
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(
|
class WidgetsAdapter(
|
||||||
val activity: SimpleActivity,
|
val activity: SimpleActivity,
|
||||||
val appWidgets: ArrayList<AppWidget>,
|
val widgetListItems: ArrayList<WidgetsListItem>
|
||||||
val itemClick: (Any) -> Unit
|
|
||||||
) : RecyclerView.Adapter<WidgetsAdapter.ViewHolder>() {
|
) : RecyclerView.Adapter<WidgetsAdapter.ViewHolder>() {
|
||||||
|
|
||||||
private var textColor = activity.getProperTextColor()
|
private var textColor = activity.getProperTextColor()
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
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)
|
return ViewHolder(view)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
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) {
|
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 {
|
itemView.apply {
|
||||||
widget_app_title.text = appWidget.appTitle
|
callback(this, adapterPosition)
|
||||||
widget_app_title.setTextColor(textColor)
|
|
||||||
|
|
||||||
widget_app_icon.setImageDrawable(appWidget.appIcon)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return itemView
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,8 +13,10 @@ import com.simplemobiletools.commons.helpers.isRPlus
|
||||||
import com.simplemobiletools.launcher.R
|
import com.simplemobiletools.launcher.R
|
||||||
import com.simplemobiletools.launcher.activities.MainActivity
|
import com.simplemobiletools.launcher.activities.MainActivity
|
||||||
import com.simplemobiletools.launcher.adapters.WidgetsAdapter
|
import com.simplemobiletools.launcher.adapters.WidgetsAdapter
|
||||||
import com.simplemobiletools.launcher.models.AppMetadata
|
|
||||||
import com.simplemobiletools.launcher.models.AppWidget
|
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.*
|
import kotlinx.android.synthetic.main.widgets_fragment.view.*
|
||||||
|
|
||||||
class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment(context, attributeSet) {
|
class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment(context, attributeSet) {
|
||||||
|
@ -29,7 +31,7 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
|
||||||
widgets_list.scrollToPosition(0)
|
widgets_list.scrollToPosition(0)
|
||||||
setupViews()
|
setupViews()
|
||||||
|
|
||||||
val appWidgets = (widgets_list.adapter as WidgetsAdapter).appWidgets
|
val appWidgets = (widgets_list.adapter as WidgetsAdapter).widgetListItems
|
||||||
setupAdapter(appWidgets)
|
setupAdapter(appWidgets)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,15 +53,38 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
|
||||||
}
|
}
|
||||||
|
|
||||||
appWidgets = appWidgets.sortedWith(compareBy({ it.appTitle }, { it.widgetTitle })).toMutableList() as ArrayList<AppWidget>
|
appWidgets = appWidgets.sortedWith(compareBy({ it.appTitle }, { it.widgetTitle })).toMutableList() as ArrayList<AppWidget>
|
||||||
setupAdapter(appWidgets)
|
splitWidgetsByApps(appWidgets)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupAdapter(appWidgets: ArrayList<AppWidget>) {
|
private fun splitWidgetsByApps(appWidgets: ArrayList<AppWidget>) {
|
||||||
activity?.runOnUiThread {
|
var currentAppPackageName = ""
|
||||||
WidgetsAdapter(activity!!, appWidgets) {
|
val widgetListItems = ArrayList<WidgetsListItem>()
|
||||||
|
var currentAppWidgets = ArrayList<AppWidget>()
|
||||||
|
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<WidgetsListItem>) {
|
||||||
|
activity?.runOnUiThread {
|
||||||
|
WidgetsAdapter(activity!!, widgetsListItems).apply {
|
||||||
widgets_list.adapter = this
|
widgets_list.adapter = this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,7 +125,7 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
|
||||||
widgets_fastscroller.setPadding(leftListPadding, 0, rightListPadding, 0)
|
widgets_fastscroller.setPadding(leftListPadding, 0, rightListPadding, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getAppMetadataFromPackage(packageName: String): AppMetadata? {
|
private fun getAppMetadataFromPackage(packageName: String): WidgetsListSection? {
|
||||||
try {
|
try {
|
||||||
val appInfo = activity!!.packageManager.getApplicationInfo(packageName, 0)
|
val appInfo = activity!!.packageManager.getApplicationInfo(packageName, 0)
|
||||||
val appTitle = activity!!.packageManager.getApplicationLabel(appInfo).toString()
|
val appTitle = activity!!.packageManager.getApplicationLabel(appInfo).toString()
|
||||||
|
@ -114,7 +139,7 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
|
||||||
}
|
}
|
||||||
|
|
||||||
if (appTitle.isNotEmpty()) {
|
if (appTitle.isNotEmpty()) {
|
||||||
return AppMetadata(appTitle, appIcon)
|
return WidgetsListSection(appTitle, appIcon)
|
||||||
}
|
}
|
||||||
} catch (ignored: Exception) {
|
} catch (ignored: Exception) {
|
||||||
} catch (error: Error) {
|
} catch (error: Error) {
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
package com.simplemobiletools.launcher.helpers
|
||||||
|
|
||||||
|
const val WIDGET_LIST_SECTION = 0
|
||||||
|
const val WIDGET_LIST_ITEMS_HOLDER = 1
|
|
@ -1,5 +0,0 @@
|
||||||
package com.simplemobiletools.launcher.models
|
|
||||||
|
|
||||||
import android.graphics.drawable.Drawable
|
|
||||||
|
|
||||||
data class AppMetadata(var appTitle: String, var appIcon: Drawable)
|
|
|
@ -2,4 +2,5 @@ package com.simplemobiletools.launcher.models
|
||||||
|
|
||||||
import android.graphics.drawable.Drawable
|
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()
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
package com.simplemobiletools.launcher.models
|
||||||
|
|
||||||
|
open class WidgetsListItem
|
|
@ -0,0 +1,3 @@
|
||||||
|
package com.simplemobiletools.launcher.models
|
||||||
|
|
||||||
|
data class WidgetsListItemsHolder(val widgets: ArrayList<AppWidget>) : WidgetsListItem()
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.simplemobiletools.launcher.models
|
||||||
|
|
||||||
|
import android.graphics.drawable.Drawable
|
||||||
|
|
||||||
|
data class WidgetsListSection(var appTitle: String, var appIcon: Drawable) : WidgetsListItem()
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/widget_list_items_holder"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:padding="@dimen/medium_margin">
|
||||||
|
|
||||||
|
</RelativeLayout>
|
Loading…
Reference in a new issue