Make user-search use new Provider interface

This commit is contained in:
Nutcake 2023-05-06 19:01:15 +02:00
parent 59cd6bc07f
commit 78269f93d2
6 changed files with 30 additions and 29 deletions

View file

@ -59,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

@ -136,7 +136,7 @@ class MessagingClient extends ChangeNotifier {
var aVal = friendHasUnreads(a) ? -3 : 0; var aVal = friendHasUnreads(a) ? -3 : 0;
var bVal = friendHasUnreads(b) ? -3 : 0; var bVal = friendHasUnreads(b) ? -3 : 0;
aVal -= a.userStatus.lastStatusChange.compareTo(b.userStatus.lastStatusChange); aVal -= a.latestMessageTime.compareTo(b.latestMessageTime);
aVal += a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus) * 2; aVal += a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus) * 2;
return aVal.compareTo(bVal); return aVal.compareTo(bVal);
}); });
@ -170,8 +170,8 @@ class MessagingClient extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
void clearUnreadsForFriend(Friend friend) { void clearUnreadsForUser(String userId) {
_unreads[friend.id]?.clear(); _unreads[userId]?.clear();
notifyListeners(); notifyListeners();
} }
@ -367,5 +367,6 @@ class MessagingClient extends ChangeNotifier {
], ],
}; };
_sendData(data); _sendData(data);
clearUnreadsForUser(batch.senderId);
} }
} }

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

@ -108,7 +108,9 @@ class _FriendsListState extends State<FriendsList> {
} }
}, },
itemBuilder: (BuildContext context) => itemBuilder: (BuildContext context) =>
OnlineStatus.values.where((element) => element != OnlineStatus.offline).map((item) => OnlineStatus.values.where((element) =>
element == OnlineStatus.online
|| element == OnlineStatus.invisible).map((item) =>
PopupMenuItem<OnlineStatus>( PopupMenuItem<OnlineStatus>(
value: item, value: item,
child: Row( child: Row(
@ -189,18 +191,15 @@ class _FriendsListState extends State<FriendsList> {
icon: Icons.person_add, icon: Icons.person_add,
onTap: () async { onTap: () async {
final mClient = Provider.of<MessagingClient>(context, listen: false); final mClient = Provider.of<MessagingClient>(context, listen: false);
bool changed = false;
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) {
mClient.refreshFriendsList();
}
}, },
), ),
MenuItemDefinition( MenuItemDefinition(

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,11 +53,7 @@ class _UserSearchState extends State<UserSearch> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
/* TODO: Use provider final mClient = Provider.of<MessagingClient>(context, listen: false);
final mClient = ClientHolder
.of(context)
.messagingClient;
*/
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text("Find Users"), title: const Text("Find Users"),
@ -74,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, onChange: widget.onFriendsChanged, isFriend: false,); // TODO: Use provider mClient.getAsFriend(user.id) != null,); return UserListTile(user: user, onChanged: () {
mClient.refreshFriendsList();
}, isFriend: mClient.getAsFriend(user.id) != null,);
}, },
); );
} else if (snapshot.hasError) { } else if (snapshot.hasError) {