🐛 Optimize posting progress

This commit is contained in:
LittleSheep 2024-12-22 00:48:06 +08:00
parent 7fc18b40db
commit f5dcf71e10
2 changed files with 16 additions and 35 deletions

View File

@ -1,4 +1,5 @@
import 'dart:io'; import 'dart:io';
import 'dart:math' as math;
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
@ -307,12 +308,14 @@ class PostWriteController extends ChangeNotifier {
place.$2, place.$2,
onProgress: (progress) { onProgress: (progress) {
// Calculate overall progress for attachments // Calculate overall progress for attachments
progress = ((i + progress) / attachments.length) * kAttachmentProgressWeight; progress = math.max(((i + progress) / attachments.length) * kAttachmentProgressWeight, progress);
notifyListeners(); notifyListeners();
}, },
); );
progress = (i + 1) / attachments.length * kAttachmentProgressWeight;
attachments[i] = PostWriteMedia(item); attachments[i] = PostWriteMedia(item);
notifyListeners();
} }
} catch (err) { } catch (err) {
isBusy = false; isBusy = false;

View File

@ -41,8 +41,7 @@ class SnAttachmentProvider {
return out; return out;
} }
Future<List<SnAttachment?>> getMultiple(List<String> rids, Future<List<SnAttachment?>> getMultiple(List<String> rids, {noCache = false}) async {
{noCache = false}) async {
final result = List<SnAttachment?>.filled(rids.length, null); final result = List<SnAttachment?>.filled(rids.length, null);
final Map<String, int> randomMapping = {}; final Map<String, int> randomMapping = {};
for (int i = 0; i < rids.length; i++) { for (int i = 0; i < rids.length; i++) {
@ -63,9 +62,7 @@ class SnAttachmentProvider {
'id': pendingFetch.join(','), 'id': pendingFetch.join(','),
}, },
); );
final out = resp.data['data'] final out = resp.data['data'].map((e) => e['id'] == 0 ? null : SnAttachment.fromJson(e)).toList();
.map((e) => e['id'] == 0 ? null : SnAttachment.fromJson(e))
.toList();
for (final item in out) { for (final item in out) {
if (item == null) continue; if (item == null) continue;
@ -79,10 +76,7 @@ class SnAttachmentProvider {
return result; return result;
} }
static Map<String, String> mimetypeOverrides = { static Map<String, String> mimetypeOverrides = {'mov': 'video/quicktime', 'mp4': 'video/mp4'};
'mov': 'video/quicktime',
'mp4': 'video/mp4'
};
Future<SnAttachment> directUploadOne( Future<SnAttachment> directUploadOne(
Uint8List data, Uint8List data,
@ -93,11 +87,8 @@ class SnAttachmentProvider {
Function(double progress)? onProgress, Function(double progress)? onProgress,
}) async { }) async {
final filePayload = MultipartFile.fromBytes(data, filename: filename); final filePayload = MultipartFile.fromBytes(data, filename: filename);
final fileAlt = filename.contains('.') final fileAlt = filename.contains('.') ? filename.substring(0, filename.lastIndexOf('.')) : filename;
? filename.substring(0, filename.lastIndexOf('.')) final fileExt = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
: filename;
final fileExt =
filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
String? mimetypeOverride; String? mimetypeOverride;
if (mimetype != null) { if (mimetype != null) {
@ -133,11 +124,8 @@ class SnAttachmentProvider {
Map<String, dynamic>? metadata, { Map<String, dynamic>? metadata, {
String? mimetype, String? mimetype,
}) async { }) async {
final fileAlt = filename.contains('.') final fileAlt = filename.contains('.') ? filename.substring(0, filename.lastIndexOf('.')) : filename;
? filename.substring(0, filename.lastIndexOf('.')) final fileExt = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
: filename;
final fileExt =
filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
String? mimetypeOverride; String? mimetypeOverride;
if (mimetype == null && mimetypeOverrides.keys.contains(fileExt)) { if (mimetype == null && mimetypeOverrides.keys.contains(fileExt)) {
@ -155,10 +143,7 @@ class SnAttachmentProvider {
if (mimetypeOverride != null) 'mimetype': mimetypeOverride, if (mimetypeOverride != null) 'mimetype': mimetypeOverride,
}); });
return ( return (SnAttachment.fromJson(resp.data['meta']), resp.data['chunk_size'] as int);
SnAttachment.fromJson(resp.data['meta']),
resp.data['chunk_size'] as int
);
} }
Future<SnAttachment> _chunkedUploadOnePart( Future<SnAttachment> _chunkedUploadOnePart(
@ -200,24 +185,17 @@ class SnAttachmentProvider {
(entry.value + 1) * chunkSize, (entry.value + 1) * chunkSize,
await file.length(), await file.length(),
); );
final data = Uint8List.fromList(await file final data = Uint8List.fromList(await file.openRead(beginCursor, endCursor).expand((chunk) => chunk).toList());
.openRead(beginCursor, endCursor)
.expand((chunk) => chunk)
.toList());
place = await _chunkedUploadOnePart( place = await _chunkedUploadOnePart(
data, data,
place.rid, place.rid,
entry.key, entry.key,
onProgress: (chunkProgress) {
final overallProgress =
(currentTask + chunkProgress) / chunks.length;
if (onProgress != null) {
onProgress(overallProgress);
}
},
); );
final overallProgress = currentTask / chunks.length;
onProgress?.call(overallProgress);
currentTask++; currentTask++;
}()); }());
} }