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(),
userProfile: UserProfile.empty(),
friendStatus: FriendStatus.accepted,
latestMessageTime: DateTime.now(),
);
final body = jsonEncode(friend.toMap(shallow: true));
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 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;
return aVal.compareTo(bVal);
});
@ -170,8 +170,8 @@ class MessagingClient extends ChangeNotifier {
notifyListeners();
}
void clearUnreadsForFriend(Friend friend) {
_unreads[friend.id]?.clear();
void clearUnreadsForUser(String userId) {
_unreads[userId]?.clear();
notifyListeners();
}
@ -367,5 +367,6 @@ class MessagingClient extends ChangeNotifier {
],
};
_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/user_profile.dart';
import 'package:flutter/material.dart';
@ -11,9 +9,10 @@ class Friend extends Comparable {
final UserStatus userStatus;
final UserProfile userProfile;
final FriendStatus friendStatus;
final DateTime latestMessageTime;
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) {
@ -24,6 +23,8 @@ class Friend extends Comparable {
userStatus: UserStatus.fromMap(map["userStatus"]),
userProfile: UserProfile.fromMap(map["profile"] ?? {}),
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),
"profile": userProfile.toMap(),
"friendStatus": friendStatus.name,
"latestMessageTime": latestMessageTime.toIso8601String(),
};
}

View file

@ -108,7 +108,9 @@ class _FriendsListState extends State<FriendsList> {
}
},
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>(
value: item,
child: Row(
@ -189,18 +191,15 @@ class _FriendsListState extends State<FriendsList> {
icon: Icons.person_add,
onTap: () async {
final mClient = Provider.of<MessagingClient>(context, listen: false);
bool changed = false;
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
UserSearch(
onFriendsChanged: () => changed = true,
ChangeNotifierProvider<MessagingClient>.value(
value: mClient,
child: const UserSearch(),
),
),
);
if (changed) {
mClient.refreshFriendsList();
}
},
),
MenuItemDefinition(

View file

@ -7,11 +7,11 @@ import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
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 bool isFriend;
final Function()? onChange;
final Function()? onChanged;
@override
State<UserListTile> createState() => _UserListTileState();
@ -63,6 +63,11 @@ class _UserListTileState extends State<UserListTile> {
.of(context)
.apiClient, user: widget.user);
}
setState(() {
_loading = false;
_localAdded = !_localAdded;
});
widget.onChanged?.call();
} catch (e, s) {
FlutterError.reportError(FlutterErrorDetails(exception: e, stack: s));
ScaffoldMessenger.of(context).showSnackBar(
@ -80,11 +85,6 @@ class _UserListTileState extends State<UserListTile> {
});
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/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/widgets/default_error_widget.dart';
import 'package:contacts_plus_plus/widgets/user_list_tile.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class SearchError {
final String message;
@ -15,9 +17,7 @@ class SearchError {
}
class UserSearch extends StatefulWidget {
const UserSearch({required this.onFriendsChanged, super.key});
final Function()? onFriendsChanged;
const UserSearch({super.key});
@override
State<StatefulWidget> createState() => _UserSearchState();
@ -53,11 +53,7 @@ class _UserSearchState extends State<UserSearch> {
@override
Widget build(BuildContext context) {
/* TODO: Use provider
final mClient = ClientHolder
.of(context)
.messagingClient;
*/
final mClient = Provider.of<MessagingClient>(context, listen: false);
return Scaffold(
appBar: AppBar(
title: const Text("Find Users"),
@ -74,7 +70,9 @@ class _UserSearchState extends State<UserSearch> {
itemCount: users.length,
itemBuilder: (context, 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) {