| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 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<AssetEntity>();
- AssetPathEntity? currentPath;
- final _currentEntity = Rxn<AssetEntity?>();
- 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<AssetPathEntity?> initPathEntity() async {
- List<AssetPathEntity> 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());
- }
- }
- }
|