Skip to content

Commit ce882f4

Browse files
committed
perf(UI): ⚡ 优化启动速度,UI 界面卡顿, Optimize the startup speed, UI interface is stuck
1 parent 6ba8776 commit ce882f4

File tree

6 files changed

+48
-50
lines changed

6 files changed

+48
-50
lines changed

android/app/src/main/kotlin/cn/ys1231/appproxy/MainActivity.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@ class MainActivity : FlutterActivity() {
3030
private val CHANNEL_VPN = "cn.ys1231/appproxy/vpn"
3131
private val CHANNEL_APP_UPDATE = "cn.ys1231/appproxy/appupdate"
3232
private var FLUTTER_VPN_CHANNEL: MethodChannel? = null
33+
private var FLUTTER_CHANNEL: MethodChannel? = null
3334

3435
private var utils: Utils? = null
3536
private var intentVpnService: Intent? = null
3637
private var iyueVpnService: IyueVPNService? = null
3738
private var isBind: Boolean = false
3839
private var currentProxy: Map<String, Any>? = null
39-
4040
private var conn: ServiceConnection? = null
4141

4242
override fun onCreate(savedInstanceState: Bundle?) {
4343
super.onCreate(savedInstanceState)
44-
utils = Utils(this)
4544
intentVpnService = Intent(this, IyueVPNService::class.java)
4645
conn = object : ServiceConnection {
4746
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
@@ -99,15 +98,17 @@ class MainActivity : FlutterActivity() {
9998

10099
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
101100
super.configureFlutterEngine(flutterEngine)
101+
utils = Utils(this)
102102

103-
MethodChannel(
103+
FLUTTER_CHANNEL = MethodChannel(
104104
flutterEngine.dartExecutor.binaryMessenger,
105105
CHANNEL
106-
).setMethodCallHandler { call, result ->
106+
)
107+
FLUTTER_CHANNEL!!.setMethodCallHandler { call, result ->
107108
if (call.method == "getAppList") {
108109
try {
109110
Log.d(TAG, "configureFlutterEngine ${call.method} ")
110-
val appList = Utils(this).getAppList()
111+
val appList = utils!!.getAppList()
111112
result.success(appList)
112113
} catch (e: Exception) {
113114
result.error("-1", e.message, null)
@@ -156,6 +157,17 @@ class MainActivity : FlutterActivity() {
156157
}
157158
}
158159
}
160+
161+
// 遍历所有 app 通知刷新
162+
Thread {
163+
Log.d(TAG, "configureFlutterEngine: start get app list info")
164+
utils!!.initAppList()
165+
runOnUiThread {
166+
Log.d(TAG, "configureFlutterEngine: call onRefresh")
167+
FLUTTER_CHANNEL!!.invokeMethod("onRefresh", null)
168+
Log.d(TAG, "configureFlutterEngine: end get app list info")
169+
}
170+
}.start()
159171
}
160172

161173
private val VPN_REQUEST_CODE = 100

android/app/src/main/kotlin/cn/ys1231/appproxy/data/Utils.kt

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import java.io.ByteArrayOutputStream
1616

1717
class Utils(private val context: Context) {
1818
private val TAG = "iyue->${this.javaClass.simpleName} "
19-
// val sharedPreferences = context.getSharedPreferences("vpnconfig", Context.MODE_PRIVATE)
19+
20+
//val sharedPreferences = context.getSharedPreferences("vpnconfig", Context.MODE_PRIVATE)
21+
private var appList: String? = null
22+
private var isFirstGetApps: Boolean = true
2023

2124
init {
2225
Log.d(TAG, "Utils init !")
@@ -30,8 +33,8 @@ class Utils(private val context: Context) {
3033
*
3134
* @return 返回包含已安装应用信息的JSON字符串,如果无应用满足条件或发生错误,则返回null。
3235
*/
33-
fun getAppList(): String? {
34-
36+
fun initAppList() {
37+
Log.d(TAG, "initAppList: start")
3538
// 通过上下文获取PackageManager对象,用于管理安装的应用程序
3639
val pm = context.packageManager
3740

@@ -78,7 +81,18 @@ class Utils(private val context: Context) {
7881
}
7982

8083
// 使用Gson将应用信息列表转换为JSON字符串并返回
81-
return Gson().toJson(appInfoList)
84+
appList = Gson().toJson(appInfoList)
85+
Log.d(TAG, "initAppList: end")
86+
}
87+
88+
fun getAppList(): String? {
89+
if (isFirstGetApps && appList != null) {
90+
isFirstGetApps = false
91+
return appList
92+
} else {
93+
initAppList()
94+
return appList
95+
}
8296
}
8397

8498
private val PackageInfo.isSystemApp: Boolean
@@ -96,20 +110,4 @@ class Utils(private val context: Context) {
96110
return bitmap
97111
}
98112

99-
// public fun setVpnStatus(status: Boolean){
100-
// var edit = sharedPreferences.edit()
101-
// edit.putBoolean("vpnStatus", status)
102-
// edit.commit()
103-
// }
104-
// public fun getVpnStatus(): Boolean{
105-
// return sharedPreferences.getBoolean("vpnStatus", false)
106-
// }
107-
// public fun setProxyName(name: String){
108-
// var edit = sharedPreferences.edit()
109-
// edit.putString("proxyName", name)
110-
// edit.commit()
111-
// }
112-
// public fun getProxyName(): String{
113-
// return sharedPreferences.getString("proxyName", "") ?: ""
114-
// }
115113
}

assets/appproxy.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"VersionName": "v0.0.8",
3-
"ModifyContent": "\r\n1、perf(startup): ⚡ #12 Optimize startup speed.\r\n2、fix(update): :bug: 国内更新通道, Domestic update channel",
2+
"VersionName": "v0.1.3",
3+
"ModifyContent": "\n\rperf(UI): :zap: 优化启动速度,UI 界面卡顿, Optimize the startup speed, UI interface is stuck",
44
"DownloadUrl": "https://pfile.ys1231.cn/modules/appproxy/"
55
}

lib/main.dart

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,7 @@ class _iyueMainPageState extends State<iyueMainPage> {
8585
// 首页Widget
8686
const ProxyListHome(),
8787
// app配置列表
88-
FutureBuilder(
89-
// 先启动后加载
90-
future: Future.delayed(const Duration(milliseconds: 100)),
91-
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
92-
// 当计算状态为等待时,显示加载动画
93-
if (snapshot.connectionState == ConnectionState.waiting) {
94-
return const Center(
95-
// 显示一个加载动画
96-
child: CircularProgressIndicator(),
97-
);
98-
} else if (snapshot.hasError) {
99-
// 当 future 出现错误时,显示错误信息
100-
return Center(
101-
child: Text('Error: ${snapshot.error}'),
102-
);
103-
} else {
104-
// 当计算完成时,显示app配置列表
105-
return const AppConfigList();
106-
}
107-
}),
108-
// const AppConfigList(),
88+
const AppConfigList(),
10989
// 设置页面
11090
const AppSettings(), // 设置页Widget
11191
];

lib/ui/app_config_list.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ class AppConfigState extends State<AppConfigList> {
112112
// app列表配置文件
113113
final AppProxyConfigData _appfile = AppProxyConfigData("proxyconfig.json");
114114

115+
// 远程调用通道
116+
final platform = const MethodChannel('cn.ys1231/appproxy');
117+
115118
// 用于更新调用子控件列表项选择状态
116119
List<GlobalKey<CardCheckboxState>> _cardKeys = [];
117120

@@ -120,7 +123,12 @@ class AppConfigState extends State<AppConfigList> {
120123
super.initState();
121124
debugPrint("iyue-> initState");
122125
_initData();
123-
getAppList();
126+
platform.setMethodCallHandler((call) async {
127+
if (call.method == 'onRefresh') {
128+
// 执行Flutter逻辑
129+
getAppList();
130+
}
131+
});
124132
}
125133

126134
// 初始化数据

lib/ui/settings.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class _AppSettingsState extends State<AppSettings> {
116116
children: [
117117
Text(S.of(context).text_describe),
118118
Text(S.of(context).text_author),
119-
Text('${S.of(context).text_update_time}:2024-11-18'),
119+
Text('${S.of(context).text_update_time}:2024-11-23'),
120120
Row(
121121
children: [
122122
const Text('github:'),

0 commit comments

Comments
 (0)