You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.6 KiB
133 lines
3.6 KiB
import 'dart:io';
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:just_audio/just_audio.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import '../../../core/utils/upload_oss.dart';
|
|
import '../../../data/services/db/sqflite_api.dart';
|
|
|
|
class MeetingController extends GetxController {
|
|
final ScrollController listController = ScrollController();
|
|
final AudioPlayer _audioPlayer = AudioPlayer();
|
|
|
|
// 是否还有更多数据
|
|
bool isLoadingMore = true;
|
|
int page = 1;
|
|
|
|
TextEditingController titleController = TextEditingController(); // 录音标题
|
|
|
|
RxList dataList = [].obs;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
listController.addListener(_onScroll);
|
|
Permission.storage.request();
|
|
_getDataList();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
listController.dispose();
|
|
titleController.dispose();
|
|
_audioPlayer.dispose();
|
|
super.onClose();
|
|
}
|
|
|
|
void _onScroll() {
|
|
if (isLoadingMore &&
|
|
listController.position.pixels >=
|
|
listController.position.maxScrollExtent - 100) {
|
|
page++;
|
|
_getDataList();
|
|
}
|
|
}
|
|
|
|
// 请求本地数据
|
|
void _getDataList() async {
|
|
List list = await SqfliteApi.getMeetingList(page);
|
|
dataList.addAll(list);
|
|
if (list.length < 10) {
|
|
isLoadingMore = false;
|
|
} else {
|
|
isLoadingMore = true;
|
|
}
|
|
}
|
|
|
|
void addAudio() async {
|
|
EasyLoading.show();
|
|
FilePickerResult? result = await FilePicker.platform.pickFiles(
|
|
withData: true,
|
|
);
|
|
if (result != null) {
|
|
PlatformFile file = result.files.first;
|
|
String filePath = await _saveFile(file);
|
|
String fileName = file.name;
|
|
await _audioPlayer.setAudioSource(AudioSource.file(filePath));
|
|
var audiourl =
|
|
await UploadOss.upload(filepath: filePath, rootDir: 'audio');
|
|
int seconds = _audioPlayer.duration?.inSeconds ?? 0;
|
|
int creationTime = DateTime.now().millisecondsSinceEpoch;
|
|
Map<String, Object?> data = {
|
|
'title': fileName,
|
|
'type': 'NOTE',
|
|
'seconds': seconds,
|
|
'filepath': filePath,
|
|
'audiourl': audiourl,
|
|
'creationtime': creationTime,
|
|
};
|
|
final meetingId = await SqfliteApi.insertMeeting(data);
|
|
if (meetingId != null) {
|
|
dataList.insert(0, {
|
|
'id': meetingId,
|
|
...data,
|
|
});
|
|
}
|
|
}
|
|
EasyLoading.dismiss();
|
|
}
|
|
|
|
Future<String> _saveFile(PlatformFile file) async {
|
|
final directory = await getApplicationDocumentsDirectory();
|
|
final String filePath = '${directory.path}/${file.name}';
|
|
final File localFile = File(filePath);
|
|
await localFile.writeAsBytes(file.bytes!);
|
|
return filePath;
|
|
}
|
|
|
|
// 修改标题
|
|
void editMeetingTitle(int id, String title) async {
|
|
if (title.isNotEmpty) {
|
|
SqfliteApi.editMeetingTitle(id, title);
|
|
int index = dataList.indexWhere((item) => item['id'] == id);
|
|
if (index != -1) {
|
|
final dataMap = Map<String, dynamic>.from(dataList[index]);
|
|
dataMap['title'] = title;
|
|
dataList[index] = dataMap;
|
|
}
|
|
}
|
|
Get.back();
|
|
}
|
|
|
|
// 删除数据
|
|
void deleteMeeting(int id, String filepath) async {
|
|
dataList.removeWhere((item) => item['id'] == id);
|
|
// 删除数据库中的记录
|
|
SqfliteApi.deleteMeeting(id);
|
|
_deleteFile(filepath);
|
|
}
|
|
|
|
// 删除文件
|
|
Future<void> _deleteFile(String filepath) async {
|
|
File file = File(filepath);
|
|
bool fileExists = await file.exists();
|
|
if (fileExists) {
|
|
await file.delete();
|
|
}
|
|
}
|
|
}
|
|
|