keyboard_manage_controller.dart 17 KB

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