Merge pull request #11 from Nutcake/messaging-state-refactor

Messaging state refactor
This commit is contained in:
Nutcake 2023-05-06 19:03:33 +02:00 committed by GitHub
commit f2e482b46a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 470 additions and 441 deletions

View file

@ -5,10 +5,10 @@ import 'package:contacts_plus_plus/clients/api_client.dart';
import 'package:contacts_plus_plus/models/friend.dart'; import 'package:contacts_plus_plus/models/friend.dart';
class FriendApi { class FriendApi {
static Future<Iterable<Friend>> getFriendsList(ApiClient client) async { static Future<List<Friend>> getFriendsList(ApiClient client) async {
final response = await client.get("/users/${client.userId}/friends"); final response = await client.get("/users/${client.userId}/friends");
ApiClient.checkResponse(response); ApiClient.checkResponse(response);
final data = jsonDecode(response.body) as List; final data = jsonDecode(response.body) as List;
return data.map((e) => Friend.fromMap(e)); return data.map((e) => Friend.fromMap(e)).toList();
} }
} }

View file

@ -4,7 +4,7 @@ import 'package:contacts_plus_plus/clients/api_client.dart';
import 'package:contacts_plus_plus/models/message.dart'; import 'package:contacts_plus_plus/models/message.dart';
class MessageApi { class MessageApi {
static Future<Iterable<Message>> getUserMessages(ApiClient client, {String userId = "", DateTime? fromTime, static Future<List<Message>> getUserMessages(ApiClient client, {String userId = "", DateTime? fromTime,
int maxItems = 50, bool unreadOnly = false}) async { int maxItems = 50, bool unreadOnly = false}) async {
final response = await client.get("/users/${client.userId}/messages" final response = await client.get("/users/${client.userId}/messages"
@ -15,6 +15,6 @@ class MessageApi {
); );
ApiClient.checkResponse(response); ApiClient.checkResponse(response);
final data = jsonDecode(response.body) as List; final data = jsonDecode(response.body) as List;
return data.map((e) => Message.fromMap(e)); return data.map((e) => Message.fromMap(e)).toList();
} }
} }

View file

@ -28,6 +28,11 @@ class UserApi {
final data = jsonDecode(response.body); final data = jsonDecode(response.body);
return UserStatus.fromMap(data); return UserStatus.fromMap(data);
} }
static Future<void> notifyOnlineInstance(ApiClient client) async {
final response = await client.post("/stats/instanceOnline/${client.authenticationData.secretMachineId.hashCode}");
ApiClient.checkResponse(response);
}
static Future<void> setStatus(ApiClient client, {required UserStatus status}) async { static Future<void> setStatus(ApiClient client, {required UserStatus status}) async {
final pkginfo = await PackageInfo.fromPlatform(); final pkginfo = await PackageInfo.fromPlatform();
@ -54,6 +59,7 @@ class UserApi {
userStatus: UserStatus.empty(), userStatus: UserStatus.empty(),
userProfile: UserProfile.empty(), userProfile: UserProfile.empty(),
friendStatus: FriendStatus.accepted, friendStatus: FriendStatus.accepted,
latestMessageTime: DateTime.now(),
); );
final body = jsonEncode(friend.toMap(shallow: true)); final body = jsonEncode(friend.toMap(shallow: true));
final response = await client.put("/users/${client.userId}/friends/${user.id}", body: body); final response = await client.put("/users/${client.userId}/friends/${user.id}", body: body);

View file

@ -1,6 +1,5 @@
import 'package:contacts_plus_plus/clients/api_client.dart'; import 'package:contacts_plus_plus/clients/api_client.dart';
import 'package:contacts_plus_plus/clients/messaging_client.dart';
import 'package:contacts_plus_plus/clients/notification_client.dart'; import 'package:contacts_plus_plus/clients/notification_client.dart';
import 'package:contacts_plus_plus/clients/settings_client.dart'; import 'package:contacts_plus_plus/clients/settings_client.dart';
import 'package:contacts_plus_plus/models/authentication_data.dart'; import 'package:contacts_plus_plus/models/authentication_data.dart';
@ -9,7 +8,6 @@ import 'package:flutter/material.dart';
class ClientHolder extends InheritedWidget { class ClientHolder extends InheritedWidget {
final ApiClient apiClient; final ApiClient apiClient;
final SettingsClient settingsClient; final SettingsClient settingsClient;
late final MessagingClient messagingClient;
final NotificationClient notificationClient = NotificationClient(); final NotificationClient notificationClient = NotificationClient();
ClientHolder({ ClientHolder({
@ -17,9 +15,7 @@ class ClientHolder extends InheritedWidget {
required AuthenticationData authenticationData, required AuthenticationData authenticationData,
required this.settingsClient, required this.settingsClient,
required super.child required super.child
}) : apiClient = ApiClient(authenticationData: authenticationData) { }) : apiClient = ApiClient(authenticationData: authenticationData);
messagingClient = MessagingClient(apiClient: apiClient, notificationClient: notificationClient);
}
static ClientHolder? maybeOf(BuildContext context) { static ClientHolder? maybeOf(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<ClientHolder>(); return context.dependOnInheritedWidgetOfExactType<ClientHolder>();
@ -34,6 +30,5 @@ class ClientHolder extends InheritedWidget {
@override @override
bool updateShouldNotify(covariant ClientHolder oldWidget) => bool updateShouldNotify(covariant ClientHolder oldWidget) =>
oldWidget.apiClient != apiClient oldWidget.apiClient != apiClient
|| oldWidget.settingsClient != settingsClient || oldWidget.settingsClient != settingsClient;
|| oldWidget.messagingClient != messagingClient;
} }

View file

@ -1,8 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:contacts_plus_plus/clients/messaging_client.dart';
import 'package:contacts_plus_plus/clients/notification_client.dart';
import 'package:contacts_plus_plus/clients/settings_client.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_phoenix/flutter_phoenix.dart'; import 'package:flutter_phoenix/flutter_phoenix.dart';

View file

@ -1,9 +1,13 @@
import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:contacts_plus_plus/apis/friend_api.dart';
import 'package:contacts_plus_plus/apis/message_api.dart'; import 'package:contacts_plus_plus/apis/message_api.dart';
import 'package:contacts_plus_plus/apis/user_api.dart';
import 'package:contacts_plus_plus/clients/notification_client.dart'; import 'package:contacts_plus_plus/clients/notification_client.dart';
import 'package:contacts_plus_plus/models/authentication_data.dart'; import 'package:contacts_plus_plus/models/authentication_data.dart';
import 'package:contacts_plus_plus/models/friend.dart'; import 'package:contacts_plus_plus/models/friend.dart';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:contacts_plus_plus/clients/api_client.dart'; import 'package:contacts_plus_plus/clients/api_client.dart';
@ -37,27 +41,52 @@ enum EventTarget {
} }
} }
class MessagingClient { class MessagingClient extends ChangeNotifier {
static const String eofChar = ""; static const String eofChar = "";
static const String _negotiationPacket = "{\"protocol\":\"json\", \"version\":1}$eofChar"; static const String _negotiationPacket = "{\"protocol\":\"json\", \"version\":1}$eofChar";
static const List<int> _reconnectTimeoutsSeconds = [0, 5, 10, 20, 60]; static const List<int> _reconnectTimeoutsSeconds = [0, 5, 10, 20, 60];
static const String taskName = "periodic-unread-check"; static const String taskName = "periodic-unread-check";
static const Duration _autoRefreshDuration = Duration(seconds: 90);
static const Duration _refreshTimeoutDuration = Duration(seconds: 30);
final ApiClient _apiClient; final ApiClient _apiClient;
final Map<String, Friend> _friendsCache = {}; final Map<String, Friend> _friendsCache = {};
final List<Friend> _sortedFriendsCache = []; // Keep a sorted copy so as to not have to sort during build()
final Map<String, MessageCache> _messageCache = {}; final Map<String, MessageCache> _messageCache = {};
final Map<String, Function> _messageUpdateListeners = {};
final Map<String, List<Message>> _unreads = {}; final Map<String, List<Message>> _unreads = {};
final Logger _logger = Logger("NeosHub"); final Logger _logger = Logger("NeosHub");
final Workmanager _workmanager = Workmanager(); final Workmanager _workmanager = Workmanager();
final NotificationClient _notificationClient; final NotificationClient _notificationClient;
Friend? selectedFriend;
Timer? _notifyOnlineTimer;
Timer? _autoRefresh;
Timer? _refreshTimeout;
int _attempts = 0; int _attempts = 0;
Function? _unreadsUpdateListener;
WebSocket? _wsChannel; WebSocket? _wsChannel;
bool _isConnecting = false; bool _isConnecting = false;
String? _initStatus;
String? get initStatus => _initStatus;
bool get websocketConnected => _wsChannel != null;
MessagingClient({required ApiClient apiClient, required NotificationClient notificationClient}) MessagingClient({required ApiClient apiClient, required NotificationClient notificationClient})
: _apiClient = apiClient, _notificationClient = notificationClient { : _apiClient = apiClient, _notificationClient = notificationClient {
start(); refreshFriendsListWithErrorHandler();
startWebsocket();
_notifyOnlineTimer = Timer.periodic(const Duration(seconds: 60), (timer) async {
// We should probably let the MessagingClient handle the entire state of USerStatus instead of mirroring like this
// but I don't feel like implementing that right now.
UserApi.setStatus(apiClient, status: await UserApi.getUserStatus(apiClient, userId: apiClient.userId));
});
}
@override
void dispose() {
_autoRefresh?.cancel();
_refreshTimeout?.cancel();
_notifyOnlineTimer?.cancel();
_wsChannel?.close();
super.dispose();
} }
void _sendData(data) { void _sendData(data) {
@ -65,11 +94,52 @@ class MessagingClient {
_wsChannel!.add(jsonEncode(data)+eofChar); _wsChannel!.add(jsonEncode(data)+eofChar);
} }
void updateFriendsCache(List<Friend> friends) { void resetStatus() {
_initStatus = null;
notifyListeners();
}
void refreshFriendsListWithErrorHandler () async {
try {
await refreshFriendsList();
} catch (e) {
_initStatus = "$e";
notifyListeners();
}
}
Future<void> refreshFriendsList() async {
if (_refreshTimeout?.isActive == true) return;
_autoRefresh?.cancel();
_autoRefresh = Timer(_autoRefreshDuration, () => refreshFriendsList());
_refreshTimeout?.cancel();
_refreshTimeout = Timer(_refreshTimeoutDuration, () {});
final unreadMessages = await MessageApi.getUserMessages(_apiClient, unreadOnly: true);
updateAllUnreads(unreadMessages.toList());
final friends = await FriendApi.getFriendsList(_apiClient);
_friendsCache.clear(); _friendsCache.clear();
for (final friend in friends) { for (final friend in friends) {
_friendsCache[friend.id] = friend; _friendsCache[friend.id] = friend;
} }
_sortedFriendsCache.clear();
_sortedFriendsCache.addAll(friends);
_sortFriendsCache();
_initStatus = "";
notifyListeners();
}
void _sortFriendsCache() {
_sortedFriendsCache.sort((a, b) {
var aVal = friendHasUnreads(a) ? -3 : 0;
var bVal = friendHasUnreads(b) ? -3 : 0;
aVal -= a.latestMessageTime.compareTo(b.latestMessageTime);
aVal += a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus) * 2;
return aVal.compareTo(bVal);
});
} }
void updateAllUnreads(List<Message> messages) { void updateAllUnreads(List<Message> messages) {
@ -95,13 +165,14 @@ class MessagingClient {
messages.add(message); messages.add(message);
} }
messages.sort(); messages.sort();
_sortFriendsCache();
_notificationClient.showUnreadMessagesNotification(messages.reversed); _notificationClient.showUnreadMessagesNotification(messages.reversed);
notifyUnreadListener(); notifyListeners();
} }
void clearUnreadsForFriend(Friend friend) { void clearUnreadsForUser(String userId) {
_unreads[friend.id]?.clear(); _unreads[userId]?.clear();
notifyUnreadListener(); notifyListeners();
} }
List<Message> getUnreadsForFriend(Friend friend) => _unreads[friend.id] ?? []; List<Message> getUnreadsForFriend(Friend friend) => _unreads[friend.id] ?? [];
@ -114,16 +185,19 @@ class MessagingClient {
Friend? getAsFriend(String userId) => _friendsCache[userId]; Friend? getAsFriend(String userId) => _friendsCache[userId];
Future<MessageCache> getMessageCache(String userId) async { List<Friend> get cachedFriends => _sortedFriendsCache;
var cache = _messageCache[userId];
if (cache == null){ MessageCache _createUserMessageCache(String userId) => MessageCache(apiClient: _apiClient, userId: userId);
cache = MessageCache(apiClient: _apiClient, userId: userId);
await cache.loadInitialMessages(); Future<void> loadUserMessageCache(String userId) async {
_messageCache[userId] = cache; final cache = getUserMessageCache(userId) ?? _createUserMessageCache(userId);
} await cache.loadMessages();
return cache; _messageCache[userId] = cache;
notifyListeners();
} }
MessageCache? getUserMessageCache(String userId) => _messageCache[userId];
static Future<void> backgroundCheckUnreads(Map<String, dynamic>? inputData) async { static Future<void> backgroundCheckUnreads(Map<String, dynamic>? inputData) async {
if (inputData == null) return; if (inputData == null) return;
final auth = AuthenticationData.fromMap(inputData); final auth = AuthenticationData.fromMap(inputData);
@ -146,11 +220,12 @@ class MessagingClient {
} }
void _onDisconnected(error) async { void _onDisconnected(error) async {
_wsChannel = null;
_logger.warning("Neos Hub connection died with error '$error', reconnecting..."); _logger.warning("Neos Hub connection died with error '$error', reconnecting...");
await start(); await startWebsocket();
} }
Future<void> start() async { Future<void> startWebsocket() async {
if (!_apiClient.isAuthenticated) { if (!_apiClient.isAuthenticated) {
_logger.info("Tried to connect to Neos Hub without authentication, this is probably fine for now."); _logger.info("Tried to connect to Neos Hub without authentication, this is probably fine for now.");
return; return;
@ -200,14 +275,6 @@ class MessagingClient {
} }
} }
void registerMessageListener(String userId, Function function) => _messageUpdateListeners[userId] = function;
void unregisterMessageListener(String userId) => _messageUpdateListeners.remove(userId);
void notifyMessageListener(String userId) => _messageUpdateListeners[userId]?.call();
void registerUnreadListener(Function function) => _unreadsUpdateListener = function;
void unregisterUnreadListener() => _unreadsUpdateListener = null;
void notifyUnreadListener() => _unreadsUpdateListener?.call();
void _handleEvent(event) { void _handleEvent(event) {
final body = jsonDecode((event.toString().replaceAll(eofChar, ""))); final body = jsonDecode((event.toString().replaceAll(eofChar, "")));
final int rawType = body["type"] ?? 0; final int rawType = body["type"] ?? 0;
@ -247,28 +314,30 @@ class MessagingClient {
case EventTarget.messageSent: case EventTarget.messageSent:
final msg = args[0]; final msg = args[0];
final message = Message.fromMap(msg, withState: MessageState.sent); final message = Message.fromMap(msg, withState: MessageState.sent);
final cache = await getMessageCache(message.recipientId); final cache = getUserMessageCache(message.recipientId) ?? _createUserMessageCache(message.recipientId);
cache.addMessage(message); cache.addMessage(message);
notifyMessageListener(message.recipientId); notifyListeners();
break; break;
case EventTarget.receiveMessage: case EventTarget.receiveMessage:
final msg = args[0]; final msg = args[0];
final message = Message.fromMap(msg); final message = Message.fromMap(msg);
final cache = await getMessageCache(message.senderId); final cache = getUserMessageCache(message.senderId) ?? _createUserMessageCache(message.senderId);
cache.addMessage(message); cache.addMessage(message);
if (!_messageUpdateListeners.containsKey(message.senderId)) { if (message.senderId != selectedFriend?.id) {
addUnread(message); addUnread(message);
} }
notifyMessageListener(message.senderId); notifyListeners();
break; break;
case EventTarget.messagesRead: case EventTarget.messagesRead:
final messageIds = args[0]["ids"] as List; final messageIds = args[0]["ids"] as List;
final recipientId = args[0]["recipientId"]; final recipientId = args[0]["recipientId"];
final cache = await getMessageCache(recipientId ?? ""); if (recipientId == null) break;
final cache = getUserMessageCache(recipientId);
if (cache == null) break;
for (var id in messageIds) { for (var id in messageIds) {
cache.setMessageState(id, MessageState.read); cache.setMessageState(id, MessageState.read);
} }
notifyMessageListener(recipientId); notifyListeners();
break; break;
} }
} }
@ -283,9 +352,9 @@ class MessagingClient {
], ],
}; };
_sendData(data); _sendData(data);
final cache = await getMessageCache(message.recipientId); final cache = getUserMessageCache(message.recipientId) ?? _createUserMessageCache(message.recipientId);
cache.messages.add(message); cache.messages.add(message);
notifyMessageListener(message.recipientId); notifyListeners();
} }
void markMessagesRead(MarkReadBatch batch) { void markMessagesRead(MarkReadBatch batch) {
@ -298,5 +367,6 @@ class MessagingClient {
], ],
}; };
_sendData(data); _sendData(data);
clearUnreadsForUser(batch.senderId);
} }
} }

