controller.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/http_handler.dart';
  8. import 'package:electronic_assistant/utils/toast_util.dart';
  9. import 'package:flutter/cupertino.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 '../talk/view.dart';
  17. class AudioPickerController extends BaseController {
  18. final audioList = RxList<AssetEntity>();
  19. AssetPathEntity? currentPath;
  20. final _currentEntity = Rxn<AssetEntity?>();
  21. AssetEntity? get currentEntity => _currentEntity.value;
  22. final refreshController = RefreshController(initialRefresh: false);
  23. int limit = 20;
  24. int totalCount = 0;
  25. @override
  26. void onReady() async {
  27. super.onReady();
  28. if (!await AudioPickerUtils.hasPermission()) {
  29. bool permission = await AudioPickerUtils.requestPermissionExtend();
  30. if (!permission) {
  31. ToastUtil.showToast(StringName.authorizationFailed.tr);
  32. return;
  33. }
  34. }
  35. currentPath = await initPathEntity();
  36. totalCount = await currentPath?.assetCountAsync ?? 0;
  37. requestList(0, limit, isClearAll: true);
  38. }
  39. void requestList(int offset, int limit, {bool? isClearAll = false}) {
  40. if (currentPath == null) {
  41. return;
  42. }
  43. AudioPickerUtils.getAssetListRange(currentPath!, offset, limit)
  44. .then((list) {
  45. if (isClearAll == true) {
  46. audioList.clear();
  47. }
  48. audioList.addAll(list);
  49. if (audioList.length >= totalCount) {
  50. debugPrint("getAssetListRange-没有更多数据了");
  51. refreshController.loadNoData();
  52. } else {
  53. refreshController.loadComplete();
  54. }
  55. refreshController.refreshCompleted();
  56. }).catchError((error) {
  57. debugPrint("requestTalkData-catchError-$error");
  58. refreshController.loadFailed();
  59. refreshController.refreshFailed();
  60. });
  61. }
  62. Future<AssetPathEntity?> initPathEntity() async {
  63. List<AssetPathEntity> listEntity =
  64. await AudioPickerUtils.getAssetPathList();
  65. if (listEntity.isEmpty) {
  66. return null;
  67. }
  68. return listEntity.first;
  69. }
  70. void onItemClick(AssetEntity entity) {
  71. _currentEntity.value = entity;
  72. }
  73. void onImportClick() async {
  74. AssetEntity? entity = _currentEntity.value;
  75. if (entity == null) {
  76. ToastUtil.showToast(StringName.pleaseChoiceLocalAudioFile.tr);
  77. return;
  78. }
  79. File? file = await entity.file;
  80. if (file == null) {
  81. ToastUtil.showToast('文件不存在');
  82. return;
  83. }
  84. //文件格式是否允许
  85. if (!FileUploadCheckHelper.isAllowAudioFile(file.path)) {
  86. ToastUtil.showToast(StringName.audioNotSupportType.tr);
  87. return;
  88. }
  89. //文件大小不能超过500M
  90. if (file.lengthSync() > 500 * 1024 * 1024) {
  91. ToastUtil.showToast(StringName.fileChoiceSizeLimit.tr);
  92. return;
  93. }
  94. //录音时长不能超过5小时不能低于3s
  95. if (entity.duration < 3) {
  96. ToastUtil.showToast(StringName.recordingDurationCannotLessThan3s.tr);
  97. return;
  98. }
  99. if (entity.duration > 5 * 60 * 60) {
  100. ToastUtil.showToast(StringName.fileAudioDurationLimit.tr);
  101. return;
  102. }
  103. //上传文件
  104. try {
  105. TalkBean bean = await talkRepository.talkCreate(
  106. const Uuid().v4(), entity.duration,
  107. localAudioUrl:
  108. FileUploadCheckHelper.joinUploadServerAudioTag(entity.id),
  109. uploadType: FileUploadType.local);
  110. Get.back();
  111. TalkPage.start(bean);
  112. } catch (e) {
  113. ErrorHandler.toastError(e);
  114. }
  115. }
  116. void onLoadMoreData() {
  117. requestList(audioList.length, limit);
  118. }
  119. void pickSystemFile() {
  120. FileUploadCheckHelper.choicePlatformLocalFileAndCreateOrder(
  121. choiceSuccessCallback: () {
  122. Get.back();
  123. });
  124. }
  125. }