intimacy_scale_controller.dart 2.8 KB

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