View file

@ -9,6 +9,7 @@ import 'package:contacts_plus_plus/widgets/login_screen.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_phoenix/flutter_phoenix.dart'; import 'package:flutter_phoenix/flutter_phoenix.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:provider/provider.dart';
import 'package:workmanager/workmanager.dart'; import 'package:workmanager/workmanager.dart';
import 'models/authentication_data.dart'; import 'models/authentication_data.dart';
@ -56,25 +57,37 @@ class _ContactsPlusPlusState extends State<ContactsPlusPlus> {
return ClientHolder( return ClientHolder(
settingsClient: widget.settingsClient, settingsClient: widget.settingsClient,
authenticationData: _authData, authenticationData: _authData,
child: MaterialApp( child: Builder(
debugShowCheckedModeBanner: false, builder: (context) {
title: 'Contacts++', final clientHolder = ClientHolder.of(context);
theme: ThemeData( return MaterialApp(
useMaterial3: true, debugShowCheckedModeBanner: false,
textTheme: _typography.white, title: 'Contacts++',
colorScheme: ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.dark) theme: ThemeData(
), useMaterial3: true,
home: _authData.isAuthenticated ? textTheme: _typography.white,
const FriendsList() : colorScheme: ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.dark)
LoginScreen( ),
onLoginSuccessful: (AuthenticationData authData) async { home: _authData.isAuthenticated ?
if (authData.isAuthenticated) { ChangeNotifierProvider( // This doesn't need to be a proxy provider since the arguments should never change during it's lifetime.
setState(() { create: (context) =>
_authData = authData; MessagingClient(
}); apiClient: clientHolder.apiClient,
} notificationClient: clientHolder.notificationClient,
}, ),
), child: const FriendsList(),
) :
LoginScreen(
onLoginSuccessful: (AuthenticationData authData) async {
if (authData.isAuthenticated) {
setState(() {
_authData = authData;
});
}
},
)
);
}
), ),
); );
} }

