2023-05-01 13:13:40 -04:00
|
|
|
import 'package:contacts_plus_plus/auxiliary.dart';
|
2023-05-06 11:47:36 -04:00
|
|
|
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
2023-05-01 13:13:40 -04:00
|
|
|
import 'package:contacts_plus_plus/models/friend.dart';
|
2023-05-06 11:47:36 -04:00
|
|
|
import 'package:contacts_plus_plus/models/message.dart';
|
2023-05-12 13:29:23 -04:00
|
|
|
import 'package:contacts_plus_plus/widgets/formatted_text.dart';
|
2023-05-07 07:38:45 -04:00
|
|
|
import 'package:contacts_plus_plus/widgets/friends/friend_online_status_indicator.dart';
|
2023-05-01 13:13:40 -04:00
|
|
|
import 'package:contacts_plus_plus/widgets/generic_avatar.dart';
|
2023-05-06 13:24:28 -04:00
|
|
|
import 'package:contacts_plus_plus/widgets/messages/messages_list.dart';
|
2023-04-30 09:43:59 -04:00
|
|
|
import 'package:flutter/material.dart';
|
2023-05-05 12:02:43 -04:00
|
|
|
import 'package:intl/intl.dart';
|
2023-05-06 11:47:36 -04:00
|
|
|
import 'package:provider/provider.dart';
|
2023-04-30 09:43:59 -04:00
|
|
|
|
|
|
|
class FriendListTile extends StatelessWidget {
|
2023-05-12 13:29:23 -04:00
|
|
|
const FriendListTile({required this.friend, required this.unreads, this.onTap, super.key});
|
2023-04-30 09:43:59 -04:00
|
|
|
|
|
|
|
final Friend friend;
|
2023-05-12 13:29:23 -04:00
|
|
|
final int unreads;
|
2023-05-03 14:03:46 -04:00
|
|
|
final Function? onTap;
|
2023-04-30 09:43:59 -04:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-04-30 17:14:29 -04:00
|
|
|
final imageUri = Aux.neosDbToHttp(friend.userProfile.iconUrl);
|
2023-05-03 14:03:46 -04:00
|
|
|
final theme = Theme.of(context);
|
2023-04-30 09:43:59 -04:00
|
|
|
return ListTile(
|
2023-04-30 17:14:29 -04:00
|
|
|
leading: GenericAvatar(imageUri: imageUri,),
|
2023-05-12 13:29:23 -04:00
|
|
|
trailing: unreads != 0
|
2023-05-03 14:03:46 -04:00
|
|
|
? Text("+$unreads", style: theme.textTheme.bodyMedium?.copyWith(color: theme.colorScheme.primary),)
|
|
|
|
: null,
|
2023-05-12 13:29:23 -04:00
|
|
|
title: Row(
|
|
|
|
children: [
|
|
|
|
Text(friend.username),
|
|
|
|
if (friend.isHeadless) Padding(
|
|
|
|
padding: const EdgeInsets.only(left: 8),
|
|
|
|
child: Icon(Icons.dns, size: 12, color: theme.colorScheme.onSecondaryContainer.withAlpha(150),),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2023-05-07 07:38:45 -04:00
|
|
|
subtitle: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
FriendOnlineStatusIndicator(userStatus: friend.userStatus),
|
|
|
|
const SizedBox(width: 4,),
|
|
|
|
Text(toBeginningOfSentenceCase(friend.userStatus.onlineStatus.name) ?? "Unknown"),
|
2023-05-12 13:29:23 -04:00
|
|
|
if (!friend.userStatus.currentSession.isNone)
|
|
|
|
...[
|
|
|
|
const Text(" in "),
|
|
|
|
Expanded(child: FormattedText(friend.userStatus.currentSession.formattedName, overflow: TextOverflow.ellipsis, maxLines: 1,))
|
|
|
|
]
|
2023-05-07 07:38:45 -04:00
|
|
|
],
|
|
|
|
),
|
2023-05-03 14:03:46 -04:00
|
|
|
onTap: () async {
|
2023-05-06 11:47:36 -04:00
|
|
|
onTap?.call();
|
|
|
|
final mClient = Provider.of<MessagingClient>(context, listen: false);
|
|
|
|
mClient.loadUserMessageCache(friend.id);
|
|
|
|
final unreads = mClient.getUnreadsForFriend(friend);
|
|
|
|
if (unreads.isNotEmpty) {
|
|
|
|
final readBatch = MarkReadBatch(
|
2023-05-06 13:24:28 -04:00
|
|
|
senderId: friend.id,
|
2023-05-06 11:47:36 -04:00
|
|
|
ids: unreads.map((e) => e.id).toList(),
|
|
|
|
readTime: DateTime.now(),
|
|
|
|
);
|
|
|
|
mClient.markMessagesRead(readBatch);
|
|
|
|
}
|
|
|
|
mClient.selectedFriend = friend;
|
|
|
|
await Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) =>
|
|
|
|
ChangeNotifierProvider<MessagingClient>.value(
|
|
|
|
value: mClient,
|
|
|
|
child: MessagesList(friend: friend),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
mClient.selectedFriend = null;
|
2023-04-30 09:43:59 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-05-07 07:38:45 -04:00
|
|
|
}
|