From 8d818f9bf82425a824296132bc909d80af92543a Mon Sep 17 00:00:00 2001 From: My-Responsitories <107370289+My-Responsitories@users.noreply.github.com> Date: Tue, 23 Sep 2025 02:26:14 +0800 Subject: [PATCH] opt: account --- lib/http/login.dart | 2 +- lib/pages/login/controller.dart | 18 +++++++++++------- lib/pages/mine/controller.dart | 3 ++- lib/utils/accounts.dart | 32 +++++++++++++++++--------------- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/lib/http/login.dart b/lib/http/login.dart index def57cfc1..8b7ced525 100644 --- a/lib/http/login.dart +++ b/lib/http/login.dart @@ -17,7 +17,7 @@ import 'package:encrypt/encrypt.dart'; class LoginHttp { static final String deviceId = LoginUtils.genDeviceId(); - static final String buvid = LoginUtils.buvid; + static String get buvid => LoginUtils.buvid; static final Map headers = { 'buvid': buvid, 'env': 'prod', diff --git a/lib/pages/login/controller.dart b/lib/pages/login/controller.dart index 5a8945244..7b61e1afd 100644 --- a/lib/pages/login/controller.dart +++ b/lib/pages/login/controller.dart @@ -690,7 +690,11 @@ class LoginPageController extends GetxController tokenInfo['refresh_token'], ); await Future.wait([account.onChange(), AnonymousAccount().delete()]); - Accounts.accountMode.updateAll((_, a) => a == account ? account : a); + for (int i = 0; i < AccountType.values.length; i++) { + if (Accounts.accountMode[i].mid == account.mid) { + Accounts.accountMode[i] = account; + } + } if (Accounts.main.isLogin) { SmartDialog.showToast('登录成功'); } else { @@ -704,7 +708,7 @@ class LoginPageController extends GetxController SmartDialog.showToast('请先登录'); return Get.toNamed('/loginPage'); } - final selectAccount = Map.of(Accounts.accountMode); + final selectAccount = List.of(Accounts.accountMode); final options = { AnonymousAccount(): '0', ...Accounts.account.toMap().map( @@ -729,9 +733,9 @@ class LoginPageController extends GetxController .map( (e) => Builder( builder: (context) => RadioGroup( - groupValue: selectAccount[e], + groupValue: selectAccount[e.index], onChanged: (v) { - selectAccount[e] = v!; + selectAccount[e.index] = v!; (context as Element).markNeedsBuild(); }, child: WrapRadioOptionsGroup( @@ -756,9 +760,9 @@ class LoginPageController extends GetxController ), TextButton( onPressed: () { - for (var i in selectAccount.entries) { - if (i.value != Accounts.get(i.key)) { - Accounts.set(i.key, i.value); + for (var (i, v) in selectAccount.indexed) { + if (v != Accounts.accountMode[i]) { + Accounts.set(AccountType.values[i], v); } } Get.back(); diff --git a/lib/pages/mine/controller.dart b/lib/pages/mine/controller.dart index 50e20fc87..3cc0ba113 100644 --- a/lib/pages/mine/controller.dart +++ b/lib/pages/mine/controller.dart @@ -229,7 +229,8 @@ class MineController } res == true ? Accounts.set(AccountType.heartbeat, AnonymousAccount()) - : Accounts.accountMode[AccountType.heartbeat] = AnonymousAccount(); + : Accounts.accountMode[AccountType.heartbeat.index] = + AnonymousAccount(); }); } else { Accounts.set(AccountType.heartbeat, Accounts.main); diff --git a/lib/utils/accounts.dart b/lib/utils/accounts.dart index 8a161624c..6798bf7a6 100644 --- a/lib/utils/accounts.dart +++ b/lib/utils/accounts.dart @@ -7,9 +7,12 @@ import 'package:hive/hive.dart'; abstract class Accounts { static late final Box account; - static final Map accountMode = {}; - static Account get main => accountMode[AccountType.main]!; - static Account get heartbeat => accountMode[AccountType.heartbeat]!; + static final List accountMode = List.filled( + AccountType.values.length, + AnonymousAccount(), + ); + static Account get main => accountMode[AccountType.main.index]; + static Account get heartbeat => accountMode[AccountType.heartbeat.index]; static Account get history { final heartbeat = Accounts.heartbeat; if (heartbeat is AnonymousAccount) { @@ -70,14 +73,11 @@ abstract class Accounts { static Future refresh() async { for (var a in account.values) { for (var t in a.type) { - accountMode[t] = a; + accountMode[t.index] = a; } } - for (var type in AccountType.values) { - accountMode[type] ??= AnonymousAccount(); - } await Future.wait( - (accountMode.values.toSet()..retainWhere((i) => !i.activited)).map( + (accountMode.toSet()..removeWhere((i) => i.activited)).map( Request.buvidActive, ), ); @@ -85,7 +85,7 @@ abstract class Accounts { static Future clear() async { await account.clear(); - for (var i in AccountType.values) { + for (int i = 0; i < AccountType.values.length; i++) { accountMode[i] = AnonymousAccount(); } await AnonymousAccount().delete(); @@ -100,9 +100,11 @@ abstract class Accounts { static Future deleteAll(Set accounts) async { var isloginMain = Accounts.main.isLogin; - Accounts.accountMode.updateAll( - (_, a) => accounts.contains(a) ? AnonymousAccount() : a, - ); + for (int i = 0; i < AccountType.values.length; i++) { + if (accounts.contains(accountMode[i])) { + accountMode[i] = AnonymousAccount(); + } + } await Future.wait(accounts.map((i) => i.delete())); if (isloginMain && !Accounts.main.isLogin) { await LoginUtils.onLogoutMain(); @@ -110,8 +112,8 @@ abstract class Accounts { } static Future set(AccountType key, Account account) async { - await (accountMode[key]?..type.remove(key))?.onChange(); - accountMode[key] = account..type.add(key); + await (accountMode[key.index]..type.remove(key)).onChange(); + accountMode[key.index] = account..type.add(key); await account.onChange(); if (!account.activited) await Request.buvidActive(account); switch (key) { @@ -129,6 +131,6 @@ abstract class Accounts { } static Account get(AccountType key) { - return accountMode[key]!; + return accountMode[key.index]; } }