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