View file

@ -1,5 +1,3 @@
import 'dart:developer';
import 'package:contacts_plus_plus/models/session.dart'; import 'package:contacts_plus_plus/models/session.dart';
import 'package:contacts_plus_plus/models/user_profile.dart'; import 'package:contacts_plus_plus/models/user_profile.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -11,9 +9,10 @@ class Friend extends Comparable {
final UserStatus userStatus; final UserStatus userStatus;
final UserProfile userProfile; final UserProfile userProfile;
final FriendStatus friendStatus; final FriendStatus friendStatus;
final DateTime latestMessageTime;
Friend({required this.id, required this.username, required this.ownerId, required this.userStatus, required this.userProfile, Friend({required this.id, required this.username, required this.ownerId, required this.userStatus, required this.userProfile,
required this.friendStatus, required this.friendStatus, required this.latestMessageTime,
}); });
factory Friend.fromMap(Map map) { factory Friend.fromMap(Map map) {
@ -24,6 +23,8 @@ class Friend extends Comparable {
userStatus: UserStatus.fromMap(map["userStatus"]), userStatus: UserStatus.fromMap(map["userStatus"]),
userProfile: UserProfile.fromMap(map["profile"] ?? {}), userProfile: UserProfile.fromMap(map["profile"] ?? {}),
friendStatus: FriendStatus.fromString(map["friendStatus"]), friendStatus: FriendStatus.fromString(map["friendStatus"]),
latestMessageTime: map["latestMessageTime"] == null
? DateTime.fromMillisecondsSinceEpoch(0) : DateTime.parse(map["latestMessageTime"]),
); );
} }
@ -35,6 +36,7 @@ class Friend extends Comparable {
"userStatus": userStatus.toMap(shallow: shallow), "userStatus": userStatus.toMap(shallow: shallow),
"profile": userProfile.toMap(), "profile": userProfile.toMap(),
"friendStatus": friendStatus.name, "friendStatus": friendStatus.name,
"latestMessageTime": latestMessageTime.toIso8601String(),
}; };
} }

