controller.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import 'dart:io';
  2. import 'package:electronic_assistant/base/base_controller.dart';
  3. import 'package:electronic_assistant/resource/string.gen.dart';
  4. import 'package:electronic_assistant/utils/audio_picker_utils.dart';
  5. import 'package:electronic_assistant/utils/error_handler.dart';
  6. import 'package:electronic_assistant/utils/file_upload_check_helper.dart';
  7. import 'package:electronic_assistant/utils/toast_util.dart';
  8. import 'package:flutter/cupertino.dart';
  9. import 'package:flutter_screenutil/flutter_screenutil.dart';
  10. import 'package:get/get.dart';
  11. import 'package:photo_manager/photo_manager.dart';
  12. import 'package:pull_to_refresh/pull_to_refresh.dart';
  13. import 'package:uuid/uuid.dart';
  14. import '../../data/bean/talks.dart';
  15. import '../../data/repositories/talk_repository.dart';
  16. import '../../dialog/alert_dialog.dart';
  17. import '../../resource/colors.gen.dart';
  18. import '../talk/view.dart';
  19. class AudioPickerController extends BaseController {
  20. final audioList = RxList<AssetEntity>();
  21. AssetPathEntity? currentPath;
  22. final _currentEntity = Rxn<AssetEntity?>();
  23. AssetEntity? get currentEntity => _currentEntity.value;
  24. final refreshController = RefreshController(initialRefresh: false);
  25. int limit = 20;
  26. int totalCount = 0;
  27. @override
  28. void onReady() async {
  29. super.onReady();
  30. bool isGrant = await checkFilePermission();
  31. if (!isGrant) {
  32. _onRecordPermissionDenied();
  33. return;
  34. }
  35. _initAudioPathList();
  36. }
  37. void _initAudioPathList() async {
  38. currentPath = await initPathEntity();
  39. totalCount = await currentPath?.assetCountAsync ?? 0;
  40. requestList(0, limit, isClearAll: true);
  41. }
  42. Future<bool> checkFilePermission({void Function()? requestPermission}) async {
  43. if (!await AudioPickerUtils.hasPermission()) {
  44. bool isAllow = await _showRequestPermissionDialog();
  45. if (isAllow) {
  46. bool permission = await AudioPickerUtils.requestPermissionExtend();
  47. if (!permission) {
  48. return false;
  49. } else {
  50. requestPermission?.call();
  51. }
  52. } else {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. _onRecordPermissionDenied() {
  59. ToastUtil.showToast(StringName.authorizationFailed.tr);
  60. }
  61. Future<bool> _showRequestPermissionDialog() async {
  62. bool? isAllow = await EAAlertDialog.show(
  63. contentWidget: Container(
  64. margin: EdgeInsets.only(top: 16.h),
  65. child: Text(
  66. textAlign: TextAlign.center,
  67. '是否允许小听获取此设备的存储权限,为您提供转文字、智能总结服务?',
  68. style: TextStyle(
  69. fontWeight: FontWeight.bold,
  70. fontSize: 15.sp,
  71. color: ColorName.primaryTextColor),
  72. ),
  73. ),
  74. cancelText: '禁止',
  75. confirmText: '允许',
  76. cancelOnTap: () {
  77. EAAlertDialog.dismiss(result: false);
  78. },
  79. confirmOnTap: () {
  80. EAAlertDialog.dismiss(result: true);
  81. });
  82. return isAllow ?? false;
  83. }
  84. void requestList(int offset, int limit, {bool? isClearAll = false}) {
  85. if (currentPath == null) {
  86. return;
  87. }
  88. AudioPickerUtils.getAssetListRange(currentPath!, offset, limit)
  89. .then((list) {
  90. if (isClearAll == true) {
  91. audioList.clear();
  92. }
  93. audioList.addAll(list);
  94. if (audioList.length >= totalCount) {
  95. debugPrint("getAssetListRange-没有更多数据了");
  96. refreshController.loadNoData();
  97. } else {
  98. refreshController.loadComplete();
  99. }
  100. refreshController.refreshCompleted();
  101. }).catchError((error) {
  102. debugPrint("requestTalkData-catchError-$error");
  103. refreshController.loadFailed();
  104. refreshController.refreshFailed();
  105. });
  106. }
  107. Future<AssetPathEntity?> initPathEntity() async {
  108. List<AssetPathEntity> listEntity =
  109. await AudioPickerUtils.getAssetPathList();
  110. if (listEntity.isEmpty) {
  111. return null;
  112. }
  113. return listEntity.first;
  114. }
  115. void onItemClick(AssetEntity entity) {
  116. _currentEntity.value = entity;
  117. }
  118. void onImportClick() async {
  119. AssetEntity? entity = _currentEntity.value;
  120. if (entity == null) {
  121. ToastUtil.showToast(StringName.pleaseChoiceLocalAudioFile.tr);
  122. return;
  123. }
  124. File? file = await entity.file;
  125. if (file == null) {
  126. ToastUtil.showToast('文件不存在');
  127. return;
  128. }
  129. //文件格式是否允许
  130. if (!FileUploadCheckHelper.isAllowAudioFile(file.path)) {
  131. ToastUtil.showToast(StringName.audioNotSupportType.tr);
  132. return;
  133. }
  134. //文件大小不能超过500M
  135. if (file.lengthSync() > 500 * 1024 * 1024) {
  136. ToastUtil.showToast(StringName.fileChoiceSizeLimit.tr);
  137. return;
  138. }
  139. //录音时长不能超过5小时不能低于3s
  140. if (entity.duration < 3) {
  141. ToastUtil.showToast(StringName.recordingDurationCannotLessThan3s.tr);
  142. return;
  143. }
  144. if (entity.duration > 5 * 60 * 60) {
  145. ToastUtil.showToast(StringName.fileAudioDurationLimit.tr);
  146. return;
  147. }
  148. //上传文件
  149. try {
  150. TalkBean bean = await talkRepository.talkCreate(
  151. const Uuid().v4(), entity.duration,
  152. localAudioUrl:
  153. FileUploadCheckHelper.joinUploadServerAudioTag(entity.id),
  154. uploadType: FileUploadType.local);
  155. Get.back();
  156. TalkPage.start(bean);
  157. } catch (e) {
  158. ErrorHandler.toastError(e);
  159. }
  160. }
  161. void onLoadMoreData() {
  162. requestList(audioList.length, limit);
  163. }
  164. void pickSystemFile() async {
  165. bool isGrant = await checkFilePermission(requestPermission: () {
  166. _initAudioPathList();
  167. });
  168. if (!isGrant) {
  169. _onRecordPermissionDenied();
  170. return;
  171. }
  172. FileUploadCheckHelper.choicePlatformLocalFileAndCreateOrder(
  173. choiceSuccessCallback: () {
  174. Get.back();
  175. });
  176. }
  177. }