controller.dart 4.5 KB

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