keyboard_manage_controller.dart 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. import 'package:collection/collection.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:injectable/injectable.dart';
  5. import 'package:keyboard/base/base_controller.dart';
  6. import 'package:keyboard/data/bean/character_info.dart';
  7. import 'package:keyboard/data/consts/event_report.dart';
  8. import 'package:keyboard/data/repository/account_repository.dart';
  9. import 'package:keyboard/data/repository/keyboard_repository.dart';
  10. import 'package:keyboard/dialog/character_add_dialog.dart';
  11. import 'package:keyboard/dialog/custom_character/custom_character_add_dialog.dart';
  12. import 'package:keyboard/dialog/login/login_dialog.dart';
  13. import 'package:keyboard/handler/event_handler.dart';
  14. import 'package:keyboard/module/character/content/character_group_content_controller.dart';
  15. import 'package:keyboard/resource/string.gen.dart';
  16. import 'package:keyboard/utils/atmob_log.dart';
  17. import 'package:keyboard/utils/toast_util.dart';
  18. import '../../data/bean/keyboard_info.dart';
  19. import '../../data/consts/error_code.dart';
  20. import '../../plugins/keyboard_android_platform.dart';
  21. import '../../utils/error_handler.dart';
  22. import '../../utils/http_handler.dart';
  23. enum KeyboardType {
  24. system, //通用键盘
  25. custom, //自定义键盘
  26. }
  27. @injectable
  28. class KeyboardManageController extends BaseController
  29. with GetTickerProviderStateMixin {
  30. final String tag = 'KeyboardManageController';
  31. final AccountRepository accountRepository;
  32. bool get isLogin => accountRepository.isLogin.value;
  33. // 自定义键盘列表
  34. final RxList<KeyboardInfo> _customKeyboardInfoList = RxList();
  35. List<KeyboardInfo> get customKeyboardInfoList => _customKeyboardInfoList;
  36. // 当前自定义键盘
  37. final Rx<KeyboardInfo> _currentCustomKeyboardInfo = KeyboardInfo().obs;
  38. KeyboardInfo get currentCustomKeyboardInfo =>
  39. _currentCustomKeyboardInfo.value;
  40. //当前自定义键盘人设列表
  41. final RxList<CharacterInfo> _currentCustomKeyboardCharacterList = RxList();
  42. RxList<CharacterInfo> get currentCustomKeyboardCharacterList =>
  43. _currentCustomKeyboardCharacterList;
  44. // 当前自定义键盘亲密度
  45. final RxInt _currentCustomIntimacy = 0.obs;
  46. int get currentCustomIntimacy => _currentCustomIntimacy.value;
  47. // 当前定制亲密度是否有变化
  48. final RxBool _customIntimacyChanged = false.obs;
  49. bool get customIntimacyChanged => _customIntimacyChanged.value;
  50. final RxBool _customKeyboardCharacterListChanged = false.obs;
  51. bool get customKeyboardCharacterListChanged =>
  52. _customKeyboardCharacterListChanged.value;
  53. // 存储排序前的定制人设列表,用于比较是否有变化
  54. List<CharacterInfo> _oldCustomCharacterList = [];
  55. // 通用键盘列表
  56. final RxList<KeyboardInfo> _generalKeyboardInfoList = RxList();
  57. List<KeyboardInfo> get generalKeyboardInfoList => _generalKeyboardInfoList;
  58. // 当前通用键盘
  59. final Rx<KeyboardInfo> _currentGeneralKeyboardInfo = KeyboardInfo().obs;
  60. KeyboardInfo get currentGeneralKeyboardInfo =>
  61. _currentGeneralKeyboardInfo.value;
  62. // 当前通用键盘人设列表
  63. final RxList<CharacterInfo> _currentGeneralKeyboardCharacterList = RxList();
  64. List<CharacterInfo> get currentGeneralKeyboardCharacterList =>
  65. _currentGeneralKeyboardCharacterList;
  66. // 当前通用键盘亲密度
  67. final RxInt _currentGeneralIntimacy = 0.obs;
  68. int get currentGeneralIntimacy => _currentGeneralIntimacy.value;
  69. // 当前通用亲密度是否有变化
  70. final RxBool _generalIntimacyChanged = false.obs;
  71. RxBool get generalIntimacyChanged => _generalIntimacyChanged;
  72. final RxBool _generalKeyboardCharacterListChanged = false.obs;
  73. bool get generalKeyboardCharacterListChanged =>
  74. _generalKeyboardCharacterListChanged.value;
  75. // 存储排序前的通用人设列表,用于比较是否有变化
  76. late List<CharacterInfo> _oldGeneralCharacterList;
  77. final KeyboardRepository keyboardRepository;
  78. // 最小人设数量
  79. final _minCount = 9;
  80. // 键盘管理类型
  81. List<String> keyboardManageType = [
  82. StringName.keyboardCustom,
  83. StringName.generalKeyboard,
  84. ];
  85. // 键盘管理页面的tabController,用于控制通用键盘和自定义键盘的切换
  86. late TabController tabController;
  87. // 键盘管理页面的pageController,用于控制通用键盘和自定义键盘的切换
  88. late PageController pageController;
  89. // 首次加载数据标志
  90. final isFirstLoad = true.obs;
  91. KeyboardManageController(this.keyboardRepository, this.accountRepository);
  92. @override
  93. void onInit() async {
  94. super.onInit();
  95. final args = Get.arguments;
  96. if (args is Map && args["customKeyboardInfo"] is KeyboardInfo) {
  97. _currentCustomKeyboardInfo.value = args["customKeyboardInfo"];
  98. }
  99. await _dataLoad();
  100. }
  101. _dataLoad() async {
  102. tabController = TabController(
  103. length: keyboardManageType.length,
  104. vsync: this,
  105. initialIndex: 0,
  106. );
  107. tabController.addListener(() {
  108. if (tabController.indexIsChanging) {
  109. switchTabKeyboardType(tabController.index);
  110. if (tabController.index == 0) {
  111. EventHandler.report(EventId.event_13001);
  112. } else {
  113. EventHandler.report(EventId.event_13003);
  114. }
  115. }
  116. });
  117. pageController = PageController();
  118. await getCustomKeyboard();
  119. if (isFirstLoad.value) {
  120. if (_customKeyboardInfoList.isEmpty) {
  121. // 去另一个tab
  122. pageController.jumpToPage(1);
  123. }
  124. isFirstLoad.value = false;
  125. }
  126. if (tabController.index == 0) {
  127. EventHandler.report(EventId.event_13001);
  128. }
  129. getGeneralKeyboard();
  130. }
  131. clickBack() {
  132. AtmobLog.i(tag, 'clickBack');
  133. Get.back();
  134. }
  135. @override
  136. void onReady() {
  137. super.onReady();
  138. }
  139. // 获取定制键盘
  140. Future<void> getCustomKeyboard() async {
  141. AtmobLog.i(tag, 'getCustomKeyboard');
  142. await keyboardRepository.getKeyboardList(type: KeyboardType.custom.name).then((
  143. keyboardListResponse,
  144. ) {
  145. AtmobLog.i(
  146. tag,
  147. 'keyboardListResponse: ${keyboardListResponse.keyboardInfos}',
  148. );
  149. _customKeyboardInfoList.value = keyboardListResponse.keyboardInfos;
  150. //检查是否是选择的键盘,如果没有选择的键盘,默认选择第一个
  151. if (_customKeyboardInfoList.isNotEmpty) {
  152. if (_currentCustomKeyboardInfo.value.id == null) {
  153. _currentCustomKeyboardInfo.value = _customKeyboardInfoList.firstWhere(
  154. (element) => element.isChoose == true,
  155. orElse: () => _customKeyboardInfoList.first,
  156. );
  157. }
  158. _currentCustomIntimacy.value =
  159. _currentCustomKeyboardInfo.value.intimacy ?? 0;
  160. _currentCustomIntimacy.listen((intimacy) {
  161. _customIntimacyChanged.value =
  162. _currentCustomKeyboardInfo.value.intimacy != intimacy;
  163. AtmobLog.d(tag, 'intimacyChanged: $_customIntimacyChanged');
  164. });
  165. String? id = _currentCustomKeyboardInfo.value.id;
  166. if (id != null) {
  167. getKeyboardCharacterList(keyboardId: id, isCustom: true);
  168. }
  169. }
  170. });
  171. }
  172. // 获取通用键盘
  173. void getGeneralKeyboard() {
  174. AtmobLog.i(tag, 'getGeneralKeyboard');
  175. keyboardRepository.getKeyboardList(type: KeyboardType.system.name).then((
  176. keyboardListResponse,
  177. ) {
  178. AtmobLog.i(
  179. tag,
  180. 'keyboardListResponse: ${keyboardListResponse.keyboardInfos}',
  181. );
  182. _generalKeyboardInfoList.value = keyboardListResponse.keyboardInfos;
  183. _currentGeneralKeyboardInfo.value = _generalKeyboardInfoList.first;
  184. _currentGeneralIntimacy.value =
  185. _currentGeneralKeyboardInfo.value.intimacy ?? 0;
  186. _currentGeneralIntimacy.listen((intimacy) {
  187. _generalIntimacyChanged.value =
  188. _currentGeneralKeyboardInfo.value.intimacy != intimacy;
  189. AtmobLog.d(tag, 'intimacyChanged: $_generalIntimacyChanged');
  190. });
  191. String? id = _currentGeneralKeyboardInfo.value.id;
  192. if (id != null) {
  193. getKeyboardCharacterList(keyboardId: id, isCustom: false);
  194. }
  195. });
  196. }
  197. // 获取当前键盘人设列表
  198. void getKeyboardCharacterList({
  199. required String keyboardId,
  200. required bool isCustom,
  201. }) {
  202. if (isCustom) {
  203. keyboardRepository.getKeyboardCharacterList(keyboardId: keyboardId).then((
  204. keyboardCharacterListResponse,
  205. ) {
  206. AtmobLog.i(
  207. tag,
  208. 'keyboardCharacterListResponse: ${keyboardCharacterListResponse.characterInfos.toString()}',
  209. );
  210. _currentCustomKeyboardCharacterList.assignAll(
  211. keyboardCharacterListResponse.characterInfos,
  212. );
  213. _oldCustomCharacterList = List<CharacterInfo>.from(
  214. _currentCustomKeyboardCharacterList,
  215. );
  216. _customKeyboardCharacterListChanged.value = false;
  217. _currentCustomKeyboardCharacterList.listen((event) {
  218. _customKeyboardCharacterListChanged.value =
  219. !ListEquality().equals(_oldCustomCharacterList, event);
  220. AtmobLog.d(
  221. tag,
  222. '_customKeyboardCharacterListChanged: $_customKeyboardCharacterListChanged',
  223. );
  224. });
  225. });
  226. } else {
  227. keyboardRepository.getKeyboardCharacterList(keyboardId: keyboardId).then((
  228. keyboardCharacterListResponse,
  229. ) {
  230. AtmobLog.i(
  231. tag,
  232. 'keyboardCharacterListResponse: ${keyboardCharacterListResponse.characterInfos.toString()}',
  233. );
  234. if (_currentGeneralKeyboardInfo.value.id == keyboardId) {
  235. _currentGeneralKeyboardCharacterList.value =
  236. keyboardCharacterListResponse.characterInfos;
  237. _oldGeneralCharacterList = List<CharacterInfo>.from(
  238. _currentGeneralKeyboardCharacterList,
  239. );
  240. _generalKeyboardCharacterListChanged.value = false;
  241. _currentGeneralKeyboardCharacterList.listen((event) {
  242. _generalKeyboardCharacterListChanged.value =
  243. !ListEquality().equals(_oldGeneralCharacterList, event);
  244. AtmobLog.d(
  245. tag,
  246. '_generalKeyboardCharacterListChanged: $_generalKeyboardCharacterListChanged',
  247. );
  248. });
  249. }
  250. if (_currentCustomKeyboardInfo.value.id == keyboardId) {
  251. _currentCustomKeyboardCharacterList.value =
  252. keyboardCharacterListResponse.characterInfos;
  253. }
  254. });
  255. }
  256. }
  257. // 切换当前定制键盘
  258. void switchCustomKeyboard(String? keyboardName) {
  259. _currentCustomKeyboardInfo.value = _customKeyboardInfoList.firstWhere(
  260. (element) => element.name == keyboardName,
  261. orElse: () => _customKeyboardInfoList.first,
  262. );
  263. String? keyboardId = _currentCustomKeyboardInfo.value.id;
  264. _currentCustomIntimacy.value =
  265. _currentCustomKeyboardInfo.value.intimacy ?? 0;
  266. if (keyboardId != null) {
  267. getKeyboardCharacterList(keyboardId: keyboardId, isCustom: true);
  268. }
  269. }
  270. // tab切换
  271. void switchTabKeyboardType(int index) {
  272. // AtmobLog.i(tag, 'onTabChanged: $index');
  273. pageController.animateToPage(
  274. index,
  275. duration: const Duration(milliseconds: 300),
  276. curve: Curves.easeInToLinear,
  277. );
  278. }
  279. // page切换
  280. void switchPageKeyboardType(int index) {
  281. // AtmobLog.i(tag, 'onPageChanged: $index');
  282. tabController.animateTo(index, duration: const Duration(milliseconds: 300));
  283. if (index == 0) {
  284. _currentGeneralIntimacy.value =
  285. _currentGeneralKeyboardInfo.value.intimacy ?? 0;
  286. _currentGeneralKeyboardCharacterList.value = _oldGeneralCharacterList;
  287. _generalIntimacyChanged.value = false;
  288. _generalKeyboardCharacterListChanged.value = false;
  289. getCustomKeyboard();
  290. } else {
  291. _currentCustomIntimacy.value =
  292. _currentCustomKeyboardInfo.value.intimacy ?? 0;
  293. if (_oldCustomCharacterList.isNotEmpty) {
  294. _currentCustomKeyboardCharacterList.value = _oldCustomCharacterList;
  295. }
  296. _customIntimacyChanged.value = false;
  297. _customKeyboardCharacterListChanged.value = false;
  298. getGeneralKeyboard();
  299. }
  300. }
  301. // 更新亲密度
  302. void updateIntimacy(int intimacy, bool isCustom) {
  303. if (isCustom) {
  304. _currentCustomIntimacy.value = intimacy;
  305. } else {
  306. _currentGeneralIntimacy.value = intimacy;
  307. }
  308. }
  309. // 排序
  310. void onReorder(int oldIndex, int newIndex, bool isCustom) {
  311. if (isCustom) {
  312. reorderList(_currentCustomKeyboardCharacterList, oldIndex, newIndex);
  313. } else {
  314. reorderList(_currentGeneralKeyboardCharacterList, oldIndex, newIndex);
  315. }
  316. }
  317. // 排序
  318. void reorderList<T>(RxList<T> list, int oldIndex, int newIndex) {
  319. AtmobLog.d(tag, 'reorderList: $oldIndex, $newIndex');
  320. final item = list.removeAt(oldIndex);
  321. list.insert(newIndex, item);
  322. }
  323. void clickSave(bool isCustom) {
  324. isCustom
  325. ? saveCustomKeyboardCharacterList()
  326. : saveGeneralKeyboardCharacterList();
  327. }
  328. void saveCustomKeyboardCharacterList() {
  329. EventHandler.report(EventId.event_13002);
  330. _saveKeyboardInfo(
  331. intimacyChanged: _customIntimacyChanged,
  332. currentIntimacy: currentCustomIntimacy,
  333. currentKeyboardInfo: _currentCustomKeyboardInfo,
  334. characterListChanged: _customKeyboardCharacterListChanged,
  335. currentCharacterList: _currentCustomKeyboardCharacterList,
  336. onUpdateSuccess: () {
  337. _oldCustomCharacterList = List<CharacterInfo>.from(
  338. _currentCustomKeyboardCharacterList,
  339. );
  340. Get.find<CharacterGroupContentController>().refreshData();
  341. // 通知键盘,刷新人设列表
  342. KeyboardAndroidPlatform.refreshCharacterList();
  343. saveSuccessGetBack();
  344. },
  345. );
  346. }
  347. void saveGeneralKeyboardCharacterList() {
  348. EventHandler.report(EventId.event_13004);
  349. _saveKeyboardInfo(
  350. intimacyChanged: _generalIntimacyChanged,
  351. currentIntimacy: currentGeneralIntimacy,
  352. currentKeyboardInfo: _currentGeneralKeyboardInfo,
  353. characterListChanged: _generalKeyboardCharacterListChanged,
  354. currentCharacterList: _currentGeneralKeyboardCharacterList,
  355. onUpdateSuccess: () {
  356. _oldGeneralCharacterList = List<CharacterInfo>.from(
  357. _currentGeneralKeyboardCharacterList,
  358. );
  359. // 通知键盘,刷新人设列表
  360. KeyboardAndroidPlatform.refreshCharacterList();
  361. },
  362. );
  363. }
  364. void _saveKeyboardInfo({
  365. required RxBool intimacyChanged,
  366. required int currentIntimacy,
  367. required Rx<KeyboardInfo> currentKeyboardInfo,
  368. required RxBool characterListChanged,
  369. required List<CharacterInfo> currentCharacterList,
  370. required VoidCallback onUpdateSuccess,
  371. }) {
  372. String? keyboardId = currentKeyboardInfo.value.id;
  373. if (intimacyChanged.value && keyboardId != null) {
  374. AtmobLog.i(tag, 'clickSave intimacyChanged');
  375. currentKeyboardInfo.value.intimacy = currentIntimacy;
  376. keyboardRepository
  377. .updateKeyboardInfo(keyboardId: keyboardId, intimacy: currentIntimacy)
  378. .then((_) async {
  379. ToastUtil.show(StringName.keyboardSaveSuccess);
  380. await keyboardRepository.refreshData();
  381. })
  382. .catchError((error) {
  383. if (error is ServerErrorException) {
  384. ErrorHandler.toastError(error);
  385. if (error.code == ErrorCode.noLoginError) {
  386. LoginDialog.show();
  387. }
  388. } else {
  389. ToastUtil.show(StringName.keyboardSaveFailed);
  390. }
  391. })
  392. .whenComplete(() => intimacyChanged.value = false);
  393. }
  394. if (characterListChanged.value && keyboardId != null) {
  395. AtmobLog.i(tag, 'clickSave keyboardChanged');
  396. List<String> characterIds =
  397. currentCharacterList.map((e) => e.id).cast<String>().toList();
  398. keyboardRepository
  399. .keyboardCharacterUpdate(
  400. keyboardId: keyboardId,
  401. characterIds: characterIds,
  402. )
  403. .then((_) async {
  404. onUpdateSuccess();
  405. ToastUtil.show(StringName.keyboardSaveSuccess);
  406. await keyboardRepository.refreshData();
  407. })
  408. .catchError((error) {
  409. if (error is ServerErrorException) {
  410. ErrorHandler.toastError(error);
  411. if (error.code == ErrorCode.noLoginError) {
  412. LoginDialog.show();
  413. }
  414. } else {
  415. ToastUtil.show(StringName.keyboardSaveFailed);
  416. }
  417. })
  418. .whenComplete(() => characterListChanged.value = false);
  419. }
  420. }
  421. void clickRemoveCharacter(CharacterInfo characterInfo, bool isCustom) {
  422. if (isCustom) {
  423. if (_currentCustomKeyboardCharacterList.length <= _minCount) {
  424. ToastUtil.show("最少需要保持$_minCount个人设");
  425. return;
  426. }
  427. AtmobLog.i(tag, 'clickRemoveCharacter');
  428. _currentCustomKeyboardCharacterList.remove(characterInfo);
  429. } else {
  430. if (_currentGeneralKeyboardCharacterList.length <= _minCount) {
  431. ToastUtil.show("最少需要保持$_minCount个人设");
  432. return;
  433. }
  434. AtmobLog.i(tag, 'clickRemoveCharacter');
  435. _currentGeneralKeyboardCharacterList.remove(characterInfo);
  436. }
  437. }
  438. clickAddCharacter({required bool isCustom}) {
  439. if (!isLogin) {
  440. ToastUtil.show("请先登录");
  441. LoginDialog.show();
  442. return;
  443. }
  444. if (isCustom) {
  445. CharacterAddDialog.show(
  446. currentKeyboardInfo: currentCustomKeyboardInfo,
  447. clickCallback: () {
  448. getKeyboardCharacterList(
  449. keyboardId: _currentCustomKeyboardInfo.value.id ?? "",
  450. isCustom: true,
  451. );
  452. },
  453. );
  454. } else {
  455. CharacterAddDialog.show(
  456. currentKeyboardInfo: currentGeneralKeyboardInfo,
  457. clickCallback: () {
  458. getGeneralKeyboard();
  459. },
  460. );
  461. }
  462. }
  463. clickCustomCharacter() {
  464. if (!isLogin) {
  465. ToastUtil.show("请先登录");
  466. LoginDialog.show();
  467. return;
  468. }
  469. AtmobLog.i(tag, 'clickCustomCharacter');
  470. CustomCharacterAddDialog.show(
  471. currentKeyboardInfo: currentCustomKeyboardInfo,
  472. clickCallback: () {
  473. getKeyboardCharacterList(
  474. keyboardId: _currentCustomKeyboardInfo.value.id ?? "",
  475. isCustom: true,
  476. );
  477. },
  478. );
  479. }
  480. @override
  481. void onClose() {
  482. tabController.dispose();
  483. pageController.dispose();
  484. super.onClose();
  485. }
  486. void saveSuccessGetBack() {
  487. Get.back(result: _currentCustomKeyboardCharacterList);
  488. }
  489. }