просмотр аватарок

This commit is contained in:
Jganenokk
2026-07-05 23:05:45 +07:00
parent 536eea26bd
commit 7ce7cf7a1d
6 changed files with 503 additions and 24 deletions
+29
View File
@@ -62,6 +62,15 @@ class PhoneLookupResult {
const PhoneLookupResult({required this.id, this.name, this.avatarUrl}); const PhoneLookupResult({required this.id, this.name, this.avatarUrl});
} }
class ContactPhotos {
final List<String> urls;
final int total;
const ContactPhotos({required this.urls, required this.total});
static const empty = ContactPhotos(urls: [], total: 0);
}
class ContactsModule { class ContactsModule {
static final ValueNotifier<int> revision = ValueNotifier<int>(0); static final ValueNotifier<int> revision = ValueNotifier<int>(0);
@@ -194,6 +203,26 @@ class ContactsModule {
} }
} }
static Future<ContactPhotos> fetchPhotos(
Api api,
int contactId, {
int from = 0,
int count = 25,
}) async {
final map = await api.sendRequestMap(Opcode.contactPhotos, {
'contactId': contactId,
'from': from,
'count': count,
});
if (map == null) return ContactPhotos.empty;
final rawUrls = map['urls'];
final urls = rawUrls is List
? rawUrls.whereType<String>().toList()
: <String>[];
final total = map['total'] is int ? map['total'] as int : urls.length;
return ContactPhotos(urls: urls, total: total);
}
static Future<List<CachedContact>> getContacts(int accountId) async { static Future<List<CachedContact>> getContacts(int accountId) async {
final rows = await AppDatabase.loadContacts(accountId); final rows = await AppDatabase.loadContacts(accountId);
return rows.map(CachedContact.fromDbRow).toList(); return rows.map(CachedContact.fromDbRow).toList();
+60
View File
@@ -0,0 +1,60 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:photo_manager/photo_manager.dart';
import 'media_cache.dart';
class MediaSaveResult {
final bool ok;
final bool toGallery;
final String? location;
final String? error;
const MediaSaveResult({
required this.ok,
this.toGallery = false,
this.location,
this.error,
});
}
Future<MediaSaveResult> saveImageFromUrl(String url) async {
if (url.isEmpty) {
return const MediaSaveResult(ok: false, error: 'нет ссылки');
}
try {
final cacheName = 'avatar_${url.hashCode & 0x7fffffff}.jpg';
final file = await MediaCache.getOrDownload(cacheName, url);
if (file == null) {
return const MediaSaveResult(ok: false, error: 'не удалось загрузить');
}
final saveName = 'avatar_${DateTime.now().millisecondsSinceEpoch}.jpg';
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
final state = await PhotoManager.requestPermissionExtend();
if (!state.isAuth && !state.hasAccess) {
return const MediaSaveResult(ok: false, error: 'нет доступа к галерее');
}
final bytes = await file.readAsBytes();
await PhotoManager.editor.saveImage(bytes, filename: saveName);
return const MediaSaveResult(ok: true, toGallery: true);
}
final dir = await _targetDirectory();
final target = File('${dir.path}${Platform.pathSeparator}$saveName');
await file.copy(target.path);
return MediaSaveResult(ok: true, location: target.path);
} catch (e) {
return MediaSaveResult(ok: false, error: e.toString());
}
}
Future<Directory> _targetDirectory() async {
try {
final downloads = await getDownloadsDirectory();
if (downloads != null) return downloads;
} catch (_) {}
return getApplicationDocumentsDirectory();
}
@@ -10,6 +10,7 @@ import '../../../core/utils/format.dart';
import '../../../l10n/app_localizations.dart'; import '../../../l10n/app_localizations.dart';
import '../../../models/chat_info.dart'; import '../../../models/chat_info.dart';
import '../../../models/contact_info.dart'; import '../../../models/contact_info.dart';
import '../../widgets/avatar_history_screen.dart';
import '../../widgets/connection_status.dart'; import '../../widgets/connection_status.dart';
import '../../widgets/glossy_pill.dart'; import '../../widgets/glossy_pill.dart';
import '../../widgets/komet_avatar.dart'; import '../../widgets/komet_avatar.dart';
@@ -1130,12 +1131,23 @@ class _ChatInfoScreenState extends State<ChatInfoScreen> {
} }
Widget _avatar() { Widget _avatar() {
return KometAvatar( final avatar = KometAvatar(
name: widget.name, name: widget.name,
imageUrl: widget.imageUrl, imageUrl: widget.imageUrl,
size: 96, size: 96,
fontSize: 36, fontSize: 36,
); );
final peerId = widget.chatType == 'DIALOG' ? _otherId : null;
if (peerId == null || widget.imageUrl.isEmpty) return avatar;
return GestureDetector(
onTap: () => AvatarHistoryScreen.open(
context,
contactId: peerId,
name: widget.name,
currentAvatarUrl: widget.imageUrl,
),
child: avatar,
);
} }
Widget _buildShimmer(ColorScheme cs) { Widget _buildShimmer(ColorScheme cs) {
@@ -7,6 +7,7 @@ import '../../../core/storage/token_storage.dart';
import '../../../core/utils/format.dart'; import '../../../core/utils/format.dart';
import '../../../l10n/app_localizations.dart'; import '../../../l10n/app_localizations.dart';
import '../../../models/contact_info.dart'; import '../../../models/contact_info.dart';
import '../../widgets/avatar_history_screen.dart';
import '../../widgets/custom_notification.dart'; import '../../widgets/custom_notification.dart';
import '../../widgets/glossy_pill.dart'; import '../../widgets/glossy_pill.dart';
import '../../widgets/komet_avatar.dart'; import '../../widgets/komet_avatar.dart';
@@ -146,11 +147,19 @@ class _ContactProfileScreenState extends State<ContactProfileScreen> {
padding: const EdgeInsets.symmetric(horizontal: 16), padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column( child: Column(
children: [ children: [
KometAvatar( GestureDetector(
name: _displayName(), onTap: () => AvatarHistoryScreen.open(
imageUrl: _avatarUrl(), context,
size: 96, contactId: widget.contactId,
fontSize: 36, name: _displayName(),
currentAvatarUrl: _avatarUrl(),
),
child: KometAvatar(
name: _displayName(),
imageUrl: _avatarUrl(),
size: 96,
fontSize: 36,
),
), ),
const SizedBox(height: 14), const SizedBox(height: 14),
_buildNameRow(cs), _buildNameRow(cs),
@@ -206,7 +215,11 @@ class _ContactProfileScreenState extends State<ContactProfileScreen> {
label: l10n.contactProfileActionChat, label: l10n.contactProfileActionChat,
onTap: _openChat, onTap: _openChat,
), ),
(icon: Symbols.notifications, label: l10n.contactProfileActionSound, onTap: null), (
icon: Symbols.notifications,
label: l10n.contactProfileActionSound,
onTap: null,
),
if (!_isBot) if (!_isBot)
(icon: Symbols.call, label: l10n.contactProfileActionCall, onTap: null), (icon: Symbols.call, label: l10n.contactProfileActionCall, onTap: null),
]; ];
@@ -250,17 +263,23 @@ class _ContactProfileScreenState extends State<ContactProfileScreen> {
final phoneStr = formatPhone(c.raw['phone']); final phoneStr = formatPhone(c.raw['phone']);
if (phoneStr != null) { if (phoneStr != null) {
rows.add(_infoRow(cs, Symbols.phone, l10n.contactProfileInfoPhone, phoneStr)); rows.add(
_infoRow(cs, Symbols.phone, l10n.contactProfileInfoPhone, phoneStr),
);
} }
final country = c.raw['country'] as String?; final country = c.raw['country'] as String?;
if (country != null && country.isNotEmpty) { if (country != null && country.isNotEmpty) {
rows.add(_infoRow(cs, Symbols.public, l10n.contactProfileInfoCountry, country)); rows.add(
_infoRow(cs, Symbols.public, l10n.contactProfileInfoCountry, country),
);
} }
final genderStr = formatGender(c.raw['gender']); final genderStr = formatGender(c.raw['gender']);
if (genderStr != null) { if (genderStr != null) {
rows.add(_infoRow(cs, Symbols.wc, l10n.contactProfileInfoGender, genderStr)); rows.add(
_infoRow(cs, Symbols.wc, l10n.contactProfileInfoGender, genderStr),
);
} }
final regTime = c.raw['registrationTime'] as int?; final regTime = c.raw['registrationTime'] as int?;
+23 -14
View File
@@ -11,6 +11,7 @@ import '../../../core/storage/app_database.dart';
import '../../../core/utils/format.dart'; import '../../../core/utils/format.dart';
import '../../../l10n/app_localizations.dart'; import '../../../l10n/app_localizations.dart';
import '../../../main.dart'; import '../../../main.dart';
import '../../widgets/avatar_history_screen.dart';
import '../../widgets/connection_status.dart'; import '../../widgets/connection_status.dart';
import '../../widgets/info_action_sheet.dart'; import '../../widgets/info_action_sheet.dart';
import '../../widgets/komet_avatar.dart'; import '../../widgets/komet_avatar.dart';
@@ -557,21 +558,29 @@ class _SettingsTabState extends State<SettingsTab> {
], ],
), ),
const SizedBox(height: 8), const SizedBox(height: 8),
Container( GestureDetector(
width: 88, onTap: () => AvatarHistoryScreen.open(
height: 88, context,
decoration: BoxDecoration( contactId: _profile?.id ?? 0,
shape: BoxShape.circle,
border: Border.all(
color: cs.primary.withValues(alpha: 0.5),
width: 2.5,
),
),
child: KometAvatar(
name: name, name: name,
imageUrl: _profile?.baseUrl, currentAvatarUrl: _profile?.baseUrl,
size: 88, ),
fontSize: 32, child: Container(
width: 88,
height: 88,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: cs.primary.withValues(alpha: 0.5),
width: 2.5,
),
),
child: KometAvatar(
name: name,
imageUrl: _profile?.baseUrl,
size: 88,
fontSize: 32,
),
), ),
), ),
const SizedBox(height: 8), const SizedBox(height: 8),
@@ -0,0 +1,350 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:material_symbols_icons/symbols.dart';
import '../../backend/modules/contacts.dart';
import '../../core/utils/media_saver.dart';
import '../../main.dart';
import 'custom_notification.dart';
class AvatarHistoryScreen extends StatefulWidget {
final int contactId;
final String? name;
final String? currentAvatarUrl;
const AvatarHistoryScreen({
super.key,
required this.contactId,
this.name,
this.currentAvatarUrl,
});
static Future<void> open(
BuildContext context, {
required int contactId,
String? name,
String? currentAvatarUrl,
}) {
final url = currentAvatarUrl;
if (url == null || url.isEmpty) return Future.value();
return Navigator.of(context).push(
MaterialPageRoute<void>(
fullscreenDialog: true,
builder: (_) => AvatarHistoryScreen(
contactId: contactId,
name: name,
currentAvatarUrl: url,
),
),
);
}
@override
State<AvatarHistoryScreen> createState() => _AvatarHistoryScreenState();
}
class _AvatarHistoryScreenState extends State<AvatarHistoryScreen> {
static const int _pageSize = 50;
static const int _maxDots = 10;
final PageController _pageController = PageController();
String? _current;
final List<String> _history = [];
List<String> _pages = const [];
int _historyTotal = 0;
int _index = 0;
bool _loading = true;
bool _loadingMore = false;
bool _saving = false;
@override
void initState() {
super.initState();
final current = widget.currentAvatarUrl;
_current = (current != null && current.isNotEmpty) ? current : null;
_rebuildPages();
_load();
}
@override
void dispose() {
_pageController.dispose();
super.dispose();
}
void _rebuildPages() {
_pages = _current == null ? List.of(_history) : [_current!, ..._history];
}
void _addHistory(List<String> urls) {
for (final url in urls) {
if (url == _current || _history.contains(url)) continue;
_history.add(url);
}
}
Future<void> _load() async {
final photos = await ContactsModule.fetchPhotos(
api,
widget.contactId,
count: _pageSize,
);
if (!mounted) return;
setState(() {
_addHistory(photos.urls);
_historyTotal = photos.total;
_rebuildPages();
_loading = false;
});
}
Future<void> _loadMore() async {
if (_loadingMore || _history.length >= _historyTotal) return;
_loadingMore = true;
final photos = await ContactsModule.fetchPhotos(
api,
widget.contactId,
from: _history.length,
count: _pageSize,
);
if (!mounted) {
_loadingMore = false;
return;
}
setState(() {
_addHistory(photos.urls);
if (photos.total > _historyTotal) _historyTotal = photos.total;
_rebuildPages();
});
_loadingMore = false;
}
void _onPageChanged(int index) {
setState(() => _index = index);
if (index >= _pages.length - 2) _loadMore();
}
void _prev() {
if (_index <= 0) return;
_pageController.previousPage(
duration: const Duration(milliseconds: 260),
curve: Curves.easeOutCubic,
);
}
void _next() {
if (_index >= _pages.length - 1) return;
_pageController.nextPage(
duration: const Duration(milliseconds: 260),
curve: Curves.easeOutCubic,
);
}
Future<void> _save() async {
if (_saving || _index >= _pages.length) return;
setState(() => _saving = true);
final result = await saveImageFromUrl(_pages[_index]);
if (!mounted) return;
setState(() => _saving = false);
final message = result.ok
? (result.toGallery
? 'Сохранено в галерею'
: 'Сохранено: ${result.location}')
: 'Не удалось сохранить: ${result.error}';
showCustomNotification(context, message);
}
int get _count {
final total = _historyTotal + (_current != null ? 1 : 0);
return total > _pages.length ? total : _pages.length;
}
@override
Widget build(BuildContext context) {
final topPad = MediaQuery.of(context).padding.top;
return Scaffold(
backgroundColor: Colors.black,
body: Stack(
children: [
Positioned.fill(child: _buildBody()),
if (_pages.length > 1 && _index > 0)
_navButton(
alignLeft: true,
icon: Symbols.chevron_left,
onTap: _prev,
),
if (_pages.length > 1 && _index < _pages.length - 1)
_navButton(
alignLeft: false,
icon: Symbols.chevron_right,
onTap: _next,
),
Positioned(
top: 0,
left: 0,
right: 0,
child: Container(
height: topPad + 76,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.black.withValues(alpha: 0.55),
Colors.transparent,
],
),
),
),
),
Positioned(
top: topPad + 4,
left: 4,
right: 4,
child: Row(
children: [
IconButton(
icon: const Icon(Symbols.close, color: Colors.white),
onPressed: () => Navigator.of(context).pop(),
),
Expanded(child: _buildCounter()),
IconButton(
icon: _saving
? const SizedBox(
width: 22,
height: 22,
child: CircularProgressIndicator(
strokeWidth: 2.2,
color: Colors.white,
),
)
: const Icon(Symbols.download, color: Colors.white),
onPressed: _pages.isEmpty || _saving ? null : _save,
),
],
),
),
if (_pages.length > 1 && _pages.length <= _maxDots)
Positioned(
bottom: MediaQuery.of(context).padding.bottom + 18,
left: 0,
right: 0,
child: _buildDots(),
),
],
),
);
}
Widget _buildCounter() {
final hasName = widget.name != null && widget.name!.isNotEmpty;
return Column(
mainAxisSize: MainAxisSize.min,
children: [
if (_pages.length > 1)
Text(
'${_index + 1} из $_count',
style: const TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w600,
),
)
else if (hasName)
Text(
widget.name!,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
if (_pages.length > 1 && hasName)
Text(
widget.name!,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(color: Colors.white70, fontSize: 12),
),
],
);
}
Widget _buildBody() {
if (_pages.isEmpty) {
return Center(
child: _loading
? const CircularProgressIndicator(color: Colors.white)
: const Text(
'Нет фотографий',
style: TextStyle(color: Colors.white54, fontSize: 15),
),
);
}
return PageView.builder(
controller: _pageController,
onPageChanged: _onPageChanged,
itemCount: _pages.length,
itemBuilder: (context, i) => Center(
child: CachedNetworkImage(
imageUrl: _pages[i],
fit: BoxFit.contain,
fadeInDuration: const Duration(milliseconds: 120),
placeholder: (_, _) => const Center(
child: CircularProgressIndicator(color: Colors.white),
),
errorWidget: (_, _, _) =>
const Icon(Symbols.broken_image, color: Colors.white54, size: 64),
),
),
);
}
Widget _navButton({
required bool alignLeft,
required IconData icon,
required VoidCallback onTap,
}) {
return Positioned(
top: 0,
bottom: 0,
left: alignLeft ? 8 : null,
right: alignLeft ? null : 8,
child: Center(
child: Material(
color: Colors.black.withValues(alpha: 0.35),
shape: const CircleBorder(),
child: InkWell(
customBorder: const CircleBorder(),
onTap: onTap,
child: Padding(
padding: const EdgeInsets.all(8),
child: Icon(icon, color: Colors.white, size: 30),
),
),
),
),
);
}
Widget _buildDots() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (var i = 0; i < _pages.length; i++)
AnimatedContainer(
duration: const Duration(milliseconds: 180),
margin: const EdgeInsets.symmetric(horizontal: 3),
width: i == _index ? 8 : 6,
height: i == _index ? 8 : 6,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: i == _index ? Colors.white : Colors.white38,
),
),
],
);
}
}