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