implemented avatar management for user profiles and chat rooms, including 1:1 cropping, integrated OCS and Spreed avatar APIs, added cache invalidation logic, and updated the account settings view to display user info and profile pictures.
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
|
||||
import 'avatar_crop_page.dart';
|
||||
import 'file_pick.dart';
|
||||
|
||||
/// Result of the user's choice inside [showAvatarActionsSheet]. The sheet
|
||||
/// only collects intent + the (cropped) image bytes — the actual upload /
|
||||
/// delete and any loading state are the caller's responsibility, so failures
|
||||
/// surface in the screen that owns the avatar, not in a transient sheet.
|
||||
sealed class AvatarSheetResult {
|
||||
const AvatarSheetResult();
|
||||
}
|
||||
|
||||
class AvatarUploadResult extends AvatarSheetResult {
|
||||
final Uint8List bytes;
|
||||
const AvatarUploadResult(this.bytes);
|
||||
}
|
||||
|
||||
class AvatarRemoveResult extends AvatarSheetResult {
|
||||
const AvatarRemoveResult();
|
||||
}
|
||||
|
||||
/// Bottom sheet with "from gallery", "take photo" and optional "remove"
|
||||
/// actions. The picker + 1:1 cropper run with the sheet still mounted, so a
|
||||
/// cancelled pick simply returns the user to the sheet. The sheet only pops
|
||||
/// once a concrete result exists (or never, if everything was cancelled).
|
||||
Future<AvatarSheetResult?> showAvatarActionsSheet(
|
||||
BuildContext context, {
|
||||
required bool allowRemove,
|
||||
}) async {
|
||||
AvatarSheetResult? result;
|
||||
await showModalBottomSheet<void>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
showDragHandle: true,
|
||||
useSafeArea: true,
|
||||
builder: (sheetContext) => SafeArea(
|
||||
child: SingleChildScrollView(
|
||||
padding: EdgeInsets.only(
|
||||
bottom: 16 + MediaQuery.viewInsetsOf(sheetContext).bottom,
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
ListTile(
|
||||
leading: const Icon(Icons.photo_library_outlined),
|
||||
title: const Text('Aus Galerie wählen'),
|
||||
onTap: () async {
|
||||
final bytes = await _pickAndCrop(
|
||||
sheetContext,
|
||||
FilePick.singleGalleryPick,
|
||||
);
|
||||
if (bytes == null || !sheetContext.mounted) return;
|
||||
result = AvatarUploadResult(bytes);
|
||||
Navigator.of(sheetContext).pop();
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.photo_camera_outlined),
|
||||
title: const Text('Foto aufnehmen'),
|
||||
onTap: () async {
|
||||
final bytes = await _pickAndCrop(
|
||||
sheetContext,
|
||||
FilePick.cameraPick,
|
||||
);
|
||||
if (bytes == null || !sheetContext.mounted) return;
|
||||
result = AvatarUploadResult(bytes);
|
||||
Navigator.of(sheetContext).pop();
|
||||
},
|
||||
),
|
||||
if (allowRemove) ...[
|
||||
const Divider(),
|
||||
ListTile(
|
||||
leading: Icon(
|
||||
Icons.delete_outline,
|
||||
color: Theme.of(sheetContext).colorScheme.error,
|
||||
),
|
||||
title: Text(
|
||||
'Profilbild entfernen',
|
||||
style: TextStyle(
|
||||
color: Theme.of(sheetContext).colorScheme.error,
|
||||
),
|
||||
),
|
||||
onTap: () {
|
||||
result = const AvatarRemoveResult();
|
||||
Navigator.of(sheetContext).pop();
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<Uint8List?> _pickAndCrop(
|
||||
BuildContext context,
|
||||
Future<XFile?> Function() pick,
|
||||
) async {
|
||||
final picked = await pick();
|
||||
if (picked == null) return null;
|
||||
final bytes = await picked.readAsBytes();
|
||||
if (!context.mounted) return null;
|
||||
return Navigator.of(context).push<Uint8List>(
|
||||
MaterialPageRoute(
|
||||
fullscreenDialog: true,
|
||||
builder: (_) => AvatarCropPage(imageBytes: bytes),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:crop_your_image/crop_your_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'app_progress_indicator.dart';
|
||||
|
||||
/// Full-screen 1:1 cropper. Pure-Flutter so it inherits the app theme and
|
||||
/// MediaQuery insets (no UCrop / native Activity needed). Returns the
|
||||
/// cropped JPEG/PNG bytes via Navigator pop, or `null` on cancel.
|
||||
class AvatarCropPage extends StatefulWidget {
|
||||
final Uint8List imageBytes;
|
||||
const AvatarCropPage({required this.imageBytes, super.key});
|
||||
|
||||
@override
|
||||
State<AvatarCropPage> createState() => _AvatarCropPageState();
|
||||
}
|
||||
|
||||
class _AvatarCropPageState extends State<AvatarCropPage> {
|
||||
final _controller = CropController();
|
||||
bool _busy = false;
|
||||
|
||||
void _confirm() {
|
||||
if (_busy) return;
|
||||
setState(() => _busy = true);
|
||||
_controller.crop();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return Scaffold(
|
||||
backgroundColor: theme.colorScheme.surface,
|
||||
appBar: AppBar(
|
||||
title: const Text('Zuschneiden'),
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
tooltip: 'Abbrechen',
|
||||
onPressed: _busy ? null : () => Navigator.of(context).pop(),
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: _busy
|
||||
? Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: AppProgressIndicator.small(
|
||||
color: theme.colorScheme.onSurface,
|
||||
),
|
||||
)
|
||||
: const Icon(Icons.check),
|
||||
tooltip: 'Bestätigen',
|
||||
onPressed: _busy ? null : _confirm,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
// Pinch-Zoom (interactive: true) lässt zwei-Finger-Gesten direkt am
|
||||
// Bildschirmrand starten und triggert dann die Android-Zurückgeste.
|
||||
// Crop-Rahmen mit Eck-Dots reicht für Avatar-Auswahl völlig aus.
|
||||
child: Crop(
|
||||
image: widget.imageBytes,
|
||||
controller: _controller,
|
||||
aspectRatio: 1.0,
|
||||
interactive: false,
|
||||
baseColor: theme.colorScheme.surface,
|
||||
maskColor: Colors.black.withValues(alpha: 0.6),
|
||||
cornerDotBuilder: (size, _) =>
|
||||
DotControl(color: theme.colorScheme.primary),
|
||||
onCropped: (result) {
|
||||
if (!mounted) return;
|
||||
switch (result) {
|
||||
case CropSuccess(:final croppedImage):
|
||||
Navigator.of(context).pop(croppedImage);
|
||||
case CropFailure():
|
||||
setState(() => _busy = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Bild konnte nicht zugeschnitten werden'),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:photo_view/photo_view.dart';
|
||||
|
||||
import '../model/account_data.dart';
|
||||
import '../model/endpoint_data.dart';
|
||||
import 'user_avatar.dart';
|
||||
|
||||
class LargeProfilePictureView extends StatelessWidget {
|
||||
final String id;
|
||||
@@ -14,14 +14,6 @@ class LargeProfilePictureView extends StatelessWidget {
|
||||
super.key,
|
||||
});
|
||||
|
||||
String _imageUrl() {
|
||||
final host = EndpointData().nextcloud().full();
|
||||
if (isGroup) {
|
||||
return 'https://$host/ocs/v2.php/apps/spreed/api/v1/room/$id/avatar';
|
||||
}
|
||||
return 'https://$host/avatar/$id/1024';
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Scaffold(
|
||||
appBar: AppBar(title: Text(isGroup ? 'Gruppenbild' : 'Profilbild')),
|
||||
@@ -29,7 +21,7 @@ class LargeProfilePictureView extends StatelessWidget {
|
||||
minScale: 0.5,
|
||||
maxScale: 3.0,
|
||||
imageProvider: Image.network(
|
||||
_imageUrl(),
|
||||
avatarUrl(id: id, isGroup: isGroup, size: 1024),
|
||||
headers: {'Authorization': AccountData().getBasicAuthHeader()},
|
||||
).image,
|
||||
backgroundDecoration: BoxDecoration(
|
||||
|
||||
@@ -13,10 +13,18 @@ class UserAvatar extends StatefulWidget {
|
||||
final String id;
|
||||
final bool isGroup;
|
||||
final int size;
|
||||
|
||||
/// Server-side pixel size requested for user avatars. `null` lets the
|
||||
/// widget pick `(size * 4).clamp(64, 1024)` — enough headroom for typical
|
||||
/// device pixel ratios. Group avatars ignore this (Spreed serves one
|
||||
/// fixed-size image per token).
|
||||
final int? requestSize;
|
||||
|
||||
const UserAvatar({
|
||||
required this.id,
|
||||
this.isGroup = false,
|
||||
this.size = 20,
|
||||
this.requestSize,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@@ -46,6 +54,48 @@ final LinkedHashMap<String, _AvatarCacheEntry> _resolvedAvatars =
|
||||
LinkedHashMap<String, _AvatarCacheEntry>();
|
||||
final Map<String, Future<_AvatarPayload?>> _pendingAvatars = {};
|
||||
|
||||
// Bumped by invalidateAvatarCache so *already mounted* avatars re-resolve.
|
||||
// Clearing the cache map alone only affects future mounts — a UserAvatar
|
||||
// elsewhere on screen (chat list, chat header) holds its bytes in State and
|
||||
// would keep showing the stale image until rebuilt. Each state listens here
|
||||
// and re-attaches: invalidated urls miss the cache and re-fetch, the rest
|
||||
// hit the cache and cost nothing.
|
||||
final ValueNotifier<int> _avatarCacheGeneration = ValueNotifier<int>(0);
|
||||
|
||||
String avatarUrl({required String id, required bool isGroup, int size = 512}) {
|
||||
final host = EndpointData().nextcloud().full();
|
||||
if (isGroup) {
|
||||
return 'https://$host/ocs/v2.php/apps/spreed/api/v1/room/$id/avatar';
|
||||
}
|
||||
return 'https://$host/avatar/$id/$size';
|
||||
}
|
||||
|
||||
/// Drops cached avatar bytes so the next mount re-fetches from the server.
|
||||
/// Call after the app uploaded or removed an avatar — without this the
|
||||
/// 30-min TTL would mask the change for the rest of the session.
|
||||
///
|
||||
/// With [id]+[isGroup], invalidates every cached size for that subject
|
||||
/// (user avatars cache per-size URL). Without arguments, clears everything.
|
||||
/// Pending fetches are also discarded so a stale in-flight response can't
|
||||
/// repopulate the cache.
|
||||
void invalidateAvatarCache({String? id, bool? isGroup}) {
|
||||
if (id == null) {
|
||||
_resolvedAvatars.clear();
|
||||
_pendingAvatars.clear();
|
||||
} else if (isGroup == true) {
|
||||
final url = avatarUrl(id: id, isGroup: true);
|
||||
_resolvedAvatars.remove(url);
|
||||
_pendingAvatars.remove(url);
|
||||
} else {
|
||||
// User avatars include the rendered size in the URL — drop every variant.
|
||||
final host = EndpointData().nextcloud().full();
|
||||
final prefix = 'https://$host/avatar/$id/';
|
||||
_resolvedAvatars.removeWhere((url, _) => url.startsWith(prefix));
|
||||
_pendingAvatars.removeWhere((url, _) => url.startsWith(prefix));
|
||||
}
|
||||
_avatarCacheGeneration.value++;
|
||||
}
|
||||
|
||||
_AvatarCacheEntry? _readAvatarCache(String url) {
|
||||
final entry = _resolvedAvatars.remove(url);
|
||||
if (entry == null) return null;
|
||||
@@ -72,6 +122,20 @@ class _UserAvatarState extends State<UserAvatar> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
_attach();
|
||||
_avatarCacheGeneration.addListener(_onCacheInvalidated);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_avatarCacheGeneration.removeListener(_onCacheInvalidated);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// Re-resolve when the cache generation changes. Cache hit → no network and
|
||||
// the visible bytes stay; cache miss (our url was invalidated) → re-fetch.
|
||||
void _onCacheInvalidated() {
|
||||
if (!mounted) return;
|
||||
setState(_attach);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -79,18 +143,20 @@ class _UserAvatarState extends State<UserAvatar> {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.id != widget.id ||
|
||||
oldWidget.isGroup != widget.isGroup ||
|
||||
oldWidget.size != widget.size) {
|
||||
oldWidget.size != widget.size ||
|
||||
oldWidget.requestSize != widget.requestSize) {
|
||||
_attach();
|
||||
}
|
||||
}
|
||||
|
||||
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}';
|
||||
}
|
||||
int _resolvedRequestSize() =>
|
||||
widget.requestSize ?? (widget.size * 4).clamp(64, 1024);
|
||||
|
||||
String _url() => avatarUrl(
|
||||
id: widget.id,
|
||||
isGroup: widget.isGroup,
|
||||
size: _resolvedRequestSize(),
|
||||
);
|
||||
|
||||
void _attach() {
|
||||
final url = _url();
|
||||
|
||||
Reference in New Issue
Block a user