profile_controller.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/dialog/login/login_dialog.dart';
  6. import 'package:keyboard/module/login/login_page.dart';
  7. import 'package:keyboard/module/profile/edit/profile_edit_page.dart';
  8. import 'package:keyboard/module/user_info/user_info_page.dart';
  9. import 'package:keyboard/module/user_profile/user_profile_page.dart';
  10. import 'package:keyboard/utils/atmob_log.dart';
  11. import 'package:get/get.dart';
  12. import 'package:keyboard/widget/platform_util.dart';
  13. import '../../data/api/response/user_info_response.dart';
  14. import '../../data/bean/keyboard_info.dart';
  15. import '../../data/consts/error_code.dart';
  16. import '../../data/repository/keyboard_repository.dart';
  17. import '../../utils/error_handler.dart';
  18. import '../../utils/http_handler.dart';
  19. import '../../utils/toast_util.dart';
  20. import '../keyboard_manage/keyboard_manage_controller.dart';
  21. @injectable
  22. class ProfileController extends BaseController {
  23. final tag = "ProfileController";
  24. final KeyboardRepository keyboardRepository;
  25. final AccountRepository accountRepository;
  26. UserInfoResponse? get userInfo => accountRepository.userInfo.value;
  27. int get userGender => userInfo?.gender ?? 0;
  28. bool get isLogin => accountRepository.isLogin.value;
  29. final Rx<KeyboardInfo> _currentCustomKeyboardInfo = KeyboardInfo().obs;
  30. KeyboardInfo get currentCustomKeyboardInfo =>
  31. _currentCustomKeyboardInfo.value;
  32. RxList<KeyboardInfo> get customKeyboardInfoList =>
  33. keyboardRepository.customKeyboardInfoList;
  34. ProfileController(this.keyboardRepository, this.accountRepository) {
  35. AtmobLog.d(tag, '....init');
  36. }
  37. @override
  38. void onInit() {
  39. super.onInit();
  40. }
  41. @override
  42. void onReady() {
  43. super.onReady();
  44. getCustomKeyboard();
  45. }
  46. Future<void> getCustomKeyboard() async {
  47. keyboardRepository.refreshData();
  48. print('getCustomKeyboard: ${customKeyboardInfoList.length}');
  49. if (keyboardRepository.chooseKeyboardInfo.value == null) {
  50. return;
  51. }
  52. _currentCustomKeyboardInfo.value =
  53. keyboardRepository.chooseKeyboardInfo.value!;
  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. if (error.code == ErrorCode.noLoginError) {
  87. if (PlatformUtil.isIOS) {
  88. LoginPage.start();
  89. } else {
  90. LoginDialog.show();
  91. }
  92. }
  93. } else {
  94. AtmobLog.d(tag, " $error");
  95. }
  96. }
  97. }else {
  98. ToastUtil.show("请选择档案~");
  99. }
  100. }
  101. clickAvatar({required bool isUser, KeyboardInfo? keyboardInfo}) async {
  102. AtmobLog.d(tag, "clickAvatar");
  103. if (!isUser) {
  104. if (isLogin == false) {
  105. ToastUtil.show('请先登录~');
  106. if (PlatformUtil.isIOS) {
  107. LoginPage.start();
  108. } else {
  109. LoginDialog.show();
  110. }
  111. return;
  112. }
  113. var result = await ProfileEditPage.start(keyboardInfo: keyboardInfo);
  114. if (result == true) {
  115. await getCustomKeyboard();
  116. }
  117. } else {
  118. UserProfilePage.start();
  119. }
  120. }
  121. }