View file

@ -140,7 +140,7 @@ class MessageCache {
return this; //lmao return this; //lmao
} }
Future<void> loadInitialMessages() async { Future<void> loadMessages() async {
final messages = await MessageApi.getUserMessages(_apiClient, userId: _userId); final messages = await MessageApi.getUserMessages(_apiClient, userId: _userId);
_messages.addAll(messages); _messages.addAll(messages);
_ensureIntegrity(); _ensureIntegrity();

View file

@ -1,9 +1,13 @@
import 'package:contacts_plus_plus/auxiliary.dart'; import 'package:contacts_plus_plus/auxiliary.dart';
import 'package:contacts_plus_plus/client_holder.dart';
import 'package:contacts_plus_plus/clients/messaging_client.dart';
import 'package:contacts_plus_plus/models/friend.dart'; import 'package:contacts_plus_plus/models/friend.dart';
import 'package:contacts_plus_plus/models/message.dart';
import 'package:contacts_plus_plus/widgets/generic_avatar.dart'; import 'package:contacts_plus_plus/widgets/generic_avatar.dart';
import 'package:contacts_plus_plus/widgets/messages_list.dart'; import 'package:contacts_plus_plus/widgets/messages_list.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
class FriendListTile extends StatelessWidget { class FriendListTile extends StatelessWidget {
const FriendListTile({required this.friend, this.unreads, this.onTap, super.key}); const FriendListTile({required this.friend, this.unreads, this.onTap, super.key});
@ -24,8 +28,32 @@ class FriendListTile extends StatelessWidget {
title: Text(friend.username), title: Text(friend.username),
subtitle: Text(toBeginningOfSentenceCase(friend.userStatus.onlineStatus.name) ?? "Unknown"), subtitle: Text(toBeginningOfSentenceCase(friend.userStatus.onlineStatus.name) ?? "Unknown"),
onTap: () async { onTap: () async {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => MessagesList(friend: friend))); onTap?.call();
await onTap?.call(); final mClient = Provider.of<MessagingClient>(context, listen: false);
mClient.loadUserMessageCache(friend.id);
final apiClient = ClientHolder
.of(context)
.apiClient;
final unreads = mClient.getUnreadsForFriend(friend);
if (unreads.isNotEmpty) {
final readBatch = MarkReadBatch(
senderId: apiClient.userId,
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;
}, },
); );
} }

View file

@ -2,10 +2,8 @@ import 'dart:async';
import 'package:contacts_plus_plus/apis/user_api.dart'; import 'package:contacts_plus_plus/apis/user_api.dart';
import 'package:contacts_plus_plus/client_holder.dart'; import 'package:contacts_plus_plus/client_holder.dart';
import 'package:contacts_plus_plus/apis/friend_api.dart'; import 'package:contacts_plus_plus/clients/messaging_client.dart';
import 'package:contacts_plus_plus/apis/message_api.dart';
import 'package:contacts_plus_plus/models/friend.dart'; import 'package:contacts_plus_plus/models/friend.dart';
import 'package:contacts_plus_plus/models/message.dart';
import 'package:contacts_plus_plus/models/personal_profile.dart'; import 'package:contacts_plus_plus/models/personal_profile.dart';
import 'package:contacts_plus_plus/widgets/default_error_widget.dart'; import 'package:contacts_plus_plus/widgets/default_error_widget.dart';
import 'package:contacts_plus_plus/widgets/expanding_input_fab.dart'; import 'package:contacts_plus_plus/widgets/expanding_input_fab.dart';
@ -15,6 +13,7 @@ import 'package:contacts_plus_plus/widgets/settings_page.dart';
import 'package:contacts_plus_plus/widgets/user_search.dart'; import 'package:contacts_plus_plus/widgets/user_search.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
class MenuItemDefinition { class MenuItemDefinition {
@ -33,72 +32,30 @@ class FriendsList extends StatefulWidget {
} }
class _FriendsListState extends State<FriendsList> { class _FriendsListState extends State<FriendsList> {
static const Duration _autoRefreshDuration = Duration(seconds: 90);
static const Duration _refreshTimeoutDuration = Duration(seconds: 30);
Future<List<Friend>>? _friendsFuture;
Future<PersonalProfile>? _userProfileFuture; Future<PersonalProfile>? _userProfileFuture;
Future<UserStatus>? _userStatusFuture; Future<UserStatus>? _userStatusFuture;
ClientHolder? _clientHolder; ClientHolder? _clientHolder;
Timer? _autoRefresh;
Timer? _refreshTimeout;
String _searchFilter = ""; String _searchFilter = "";
@override
void dispose() {
_autoRefresh?.cancel();
_refreshTimeout?.cancel();
super.dispose();
}
@override @override
void didChangeDependencies() async { void didChangeDependencies() async {
super.didChangeDependencies(); super.didChangeDependencies();
final clientHolder = ClientHolder.of(context); final clientHolder = ClientHolder.of(context);
if (_clientHolder != clientHolder) { if (_clientHolder != clientHolder) {
_clientHolder = clientHolder; _clientHolder = clientHolder;
final mClient = _clientHolder!.messagingClient;
mClient.registerUnreadListener(() {
if (context.mounted) {
setState(() {});
} else {
mClient.unregisterUnreadListener();
}
});
_refreshFriendsList();
final apiClient = _clientHolder!.apiClient; final apiClient = _clientHolder!.apiClient;
_userProfileFuture = UserApi.getPersonalProfile(apiClient); _userProfileFuture = UserApi.getPersonalProfile(apiClient);
_refreshUserStatus();
} }
} }
void _refreshFriendsList() { void _refreshUserStatus() {
if (_refreshTimeout?.isActive == true) return;
final apiClient = _clientHolder!.apiClient; final apiClient = _clientHolder!.apiClient;
_friendsFuture = FriendApi.getFriendsList(apiClient).then((Iterable<Friend> value) async {
final unreadMessages = await MessageApi.getUserMessages(apiClient, unreadOnly: true);
final mClient = _clientHolder?.messagingClient;
if (mClient == null) return [];
mClient.updateAllUnreads(unreadMessages.toList());
final friends = value.toList()
..sort((a, b) {
var aVal = mClient.friendHasUnreads(a) ? -3 : 0;
var bVal = mClient.friendHasUnreads(b) ? -3 : 0;
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);
return friends;
});
_userStatusFuture = UserApi.getUserStatus(apiClient, userId: apiClient.userId).then((value) async { _userStatusFuture = UserApi.getUserStatus(apiClient, userId: apiClient.userId).then((value) async {
if (value.onlineStatus == OnlineStatus.offline) { if (value.onlineStatus == OnlineStatus.offline) {
final newStatus = value.copyWith( final newStatus = value.copyWith(
onlineStatus: OnlineStatus.values[_clientHolder!.settingsClient.currentSettings.lastOnlineStatus.valueOrDefault] onlineStatus: OnlineStatus.values[_clientHolder!.settingsClient.currentSettings.lastOnlineStatus
.valueOrDefault]
); );
await UserApi.setStatus(apiClient, status: newStatus); await UserApi.setStatus(apiClient, status: newStatus);
return newStatus; return newStatus;
@ -109,93 +66,109 @@ class _FriendsListState extends State<FriendsList> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final apiClient = ClientHolder.of(context).apiClient; final clientHolder = ClientHolder.of(context);
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text("Contacts++"), title: const Text("Contacts++"),
actions: [ actions: [
FutureBuilder( FutureBuilder(
future: _userStatusFuture, future: _userStatusFuture,
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.hasData) { if (snapshot.hasData) {
final userStatus = snapshot.data as UserStatus; final userStatus = snapshot.data as UserStatus;
return PopupMenuButton<OnlineStatus>( return PopupMenuButton<OnlineStatus>(
child: Row( child: Row(
children: [ children: [
Padding( Padding(
padding: const EdgeInsets.only(right: 8.0), padding: const EdgeInsets.only(right: 8.0),
child: Icon(Icons.circle, size: 16, color: userStatus.onlineStatus.color,), child: Icon(Icons.circle, size: 16, color: userStatus.onlineStatus.color,),
), ),
Text(toBeginningOfSentenceCase(userStatus.onlineStatus.name) ?? "Unknown"), Text(toBeginningOfSentenceCase(userStatus.onlineStatus.name) ?? "Unknown"),
], ],
), ),
onSelected: (OnlineStatus onlineStatus) async { onSelected: (OnlineStatus onlineStatus) async {
try { try {
final newStatus = userStatus.copyWith(onlineStatus: onlineStatus); final newStatus = userStatus.copyWith(onlineStatus: onlineStatus);
setState(() { setState(() {
_userStatusFuture = Future.value(newStatus.copyWith(lastStatusChange: DateTime.now())); _userStatusFuture = Future.value(newStatus.copyWith(lastStatusChange: DateTime.now()));
}); });
final settingsClient = ClientHolder.of(context).settingsClient; final settingsClient = ClientHolder
await UserApi.setStatus(apiClient, status: newStatus); .of(context)
await settingsClient.changeSettings(settingsClient.currentSettings.copyWith(lastOnlineStatus: onlineStatus.index)); .settingsClient;
} catch (e, s) { await UserApi.setStatus(clientHolder.apiClient, status: newStatus);
FlutterError.reportError(FlutterErrorDetails(exception: e, stack: s)); await settingsClient.changeSettings(
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Failed to set online-status."))); settingsClient.currentSettings.copyWith(lastOnlineStatus: onlineStatus.index));
setState(() { } catch (e, s) {
_userStatusFuture = Future.value(userStatus); FlutterError.reportError(FlutterErrorDetails(exception: e, stack: s));
}); ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text(
} "Failed to set online-status.")));
}, setState(() {
itemBuilder: (BuildContext context) => _userStatusFuture = Future.value(userStatus);
OnlineStatus.values.where((element) => element != OnlineStatus.offline).map((item) => });
PopupMenuItem<OnlineStatus>( }
value: item, },
child: Row( itemBuilder: (BuildContext context) =>
mainAxisAlignment: MainAxisAlignment.start, OnlineStatus.values.where((element) =>
children: [ element == OnlineStatus.online
Icon(Icons.circle, size: 16, color: item.color,), || element == OnlineStatus.invisible).map((item) =>
const SizedBox(width: 8,), PopupMenuItem<OnlineStatus>(
Text(toBeginningOfSentenceCase(item.name)!), value: item,
], child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(Icons.circle, size: 16, color: item.color,),
const SizedBox(width: 8,),
Text(toBeginningOfSentenceCase(item.name)!),
],
),
), ),
), ).toList());
).toList()); } else if (snapshot.hasError) {
} else if (snapshot.hasError) { return TextButton.icon(
return TextButton.icon( style: TextButton.styleFrom(
style: TextButton.styleFrom( foregroundColor: Theme
foregroundColor: Theme.of(context).colorScheme.onSurface, .of(context)
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2) .colorScheme
), .onSurface,
onPressed: () { padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2)
setState(() {
_userStatusFuture = null;
});
setState(() {
_userStatusFuture = UserApi.getUserStatus(apiClient, userId: apiClient.userId);
});
},
icon: const Icon(Icons.warning),
label: const Text("Retry"),
);
} else {
return TextButton.icon(
style: TextButton.styleFrom(
disabledForegroundColor: Theme.of(context).colorScheme.onSurface,
),
onPressed: null,
icon: Container(
width: 16,
height: 16,
margin: const EdgeInsets.only(right: 4),
child: CircularProgressIndicator(
strokeWidth: 2,
color: Theme.of(context).colorScheme.onSurface,
), ),
), onPressed: () {
label: const Text("Loading"), setState(() {
); _userStatusFuture = null;
});
setState(() {
_userStatusFuture = UserApi.getUserStatus(clientHolder.apiClient, userId: clientHolder.apiClient
.userId);
});
},
icon: const Icon(Icons.warning),
label: const Text("Retry"),
);
} else {
return TextButton.icon(
style: TextButton.styleFrom(
disabledForegroundColor: Theme
.of(context)
.colorScheme
.onSurface,
),
onPressed: null,
icon: Container(
width: 16,
height: 16,
margin: const EdgeInsets.only(right: 4),
child: CircularProgressIndicator(
strokeWidth: 2,
color: Theme
.of(context)
.colorScheme
.onSurface,
),
),
label: const Text("Loading"),
);
}
} }
}
), ),
Padding( Padding(
padding: const EdgeInsets.only(left: 4, right: 4), padding: const EdgeInsets.only(left: 4, right: 4),
@ -210,33 +183,23 @@ class _FriendsListState extends State<FriendsList> {
name: "Settings", name: "Settings",
icon: Icons.settings, icon: Icons.settings,
onTap: () async { onTap: () async {
_autoRefresh?.cancel();
await Navigator.of(context).push(MaterialPageRoute(builder: (context) => const SettingsPage())); await Navigator.of(context).push(MaterialPageRoute(builder: (context) => const SettingsPage()));
_autoRefresh = Timer(_autoRefreshDuration, () => setState(() => _refreshFriendsList()));
}, },
), ),
MenuItemDefinition( MenuItemDefinition(
name: "Find Users", name: "Find Users",
icon: Icons.person_add, icon: Icons.person_add,
onTap: () async { onTap: () async {
bool changed = false; final mClient = Provider.of<MessagingClient>(context, listen: false);
_autoRefresh?.cancel();
await Navigator.of(context).push( await Navigator.of(context).push(
MaterialPageRoute( MaterialPageRoute(
builder: (context) => builder: (context) =>
UserSearch( ChangeNotifierProvider<MessagingClient>.value(
onFriendsChanged: () => changed = true, value: mClient,
child: const UserSearch(),
), ),
), ),
); );
if (changed) {
_refreshTimeout?.cancel();
setState(() {
_refreshFriendsList();
});
} else {
_autoRefresh = Timer(_autoRefreshDuration, () => setState(() => _refreshFriendsList()));
}
}, },
), ),
MenuItemDefinition( MenuItemDefinition(
@ -247,24 +210,26 @@ class _FriendsListState extends State<FriendsList> {
context: context, context: context,
builder: (context) { builder: (context) {
return FutureBuilder( return FutureBuilder(
future: _userProfileFuture, future: _userProfileFuture,
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.hasData) { if (snapshot.hasData) {
final profile = snapshot.data as PersonalProfile; final profile = snapshot.data as PersonalProfile;
return MyProfileDialog(profile: profile); return MyProfileDialog(profile: profile);
} else if (snapshot.hasError) { } else if (snapshot.hasError) {
return DefaultErrorWidget( return DefaultErrorWidget(
title: "Failed to load personal profile.", title: "Failed to load personal profile.",
onRetry: () { onRetry: () {
setState(() { setState(() {
_userProfileFuture = UserApi.getPersonalProfile(ClientHolder.of(context).apiClient); _userProfileFuture = UserApi.getPersonalProfile(ClientHolder
}); .of(context)
}, .apiClient);
); });
} else { },
return const Center(child: CircularProgressIndicator(),); );
} else {
return const Center(child: CircularProgressIndicator(),);
}
} }
}
); );
}, },
); );
@ -287,63 +252,46 @@ class _FriendsListState extends State<FriendsList> {
], ],
), ),
body: Stack( body: Stack(
alignment: Alignment.topCenter,
children: [ children: [
RefreshIndicator( Consumer<MessagingClient>(
onRefresh: () async { builder: (context, mClient, _) {
_refreshFriendsList(); if (mClient.initStatus == null) {
await _friendsFuture; // Keep the indicator running until everything's loaded return const LinearProgressIndicator();
}, } else if (mClient.initStatus!.isNotEmpty) {
child: FutureBuilder( return Column(
future: _friendsFuture, children: [
builder: (context, snapshot) { Expanded(
if (snapshot.hasData) { child: DefaultErrorWidget(
var friends = (snapshot.data as List<Friend>); message: mClient.initStatus,
if (_searchFilter.isNotEmpty) { onRetry: () async {
friends = friends.where((element) => mClient.resetStatus();
element.username.toLowerCase().contains(_searchFilter.toLowerCase())).toList(); mClient.refreshFriendsListWithErrorHandler();
friends.sort((a, b) => a.username.length.compareTo(b.username.length)); },
} ),
return ListView.builder( ),
itemCount: friends.length, ],
itemBuilder: (context, index) { );
final friend = friends[index]; } else {
final unreads = _clientHolder?.messagingClient.getUnreadsForFriend(friend) ?? []; var friends = List.from(mClient.cachedFriends); // Explicit copy.
return FriendListTile( if (_searchFilter.isNotEmpty) {
friend: friend, friends = friends.where((element) =>
unreads: unreads.length, element.username.toLowerCase().contains(_searchFilter.toLowerCase())).toList();
onTap: () async { friends.sort((a, b) => a.username.length.compareTo(b.username.length));
if (unreads.isNotEmpty) {
final readBatch = MarkReadBatch(
senderId: _clientHolder!.apiClient.userId,
ids: unreads.map((e) => e.id).toList(),
readTime: DateTime.now(),
);
_clientHolder!.messagingClient.markMessagesRead(readBatch);
}
setState(() {
unreads.clear();
});
},
);
},
);
} else if (snapshot.hasError) {
FlutterError.reportError(
FlutterErrorDetails(exception: snapshot.error!, stack: snapshot.stackTrace));
return DefaultErrorWidget(
message: "${snapshot.error}",
onRetry: () {
_refreshTimeout?.cancel();
setState(() {
_refreshFriendsList();
});
},
);
} else {
return const LinearProgressIndicator();
} }
return ListView.builder(
itemCount: friends.length,
itemBuilder: (context, index) {
final friend = friends[index];
final unreads = mClient.getUnreadsForFriend(friend);
return FriendListTile(
friend: friend,
unreads: unreads.length,
);
},
);
} }
), }
), ),
Align( Align(
alignment: Alignment.bottomCenter, alignment: Alignment.bottomCenter,
@ -366,4 +314,4 @@ class _FriendsListState extends State<FriendsList> {
), ),
); );
} }
} }

View file

@ -1,17 +1,16 @@
import 'dart:async';
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
import 'package:contacts_plus_plus/client_holder.dart'; import 'package:contacts_plus_plus/client_holder.dart';
import 'package:contacts_plus_plus/auxiliary.dart'; import 'package:contacts_plus_plus/auxiliary.dart';
import 'package:contacts_plus_plus/clients/messaging_client.dart';
import 'package:contacts_plus_plus/models/friend.dart'; import 'package:contacts_plus_plus/models/friend.dart';
import 'package:contacts_plus_plus/models/message.dart'; import 'package:contacts_plus_plus/models/message.dart';
import 'package:contacts_plus_plus/models/session.dart'; import 'package:contacts_plus_plus/models/session.dart';
import 'package:contacts_plus_plus/widgets/default_error_widget.dart';
import 'package:contacts_plus_plus/widgets/message_audio_player.dart'; import 'package:contacts_plus_plus/widgets/message_audio_player.dart';
import 'package:contacts_plus_plus/widgets/generic_avatar.dart'; import 'package:contacts_plus_plus/widgets/generic_avatar.dart';
import 'package:contacts_plus_plus/widgets/message_session_invite.dart'; import 'package:contacts_plus_plus/widgets/message_session_invite.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
class MessagesList extends StatefulWidget { class MessagesList extends StatefulWidget {
const MessagesList({required this.friend, super.key}); const MessagesList({required this.friend, super.key});
@ -23,46 +22,18 @@ class MessagesList extends StatefulWidget {
} }
class _MessagesListState extends State<MessagesList> { class _MessagesListState extends State<MessagesList> {
Future<MessageCache>? _messageCacheFuture;
final TextEditingController _messageTextController = TextEditingController(); final TextEditingController _messageTextController = TextEditingController();
final ScrollController _sessionListScrollController = ScrollController(); final ScrollController _sessionListScrollController = ScrollController();
final ScrollController _messageScrollController = ScrollController(); final ScrollController _messageScrollController = ScrollController();
ClientHolder? _clientHolder;
bool _isSendable = false; bool _isSendable = false;
bool _showSessionListChevron = false; bool _showSessionListScrollChevron = false;
bool _messageCacheFutureComplete = false;
double get _shevronOpacity => _showSessionListChevron ? 1.0 : 0.0; double get _shevronOpacity => _showSessionListScrollChevron ? 1.0 : 0.0;
@override
void didChangeDependencies() {
super.didChangeDependencies();
final clientHolder = ClientHolder.of(context);
if (_clientHolder != clientHolder) {
_clientHolder = clientHolder;
}
_loadMessages();
}
void _loadMessages() {
_messageCacheFutureComplete = false;
_messageCacheFuture = _clientHolder?.messagingClient.getMessageCache(widget.friend.id)
.whenComplete(() => _messageCacheFutureComplete = true);
final mClient = _clientHolder?.messagingClient;
final id = widget.friend.id;
mClient?.registerMessageListener(id, () {
if (context.mounted) {
setState(() {});
} else {
mClient.unregisterMessageListener(id);
}
});
}
@override @override
void dispose() { void dispose() {
_clientHolder?.messagingClient.unregisterMessageListener(widget.friend.id);
_messageTextController.dispose(); _messageTextController.dispose();
_sessionListScrollController.dispose(); _sessionListScrollController.dispose();
super.dispose(); super.dispose();
@ -72,25 +43,15 @@ class _MessagesListState extends State<MessagesList> {
void initState() { void initState() {
super.initState(); super.initState();
_sessionListScrollController.addListener(() { _sessionListScrollController.addListener(() {
if (_sessionListScrollController.position.maxScrollExtent > 0 && !_showSessionListChevron) { if (_sessionListScrollController.position.maxScrollExtent > 0 && !_showSessionListScrollChevron) {
setState(() { setState(() {
_showSessionListChevron = true; _showSessionListScrollChevron = true;
}); });
} }
if (_sessionListScrollController.position.atEdge && _sessionListScrollController.position.pixels > 0 if (_sessionListScrollController.position.atEdge && _sessionListScrollController.position.pixels > 0
&& _showSessionListChevron) { && _showSessionListScrollChevron) {
setState(() { setState(() {
_showSessionListChevron = false; _showSessionListScrollChevron = false;
});
}
});
_messageScrollController.addListener(() {
if (_messageScrollController.position.atEdge && _messageScrollController.position.pixels > 0 &&
_messageScrollController.position.maxScrollExtent > 0 && _messageCacheFutureComplete) {
setState(() {
_messageCacheFutureComplete = false;
_messageCacheFuture = _clientHolder?.messagingClient.getMessageCache(widget.friend.id)
.then((value) => value.loadOlderMessages()).whenComplete(() => _messageCacheFutureComplete = true);
}); });
} }
}); });
@ -156,68 +117,56 @@ class _MessagesListState extends State<MessagesList> {
), ),
), ),
Expanded( Expanded(
child: FutureBuilder( child: Consumer<MessagingClient>(
future: _messageCacheFuture, builder: (context, mClient, _) {
builder: (context, snapshot) { final cache = mClient.getUserMessageCache(widget.friend.id);
if (snapshot.hasData) { if (cache == null) {
final cache = snapshot.data as MessageCache;
if (cache.messages.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.message_outlined),
Padding(
padding: const EdgeInsets.symmetric(vertical: 24),
child: Text(
"There are no messages here\nWhy not say hello?",
textAlign: TextAlign.center,
style: Theme
.of(context)
.textTheme
.titleMedium,
),
)
],
),
);
}
return ListView.builder(
controller: _messageScrollController,
reverse: true,
itemCount: cache.messages.length,
itemBuilder: (context, index) {
final entry = cache.messages[index];
final widget = entry.senderId == apiClient.userId
? MyMessageBubble(message: entry)
: OtherMessageBubble(message: entry);
if (index == cache.messages.length-1) {
return Padding(
padding: const EdgeInsets.only(top: 12),
child: widget,
);
}
return widget;
},
);
} else if (snapshot.hasError) {
return DefaultErrorWidget(
message: "${snapshot.error}",
onRetry: () {
setState(() {
_loadMessages();
});
},
);
} else {
return Column( return Column(
mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.end,
children: const [ children: const [
LinearProgressIndicator(), LinearProgressIndicator()
], ],
); );
} }
if (cache.messages.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.message_outlined),
Padding(
padding: const EdgeInsets.symmetric(vertical: 24),
child: Text(
"There are no messages here\nWhy not say hello?",
textAlign: TextAlign.center,
style: Theme
.of(context)
.textTheme
.titleMedium,
),
)
],
),
);
}
return ListView.builder(
controller: _messageScrollController,
reverse: true,
itemCount: cache.messages.length,
itemBuilder: (context, index) {
final entry = cache.messages[index];
final widget = entry.senderId == apiClient.userId
? MyMessageBubble(message: entry)
: OtherMessageBubble(message: entry);
if (index == cache.messages.length - 1) {
return Padding(
padding: const EdgeInsets.only(top: 12),
child: widget,
);
}
return widget;
},
);
}, },
), ),
), ),
@ -265,39 +214,43 @@ class _MessagesListState extends State<MessagesList> {
), ),
Padding( Padding(
padding: const EdgeInsets.only(left: 8, right: 4.0), padding: const EdgeInsets.only(left: 8, right: 4.0),
child: IconButton( child: Consumer<MessagingClient>(
splashRadius: 24, builder: (context, mClient, _) {
onPressed: _isSendable && _clientHolder != null ? () async { return IconButton(
setState(() { splashRadius: 24,
_isSendable = false; onPressed: _isSendable ? () async {
}); setState(() {
final message = Message( _isSendable = false;
id: Message.generateId(), });
recipientId: widget.friend.id, final message = Message(
senderId: apiClient.userId, id: Message.generateId(),
type: MessageType.text, recipientId: widget.friend.id,
content: _messageTextController.text, senderId: apiClient.userId,
sendTime: DateTime.now().toUtc(), type: MessageType.text,
content: _messageTextController.text,
sendTime: DateTime.now().toUtc(),
);
try {
mClient.sendMessage(message);
_messageTextController.clear();
setState(() {});
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Failed to send message\n$e",
maxLines: null,
),
),
);
setState(() {
_isSendable = true;
});
}
} : null,
iconSize: 28,
icon: const Icon(Icons.send),
); );
try { }
_clientHolder!.messagingClient.sendMessage(message);
_messageTextController.clear();
setState(() {});
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Failed to send message\n$e",
maxLines: null,
),
),
);
setState(() {
_isSendable = true;
});
}
} : null,
iconSize: 28,
icon: const Icon(Icons.send),
), ),
) )
], ],

