talk_repository.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import 'dart:io';
  2. import 'package:electronic_assistant/data/api/atmob_api.dart';
  3. import 'package:electronic_assistant/data/api/atmob_file_api.dart';
  4. import 'package:electronic_assistant/data/api/request/talk_create_request.dart';
  5. import 'package:electronic_assistant/data/api/request/talk_delete_request.dart';
  6. import 'package:electronic_assistant/data/api/request/talk_file_request.dart';
  7. import 'package:flutter_foreground_task/flutter_foreground_task.dart';
  8. import 'package:get/get.dart';
  9. import '../../utils/foreground_util.dart';
  10. import '../../utils/http_handler.dart';
  11. import '../api/request/talk_generate_request.dart';
  12. import '../api/request/talk_paginate_request.dart';
  13. import '../api/request/talk_rename_request.dart';
  14. import '../api/request/talk_request.dart';
  15. import '../api/response/talk_check_electric_response.dart';
  16. import '../api/response/talk_info_response.dart';
  17. import '../api/response/talk_paginate_response.dart';
  18. import '../bean/talk_original.dart';
  19. import '../bean/talks.dart';
  20. import 'account_repository.dart';
  21. class TalkRepository {
  22. TalkRepository._();
  23. final Set<String> _uploadingTalkIds = {};
  24. final Map<String, RxDouble> _uploadingTalkProgress = {};
  25. final _talkList = RxList<TalkBean>();
  26. RxList<TalkBean> get talkList => _talkList;
  27. bool isUploadingTalk(String talkId) {
  28. return _uploadingTalkIds.contains(talkId);
  29. }
  30. void renovateTalkData(TalkBean talkInfo) {
  31. for (int i = 0; i < _talkList.length; i++) {
  32. if (_talkList[i].id == talkInfo.id) {
  33. _talkList[i].updateBean(talkInfo);
  34. break;
  35. }
  36. }
  37. }
  38. void setTalkList(List<TalkBean> list) {
  39. _talkList.assignAll(list);
  40. }
  41. void addNewTalkData(TalkBean talkInfo) {
  42. _talkList.insert(0, talkInfo);
  43. }
  44. clearTalkList() {
  45. _talkList.clear();
  46. }
  47. Future<TalkPaginateResponse> refreshHomeTalkData({int? sortType = 1}) {
  48. int limit;
  49. if (_talkList.isEmpty) {
  50. limit = 10;
  51. } else {
  52. limit = _talkList.length;
  53. }
  54. return requestTalkPagePaginate(0, limit,
  55. sortType: sortType, isClearAll: true);
  56. }
  57. Future<TalkPaginateResponse> requestTalkPagePaginate(int offset, int limit,
  58. {String? searchKeyword, int? sortType = 1, bool? isClearAll = false}) {
  59. return talkPagePaginate(offset, limit,
  60. searchKeyword: searchKeyword, sortType: sortType)
  61. .then((response) {
  62. if (isClearAll == true) {
  63. _talkList.clear();
  64. }
  65. if (response.list != null) {
  66. _talkList.addAll(response.list!);
  67. }
  68. return response;
  69. });
  70. }
  71. ///sortType 1:按创建时间倒序 2:按更新时间倒序
  72. Future<TalkPaginateResponse> talkPagePaginate(int offset, int limit,
  73. {String? searchKeyword, int? sortType = 1}) {
  74. return atmobApi
  75. .talkPagePaginate(TalkPaginateRequest(offset, limit,
  76. searchKeyword: searchKeyword, sortType: sortType))
  77. .then(HttpHandler.handle(false));
  78. }
  79. Future<List<TalkOriginal>> talkOriginal(String? talkId) {
  80. return atmobApi
  81. .talkOriginal(
  82. TalkRequest(talkId, isExample: !accountRepository.isLogin.value))
  83. .then(HttpHandler.handle(false))
  84. .then((data) {
  85. if (data.list != null) {
  86. return data.list!;
  87. } else {
  88. return [];
  89. }
  90. });
  91. }
  92. // duration 音频时长,单位为秒
  93. Future<TalkCheckElectricResponse> checkElectric(double duration) {
  94. return atmobApi
  95. .checkElectric(TalkGenerateRequest(duration))
  96. .then(HttpHandler.handle(false));
  97. }
  98. Future<TalkInfoResponse> talkInfo(String id) {
  99. return atmobApi
  100. .talkInfo(TalkRequest(id, isExample: !accountRepository.isLogin.value))
  101. .then(HttpHandler.handle(true));
  102. }
  103. Future<void> talkRename(String? id, String? title) {
  104. return atmobApi
  105. .talkRename(TalkRenameRequest(id, title))
  106. .then(HttpHandler.handle(true));
  107. }
  108. Future<void> talkDelete(String? id) {
  109. return atmobApi
  110. .talkDelete(TalkDeleteRequest(id))
  111. .then(HttpHandler.handle(true));
  112. }
  113. Future<TalkBean> talkCreate(String requestId, int duration,
  114. {String? localAudioUrl, int? uploadType}) {
  115. return atmobApi
  116. .talkCreate(TalkCreateRequest(duration, requestId,
  117. localAudioUrl: localAudioUrl, uploadType: uploadType))
  118. .then(HttpHandler.handle(true))
  119. .then((bean) {
  120. //添加新的录音到最新记录
  121. talkRepository.addNewTalkData(bean);
  122. return bean;
  123. });
  124. }
  125. Future<String> uploadTalkFile(String talkId, double duration, File file) {
  126. _uploadingTalkIds.add(talkId);
  127. startForegroundService(
  128. serviceId: talkId.hashCode,
  129. notificationTitle: '正在上传录音',
  130. notificationText: '请勿关闭应用',
  131. callback: setUploadCallback);
  132. _uploadingTalkProgress[talkId] = RxDouble(0);
  133. return atmobFileApi
  134. .uploadTalkFile(TalkFileRequest(talkId, duration, file: file).toJson(),
  135. onSendProgress: (count, total) {
  136. if (_uploadingTalkProgress[talkId] == null) {
  137. _uploadingTalkProgress[talkId] = RxDouble(0);
  138. } else {
  139. _uploadingTalkProgress[talkId]!.value = count / total;
  140. }
  141. })
  142. .then(HttpHandler.handle(true))
  143. .then((response) {
  144. _uploadingTalkIds.remove(talkId);
  145. return response.taskId;
  146. })
  147. .catchError((error) {
  148. _uploadingTalkIds.remove(talkId);
  149. throw error;
  150. })
  151. .whenComplete(() {
  152. stopForegroundService();
  153. });
  154. }
  155. RxDouble getUploadProgress(String talkId) {
  156. if (_uploadingTalkProgress[talkId] == null) {
  157. _uploadingTalkProgress[talkId] = RxDouble(0);
  158. }
  159. return _uploadingTalkProgress[talkId]!;
  160. }
  161. }
  162. class UploadTaskHandler extends TaskHandler {
  163. @override
  164. Future<void> onDestroy(DateTime timestamp) {
  165. // TODO: implement onDestroy
  166. return Future.value();
  167. }
  168. @override
  169. void onRepeatEvent(DateTime timestamp) {
  170. // TODO: implement onRepeatEvent
  171. }
  172. @override
  173. Future<void> onStart(DateTime timestamp, TaskStarter starter) {
  174. // TODO: implement onStart
  175. return Future.value();
  176. }
  177. }
  178. @pragma('vm:entry-point')
  179. void setUploadCallback() {
  180. // The setTaskHandler function must be called to handle the task in the background.
  181. FlutterForegroundTask.setTaskHandler(UploadTaskHandler());
  182. }
  183. final talkRepository = TalkRepository._();