chore(android) clean up android files sending

This commit is contained in:
ksenia312
2024-02-03 20:05:19 +01:00
parent 695067fc4e
commit 32a9f2a923
8 changed files with 283 additions and 214 deletions
+21 -17
View File
@@ -9,25 +9,12 @@ import 'package:path_provider/path_provider.dart';
class FilesSocket {
FilesSocket.startListening({
required this.content,
required this.listener,
required this.onDestroy,
required WebSocket socket,
required NearbyServiceFilesListener? listener,
required void Function(FilesSocket) onDestroy,
}) : _socket = socket {
_socket.listen(
(event) async {
if (event is List<int>) {
addChunk(event);
} else if (event == separateCommandOf(_currentFileIndex)) {
_futures.add(_createFile(_currentFileIndex));
_currentFileIndex = _currentFileIndex + 1;
_bytesTable['$_currentFileIndex'] = [];
} else if (event == finishCommand) {
await Future.wait(_futures);
Logger.info('Files pack ${content.id} was created');
listener?.onData.call(_files);
onDestroy(this);
}
},
_listener,
onError: listener?.onError,
cancelOnError: listener?.cancelOnError,
onDone: listener?.onDone,
@@ -35,13 +22,15 @@ class FilesSocket {
listener?.onCreated?.call();
}
static const finishCommand = '_@@FINISH_SENDING_FILE_';
static const finishCommand = '_@@FINISH_SENDING_FILES_';
static const separateCommand = '_@@SEPARATE_SENDING_FILE_';
static String separateCommandOf(int index) => '$separateCommand$index';
final NearbyMessageFilesContent content;
final void Function(String) onDestroy;
final NearbyServiceFilesListener? listener;
final WebSocket _socket;
final _files = <NearbyFile>[];
@@ -66,6 +55,21 @@ class FilesSocket {
}
}
Future<void> _listener(dynamic event) async {
if (event is List<int>) {
addChunk(event);
} else if (event == separateCommandOf(_currentFileIndex)) {
_futures.add(_createFile(_currentFileIndex));
_currentFileIndex = _currentFileIndex + 1;
_bytesTable['$_currentFileIndex'] = [];
} else if (event == finishCommand) {
await Future.wait(_futures);
Logger.info('Files pack ${content.id} was created');
listener?.onData.call(_files);
onDestroy(content.id);
}
}
Future<void> _createFile(int index) async {
try {
final bytes = _bytesTable['$index']!;