veilidchat/lib/contacts/views/contact_item_widget.dart

94 lines
3.0 KiB
Dart
Raw Normal View History

2023-08-04 01:00:38 -04:00
import 'package:flutter/material.dart';
2023-08-07 00:55:57 -04:00
import 'package:flutter_animate/flutter_animate.dart';
2024-01-30 17:03:14 -05:00
import 'package:flutter_bloc/flutter_bloc.dart';
2023-08-05 23:58:13 -04:00
import 'package:flutter_translate/flutter_translate.dart';
2024-01-30 17:03:14 -05:00
import '../../chat_list/chat_list.dart';
2024-01-30 19:47:22 -05:00
import '../../layout/layout.dart';
2024-01-09 20:58:27 -05:00
import '../../proto/proto.dart' as proto;
import '../../theme/theme.dart';
2024-01-30 17:03:14 -05:00
import '../contacts.dart';
2023-08-04 01:00:38 -04:00
2024-01-09 20:58:27 -05:00
class ContactItemWidget extends StatelessWidget {
2024-02-27 12:45:58 -05:00
const ContactItemWidget(
2024-06-15 23:29:15 -04:00
{required proto.Contact contact, required bool disabled, super.key})
: _disabled = disabled,
_contact = contact;
2023-08-04 01:00:38 -04:00
@override
// ignore: prefer_expression_function_bodies
2024-01-09 20:58:27 -05:00
Widget build(
BuildContext context,
) {
2024-05-27 18:04:00 -04:00
final localConversationRecordKey =
2024-06-15 23:29:15 -04:00
_contact.localConversationRecordKey.toVeilid();
2023-08-07 00:55:57 -04:00
2024-04-10 16:13:08 -04:00
const selected = false; // xxx: eventually when we have selectable contacts:
2024-05-27 18:04:00 -04:00
// activeContactCubit.state == localConversationRecordKey;
2024-04-07 23:16:06 -04:00
2024-06-15 23:29:15 -04:00
final tileDisabled = _disabled || context.watch<ContactListCubit>().isBusy;
late final String title;
late final String subtitle;
if (_contact.nickname.isNotEmpty) {
title = _contact.nickname;
if (_contact.profile.pronouns.isNotEmpty) {
subtitle = '${_contact.profile.name} (${_contact.profile.pronouns})';
} else {
subtitle = _contact.profile.name;
}
} else {
title = _contact.profile.name;
if (_contact.profile.pronouns.isNotEmpty) {
subtitle = '(${_contact.profile.pronouns})';
} else {
subtitle = '';
}
}
2024-01-30 17:03:14 -05:00
return SliderTile(
2024-06-15 23:29:15 -04:00
key: ObjectKey(_contact),
disabled: tileDisabled,
selected: selected,
tileScale: ScaleKind.primary,
2024-06-15 23:29:15 -04:00
title: title,
subtitle: subtitle,
icon: Icons.person,
onTap: () async {
// Start a chat
final chatListCubit = context.read<ChatListCubit>();
2024-01-30 17:03:14 -05:00
2024-06-15 23:29:15 -04:00
await chatListCubit.getOrCreateChatSingleContact(contact: _contact);
// Click over to chats
if (context.mounted) {
await MainPager.of(context)
?.pageController
.animateToPage(1, duration: 250.ms, curve: Curves.easeInOut);
}
},
endActions: [
SliderTileAction(
icon: Icons.delete,
label: translate('button.delete'),
actionScale: ScaleKind.tertiary,
onPressed: (context) async {
final contactListCubit = context.read<ContactListCubit>();
final chatListCubit = context.read<ChatListCubit>();
2023-08-04 01:00:38 -04:00
// Remove any chats for this contact
await chatListCubit.deleteChat(
2024-05-27 18:04:00 -04:00
localConversationRecordKey: localConversationRecordKey);
2023-08-04 01:00:38 -04:00
// Delete the contact itself
2024-06-15 23:29:15 -04:00
await contactListCubit.deleteContact(
localConversationRecordKey: localConversationRecordKey);
})
],
);
2023-08-04 01:00:38 -04:00
}
2024-06-15 23:29:15 -04:00
////////////////////////////////////////////////////////////////////////////
final proto.Contact _contact;
final bool _disabled;
2023-08-04 01:00:38 -04:00
}