intimacy_scale_controller.dart 2.3 KB

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