|
| 1 | +import 'package:collection/collection.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | + |
| 4 | +import '../model/narrow.dart'; |
| 5 | +import '../model/recent_dm_conversations.dart'; |
| 6 | +import 'content.dart'; |
| 7 | +import 'icons.dart'; |
| 8 | +import 'message_list.dart'; |
| 9 | +import 'page.dart'; |
| 10 | +import 'store.dart'; |
| 11 | +import 'text.dart'; |
| 12 | + |
| 13 | +class RecentDmConversationsPage extends StatefulWidget { |
| 14 | + const RecentDmConversationsPage({super.key}); |
| 15 | + |
| 16 | + static Route<void> buildRoute({required BuildContext context}) { |
| 17 | + return MaterialAccountPageRoute(context: context, |
| 18 | + builder: (context) => const RecentDmConversationsPage()); |
| 19 | + } |
| 20 | + |
| 21 | + @override |
| 22 | + State<RecentDmConversationsPage> createState() => _RecentDmConversationsPageState(); |
| 23 | +} |
| 24 | + |
| 25 | +class _RecentDmConversationsPageState extends State<RecentDmConversationsPage> with PerAccountStoreAwareStateMixin<RecentDmConversationsPage> { |
| 26 | + RecentDmConversationsView? model; |
| 27 | + |
| 28 | + @override |
| 29 | + void onNewStore() { |
| 30 | + model?.removeListener(_modelChanged); |
| 31 | + model = PerAccountStoreWidget.of(context).recentDmConversationsView |
| 32 | + ..addListener(_modelChanged); |
| 33 | + } |
| 34 | + |
| 35 | + void _modelChanged() { |
| 36 | + setState(() { |
| 37 | + // The actual state lives in [model]. |
| 38 | + // This method was called because that just changed. |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + Widget _buildItem(BuildContext context, DmNarrow narrow) { |
| 43 | + final allRecipientIds = narrow.allRecipientIds; |
| 44 | + final store = PerAccountStoreWidget.of(context); |
| 45 | + final selfUser = store.users[store.account.userId]!; |
| 46 | + final recipientsSansSelf = allRecipientIds |
| 47 | + .whereNot((id) => id == selfUser.userId) |
| 48 | + .map((id) => store.users[id]!) |
| 49 | + .toList(); |
| 50 | + |
| 51 | + final Widget title; |
| 52 | + final Widget avatar; |
| 53 | + switch (recipientsSansSelf.length) { |
| 54 | + case 0: { |
| 55 | + title = Text(selfUser.fullName); |
| 56 | + avatar = Avatar(userId: selfUser.userId); |
| 57 | + break; |
| 58 | + } |
| 59 | + case 1: { |
| 60 | + final otherUser = recipientsSansSelf.single; |
| 61 | + title = Text(otherUser.fullName); |
| 62 | + avatar = Avatar(userId: otherUser.userId); |
| 63 | + break; |
| 64 | + } |
| 65 | + default: { |
| 66 | + // TODO(i18n): List formatting, like you can do in JavaScript: |
| 67 | + // new Intl.ListFormat('ja').format(['Chris', 'Greg', 'Alya']) |
| 68 | + // // 'Chris、Greg、Alya' |
| 69 | + title = Text(recipientsSansSelf.map((r) => r.fullName).join(', ')); |
| 70 | + avatar = ColoredBox(color: const Color(0x33808080), |
| 71 | + child: Center( |
| 72 | + child: Icon(ZulipIcons.group_dm, color: Colors.black.withOpacity(0.5)))); |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return InkWell( |
| 78 | + onTap: () { |
| 79 | + Navigator.push(context, |
| 80 | + MessageListPage.buildRoute(context: context, narrow: narrow)); |
| 81 | + }, |
| 82 | + child: ConstrainedBox(constraints: const BoxConstraints(minHeight: 48), |
| 83 | + child: Row(crossAxisAlignment: CrossAxisAlignment.center, children: [ |
| 84 | + Padding(padding: const EdgeInsets.fromLTRB(12, 8, 0, 8), |
| 85 | + child: _AvatarWrapper(child: avatar)), |
| 86 | + const SizedBox(width: 8), |
| 87 | + Expanded(child: Padding( |
| 88 | + padding: const EdgeInsets.symmetric(vertical: 4), |
| 89 | + child: DefaultTextStyle( |
| 90 | + style: const TextStyle( |
| 91 | + fontFamily: 'Source Sans 3', |
| 92 | + fontSize: 17, |
| 93 | + height: (20 / 17), |
| 94 | + color: Color(0xFF222222), |
| 95 | + ).merge(weightVariableTextStyle(context)), |
| 96 | + maxLines: 2, |
| 97 | + overflow: TextOverflow.ellipsis, |
| 98 | + child: title), |
| 99 | + )), |
| 100 | + const SizedBox(width: 8), |
| 101 | + // TODO: Unread count |
| 102 | + ]))); |
| 103 | + } |
| 104 | + |
| 105 | + @override |
| 106 | + Widget build(BuildContext context) { |
| 107 | + final sorted = model!.sorted; |
| 108 | + return Scaffold( |
| 109 | + appBar: AppBar(title: const Text('Direct messages')), |
| 110 | + body: ListView.builder( |
| 111 | + itemCount: sorted.length, |
| 112 | + itemBuilder: (context, index) => _buildItem(context, sorted[index]))); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/// Clips and sizes an avatar for a list item in [RecentDmConversationsPage]. |
| 117 | +class _AvatarWrapper extends StatelessWidget { |
| 118 | + const _AvatarWrapper({required this.child}); |
| 119 | + |
| 120 | + final Widget child; |
| 121 | + |
| 122 | + @override |
| 123 | + Widget build(BuildContext context) { |
| 124 | + return SizedBox.square( |
| 125 | + dimension: 32, |
| 126 | + child: ClipRRect( |
| 127 | + borderRadius: const BorderRadius.all(Radius.circular(3)), |
| 128 | + clipBehavior: Clip.antiAlias, |
| 129 | + child: child)); |
| 130 | + } |
| 131 | +} |
0 commit comments