controller.dart 3.9 KB

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