controller.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import 'package:electronic_assistant/base/base_controller.dart';
  2. import 'package:electronic_assistant/data/bean/talks.dart';
  3. import 'package:electronic_assistant/data/consts/event_report_id.dart';
  4. import 'package:electronic_assistant/data/repositories/account_repository.dart';
  5. import 'package:electronic_assistant/handler/event_handler.dart';
  6. import 'package:electronic_assistant/module/record/constants.dart';
  7. import 'package:electronic_assistant/module/record/record_handler.dart';
  8. import 'package:electronic_assistant/module/store/view.dart';
  9. import 'package:electronic_assistant/module/talk/view.dart';
  10. import 'package:electronic_assistant/utils/desktop_shortcut_utils.dart';
  11. import 'package:electronic_assistant/utils/toast_util.dart';
  12. import 'package:get/get.dart';
  13. import '../../data/api/response/user_info_response.dart';
  14. import '../../data/consts/error_code.dart';
  15. import '../../dialog/alert_dialog.dart';
  16. import '../../router/app_pages.dart';
  17. import '../../utils/de_bounce.dart';
  18. import '../../utils/http_handler.dart';
  19. class RecordController extends BaseController {
  20. Rx<RecordStatus> currentStatus = recordHandler.currentStatus;
  21. RxDouble currentDuration = recordHandler.currentDuration;
  22. final Debounce _saveDebounce = Debounce(debounceTime: 500);
  23. UserInfoResponse? get userInfo => accountRepository.userInfo.value;
  24. final RxBool isHideIntegrationInsufficient = RxBool(false);
  25. @override
  26. void onInit() {
  27. super.onInit();
  28. recordHandler.init();
  29. }
  30. @override
  31. void onReady() async {
  32. super.onReady();
  33. EventHandler.report(EventId.event_100010,
  34. params: {EventId.id: EventId.id_001});
  35. if (!await RecordHandler.hasUnUploadRecord()) {
  36. _startOrContinueRecord();
  37. }
  38. }
  39. @override
  40. void onClose() {
  41. super.onClose();
  42. recordHandler.onClose();
  43. }
  44. RxInt count = 0.obs; // 使用 .obs 将其转换为 Rx 类型
  45. void addShortcut() {
  46. DesktopShortcutUtils.requestAddDesktopShortcut();
  47. }
  48. void onBackClick() {
  49. Get.back();
  50. }
  51. void onActionClick() {
  52. RecordStatus nextStatus = currentStatus.value.nextStatus;
  53. if (nextStatus == RecordStatus.recording) {
  54. _startOrContinueRecord();
  55. } else {
  56. recordHandler.stopRecord();
  57. }
  58. }
  59. void onCancelClick() {
  60. if (currentStatus.value == RecordStatus.pending) {
  61. return;
  62. }
  63. EAAlertDialog.show(
  64. title: "是否删除当前录音?",
  65. confirmText: "删除",
  66. cancelText: "取消",
  67. confirmOnTap: () async {
  68. await recordHandler.deleteCurrentRecord();
  69. EAAlertDialog.dismiss();
  70. Get.back();
  71. },
  72. cancelOnTap: () {
  73. EAAlertDialog.dismiss();
  74. },
  75. );
  76. }
  77. void onSaveClick() {
  78. _saveDebounce.onClick(() {
  79. if (currentStatus.value == RecordStatus.pending) {
  80. return;
  81. }
  82. EventHandler.report(EventId.event_100011);
  83. _saveCurrentRecord();
  84. });
  85. }
  86. Future<void> _startOrContinueRecord() async {
  87. recordHandler.startOrContinueRecord();
  88. }
  89. Future<void> _saveCurrentRecord() async {
  90. try {
  91. TalkBean talkInfo = await recordHandler.saveCurrentRecord();
  92. _dealSuccessNextStep(talkInfo);
  93. } catch (error) {
  94. if (error is ServerErrorException) {
  95. if (error.code == ErrorCode.errorCodeNoLogin) {
  96. ToastUtil.showToast("录音已保存,请登录");
  97. Get.toNamed(RoutePath.login)?.then((loginSuccess) {
  98. loginSuccess != null && loginSuccess ? _saveCurrentRecord() : null;
  99. });
  100. } else {
  101. ToastUtil.showToast("${error.message}");
  102. }
  103. } else {
  104. ToastUtil.showToast("录音已保存,请检查网络并重试");
  105. }
  106. }
  107. }
  108. void _dealSuccessNextStep(TalkBean talkInfo) {
  109. DesktopShortcutUtils.isShowTipsDialog(() {
  110. _routerToTalkPage(talkInfo);
  111. });
  112. }
  113. //跳转至谈话详情界面
  114. void _routerToTalkPage(TalkBean talkInfo) {
  115. Get.back();
  116. TalkPage.start(talkInfo, eventTag: EventId.id_001);
  117. }
  118. void onAvailableTimeClick() {
  119. StorePage.start();
  120. }
  121. void onCloseAvailableTimeClick() {
  122. isHideIntegrationInsufficient.value = true;
  123. }
  124. }