profile_controller.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import 'package:get/get_rx/src/rx_types/rx_types.dart';
  2. import 'package:injectable/injectable.dart';
  3. import 'package:keyboard/base/base_controller.dart';
  4. import 'package:keyboard/data/repository/account_repository.dart';
  5. import 'package:keyboard/module/profile/edit/profile_edit_page.dart';
  6. import 'package:keyboard/utils/atmob_log.dart';
  7. import 'package:get/get.dart';
  8. import '../../data/api/response/user_info_response.dart';
  9. import '../../data/bean/keyboard_info.dart';
  10. import '../../data/repository/keyboard_repository.dart';
  11. import '../../utils/error_handler.dart';
  12. import '../../utils/http_handler.dart';
  13. import '../../utils/toast_util.dart';
  14. import '../keyboard_manage/keyboard_manage_controller.dart';
  15. @injectable
  16. class ProfileController extends BaseController {
  17. final tag = "ProfileController";
  18. final KeyboardRepository keyboardRepository;
  19. final AccountRepository accountRepository;
  20. UserInfoResponse? get userInfo => accountRepository.userInfo.value;
  21. int get userGender => userInfo?.gender ?? 0;
  22. final Rx<KeyboardInfo> _currentCustomKeyboardInfo = KeyboardInfo().obs;
  23. KeyboardInfo get currentCustomKeyboardInfo =>
  24. _currentCustomKeyboardInfo.value;
  25. final RxList<KeyboardInfo> _customKeyboardInfoList = RxList();
  26. RxList<KeyboardInfo> get keyboardInfoList => _customKeyboardInfoList;
  27. ProfileController(this.keyboardRepository, this.accountRepository) {
  28. AtmobLog.d(tag, '....init');
  29. }
  30. @override
  31. void onInit() {
  32. super.onInit();
  33. getCustomKeyboard();
  34. }
  35. // 获取定制键盘
  36. Future<void> getCustomKeyboard() async {
  37. AtmobLog.i(tag, 'getCustomKeyboard');
  38. keyboardRepository.getKeyboardList(type: KeyboardType.custom.name).then((
  39. keyboardListResponse,
  40. ) {
  41. AtmobLog.i(
  42. tag,
  43. 'keyboardListResponse: ${keyboardListResponse.keyboardInfos}',
  44. );
  45. _customKeyboardInfoList.value = keyboardListResponse.keyboardInfos;
  46. //检查是否是选择的键盘,如果没有选择的键盘,默认选择第一个
  47. if (_customKeyboardInfoList.isNotEmpty) {
  48. _currentCustomKeyboardInfo.value = _customKeyboardInfoList.firstWhere(
  49. (element) => element.isChoose == true,
  50. orElse: () => _customKeyboardInfoList.first,
  51. );
  52. }
  53. });
  54. }
  55. clickOnChangeKeyboard(KeyboardInfo keyboardInfo) {
  56. AtmobLog.d(tag, 'clickOnChangeKeyboard: ${keyboardInfo.toJson()}');
  57. _currentCustomKeyboardInfo.value = keyboardInfo;
  58. }
  59. clickBack() {
  60. Get.back(result: _currentCustomKeyboardInfo.value);
  61. }
  62. clickAddButton() async {
  63. AtmobLog.d(tag, "clickAddButton");
  64. var result = await ProfileEditPage.start();
  65. if (result == true) {
  66. await getCustomKeyboard();
  67. }
  68. }
  69. clickSaveButton() async {
  70. AtmobLog.d(tag, "clickSaveButton");
  71. final keyboardInfo = _currentCustomKeyboardInfo.value;
  72. if (_currentCustomKeyboardInfo.value.isChoose == true) {
  73. ToastUtil.show("当前键盘已选择");
  74. clickBack();
  75. return;
  76. }
  77. if (keyboardInfo.id?.isNotEmpty == true) {
  78. try {
  79. await keyboardRepository.keyboardChoose(keyboardId: keyboardInfo.id!);
  80. keyboardRepository.refreshData();
  81. ToastUtil.show("当前键盘已选择");
  82. clickBack();
  83. } catch (error) {
  84. if (error is ServerErrorException) {
  85. ErrorHandler.toastError(error);
  86. } else {
  87. AtmobLog.d(tag, " $error");
  88. }
  89. }
  90. }
  91. }
  92. clickAvatar({required bool isUser, KeyboardInfo? keyboardInfo}) async {
  93. AtmobLog.d(tag, "clickAvatar");
  94. if (!isUser) {
  95. var result = await ProfileEditPage.start(keyboardInfo: keyboardInfo);
  96. if (result == true) {
  97. await getCustomKeyboard();
  98. }
  99. }
  100. }
  101. }