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/error_handler.dart'; import 'package:electronic_assistant/utils/file_upload_check_helper.dart'; import 'package:electronic_assistant/utils/http_handler.dart'; import 'package:electronic_assistant/utils/toast_util.dart'; import 'package:flutter/cupertino.dart'; import 'package:get/get.dart'; import 'package:photo_manager/photo_manager.dart'; import 'package:pull_to_refresh/pull_to_refresh.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; final refreshController = RefreshController(initialRefresh: false); int limit = 20; int totalCount = 0; @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(); totalCount = await currentPath?.assetCountAsync ?? 0; requestList(0, limit, isClearAll: true); } void requestList(int offset, int limit, {bool? isClearAll = false}) { if (currentPath == null) { return; } AudioPickerUtils.getAssetListRange(currentPath!, offset, limit) .then((list) { if (isClearAll == true) { audioList.clear(); } audioList.addAll(list); if (audioList.length >= totalCount) { debugPrint("getAssetListRange-没有更多数据了"); refreshController.loadNoData(); } else { refreshController.loadComplete(); } refreshController.refreshCompleted(); }).catchError((error) { debugPrint("requestTalkData-catchError-$error"); refreshController.loadFailed(); refreshController.refreshFailed(); }); } 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; } //文件格式是否允许 if (!FileUploadCheckHelper.isAllowAudioFile(file.path)) { ToastUtil.showToast(StringName.audioNotSupportType.tr); return; } //文件大小不能超过500M if (file.lengthSync() > 500 * 1024 * 1024) { ToastUtil.showToast(StringName.fileChoiceSizeLimit.tr); return; } //录音时长不能超过5小时不能低于3s if (entity.duration < 3) { ToastUtil.showToast(StringName.recordingDurationCannotLessThan3s.tr); return; } if (entity.duration > 5 * 60 * 60) { ToastUtil.showToast(StringName.fileAudioDurationLimit.tr); 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) { ErrorHandler.toastError(e); } } void onLoadMoreData() { requestList(audioList.length, limit); } void pickSystemFile() { FileUploadCheckHelper.choicePlatformLocalFileAndCreateOrder( choiceSuccessCallback: () { Get.back(); }); } }