fluffychat/lib/widgets/contacts_list.dart

117 lines
3.4 KiB
Dart
Raw Normal View History

2021-03-27 18:04:31 +01:00
import 'dart:async';
2021-10-26 18:50:34 +02:00
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
2021-10-26 18:50:34 +02:00
import 'package:vrouter/vrouter.dart';
2021-05-22 08:53:52 +02:00
import 'package:fluffychat/widgets/avatar.dart';
import 'package:fluffychat/widgets/matrix.dart';
import '../utils/matrix_sdk_extensions.dart/client_presence_extension.dart';
import '../utils/matrix_sdk_extensions.dart/presence_extension.dart';
2021-03-27 18:04:31 +01:00
class ContactsList extends StatefulWidget {
final TextEditingController searchController;
const ContactsList({
Key key,
@required this.searchController,
}) : super(key: key);
@override
_ContactsState createState() => _ContactsState();
}
class _ContactsState extends State<ContactsList> {
StreamSubscription _onSync;
@override
void dispose() {
_onSync?.cancel();
super.dispose();
}
DateTime _lastSetState = DateTime.now();
Timer _coolDown;
void _updateView() {
_lastSetState = DateTime.now();
setState(() => null);
}
@override
Widget build(BuildContext context) {
final client = Matrix.of(context).client;
_onSync ??= client.onSync.stream.listen((_) {
if (DateTime.now().millisecondsSinceEpoch -
_lastSetState.millisecondsSinceEpoch <
1000) {
_coolDown?.cancel();
2021-10-14 18:09:30 +02:00
_coolDown = Timer(const Duration(seconds: 1), _updateView);
2021-03-27 18:04:31 +01:00
} else {
_updateView();
}
});
final contactList = Matrix.of(context)
.client
.contactList
.where((p) => p.senderId
.toLowerCase()
.contains(widget.searchController.text.toLowerCase()))
.toList();
return ListView.builder(
itemCount: contactList.length,
itemBuilder: (_, i) => _ContactListTile(contact: contactList[i]),
);
}
}
class _ContactListTile extends StatelessWidget {
final Presence contact;
const _ContactListTile({Key key, @required this.contact}) : super(key: key);
@override
Widget build(BuildContext context) {
return FutureBuilder<Profile>(
future:
Matrix.of(context).client.getProfileFromUserId(contact.senderId),
builder: (context, snapshot) {
final displayname =
snapshot.data?.displayName ?? contact.senderId.localpart;
2021-03-27 18:04:31 +01:00
final avatarUrl = snapshot.data?.avatarUrl;
return ListTile(
2021-10-14 18:09:30 +02:00
leading: SizedBox(
2021-03-27 18:04:31 +01:00
width: Avatar.defaultSize,
height: Avatar.defaultSize,
child: Stack(
children: [
Center(child: Avatar(avatarUrl, displayname)),
Align(
alignment: Alignment.bottomRight,
child: Icon(
Icons.circle,
color: contact.color,
size: 12,
),
),
],
),
),
title: Text(displayname),
subtitle: Text(contact.getLocalizedStatusMessage(context),
style: contact.presence.statusMsg?.isNotEmpty ?? false
? TextStyle(
2021-05-24 10:59:00 +02:00
color: Theme.of(context).colorScheme.secondary,
2021-03-27 18:04:31 +01:00
fontWeight: FontWeight.bold,
)
: null),
2021-08-15 13:26:16 +02:00
onTap: () => VRouter.of(context).toSegments([
'rooms',
Matrix.of(context)
.client
.getDirectChatFromUserId(contact.senderId)
]),
2021-03-27 18:04:31 +01:00
);
});
}
}