intimacy_scale_controller.dart 2.6 KB

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