talk_repository.dart 7.3 KB

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