🐛 Fix attachment displaying according the latest server

This commit is contained in:
LittleSheep 2024-07-29 17:56:36 +08:00
parent 3ca98fa58c
commit 3db6850d89
6 changed files with 65 additions and 32 deletions

View File

@ -12,7 +12,8 @@ class Attachment {
String usage;
String mimetype;
String hash;
String destination;
int destination;
bool isAnalyzed;
Map<String, dynamic>? metadata;
bool isMature;
Account? account;
@ -31,6 +32,7 @@ class Attachment {
required this.mimetype,
required this.hash,
required this.destination,
required this.isAnalyzed,
required this.metadata,
required this.isMature,
required this.account,
@ -50,6 +52,7 @@ class Attachment {
mimetype: json['mimetype'],
hash: json['hash'],
destination: json['destination'],
isAnalyzed: json['is_analyzed'],
metadata: json['metadata'],
isMature: json['is_mature'],
account: json['account'] != null ? Account.fromJson(json['account']) : null,
@ -69,6 +72,7 @@ class Attachment {
'mimetype': mimetype,
'hash': hash,
'destination': destination,
'is_analyzed': isAnalyzed,
'metadata': metadata,
'is_mature': isMature,
'account': account?.toJson(),

View File

@ -6,6 +6,7 @@ import 'dart:typed_data';
import 'package:crypto/crypto.dart';
import 'package:get/get.dart';
import 'package:path/path.dart';
import 'package:solian/models/attachment.dart';
import 'package:solian/platform.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/services.dart';
@ -65,17 +66,24 @@ class AttachmentProvider extends GetConnect {
httpClient.baseUrl = ServiceFinder.buildUrl('files', null);
}
final Map<int, Response> _cachedResponses = {};
final Map<int, Attachment> _cachedResponses = {};
Future<Response> getMetadata(int id, {noCache = false}) async {
Future<Attachment?> getMetadata(int id, {noCache = false}) async {
if (!noCache && _cachedResponses.containsKey(id)) {
return _cachedResponses[id]!;
}
final resp = await get('/attachments/$id/meta');
_cachedResponses[id] = resp;
if (resp.statusCode == 200) {
final result = Attachment.fromJson(resp.body);
if (result.destination != 0 && result.isAnalyzed) {
_cachedResponses[id] = result;
}
print(result);
return result;
}
return resp;
return null;
}
Future<Response> createAttachment(

View File

@ -1,5 +1,6 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:get/get.dart';
import 'package:media_kit/media_kit.dart';
import 'package:media_kit_video/media_kit_video.dart';
@ -83,7 +84,9 @@ class _AttachmentItemState extends State<AttachmentItem> {
onPressed: () {
launchUrlString(
ServiceFinder.buildUrl(
'files', '/attachments/${widget.item.id}'),
'files',
'/attachments/${widget.item.id}',
),
);
},
),
@ -125,14 +128,27 @@ class _AttachmentItemImage extends StatelessWidget {
if (PlatformInfo.canCacheImage)
CachedNetworkImage(
fit: fit,
imageUrl:
ServiceFinder.buildUrl('files', '/attachments/${item.id}'),
progressIndicatorBuilder: (context, url, downloadProgress) =>
Center(
imageUrl: ServiceFinder.buildUrl(
'files',
'/attachments/${item.id}',
),
progressIndicatorBuilder: (context, url, downloadProgress) {
return Center(
child: CircularProgressIndicator(
value: downloadProgress.progress,
),
);
},
errorWidget: (context, url, error) {
return Material(
color: Theme.of(context).colorScheme.surface,
child: Center(
child: const Icon(Icons.close, size: 32)
.animate(onPlay: (e) => e.repeat(reverse: true))
.fade(duration: 500.ms),
),
);
},
)
else
Image.network(
@ -150,6 +166,16 @@ class _AttachmentItemImage extends StatelessWidget {
),
);
},
errorBuilder: (context, error, stackTrace) {
return Material(
color: Theme.of(context).colorScheme.surface,
child: Center(
child: const Icon(Icons.close, size: 32)
.animate(onPlay: (e) => e.repeat(reverse: true))
.fade(duration: 500.ms),
),
);
},
),
if (showBadge && badge != null)
Positioned(

View File

@ -4,6 +4,7 @@ import 'dart:ui';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:dismissible_page/dismissible_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:get/get.dart';
import 'package:solian/models/attachment.dart';
import 'package:solian/widgets/attachments/attachment_item.dart';
@ -43,7 +44,7 @@ class _AttachmentListState extends State<AttachmentList> {
List<Attachment?> _attachmentsMeta = List.empty();
void getMetadataList() {
void _getMetadataList() {
final AttachmentProvider provider = Get.find();
if (widget.attachmentsId.isEmpty) {
@ -56,8 +57,8 @@ class _AttachmentListState extends State<AttachmentList> {
for (var idx = 0; idx < widget.attachmentsId.length; idx++) {
provider.getMetadata(widget.attachmentsId[idx]).then((resp) {
progress++;
if (resp.body != null) {
_attachmentsMeta[idx] = Attachment.fromJson(resp.body);
if (resp != null) {
_attachmentsMeta[idx] = resp;
}
if (progress == widget.attachmentsId.length) {
calculateAspectRatio();
@ -125,7 +126,7 @@ class _AttachmentListState extends State<AttachmentList> {
@override
void initState() {
super.initState();
getMetadataList();
_getMetadataList();
}
@override
@ -234,19 +235,13 @@ class AttachmentListEntry extends StatelessWidget {
Widget build(BuildContext context) {
if (item == null) {
return Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 280),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
child: Icon(
Icons.close,
size: 32,
color: Theme.of(context).colorScheme.onSurface,
),
],
),
),
)
.animate(onPlay: (e) => e.repeat(reverse: true))
.fade(duration: 500.ms),
);
}

View File

@ -226,7 +226,7 @@ class _AttachmentPublishPopupState extends State<AttachmentPublishPopup> {
for (var idx = 0; idx < widget.current.length; idx++) {
provider.getMetadata(widget.current[idx]).then((resp) {
progress++;
_attachments[idx] = Attachment.fromJson(resp.body);
_attachments[idx] = resp;
if (progress == widget.current.length) {
setState(() {
_isBusy = false;

View File

@ -2,7 +2,7 @@ name: solian
description: "The Solar Network App"
publish_to: "none"
version: 1.1.0+50
version: 1.2.0
environment:
sdk: ">=3.3.4 <4.0.0"