View file

@ -7,11 +7,11 @@ import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
class UserListTile extends StatefulWidget { class UserListTile extends StatefulWidget {
const UserListTile({required this.user, required this.isFriend, required this.onChange, super.key}); const UserListTile({required this.user, required this.isFriend, required this.onChanged, super.key});
final User user; final User user;
final bool isFriend; final bool isFriend;
final Function()? onChange; final Function()? onChanged;
@override @override
State<UserListTile> createState() => _UserListTileState(); State<UserListTile> createState() => _UserListTileState();
@ -63,6 +63,11 @@ class _UserListTileState extends State<UserListTile> {
.of(context) .of(context)
.apiClient, user: widget.user); .apiClient, user: widget.user);
} }
setState(() {
_loading = false;
_localAdded = !_localAdded;
});
widget.onChanged?.call();
} catch (e, s) { } catch (e, s) {
FlutterError.reportError(FlutterErrorDetails(exception: e, stack: s)); FlutterError.reportError(FlutterErrorDetails(exception: e, stack: s));
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
@ -80,11 +85,6 @@ class _UserListTileState extends State<UserListTile> {
}); });
return; return;
} }
setState(() {
_loading = false;
_localAdded = !_localAdded;
});
widget.onChange?.call();
}, },
), ),
); );

