intimacy_scale_controller.dart 2.7 KB

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