diff --git a/app/src/main/java/com/fankes/coloros/notify/ui/activity/ConfigureActivity.kt b/app/src/main/java/com/fankes/coloros/notify/ui/activity/ConfigureActivity.kt index 156aa3a..1cfe5a9 100644 --- a/app/src/main/java/com/fankes/coloros/notify/ui/activity/ConfigureActivity.kt +++ b/app/src/main/java/com/fankes/coloros/notify/ui/activity/ConfigureActivity.kt @@ -24,10 +24,6 @@ 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 com.fankes.coloros.notify.R import com.fankes.coloros.notify.bean.IconDataBean @@ -116,46 +112,33 @@ class ConfigureActivity : BaseActivity() { binding.configTitleSync.setOnClickListener { onStartRefresh() } /** 设置列表元素和 Adapter */ binding.configListView.apply { - adapter = object : BaseAdapter() { - - override fun getCount() = iconDatas.size - - override fun getItem(position: Int) = iconDatas[position] - - override fun getItemId(position: Int) = position.toLong() - - 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) + bindAdapter { + onBindDatas { iconDatas } + onBindViews { binding, position -> + iconDatas[position].also { bean -> + binding.adpAppIcon.setImageBitmap(bean.iconBitmap) + (if (bean.iconColor != 0) bean.iconColor else resources.getColor(R.color.colorTextGray)).also { color -> + binding.adpAppIcon.setColorFilter(color) + binding.adpAppName.setTextColor(color) } - holder.adpAppName.text = it.appName - holder.adpAppPkgName.text = it.packageName - holder.adpCbrName.text = "贡献者:" + it.contributorName - isAppNotifyHookOf(it).also { e -> - holder.adpAppOpenSwitch.isChecked = e - holder.adpAppAllSwitch.isEnabled = e + binding.adpAppName.text = bean.appName + binding.adpAppPkgName.text = bean.packageName + binding.adpCbrName.text = "贡献者:" + bean.contributorName + isAppNotifyHookOf(bean).also { e -> + binding.adpAppOpenSwitch.isChecked = e + binding.adpAppAllSwitch.isEnabled = e } - holder.adpAppOpenSwitch.setOnCheckedChangeListener { btn, b -> + binding.adpAppOpenSwitch.setOnCheckedChangeListener { btn, b -> if (btn.isPressed.not()) return@setOnCheckedChangeListener - putAppNotifyHookOf(it, b) - holder.adpAppAllSwitch.isEnabled = b + putAppNotifyHookOf(bean, b) + binding.adpAppAllSwitch.isEnabled = b SystemUITool.refreshSystemUI(context = this@ConfigureActivity) } - holder.adpAppAllSwitch.isChecked = isAppNotifyHookAllOf(it) - holder.adpAppAllSwitch.setOnCheckedChangeListener { btn, b -> + binding.adpAppAllSwitch.isChecked = isAppNotifyHookAllOf(bean) + binding.adpAppAllSwitch.setOnCheckedChangeListener { btn, b -> if (btn.isPressed.not()) return@setOnCheckedChangeListener fun saveState() { - putAppNotifyHookAllOf(it, b) + putAppNotifyHookAllOf(bean, b) SystemUITool.refreshSystemUI(context = this@ConfigureActivity) } if (b) showDialog { @@ -169,7 +152,6 @@ class ConfigureActivity : BaseActivity() { } else saveState() } } - return cView!! } }.apply { setOnItemLongClickListener { _, _, p, _ -> diff --git a/app/src/main/java/com/fankes/coloros/notify/utils/factory/BaseAdapterFactory.kt b/app/src/main/java/com/fankes/coloros/notify/utils/factory/BaseAdapterFactory.kt new file mode 100644 index 0000000..ea15325 --- /dev/null +++ b/app/src/main/java/com/fankes/coloros/notify/utils/factory/BaseAdapterFactory.kt @@ -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. + *

+ * + * 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 + * + * + * 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 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(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") + } + } + } +} \ No newline at end of file