Browse Source

feat(neon_files): increase precision of upload/download tasks

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/556/head
Nikolas Rimikis 1 year ago
parent
commit
3cb05179af
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 6
      packages/neon/neon_files/lib/utils/download_task.dart
  2. 6
      packages/neon/neon_files/lib/utils/upload_task.dart
  3. 6
      packages/neon/neon_files/lib/widgets/browser_view.dart
  4. 6
      packages/neon/neon_files/lib/widgets/file_list_tile.dart

6
packages/neon/neon_files/lib/utils/download_task.dart

@ -7,7 +7,9 @@ class DownloadTask {
final List<String> path; final List<String> path;
final _streamController = StreamController<int>(); final _streamController = StreamController<double>();
/// Upload progress in percent [0, 1].
late final progress = _streamController.stream.asBroadcastStream(); late final progress = _streamController.stream.asBroadcastStream();
Future execute(final NextcloudClient client, final IOSink sink) async { Future execute(final NextcloudClient client, final IOSink sink) async {
@ -20,7 +22,7 @@ class DownloadTask {
sink.add(chunk); sink.add(chunk);
downloaded += chunk.length; downloaded += chunk.length;
_streamController.add((downloaded / response.contentLength * 100).toInt()); _streamController.add(downloaded / response.contentLength);
if (downloaded >= response.contentLength) { if (downloaded >= response.contentLength) {
completer.complete(); completer.complete();

6
packages/neon/neon_files/lib/utils/upload_task.dart

@ -11,7 +11,9 @@ class UploadTask {
final int size; final int size;
final DateTime lastModified; final DateTime lastModified;
final _streamController = StreamController<int>(); final _streamController = StreamController<double>();
/// Upload progress in percent [0, 1].
late final progress = _streamController.stream.asBroadcastStream(); late final progress = _streamController.stream.asBroadcastStream();
Future execute(final NextcloudClient client, final Stream<List<int>> stream) async { Future execute(final NextcloudClient client, final Stream<List<int>> stream) async {
@ -19,7 +21,7 @@ class UploadTask {
await client.webdav.putStream( await client.webdav.putStream(
stream.map((final chunk) { stream.map((final chunk) {
uploaded += chunk.length; uploaded += chunk.length;
_streamController.add((uploaded / size * 100).toInt()); _streamController.add(uploaded / size);
return Uint8List.fromList(chunk); return Uint8List.fromList(chunk);
}), }),

6
packages/neon/neon_files/lib/widgets/browser_view.dart

@ -68,7 +68,7 @@ class _FilesBrowserViewState extends State<FilesBrowserView> {
(final task) => (final task) =>
sorted.where((final file) => _pathMatchesFile(task.path, file.name)).isEmpty, sorted.where((final file) => _pathMatchesFile(task.path, file.name)).isEmpty,
)) ...[ )) ...[
StreamBuilder<int>( StreamBuilder<double>(
stream: uploadTask.progress, stream: uploadTask.progress,
builder: (final context, final uploadTaskProgressSnapshot) => builder: (final context, final uploadTaskProgressSnapshot) =>
!uploadTaskProgressSnapshot.hasData !uploadTaskProgressSnapshot.hasData
@ -95,11 +95,11 @@ class _FilesBrowserViewState extends State<FilesBrowserView> {
final matchingDownloadTasks = downloadTasksSnapshot.requireData final matchingDownloadTasks = downloadTasksSnapshot.requireData
.where((final task) => _pathMatchesFile(task.path, file.name)); .where((final task) => _pathMatchesFile(task.path, file.name));
return StreamBuilder<int?>( return StreamBuilder<double?>(
stream: stream:
matchingUploadTasks.isNotEmpty ? matchingUploadTasks.first.progress : null, matchingUploadTasks.isNotEmpty ? matchingUploadTasks.first.progress : null,
builder: (final context, final uploadTaskProgressSnapshot) => builder: (final context, final uploadTaskProgressSnapshot) =>
StreamBuilder<int?>( StreamBuilder<double?>(
stream: matchingDownloadTasks.isNotEmpty stream: matchingDownloadTasks.isNotEmpty
? matchingDownloadTasks.first.progress ? matchingDownloadTasks.first.progress
: null, : null,

6
packages/neon/neon_files/lib/widgets/file_list_tile.dart

@ -19,8 +19,8 @@ class FileListTile extends StatelessWidget {
final BuildContext context; final BuildContext context;
final FileDetails details; final FileDetails details;
final int? uploadProgress; final double? uploadProgress;
final int? downloadProgress; final double? downloadProgress;
final bool enableFileActions; final bool enableFileActions;
final Function(FileDetails)? onPickFile; final Function(FileDetails)? onPickFile;
@ -33,7 +33,7 @@ class FileListTile extends StatelessWidget {
return null; return null;
} }
return (uploadProgress ?? downloadProgress)! / 100; return (uploadProgress ?? downloadProgress)!;
} }
@override @override

Loading…
Cancel
Save