diff --git a/lib/controllers/post_write_controller.dart b/lib/controllers/post_write_controller.dart index 6f61470..aae9232 100644 --- a/lib/controllers/post_write_controller.dart +++ b/lib/controllers/post_write_controller.dart @@ -1,4 +1,5 @@ import 'dart:io'; +import 'dart:math' as math; import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; @@ -307,12 +308,14 @@ class PostWriteController extends ChangeNotifier { place.$2, onProgress: (progress) { // Calculate overall progress for attachments - progress = ((i + progress) / attachments.length) * kAttachmentProgressWeight; + progress = math.max(((i + progress) / attachments.length) * kAttachmentProgressWeight, progress); notifyListeners(); }, ); + progress = (i + 1) / attachments.length * kAttachmentProgressWeight; attachments[i] = PostWriteMedia(item); + notifyListeners(); } } catch (err) { isBusy = false; diff --git a/lib/providers/sn_attachment.dart b/lib/providers/sn_attachment.dart index ab7142f..1b2a54f 100644 --- a/lib/providers/sn_attachment.dart +++ b/lib/providers/sn_attachment.dart @@ -41,8 +41,7 @@ class SnAttachmentProvider { return out; } - Future> getMultiple(List rids, - {noCache = false}) async { + Future> getMultiple(List rids, {noCache = false}) async { final result = List.filled(rids.length, null); final Map randomMapping = {}; for (int i = 0; i < rids.length; i++) { @@ -63,9 +62,7 @@ class SnAttachmentProvider { 'id': pendingFetch.join(','), }, ); - final out = resp.data['data'] - .map((e) => e['id'] == 0 ? null : SnAttachment.fromJson(e)) - .toList(); + final out = resp.data['data'].map((e) => e['id'] == 0 ? null : SnAttachment.fromJson(e)).toList(); for (final item in out) { if (item == null) continue; @@ -79,10 +76,7 @@ class SnAttachmentProvider { return result; } - static Map mimetypeOverrides = { - 'mov': 'video/quicktime', - 'mp4': 'video/mp4' - }; + static Map mimetypeOverrides = {'mov': 'video/quicktime', 'mp4': 'video/mp4'}; Future directUploadOne( Uint8List data, @@ -93,11 +87,8 @@ class SnAttachmentProvider { Function(double progress)? onProgress, }) async { final filePayload = MultipartFile.fromBytes(data, filename: filename); - final fileAlt = filename.contains('.') - ? filename.substring(0, filename.lastIndexOf('.')) - : filename; - final fileExt = - filename.substring(filename.lastIndexOf('.') + 1).toLowerCase(); + final fileAlt = filename.contains('.') ? filename.substring(0, filename.lastIndexOf('.')) : filename; + final fileExt = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase(); String? mimetypeOverride; if (mimetype != null) { @@ -133,11 +124,8 @@ class SnAttachmentProvider { Map? metadata, { String? mimetype, }) async { - final fileAlt = filename.contains('.') - ? filename.substring(0, filename.lastIndexOf('.')) - : filename; - final fileExt = - filename.substring(filename.lastIndexOf('.') + 1).toLowerCase(); + final fileAlt = filename.contains('.') ? filename.substring(0, filename.lastIndexOf('.')) : filename; + final fileExt = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase(); String? mimetypeOverride; if (mimetype == null && mimetypeOverrides.keys.contains(fileExt)) { @@ -155,10 +143,7 @@ class SnAttachmentProvider { if (mimetypeOverride != null) 'mimetype': mimetypeOverride, }); - return ( - SnAttachment.fromJson(resp.data['meta']), - resp.data['chunk_size'] as int - ); + return (SnAttachment.fromJson(resp.data['meta']), resp.data['chunk_size'] as int); } Future _chunkedUploadOnePart( @@ -200,24 +185,17 @@ class SnAttachmentProvider { (entry.value + 1) * chunkSize, await file.length(), ); - final data = Uint8List.fromList(await file - .openRead(beginCursor, endCursor) - .expand((chunk) => chunk) - .toList()); + final data = Uint8List.fromList(await file.openRead(beginCursor, endCursor).expand((chunk) => chunk).toList()); place = await _chunkedUploadOnePart( data, place.rid, entry.key, - onProgress: (chunkProgress) { - final overallProgress = - (currentTask + chunkProgress) / chunks.length; - if (onProgress != null) { - onProgress(overallProgress); - } - }, ); + final overallProgress = currentTask / chunks.length; + onProgress?.call(overallProgress); + currentTask++; }()); }