OpenContacts/lib/widgets/friends_list.dart

170 lines
5.8 KiB
Dart
Raw Normal View History

2023-04-30 09:43:59 -04:00
import 'dart:async';
2023-05-01 13:13:40 -04:00
import 'package:contacts_plus_plus/api_client.dart';
import 'package:contacts_plus_plus/apis/friend_api.dart';
2023-05-03 14:03:46 -04:00
import 'package:contacts_plus_plus/apis/message_api.dart';
2023-05-01 13:13:40 -04:00
import 'package:contacts_plus_plus/models/friend.dart';
2023-05-03 14:03:46 -04:00
import 'package:contacts_plus_plus/models/message.dart';
2023-05-01 13:13:40 -04:00
import 'package:contacts_plus_plus/widgets/expanding_input_fab.dart';
import 'package:contacts_plus_plus/widgets/friend_list_tile.dart';
import 'package:contacts_plus_plus/widgets/settings_page.dart';
2023-04-29 13:18:46 -04:00
import 'package:flutter/material.dart';
class FriendsList extends StatefulWidget {
const FriendsList({super.key});
2023-04-29 13:18:46 -04:00
@override
State<FriendsList> createState() => _FriendsListState();
2023-04-29 13:18:46 -04:00
}
class _FriendsListState extends State<FriendsList> {
2023-05-03 12:43:06 -04:00
Future<List<Friend>>? _friendsFuture;
2023-04-30 07:39:09 -04:00
ClientHolder? _clientHolder;
2023-04-30 09:43:59 -04:00
Timer? _debouncer;
2023-05-03 12:43:06 -04:00
String _searchFilter = "";
2023-05-03 14:03:46 -04:00
final _unreads = <String, List<Message>>{};
2023-04-30 09:43:59 -04:00
@override
void dispose() {
_debouncer?.cancel();
super.dispose();
}
2023-04-29 15:26:12 -04:00
@override
2023-04-30 07:39:09 -04:00
void didChangeDependencies() {
super.didChangeDependencies();
final clientHolder = ClientHolder.of(context);
if (_clientHolder != clientHolder) {
_clientHolder = clientHolder;
_refreshFriendsList();
}
2023-04-29 15:26:12 -04:00
}
void _refreshFriendsList() {
2023-05-03 14:03:46 -04:00
_friendsFuture = FriendApi.getFriendsList(_clientHolder!.client).then((Iterable<Friend> value) async {
final unreadMessages = await MessageApi.getUserMessages(_clientHolder!.client, unreadOnly: true);
_unreads.clear();
for (final msg in unreadMessages) {
if (msg.senderId != _clientHolder!.client.userId) {
final value = _unreads[msg.senderId];
if (value == null) {
_unreads[msg.senderId] = [msg];
} else {
value.add(msg);
}
2023-04-29 15:26:12 -04:00
}
2023-05-03 14:03:46 -04:00
}
final friends = value.toList()
..sort((a, b) {
var aVal = _unreads.containsKey(a.id) ? -3 : 0;
var bVal = _unreads.containsKey(b.id) ? -3 : 0;
aVal -= a.userStatus.lastStatusChange.compareTo(b.userStatus.lastStatusChange);
aVal += a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus) * 2;
return aVal.compareTo(bVal);
});
return friends;
});
2023-04-29 15:26:12 -04:00
}
2023-04-29 13:18:46 -04:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2023-04-30 09:43:59 -04:00
title: const Text("Contacts++"),
actions: [
IconButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => const SettingsPage()));
},
icon: const Icon(Icons.settings),
)
],
2023-04-29 13:18:46 -04:00
),
2023-04-30 09:43:59 -04:00
body: Stack(
children: [
RefreshIndicator(
onRefresh: () async {
_refreshFriendsList();
2023-05-03 12:43:06 -04:00
await _friendsFuture;
2023-04-30 09:43:59 -04:00
},
child: FutureBuilder(
2023-05-03 12:43:06 -04:00
future: _friendsFuture,
2023-04-30 09:43:59 -04:00
builder: (context, snapshot) {
if (snapshot.hasData) {
2023-05-03 14:03:46 -04:00
var friends = (snapshot.data as List<Friend>);
2023-05-03 12:43:06 -04:00
if (_searchFilter.isNotEmpty) {
2023-05-03 14:03:46 -04:00
friends = friends.where((element) => element.username.contains(_searchFilter)).toList();
friends.sort((a, b) => a.username.length.compareTo(b.username.length));
2023-05-03 12:43:06 -04:00
}
2023-04-30 09:43:59 -04:00
return ListView.builder(
2023-05-03 14:03:46 -04:00
itemCount: friends.length,
itemBuilder: (context, index) {
final friend = friends[index];
final unread = _unreads[friend.id] ?? [];
return FriendListTile(
friend: friend,
unreads: unread.length,
onTap: () async {
if (unread.isNotEmpty) {
final readBatch = MarkReadBatch(
senderId: _clientHolder!.client.userId,
ids: unread.map((e) => e.id).toList(),
readTime: DateTime.now(),
);
_clientHolder!.hub.markMessagesRead(readBatch);
}
setState(() {
unread.clear();
});
},
);
},
2023-04-30 09:43:59 -04:00
);
} else if (snapshot.hasError) {
2023-04-30 17:14:29 -04:00
FlutterError.reportError(FlutterErrorDetails(exception: snapshot.error!, stack: snapshot.stackTrace));
2023-04-30 09:43:59 -04:00
return Center(
child: Padding(
padding: const EdgeInsets.all(64),
child: Text(
"Something went wrong: ${snapshot.error}",
softWrap: true,
style: Theme
.of(context)
.textTheme
.labelMedium,
2023-04-30 07:39:09 -04:00
),
),
2023-04-29 15:26:12 -04:00
);
2023-04-30 09:43:59 -04:00
} else {
2023-04-30 17:14:29 -04:00
return const SizedBox.shrink();
2023-04-30 09:43:59 -04:00
}
}
),
),
Align(
alignment: Alignment.bottomCenter,
child: ExpandingInputFab(
onInputChanged: (String text) {
2023-04-30 17:14:29 -04:00
setState(() {
2023-05-03 12:43:06 -04:00
_searchFilter = text;
2023-04-30 09:43:59 -04:00
});
},
onExpansionChanged: (expanded) {
if (_debouncer?.isActive ?? false) _debouncer?.cancel();
if (!expanded) {
setState(() {
2023-05-03 12:43:06 -04:00
_searchFilter = "";
2023-04-30 09:43:59 -04:00
});
}
},
),
),
],
2023-04-29 13:18:46 -04:00
),
);
}
2023-04-30 09:43:59 -04:00
}