View file

@ -2,10 +2,12 @@ import 'dart:async';
import 'package:contacts_plus_plus/apis/user_api.dart'; import 'package:contacts_plus_plus/apis/user_api.dart';
import 'package:contacts_plus_plus/client_holder.dart'; import 'package:contacts_plus_plus/client_holder.dart';
import 'package:contacts_plus_plus/clients/messaging_client.dart';
import 'package:contacts_plus_plus/models/user.dart'; import 'package:contacts_plus_plus/models/user.dart';
import 'package:contacts_plus_plus/widgets/default_error_widget.dart'; import 'package:contacts_plus_plus/widgets/default_error_widget.dart';
import 'package:contacts_plus_plus/widgets/user_list_tile.dart'; import 'package:contacts_plus_plus/widgets/user_list_tile.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class SearchError { class SearchError {
final String message; final String message;
@ -15,9 +17,7 @@ class SearchError {
} }
class UserSearch extends StatefulWidget { class UserSearch extends StatefulWidget {
const UserSearch({required this.onFriendsChanged, super.key}); const UserSearch({super.key});
final Function()? onFriendsChanged;
@override @override
State<StatefulWidget> createState() => _UserSearchState(); State<StatefulWidget> createState() => _UserSearchState();
@ -53,9 +53,7 @@ class _UserSearchState extends State<UserSearch> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final mClient = ClientHolder final mClient = Provider.of<MessagingClient>(context, listen: false);
.of(context)
.messagingClient;
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text("Find Users"), title: const Text("Find Users"),
@ -72,7 +70,9 @@ class _UserSearchState extends State<UserSearch> {
itemCount: users.length, itemCount: users.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
final user = users[index]; final user = users[index];
return UserListTile(user: user, isFriend: mClient.getAsFriend(user.id) != null, onChange: widget.onFriendsChanged); return UserListTile(user: user, onChanged: () {
mClient.refreshFriendsList();
}, isFriend: mClient.getAsFriend(user.id) != null,);
}, },
); );
} else if (snapshot.hasError) { } else if (snapshot.hasError) {

View file

@ -368,6 +368,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.8.0" version: "1.8.0"
nested:
dependency: transitive
description:
name: nested
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
octo_image: octo_image:
dependency: transitive dependency: transitive
description: description:
@ -488,6 +496,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "4.2.4" version: "4.2.4"
provider:
dependency: "direct main"
description:
name: provider
sha256: cdbe7530b12ecd9eb455bdaa2fcb8d4dad22e80b8afb4798b41479d5ce26847f
url: "https://pub.dev"
source: hosted
version: "6.0.5"
rxdart: rxdart:
dependency: transitive dependency: transitive
description: description:

View file

@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts # In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix. # of the product and file versions while build-number is used as the build suffix.
version: 1.0.1+1 version: 1.0.2+1
environment: environment:
sdk: '>=2.19.6 <3.0.0' sdk: '>=2.19.6 <3.0.0'
@ -52,6 +52,7 @@ dependencies:
flutter_local_notifications: ^14.0.0+1 flutter_local_notifications: ^14.0.0+1
collection: ^1.17.0 collection: ^1.17.0
package_info_plus: ^3.1.2 package_info_plus: ^3.1.2
provider: ^6.0.5
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: