Merge ListView's adapter to BaseAdapterFactory

This commit is contained in:
2022-06-03 01:56:11 +08:00
parent c02bc2314a
commit 80b402ee40
2 changed files with 107 additions and 38 deletions

View File

@@ -24,10 +24,6 @@
package com.fankes.coloros.notify.ui.activity package com.fankes.coloros.notify.ui.activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import androidx.core.view.isVisible import androidx.core.view.isVisible
import com.fankes.coloros.notify.R import com.fankes.coloros.notify.R
import com.fankes.coloros.notify.bean.IconDataBean import com.fankes.coloros.notify.bean.IconDataBean
@@ -116,46 +112,33 @@ class ConfigureActivity : BaseActivity<ActivityConfigBinding>() {
binding.configTitleSync.setOnClickListener { onStartRefresh() } binding.configTitleSync.setOnClickListener { onStartRefresh() }
/** 设置列表元素和 Adapter */ /** 设置列表元素和 Adapter */
binding.configListView.apply { binding.configListView.apply {
adapter = object : BaseAdapter() { bindAdapter {
onBindDatas { iconDatas }
override fun getCount() = iconDatas.size onBindViews<AdapterConfigBinding> { binding, position ->
iconDatas[position].also { bean ->
override fun getItem(position: Int) = iconDatas[position] binding.adpAppIcon.setImageBitmap(bean.iconBitmap)
(if (bean.iconColor != 0) bean.iconColor else resources.getColor(R.color.colorTextGray)).also { color ->
override fun getItemId(position: Int) = position.toLong() binding.adpAppIcon.setColorFilter(color)
binding.adpAppName.setTextColor(color)
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var cView = convertView
val holder: AdapterConfigBinding
if (convertView == null) {
holder = AdapterConfigBinding.inflate(LayoutInflater.from(context))
cView = holder.root
cView.tag = holder
} else holder = convertView.tag as AdapterConfigBinding
getItem(position).also {
holder.adpAppIcon.setImageBitmap(it.iconBitmap)
(if (it.iconColor != 0) it.iconColor else resources.getColor(R.color.colorTextGray)).also { color ->
holder.adpAppIcon.setColorFilter(color)
holder.adpAppName.setTextColor(color)
} }
holder.adpAppName.text = it.appName binding.adpAppName.text = bean.appName
holder.adpAppPkgName.text = it.packageName binding.adpAppPkgName.text = bean.packageName
holder.adpCbrName.text = "贡献者:" + it.contributorName binding.adpCbrName.text = "贡献者:" + bean.contributorName
isAppNotifyHookOf(it).also { e -> isAppNotifyHookOf(bean).also { e ->
holder.adpAppOpenSwitch.isChecked = e binding.adpAppOpenSwitch.isChecked = e
holder.adpAppAllSwitch.isEnabled = e binding.adpAppAllSwitch.isEnabled = e
} }
holder.adpAppOpenSwitch.setOnCheckedChangeListener { btn, b -> binding.adpAppOpenSwitch.setOnCheckedChangeListener { btn, b ->
if (btn.isPressed.not()) return@setOnCheckedChangeListener if (btn.isPressed.not()) return@setOnCheckedChangeListener
putAppNotifyHookOf(it, b) putAppNotifyHookOf(bean, b)
holder.adpAppAllSwitch.isEnabled = b binding.adpAppAllSwitch.isEnabled = b
SystemUITool.refreshSystemUI(context = this@ConfigureActivity) SystemUITool.refreshSystemUI(context = this@ConfigureActivity)
} }
holder.adpAppAllSwitch.isChecked = isAppNotifyHookAllOf(it) binding.adpAppAllSwitch.isChecked = isAppNotifyHookAllOf(bean)
holder.adpAppAllSwitch.setOnCheckedChangeListener { btn, b -> binding.adpAppAllSwitch.setOnCheckedChangeListener { btn, b ->
if (btn.isPressed.not()) return@setOnCheckedChangeListener if (btn.isPressed.not()) return@setOnCheckedChangeListener
fun saveState() { fun saveState() {
putAppNotifyHookAllOf(it, b) putAppNotifyHookAllOf(bean, b)
SystemUITool.refreshSystemUI(context = this@ConfigureActivity) SystemUITool.refreshSystemUI(context = this@ConfigureActivity)
} }
if (b) showDialog { if (b) showDialog {
@@ -169,7 +152,6 @@ class ConfigureActivity : BaseActivity<ActivityConfigBinding>() {
} else saveState() } else saveState()
} }
} }
return cView!!
} }
}.apply { }.apply {
setOnItemLongClickListener { _, _, p, _ -> setOnItemLongClickListener { _, _, p, _ ->

View File

@@ -0,0 +1,87 @@
/*
* ColorOSNotifyIcon - Optimize notification icons for ColorOS and adapt to native notification icon specifications.
* Copyright (C) 2019-2022 Fankes Studio(qzmmcn@163.com)
* https://github.com/fankes/ColorOSNotifyIcon
*
* This software is non-free but opensource software: you can redistribute it
* and/or modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
* <p>
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* and eula along with this software. If not, see
* <https://www.gnu.org/licenses/>
*
* This file is Created by fankes on 2022/6/3.
*/
package com.fankes.coloros.notify.utils.factory
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import androidx.viewbinding.ViewBinding
import com.highcapable.yukihookapi.hook.factory.method
import com.highcapable.yukihookapi.hook.type.android.LayoutInflaterClass
/**
* 绑定 [BaseAdapter] 到 [ListView]
* @param initiate 方法体
* @return [BaseAdapter]
*/
inline fun ListView.bindAdapter(initiate: BaseAdapterCreater.() -> Unit) =
BaseAdapterCreater(context).apply(initiate).baseAdapter?.apply { adapter = this } ?: error("BaseAdapter not binded")
/**
* [BaseAdapter] 创建类
* @param context 实例
*/
class BaseAdapterCreater(val context: Context) {
/** 当前 [List] 回调 */
var listDataCallback: (() -> List<*>)? = null
/** 当前 [BaseAdapter] */
var baseAdapter: BaseAdapter? = null
/**
* 绑定 [List] 到 [ListView]
* @param result 回调数据
*/
fun onBindDatas(result: (() -> List<*>)) {
listDataCallback = result
}
/**
* 绑定 [BaseAdapter] 到 [ListView]
* @param bindViews 回调 - ([VB] 每项,[Int] 下标)
*/
inline fun <reified VB : ViewBinding> onBindViews(crossinline bindViews: (binding: VB, position: Int) -> Unit) {
baseAdapter = object : BaseAdapter() {
override fun getCount() = listDataCallback?.let { it() }?.size ?: 0
override fun getItem(position: Int) = listDataCallback?.let { it() }?.get(position)
override fun getItemId(position: Int) = position.toLong()
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var holderView = convertView
val holder: VB
if (convertView == null) {
holder = VB::class.java.method {
name = "inflate"
param(LayoutInflaterClass)
}.get().invoke<VB>(LayoutInflater.from(context)) ?: error("ViewHolder binding failed")
holderView = holder.root.apply { tag = holder }
} else holder = convertView.tag as VB
bindViews(holder, position)
return holderView ?: error("ViewHolder binding failed")
}
}
}
}