Implementation
Future<TextStreamInfo> sendText(String text, {SendTextOptions? options}) async {
final streamId = Uuid().v4();
final textInBytes = text.codeUnits;
final totalTextLength = textInBytes.length;
final fileIds = options?.attachments.map((f) => Uuid().v4()).toList();
var len = 0;
if (fileIds != null && fileIds.isNotEmpty) {
len = fileIds.length + 1;
} else {
len = 1;
}
final progresses = List<num>.filled(len, 0);
handleProgress(num progress, int idx) {
progresses[idx] = progress;
final totalProgress = progresses.reduce((acc, val) => acc + val);
options?.onProgress?.call(totalProgress.toDouble() / len);
}
final writer = await streamText(StreamTextOptions(
streamId: streamId,
totalSize: totalTextLength,
destinationIdentities: options?.destinationIdentities ?? [],
topic: options?.topic,
attachedStreamIds: fileIds ?? [],
attributes: options?.attributes ?? {},
));
await writer.write(text);
// set text part of progress to 1
handleProgress(1, 0);
await writer.close();
if (options?.attachments != null) {
var idx = 0;
await Future.wait<void>(
options?.attachments.map(
(file) {
final curIdx = idx++;
return _sendFile(
fileIds![curIdx],
file,
SendFileOptions(
topic: options.topic,
mimeType: mime(basename(file.path)),
onProgress: (progress) {
handleProgress(progress, curIdx + 1);
}),
);
},
).toList() ??
[],
);
}
return writer.info;
}