forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversation_photo_image.dart
More file actions
145 lines (126 loc) · 4.71 KB
/
Copy pathconversation_photo_image.dart
File metadata and controls
145 lines (126 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:omi/backend/http/api/conversations.dart';
import 'package:omi/backend/schema/conversation.dart';
import 'package:omi/utils/l10n_extensions.dart';
typedef ConversationPhotoStorageFetcher = Future<Uint8List?> Function(String conversationId, String photoId);
/// Caches a photo's storage request for the lifetime of the owning surface.
///
/// Inline photos are decoded without a network request. Terminal misses are
/// cached too, so an unavailable permanent photo does not retry on every build.
class ConversationPhotoBytesCache {
final ConversationPhotoStorageFetcher _fetchStorageImage;
final Map<String, Future<Uint8List?>> _storageRequests = {};
ConversationPhotoBytesCache({ConversationPhotoStorageFetcher? fetchStorageImage})
: _fetchStorageImage = fetchStorageImage ?? getConversationPhotoImage;
Future<Uint8List?> load(ConversationPhoto photo, String? conversationId) {
if (photo.base64.isNotEmpty) {
return Future.value(_decodeInlinePhoto(photo.base64));
}
final normalizedConversationId = conversationId?.trim() ?? '';
final storageId = photo.storageId?.trim() ?? '';
if (normalizedConversationId.isEmpty || storageId.isEmpty) {
return Future.value(null);
}
final cacheKey = '$normalizedConversationId\u0000${photo.id}';
return _storageRequests.putIfAbsent(cacheKey, () => _fetchStorageImageSafely(normalizedConversationId, photo.id));
}
Future<Uint8List?> _fetchStorageImageSafely(String conversationId, String photoId) async {
try {
return await _fetchStorageImage(conversationId, photoId);
} catch (_) {
return null;
}
}
}
Uint8List? _decodeInlinePhoto(String value) {
try {
return base64Decode(value);
} on FormatException {
return null;
}
}
final ConversationPhotoBytesCache _defaultConversationPhotoBytesCache = ConversationPhotoBytesCache();
Future<Uint8List?> loadConversationPhotoBytes(ConversationPhoto photo, String? conversationId) {
return _defaultConversationPhotoBytesCache.load(photo, conversationId);
}
class ConversationPhotoImage extends StatefulWidget {
final ConversationPhoto photo;
final String? conversationId;
final BoxFit fit;
final Color? color;
final BlendMode? colorBlendMode;
final ConversationPhotoBytesCache? cache;
const ConversationPhotoImage({
super.key,
required this.photo,
this.conversationId,
this.fit = BoxFit.cover,
this.color,
this.colorBlendMode,
this.cache,
});
@override
State<ConversationPhotoImage> createState() => _ConversationPhotoImageState();
}
class _ConversationPhotoImageState extends State<ConversationPhotoImage> {
late Future<Uint8List?> _bytesFuture;
ConversationPhotoBytesCache get _cache => widget.cache ?? _defaultConversationPhotoBytesCache;
@override
void initState() {
super.initState();
_bytesFuture = _cache.load(widget.photo, widget.conversationId);
}
@override
void didUpdateWidget(covariant ConversationPhotoImage oldWidget) {
super.didUpdateWidget(oldWidget);
final photoChanged = oldWidget.photo.id != widget.photo.id ||
oldWidget.photo.base64 != widget.photo.base64 ||
oldWidget.photo.storageId != widget.photo.storageId ||
oldWidget.conversationId != widget.conversationId ||
oldWidget.cache != widget.cache;
if (photoChanged) {
_bytesFuture = _cache.load(widget.photo, widget.conversationId);
}
}
@override
Widget build(BuildContext context) {
return FutureBuilder<Uint8List?>(
future: _bytesFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting || snapshot.connectionState == ConnectionState.active) {
return const Center(child: CircularProgressIndicator());
}
final bytes = snapshot.data;
if (bytes == null || bytes.isEmpty) {
return Semantics(
container: true,
excludeSemantics: true,
label: context.l10n.syncStatusFileUnavailable,
child: ColoredBox(
color: Colors.black12,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.image_not_supported_outlined),
const SizedBox(height: 8),
Text(context.l10n.syncStatusFileUnavailable),
],
),
),
),
);
}
return Image.memory(
bytes,
fit: widget.fit,
gaplessPlayback: true,
color: widget.color,
colorBlendMode: widget.colorBlendMode,
);
},
);
}
}