import 'dart:io'; import 'package:electronic_assistant/base/base_controller.dart'; import 'package:electronic_assistant/resource/string.gen.dart'; import 'package:electronic_assistant/utils/audio_picker_utils.dart'; import 'package:electronic_assistant/utils/file_upload_check_helper.dart'; import 'package:electronic_assistant/utils/toast_util.dart'; import 'package:get/get.dart'; import 'package:photo_manager/photo_manager.dart'; import 'package:uuid/uuid.dart'; import '../../data/bean/talks.dart'; import '../../data/repositories/talk_repository.dart'; import '../talk/view.dart'; class AudioPickerController extends BaseController { final audioList = RxList(); AssetPathEntity? currentPath; final _currentEntity = Rxn(); AssetEntity? get currentEntity => _currentEntity.value; @override void onReady() async { super.onReady(); if (!await AudioPickerUtils.hasPermission()) { bool permission = await AudioPickerUtils.requestPermissionExtend(); if (!permission) { ToastUtil.showToast(StringName.authorizationFailed.tr); return; } } currentPath = await initPathEntity(); requestList(); } void requestList() { if (currentPath == null) { return; } AudioPickerUtils.getAssetList(currentPath!, 0).then((value) { audioList.addAll(value); }); } Future initPathEntity() async { List listEntity = await AudioPickerUtils.getAssetPathList(); if (listEntity.isEmpty) { return null; } return listEntity.first; } void onItemClick(AssetEntity entity) { _currentEntity.value = entity; } void onImportClick() async { AssetEntity? entity = _currentEntity.value; if (entity == null) { ToastUtil.showToast(StringName.pleaseChoiceLocalAudioFile.tr); return; } File? file = await entity.file; if (file == null) { ToastUtil.showToast('文件不存在'); return; } //上传文件 try { TalkBean bean = await talkRepository.talkCreate( const Uuid().v4(), entity.duration, localAudioUrl: FileUploadCheckHelper.joinUploadServerAudioTag(entity.id), uploadType: FileUploadType.local); Get.back(); TalkPage.start(bean); } catch (e) { ToastUtil.showToast(e.toString()); } } }