⚡ Improve the speed of fetching attachments meta via batch api
This commit is contained in:
parent
0ad4854443
commit
07771e8979
@ -4,6 +4,7 @@ import 'dart:typed_data';
|
|||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:solian/models/attachment.dart';
|
import 'package:solian/models/attachment.dart';
|
||||||
|
import 'package:solian/models/pagination.dart';
|
||||||
import 'package:solian/providers/auth.dart';
|
import 'package:solian/providers/auth.dart';
|
||||||
import 'package:solian/services.dart';
|
import 'package:solian/services.dart';
|
||||||
import 'package:dio/dio.dart' as dio;
|
import 'package:dio/dio.dart' as dio;
|
||||||
@ -21,6 +22,48 @@ class AttachmentProvider extends GetConnect {
|
|||||||
|
|
||||||
final Map<int, Attachment> _cachedResponses = {};
|
final Map<int, Attachment> _cachedResponses = {};
|
||||||
|
|
||||||
|
Future<List<Attachment?>> listMetadata(
|
||||||
|
List<int> id, {
|
||||||
|
noCache = false,
|
||||||
|
}) async {
|
||||||
|
List<Attachment?> result = List.filled(id.length, null);
|
||||||
|
List<int> pendingQuery = List.empty(growable: true);
|
||||||
|
if (!noCache) {
|
||||||
|
for (var idx = 0; idx < id.length; idx++) {
|
||||||
|
if (_cachedResponses.containsKey(id[idx])) {
|
||||||
|
result[idx] = _cachedResponses[id[idx]];
|
||||||
|
} else {
|
||||||
|
pendingQuery.add(id[idx]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final resp = await get(
|
||||||
|
'/attachments?take=${pendingQuery.length}&id=${pendingQuery.join(',')}',
|
||||||
|
);
|
||||||
|
if (resp.statusCode != 200) return result;
|
||||||
|
|
||||||
|
final rawOut = PaginationResult.fromJson(resp.body);
|
||||||
|
if (rawOut.data == null) return result;
|
||||||
|
|
||||||
|
final List<Attachment> out =
|
||||||
|
rawOut.data!.map((x) => Attachment.fromJson(x)).toList();
|
||||||
|
for (final item in out) {
|
||||||
|
if (item.destination != 0 && item.isAnalyzed) {
|
||||||
|
_cachedResponses[item.id] = item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (var i = 0; i < out.length; i++) {
|
||||||
|
for (var j = 0; j < id.length; j++) {
|
||||||
|
if (out[i].id == id[j]) {
|
||||||
|
result[j] = out[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
Future<Attachment?> getMetadata(int id, {noCache = false}) async {
|
Future<Attachment?> getMetadata(int id, {noCache = false}) async {
|
||||||
if (!noCache && _cachedResponses.containsKey(id)) {
|
if (!noCache && _cachedResponses.containsKey(id)) {
|
||||||
return _cachedResponses[id]!;
|
return _cachedResponses[id]!;
|
||||||
|
@ -222,20 +222,14 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
|
|||||||
|
|
||||||
setState(() => _isBusy = true);
|
setState(() => _isBusy = true);
|
||||||
|
|
||||||
int progress = 0;
|
attach.listMetadata(widget.initialAttachments).then((result) {
|
||||||
for (var idx = 0; idx < widget.initialAttachments.length; idx++) {
|
|
||||||
attach.getMetadata(widget.initialAttachments[idx]).then((resp) {
|
|
||||||
progress++;
|
|
||||||
_attachments[idx] = resp;
|
|
||||||
if (progress == widget.initialAttachments.length) {
|
|
||||||
setState(() {
|
setState(() {
|
||||||
|
_attachments = result;
|
||||||
_isBusy = false;
|
_isBusy = false;
|
||||||
_isFirstTimeBusy = false;
|
_isFirstTimeBusy = false;
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void _showAttachmentPreview(Attachment element) {
|
void _showAttachmentPreview(Attachment element) {
|
||||||
context.pushTransparentRoute(
|
context.pushTransparentRoute(
|
||||||
|
@ -45,7 +45,7 @@ class _AttachmentListState extends State<AttachmentList> {
|
|||||||
List<Attachment?> _attachmentsMeta = List.empty();
|
List<Attachment?> _attachmentsMeta = List.empty();
|
||||||
|
|
||||||
void _getMetadataList() {
|
void _getMetadataList() {
|
||||||
final AttachmentProvider provider = Get.find();
|
final AttachmentProvider attach = Get.find();
|
||||||
|
|
||||||
if (widget.attachmentsId.isEmpty) {
|
if (widget.attachmentsId.isEmpty) {
|
||||||
return;
|
return;
|
||||||
@ -53,25 +53,15 @@ class _AttachmentListState extends State<AttachmentList> {
|
|||||||
_attachmentsMeta = List.filled(widget.attachmentsId.length, null);
|
_attachmentsMeta = List.filled(widget.attachmentsId.length, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
int progress = 0;
|
attach.listMetadata(widget.attachmentsId).then((result) {
|
||||||
for (var idx = 0; idx < widget.attachmentsId.length; idx++) {
|
setState(() {
|
||||||
provider.getMetadata(widget.attachmentsId[idx]).then((resp) {
|
_attachmentsMeta = result;
|
||||||
progress++;
|
_isLoading = false;
|
||||||
if (resp != null) {
|
});
|
||||||
_attachmentsMeta[idx] = resp;
|
|
||||||
}
|
|
||||||
if (progress == widget.attachmentsId.length) {
|
|
||||||
calculateAspectRatio();
|
|
||||||
|
|
||||||
if (mounted) {
|
|
||||||
setState(() => _isLoading = false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void calculateAspectRatio() {
|
void _calculateAspectRatio() {
|
||||||
bool isConsistent = true;
|
bool isConsistent = true;
|
||||||
double? consistentValue;
|
double? consistentValue;
|
||||||
int portrait = 0, square = 0, landscape = 0;
|
int portrait = 0, square = 0, landscape = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user