OpenContacts/lib/widgets/friends_list.dart

226 lines
8.5 KiB
Dart
Raw Normal View History

2023-04-30 09:43:59 -04:00
import 'dart:async';
import 'package:contacts_plus_plus/clients/api_client.dart';
2023-05-01 13:13:40 -04:00
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';
import 'package:contacts_plus_plus/widgets/default_error_widget.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-05-04 12:16:19 -04:00
import 'package:contacts_plus_plus/widgets/user_search.dart';
2023-04-29 13:18:46 -04:00
import 'package:flutter/material.dart';
2023-05-04 12:16:19 -04:00
class MenuItemDefinition {
final String name;
final IconData icon;
2023-05-04 13:38:35 -04:00
final Function() onTap;
2023-05-04 12:16:19 -04:00
const MenuItemDefinition({required this.name, required this.icon, required this.onTap});
}
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> {
static const Duration _autoRefreshDuration = Duration(seconds: 90);
static const Duration _refreshTimeoutDuration = Duration(seconds: 30);
2023-05-03 12:43:06 -04:00
Future<List<Friend>>? _friendsFuture;
2023-04-30 07:39:09 -04:00
ClientHolder? _clientHolder;
Timer? _autoRefresh;
Timer? _refreshTimeout;
2023-05-03 12:43:06 -04:00
String _searchFilter = "";
2023-04-30 09:43:59 -04:00
@override
void dispose() {
_autoRefresh?.cancel();
_refreshTimeout?.cancel();
2023-04-30 09:43:59 -04:00
super.dispose();
}
2023-04-29 15:26:12 -04:00
@override
void didChangeDependencies() async {
2023-04-30 07:39:09 -04:00
super.didChangeDependencies();
final clientHolder = ClientHolder.of(context);
if (_clientHolder != clientHolder) {
_clientHolder = clientHolder;
final mClient = _clientHolder!.messagingClient;
mClient.registerUnreadListener(() {
if (context.mounted) {
setState(() {});
} else {
mClient.unregisterUnreadListener();
}
});
2023-04-30 07:39:09 -04:00
_refreshFriendsList();
}
2023-04-29 15:26:12 -04:00
}
void _refreshFriendsList() {
if (_refreshTimeout?.isActive == true) return;
_friendsFuture = FriendApi.getFriendsList(_clientHolder!.apiClient).then((Iterable<Friend> value) async {
final unreadMessages = await MessageApi.getUserMessages(_clientHolder!.apiClient, unreadOnly: true);
final mClient = _clientHolder?.messagingClient;
if (mClient == null) return [];
mClient.updateAllUnreads(unreadMessages.toList());
2023-05-03 14:03:46 -04:00
final friends = value.toList()
..sort((a, b) {
var aVal = mClient.friendHasUnreads(a) ? -3 : 0;
var bVal = mClient.friendHasUnreads(b) ? -3 : 0;
2023-05-03 14:03:46 -04:00
aVal -= a.userStatus.lastStatusChange.compareTo(b.userStatus.lastStatusChange);
aVal += a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus) * 2;
return aVal.compareTo(bVal);
});
_autoRefresh?.cancel();
_autoRefresh = Timer(_autoRefreshDuration, () => setState(() => _refreshFriendsList()));
_refreshTimeout?.cancel();
_refreshTimeout = Timer(_refreshTimeoutDuration, () {});
_clientHolder?.messagingClient.updateFriendsCache(friends);
2023-05-03 14:03:46 -04:00
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: [
2023-05-04 12:16:19 -04:00
Padding(
padding: const EdgeInsets.only(right: 4),
child: PopupMenuButton<MenuItemDefinition>(
icon: const Icon(Icons.more_vert),
2023-05-04 13:38:35 -04:00
onSelected: (MenuItemDefinition itemDef) async {
await itemDef.onTap();
2023-05-04 12:16:19 -04:00
},
itemBuilder: (BuildContext context) => [
2023-05-04 13:38:35 -04:00
MenuItemDefinition(name: "Settings", icon: Icons.settings, onTap: () async {
_autoRefresh?.cancel();
await Navigator.of(context).push(MaterialPageRoute(builder: (context) => const SettingsPage()));
_autoRefresh = Timer(_autoRefreshDuration, () => setState(() => _refreshFriendsList()));
2023-05-04 12:16:19 -04:00
}),
2023-05-04 13:38:35 -04:00
MenuItemDefinition(name: "Find Users", icon: Icons.person_add, onTap: () async {
bool changed = false;
2023-05-04 13:38:35 -04:00
_autoRefresh?.cancel();
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
UserSearch(
onFriendsChanged: () => changed = true,
),
),
);
if (changed) {
_refreshTimeout?.cancel();
setState(() {
_refreshFriendsList();
});
} else {
_autoRefresh = Timer(_autoRefreshDuration, () => setState(() => _refreshFriendsList()));
}
2023-05-05 05:29:54 -04:00
}),
2023-05-04 12:16:19 -04:00
].map((item) =>
PopupMenuItem<MenuItemDefinition>(
value: item,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(item.name),
Icon(item.icon),
],
),
),
).toList(),
),
)
],
2023-04-29 13:18:46 -04:00
),
2023-04-30 09:43:59 -04:00
body: Stack(
children: [
RefreshIndicator(
onRefresh: () async {
_refreshFriendsList();
await _friendsFuture; // Keep the indicator running until everything's loaded
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) {
friends = friends.where((element) => element.username.toLowerCase().contains(_searchFilter.toLowerCase())).toList();
2023-05-03 14:03:46 -04:00
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 unreads = _clientHolder?.messagingClient.getUnreadsForFriend(friend) ?? [];
2023-05-03 14:03:46 -04:00
return FriendListTile(
friend: friend,
unreads: unreads.length,
2023-05-03 14:03:46 -04:00
onTap: () async {
if (unreads.isNotEmpty) {
2023-05-03 14:03:46 -04:00
final readBatch = MarkReadBatch(
senderId: _clientHolder!.apiClient.userId,
ids: unreads.map((e) => e.id).toList(),
2023-05-03 14:03:46 -04:00
readTime: DateTime.now(),
);
_clientHolder!.messagingClient.markMessagesRead(readBatch);
2023-05-03 14:03:46 -04:00
}
setState(() {
unreads.clear();
2023-05-03 14:03:46 -04:00
});
},
);
},
2023-04-30 09:43:59 -04:00
);
} else if (snapshot.hasError) {
2023-05-04 12:16:19 -04:00
FlutterError.reportError(
FlutterErrorDetails(exception: snapshot.error!, stack: snapshot.stackTrace));
return DefaultErrorWidget(
message: "${snapshot.error}",
onRetry: () {
_refreshTimeout?.cancel();
setState(() {
_refreshFriendsList();
});
},
2023-04-29 15:26:12 -04:00
);
2023-04-30 09:43:59 -04:00
} else {
return const LinearProgressIndicator();
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 (!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
}