refactored timetable
This commit is contained in:
+125
-31
@@ -1,45 +1,139 @@
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../model/accountData.dart';
|
||||
import '../model/endpointData.dart';
|
||||
|
||||
class UserAvatar extends StatelessWidget {
|
||||
class UserAvatar extends StatefulWidget {
|
||||
final String id;
|
||||
final bool isGroup;
|
||||
final int size;
|
||||
const UserAvatar({required this.id, this.isGroup = false, this.size = 20, super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if(isGroup) {
|
||||
return CircleAvatar(
|
||||
foregroundImage: Image(
|
||||
image: CachedNetworkImageProvider(
|
||||
'https://${AccountData().buildHttpAuthString()}@${EndpointData().nextcloud().full()}/ocs/v2.php/apps/spreed/api/v1/room/$id/avatar',
|
||||
errorListener: (p0) {}
|
||||
)
|
||||
).image,
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
onForegroundImageError: (o, t) {},
|
||||
radius: size.toDouble(),
|
||||
child: Icon(Icons.group, size: size.toDouble()),
|
||||
);
|
||||
} else {
|
||||
return CircleAvatar(
|
||||
foregroundImage: Image(
|
||||
image: CachedNetworkImageProvider(
|
||||
'https://${EndpointData().nextcloud().full()}/avatar/$id/$size',
|
||||
errorListener: (p0) {}
|
||||
),
|
||||
).image,
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
onForegroundImageError: (o, t) {},
|
||||
radius: size.toDouble(),
|
||||
child: Icon(Icons.person, size: size.toDouble()),
|
||||
);
|
||||
State<UserAvatar> createState() => _UserAvatarState();
|
||||
}
|
||||
|
||||
class _AvatarPayload {
|
||||
final Uint8List bytes;
|
||||
final bool isSvg;
|
||||
_AvatarPayload(this.bytes, this.isSvg);
|
||||
}
|
||||
|
||||
final Map<String, Future<_AvatarPayload?>> _avatarCache = {};
|
||||
|
||||
class _UserAvatarState extends State<UserAvatar> {
|
||||
late Future<_AvatarPayload?> _payload;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_payload = _load();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(UserAvatar oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.id != widget.id ||
|
||||
oldWidget.isGroup != widget.isGroup ||
|
||||
oldWidget.size != widget.size) {
|
||||
_payload = _load();
|
||||
}
|
||||
}
|
||||
|
||||
String _url() {
|
||||
final host = EndpointData().nextcloud().full();
|
||||
if (widget.isGroup) {
|
||||
return 'https://$host/ocs/v2.php/apps/spreed/api/v1/room/${widget.id}/avatar';
|
||||
}
|
||||
return 'https://$host/avatar/${widget.id}/${widget.size}';
|
||||
}
|
||||
|
||||
Future<_AvatarPayload?> _load() {
|
||||
final url = _url();
|
||||
return _avatarCache.putIfAbsent(url, () => _fetch(url));
|
||||
}
|
||||
|
||||
Future<_AvatarPayload?> _fetch(String url) async {
|
||||
try {
|
||||
final auth = base64Encode(utf8.encode(AccountData().buildHttpAuthString()));
|
||||
final response = await http.get(
|
||||
Uri.parse(url),
|
||||
headers: {
|
||||
'Authorization': 'Basic $auth',
|
||||
'Accept': 'image/png,image/jpeg,image/webp,image/svg+xml',
|
||||
},
|
||||
);
|
||||
if (response.statusCode != 200 || response.bodyBytes.isEmpty) return null;
|
||||
|
||||
final contentType = response.headers['content-type']?.toLowerCase() ?? '';
|
||||
final bytes = response.bodyBytes;
|
||||
final isSvg = contentType.contains('svg') || _looksLikeSvg(bytes);
|
||||
return _AvatarPayload(bytes, isSvg);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static bool _looksLikeSvg(Uint8List bytes) {
|
||||
final head = utf8.decode(
|
||||
bytes.sublist(0, bytes.length < 256 ? bytes.length : 256),
|
||||
allowMalformed: true,
|
||||
).trimLeft();
|
||||
return head.startsWith('<?xml') || head.startsWith('<svg');
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final radius = widget.size.toDouble();
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return FutureBuilder<_AvatarPayload?>(
|
||||
future: _payload,
|
||||
builder: (context, snapshot) {
|
||||
final payload = snapshot.data;
|
||||
|
||||
Widget content;
|
||||
if (payload == null) {
|
||||
content = Icon(
|
||||
widget.isGroup ? Icons.group : Icons.person,
|
||||
size: radius,
|
||||
color: Colors.white,
|
||||
);
|
||||
} else if (payload.isSvg) {
|
||||
content = SvgPicture.memory(
|
||||
payload.bytes,
|
||||
width: radius * 2,
|
||||
height: radius * 2,
|
||||
fit: BoxFit.cover,
|
||||
);
|
||||
} else {
|
||||
content = Image.memory(
|
||||
payload.bytes,
|
||||
width: radius * 2,
|
||||
height: radius * 2,
|
||||
fit: BoxFit.cover,
|
||||
gaplessPlayback: true,
|
||||
);
|
||||
}
|
||||
|
||||
return CircleAvatar(
|
||||
radius: radius,
|
||||
backgroundColor: theme.primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
child: ClipOval(
|
||||
child: SizedBox(
|
||||
width: radius * 2,
|
||||
height: radius * 2,
|
||||
child: content,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user