controller.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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:get/get.dart';
  8. import 'package:photo_manager/photo_manager.dart';
  9. import 'package:uuid/uuid.dart';
  10. import '../../data/bean/talks.dart';
  11. import '../../data/repositories/talk_repository.dart';
  12. import '../talk/view.dart';
  13. class AudioPickerController extends BaseController {
  14. final audioList = RxList<AssetEntity>();
  15. AssetPathEntity? currentPath;
  16. final _currentEntity = Rxn<AssetEntity?>();
  17. AssetEntity? get currentEntity => _currentEntity.value;
  18. @override
  19. void onReady() async {
  20. super.onReady();
  21. if (!await AudioPickerUtils.hasPermission()) {
  22. bool permission = await AudioPickerUtils.requestPermissionExtend();
  23. if (!permission) {
  24. ToastUtil.showToast(StringName.authorizationFailed.tr);
  25. return;
  26. }
  27. }
  28. currentPath = await initPathEntity();
  29. requestList();
  30. }
  31. void requestList() {
  32. if (currentPath == null) {
  33. return;
  34. }
  35. AudioPickerUtils.getAssetList(currentPath!, 0).then((value) {
  36. audioList.addAll(value);
  37. });
  38. }
  39. Future<AssetPathEntity?> initPathEntity() async {
  40. List<AssetPathEntity> listEntity =
  41. await AudioPickerUtils.getAssetPathList();
  42. if (listEntity.isEmpty) {
  43. return null;
  44. }
  45. return listEntity.first;
  46. }
  47. void onItemClick(AssetEntity entity) {
  48. _currentEntity.value = entity;
  49. }
  50. void onImportClick() async {
  51. AssetEntity? entity = _currentEntity.value;
  52. if (entity == null) {
  53. ToastUtil.showToast(StringName.pleaseChoiceLocalAudioFile.tr);
  54. return;
  55. }
  56. File? file = await entity.file;
  57. if (file == null) {
  58. ToastUtil.showToast('文件不存在');
  59. return;
  60. }
  61. //上传文件
  62. try {
  63. TalkBean bean = await talkRepository.talkCreate(
  64. const Uuid().v4(), entity.duration,
  65. localAudioUrl:
  66. FileUploadCheckHelper.joinUploadServerAudioTag(entity.id),
  67. uploadType: FileUploadType.local);
  68. Get.back();
  69. TalkPage.start(bean);
  70. } catch (e) {
  71. ToastUtil.showToast(e.toString());
  72. }
  73. }
  74. }