intimacy_scale_controller.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import 'package:injectable/injectable.dart';
  2. import 'package:keyboard/base/base_controller.dart';
  3. import 'package:get/get.dart';
  4. import 'package:keyboard/data/repository/keyboard_repository.dart';
  5. import 'package:keyboard/dialog/loading_dialog.dart';
  6. import 'package:keyboard/module/profile/profile_page.dart';
  7. import 'package:keyboard/utils/toast_util.dart';
  8. import '../../data/bean/keyboard_info.dart';
  9. import '../../data/consts/error_code.dart';
  10. import '../../data/consts/event_report.dart';
  11. import '../../handler/event_handler.dart';
  12. import '../../plugins/keyboard_android_platform.dart';
  13. import '../../utils/error_handler.dart';
  14. import '../../utils/http_handler.dart';
  15. import '../../widget/flutter_ruler_picker.dart';
  16. import '../store/store_page.dart';
  17. @injectable
  18. class IntimacyScaleController extends BaseController {
  19. final tag = "IntimacyScaleController";
  20. final KeyboardRepository keyboardRepository;
  21. Rxn<KeyboardInfo> get currentKeyboardInfo =>
  22. keyboardRepository.chooseKeyboardInfo;
  23. late Rx<RulerPickerController> rulerPickerController;
  24. List<RulerRange> ranges = const [RulerRange(begin: 0, end: 100, scale: 1)];
  25. // 当前亲密度
  26. RxInt currentCustomIntimacy = 0.obs;
  27. IntimacyScaleController(this.keyboardRepository);
  28. @override
  29. void onInit() {
  30. super.onInit();
  31. currentCustomIntimacy.value = currentKeyboardInfo.value?.intimacy ?? 30;
  32. rulerPickerController =
  33. RulerPickerController(
  34. value: currentKeyboardInfo.value?.intimacy ?? 30,
  35. ).obs;
  36. rulerPickerController.value.addListener(() {
  37. currentCustomIntimacy.value = rulerPickerController.value.value.toInt();
  38. });
  39. }
  40. @override
  41. void onReady() {
  42. super.onReady();
  43. }
  44. @override
  45. void onClose() {
  46. super.onClose();
  47. }
  48. void clickBack() {
  49. Get.back();
  50. }
  51. void onChangeIntimacy(num value) {
  52. currentCustomIntimacy.value = value.toInt();
  53. }
  54. Future<void> clickSaveButton() async {
  55. EventHandler.report(EventId.event_07001);
  56. if (currentKeyboardInfo.value == null &&
  57. currentKeyboardInfo.value?.id == null) {
  58. ToastUtil.show("请选择ta的键盘");
  59. ProfilePage.start();
  60. return;
  61. }
  62. CustomLoadingDialog.show();
  63. try {
  64. await keyboardRepository.updateKeyboardInfo(
  65. keyboardId: currentKeyboardInfo.value!.id!,
  66. intimacy: currentCustomIntimacy.value,
  67. );
  68. // 通知键盘插件,更新键盘的亲密度值
  69. var keyboardInfo = currentKeyboardInfo.value!;
  70. keyboardInfo.intimacy = currentCustomIntimacy.value;
  71. KeyboardAndroidPlatform.updateKeyboardInfo(keyboardInfo);
  72. await keyboardRepository.refreshData();
  73. Future.delayed(const Duration(milliseconds: 500), () {
  74. KeyboardAndroidPlatform.refreshData();
  75. });
  76. Get.back();
  77. } catch (error) {
  78. CustomLoadingDialog.hide();
  79. if (error is ServerErrorException) {
  80. if (error.code == ErrorCode.noLoginError) {
  81. ErrorHandler.toastError(error);
  82. } else {
  83. ErrorHandler.toastError(error);
  84. }
  85. } else {
  86. ErrorHandler.toastError(error);
  87. }
  88. }
  89. }
  90. }