keyboard_manage_controller.dart 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. RxList<KeyboardInfo> get customKeyboardInfoList => _customKeyboardInfoList;
  25. // 当前自定义键盘
  26. final Rx<KeyboardInfo> _currentCustomKeyboardInfo = KeyboardInfo().obs;
  27. Rx<KeyboardInfo> get currentCustomKeyboardInfo => _currentCustomKeyboardInfo;
  28. //当前自定义键盘人设列表
  29. final RxList<CharacterInfo> _currentCustomKeyboardCharacterList = RxList();
  30. RxList<CharacterInfo> get currentCustomKeyboardCharacterList =>
  31. _currentCustomKeyboardCharacterList;
  32. // 当前自定义键盘亲密度
  33. final RxInt _currentCustomIntimacy = 0.obs;
  34. RxInt get currentCustomIntimacy => _currentCustomIntimacy;
  35. // 当前定制亲密度是否有变化
  36. final RxBool _customIntimacyChanged = false.obs;
  37. RxBool get customIntimacyChanged => _customIntimacyChanged;
  38. final RxBool _customKeyboardCharacterListChanged = false.obs;
  39. RxBool get customKeyboardCharacterListChanged =>
  40. _customKeyboardCharacterListChanged;
  41. // 存储排序前的定制人设列表,用于比较是否有变化
  42. late List<CharacterInfo> _oldCustomCharacterList;
  43. // 通用键盘列表
  44. final RxList<KeyboardInfo> _generalKeyboardInfoList = RxList();
  45. RxList<KeyboardInfo> get generalKeyboardInfoList => _generalKeyboardInfoList;
  46. // 当前通用键盘
  47. final Rx<KeyboardInfo> _currentGeneralKeyboardInfo = KeyboardInfo().obs;
  48. Rx<KeyboardInfo> get currentGeneralKeyboardInfo =>
  49. _currentGeneralKeyboardInfo;
  50. // 当前通用键盘人设列表
  51. final RxList<CharacterInfo> _currentGeneralKeyboardCharacterList = RxList();
  52. RxList<CharacterInfo> get currentGeneralKeyboardCharacterList =>
  53. _currentGeneralKeyboardCharacterList;
  54. // 当前通用键盘亲密度
  55. final RxInt _currentGeneralIntimacy = 0.obs;
  56. RxInt get currentGeneralIntimacy => _currentGeneralIntimacy;
  57. // 当前通用亲密度是否有变化
  58. final RxBool _generalIntimacyChanged = false.obs;
  59. RxBool get generalIntimacyChanged => _generalIntimacyChanged;
  60. final RxBool _generalKeyboardCharacterListChanged = false.obs;
  61. RxBool get generalKeyboardCharacterListChanged =>
  62. _generalKeyboardCharacterListChanged;
  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.value =
  172. keyboardCharacterListResponse.characterInfos;
  173. _oldCustomCharacterList = List<CharacterInfo>.from(
  174. _currentCustomKeyboardCharacterList,
  175. );
  176. _customKeyboardCharacterListChanged.value = false;
  177. _currentCustomKeyboardCharacterList.listen((event) {
  178. _customKeyboardCharacterListChanged.value =
  179. !ListEquality().equals(_oldCustomCharacterList, event);
  180. AtmobLog.d(
  181. tag,
  182. '_customKeyboardCharacterListChanged: $_customKeyboardCharacterListChanged',
  183. );
  184. });
  185. });
  186. } else {
  187. keyboardRepository.getKeyboardCharacterList(keyboardId: keyboardId).then((
  188. keyboardCharacterListResponse,
  189. ) {
  190. AtmobLog.i(
  191. tag,
  192. 'keyboardCharacterListResponse: ${keyboardCharacterListResponse.characterInfos.toString()}',
  193. );
  194. if (_currentGeneralKeyboardInfo.value.id == keyboardId) {
  195. _currentGeneralKeyboardCharacterList.value =
  196. keyboardCharacterListResponse.characterInfos;
  197. _oldGeneralCharacterList = List<CharacterInfo>.from(
  198. _currentGeneralKeyboardCharacterList,
  199. );
  200. _generalKeyboardCharacterListChanged.value = false;
  201. _currentGeneralKeyboardCharacterList.listen((event) {
  202. _generalKeyboardCharacterListChanged.value =
  203. !ListEquality().equals(_oldGeneralCharacterList, event);
  204. AtmobLog.d(
  205. tag,
  206. '_generalKeyboardCharacterListChanged: $_generalKeyboardCharacterListChanged',
  207. );
  208. });
  209. }
  210. if (_currentCustomKeyboardInfo.value.id == keyboardId) {
  211. _currentCustomKeyboardCharacterList.value =
  212. keyboardCharacterListResponse.characterInfos;
  213. }
  214. });
  215. }
  216. }
  217. // 切换当前定制键盘
  218. void switchCustomKeyboard(String? keyboardName) {
  219. _currentCustomKeyboardInfo.value = _customKeyboardInfoList.firstWhere(
  220. (element) => element.name == keyboardName,
  221. orElse: () => _customKeyboardInfoList.first,
  222. );
  223. String? keyboardId = _currentCustomKeyboardInfo.value.id;
  224. _currentCustomIntimacy.value =
  225. _currentCustomKeyboardInfo.value.intimacy ?? 0;
  226. if (keyboardId != null) {
  227. getKeyboardCharacterList(keyboardId: keyboardId, isCustom: true);
  228. }
  229. }
  230. // 切换当前通用键盘
  231. void switchGeneralKeyboard(String? keyboardName) {
  232. _currentGeneralKeyboardInfo.value = _generalKeyboardInfoList.firstWhere(
  233. (element) => element.name == keyboardName,
  234. orElse: () => _generalKeyboardInfoList.first,
  235. );
  236. String? keyboardId = _currentGeneralKeyboardInfo.value.id;
  237. _currentGeneralIntimacy.value =
  238. _currentGeneralKeyboardInfo.value.intimacy ?? 0;
  239. if (keyboardId != null) {
  240. getKeyboardCharacterList(keyboardId: keyboardId, isCustom: false);
  241. }
  242. }
  243. // tab切换
  244. void switchTabKeyboardType(int index) {
  245. // AtmobLog.i(tag, 'onTabChanged: $index');
  246. pageController.animateToPage(
  247. index,
  248. duration: const Duration(milliseconds: 300),
  249. curve: Curves.easeInToLinear,
  250. );
  251. }
  252. // page切换
  253. void switchPageKeyboardType(int index) {
  254. // AtmobLog.i(tag, 'onPageChanged: $index');
  255. tabController.animateTo(index, duration: const Duration(milliseconds: 300));
  256. if (index == 0) {
  257. _currentGeneralIntimacy.value =
  258. _currentGeneralKeyboardInfo.value.intimacy ?? 0;
  259. _currentGeneralKeyboardCharacterList.value = _oldGeneralCharacterList;
  260. _generalIntimacyChanged.value = false;
  261. _generalKeyboardCharacterListChanged.value = false;
  262. getCustomKeyboard();
  263. } else {
  264. _currentCustomIntimacy.value =
  265. _currentCustomKeyboardInfo.value.intimacy ?? 0;
  266. _currentCustomKeyboardCharacterList.value = _oldCustomCharacterList;
  267. _customIntimacyChanged.value = false;
  268. _customKeyboardCharacterListChanged.value = false;
  269. getGeneralKeyboard();
  270. }
  271. }
  272. // 更新亲密度
  273. void updateIntimacy(int intimacy, bool isCustom) {
  274. if (isCustom) {
  275. _currentCustomIntimacy.value = intimacy;
  276. } else {
  277. _currentGeneralIntimacy.value = intimacy;
  278. }
  279. }
  280. // 排序
  281. void onReorder(int oldIndex, int newIndex, bool isCustom) {
  282. if (isCustom) {
  283. reorderList(_currentCustomKeyboardCharacterList, oldIndex, newIndex);
  284. } else {
  285. reorderList(_currentGeneralKeyboardCharacterList, oldIndex, newIndex);
  286. }
  287. }
  288. // 排序
  289. void reorderList<T>(RxList<T> list, int oldIndex, int newIndex) {
  290. AtmobLog.d(tag, 'reorderList: $oldIndex, $newIndex');
  291. final item = list.removeAt(oldIndex);
  292. list.insert(newIndex, item);
  293. }
  294. void clickSave(bool isCustom) {
  295. isCustom
  296. ? saveCustomKeyboardCharacterList()
  297. : saveGeneralKeyboardCharacterList();
  298. }
  299. void saveCustomKeyboardCharacterList() {
  300. if (_customIntimacyChanged.value) {
  301. AtmobLog.i(tag, 'clickSave intimacyChanged');
  302. _currentCustomKeyboardInfo.value.intimacy = currentCustomIntimacy.value;
  303. String? keyboardId = _currentCustomKeyboardInfo.value.id;
  304. if (keyboardId != null) {
  305. keyboardRepository
  306. .updateKeyboardInfo(
  307. keyboardId: keyboardId,
  308. intimacy: currentCustomIntimacy.value,
  309. )
  310. .then((value) {
  311. ToastUtil.show(StringName.keyboardSaveSuccess);
  312. CharacterGroupContentController characterGroupContentController =
  313. Get.find<CharacterGroupContentController>();
  314. characterGroupContentController.refreshData();
  315. })
  316. .catchError((error) {
  317. ToastUtil.show(StringName.keyboardSaveFailed);
  318. _customIntimacyChanged.value = false;
  319. });
  320. }
  321. }
  322. if (_customKeyboardCharacterListChanged.value) {
  323. AtmobLog.i(tag, 'clickSave keyboardChanged');
  324. String? keyboardId = _currentCustomKeyboardInfo.value.id;
  325. if (keyboardId != null) {
  326. List<String> characterIds =
  327. _currentCustomKeyboardCharacterList
  328. .map((e) => e.id)
  329. .toList()
  330. .cast<String>();
  331. keyboardRepository
  332. .keyboardCharacterUpdate(
  333. characterIds: characterIds,
  334. keyboardId: keyboardId,
  335. )
  336. .then((value) {
  337. _oldCustomCharacterList = List<CharacterInfo>.from(
  338. _currentCustomKeyboardCharacterList,
  339. );
  340. CharacterGroupContentController characterGroupContentController =
  341. Get.find<CharacterGroupContentController>();
  342. characterGroupContentController.refreshData();
  343. ToastUtil.show(StringName.keyboardSaveSuccess);
  344. })
  345. .catchError((error) {
  346. ToastUtil.show(StringName.keyboardSaveFailed);
  347. _customKeyboardCharacterListChanged.value = false;
  348. });
  349. }
  350. }
  351. }
  352. void saveGeneralKeyboardCharacterList() {
  353. if (_generalIntimacyChanged.value) {
  354. AtmobLog.i(tag, 'clickSave intimacyChanged');
  355. _currentGeneralKeyboardInfo.value.intimacy = currentGeneralIntimacy.value;
  356. String? keyboardId = _currentGeneralKeyboardInfo.value.id;
  357. if (keyboardId != null) {
  358. keyboardRepository
  359. .updateKeyboardInfo(
  360. keyboardId: keyboardId,
  361. intimacy: currentGeneralIntimacy.value,
  362. )
  363. .then((value) {
  364. ToastUtil.show(StringName.keyboardSaveSuccess);
  365. })
  366. .catchError((error) {
  367. ToastUtil.show(StringName.keyboardSaveFailed);
  368. _generalIntimacyChanged.value = false;
  369. });
  370. }
  371. }
  372. if (_generalKeyboardCharacterListChanged.value) {
  373. AtmobLog.i(tag, 'clickSave keyboardChanged');
  374. String? keyboardId = _currentGeneralKeyboardInfo.value.id;
  375. if (keyboardId != null) {
  376. List<String> characterIds =
  377. _currentGeneralKeyboardCharacterList
  378. .map((e) => e.id)
  379. .toList()
  380. .cast<String>();
  381. keyboardRepository
  382. .keyboardCharacterUpdate(
  383. characterIds: characterIds,
  384. keyboardId: keyboardId,
  385. )
  386. .then((value) {
  387. _oldGeneralCharacterList = List<CharacterInfo>.from(
  388. _currentGeneralKeyboardCharacterList,
  389. );
  390. ToastUtil.show(StringName.keyboardSaveSuccess);
  391. })
  392. .catchError((error) {
  393. ToastUtil.show(StringName.keyboardSaveFailed);
  394. _generalKeyboardCharacterListChanged.value = false;
  395. });
  396. }
  397. }
  398. }
  399. void clickRemoveCharacter(CharacterInfo characterInfo, bool isCustom) {
  400. if (isCustom) {
  401. if (_currentCustomKeyboardCharacterList.length <= _minCount) {
  402. ToastUtil.show("最少需要保持$_minCount个人设");
  403. return;
  404. }
  405. AtmobLog.i(tag, 'clickRemoveCharacter');
  406. _currentCustomKeyboardCharacterList.remove(characterInfo);
  407. } else {
  408. if (_currentGeneralKeyboardCharacterList.length <= _minCount) {
  409. ToastUtil.show("最少需要保持$_minCount个人设");
  410. return;
  411. }
  412. AtmobLog.i(tag, 'clickRemoveCharacter');
  413. _currentGeneralKeyboardCharacterList.remove(characterInfo);
  414. }
  415. }
  416. clickAddCharacter({required bool isCustom}) {
  417. if (isCustom) {
  418. AtmobLog.i(tag, 'clickAddCharacter');
  419. CharacterAddDialog.show(
  420. clickCallback: () {
  421. AtmobLog.i(tag, 'clickAddCharacter');
  422. },
  423. );
  424. } else {
  425. AtmobLog.i(tag, 'clickAddCharacter');
  426. }
  427. }
  428. clickCustomCharacter() {
  429. AtmobLog.i(tag, 'clickCustomCharacter');
  430. }
  431. @override
  432. void onClose() {
  433. tabController.dispose();
  434. pageController.dispose();
  435. super.onClose();
  436. }
  437. }