Change the way friend status is determined
This commit is contained in:
parent
2bb3fe6979
commit
40c4d82b81
8 changed files with 48 additions and 28 deletions
|
@ -44,7 +44,8 @@ extension NeosStringExtensions on Uri {
|
||||||
}
|
}
|
||||||
|
|
||||||
class Aux {
|
class Aux {
|
||||||
static String neosDbToHttp(String neosdb) {
|
static String neosDbToHttp(String? neosdb) {
|
||||||
|
if (neosdb == null || neosdb.isEmpty) return "";
|
||||||
final fullUri = neosdb.replaceFirst("neosdb:///", Config.neosCdnUrl);
|
final fullUri = neosdb.replaceFirst("neosdb:///", Config.neosCdnUrl);
|
||||||
final lastPeriodIndex = fullUri.lastIndexOf(".");
|
final lastPeriodIndex = fullUri.lastIndexOf(".");
|
||||||
if (lastPeriodIndex != -1 && fullUri.length - lastPeriodIndex < 8) {
|
if (lastPeriodIndex != -1 && fullUri.length - lastPeriodIndex < 8) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:contacts_plus_plus/clients/neos_hub.dart';
|
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
||||||
import 'package:contacts_plus_plus/clients/settings_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';
|
||||||
|
@ -148,7 +148,7 @@ class ApiClient {
|
||||||
class ClientHolder extends InheritedWidget {
|
class ClientHolder extends InheritedWidget {
|
||||||
final ApiClient apiClient;
|
final ApiClient apiClient;
|
||||||
final SettingsClient settingsClient;
|
final SettingsClient settingsClient;
|
||||||
late final NeosHub hub;
|
late final MessagingClient messagingClient;
|
||||||
|
|
||||||
ClientHolder({
|
ClientHolder({
|
||||||
super.key,
|
super.key,
|
||||||
|
@ -156,7 +156,7 @@ class ClientHolder extends InheritedWidget {
|
||||||
required this.settingsClient,
|
required this.settingsClient,
|
||||||
required super.child
|
required super.child
|
||||||
}) : apiClient = ApiClient(authenticationData: authenticationData) {
|
}) : apiClient = ApiClient(authenticationData: authenticationData) {
|
||||||
hub = NeosHub(apiClient: apiClient);
|
messagingClient = MessagingClient(apiClient: apiClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ClientHolder? maybeOf(BuildContext context) {
|
static ClientHolder? maybeOf(BuildContext context) {
|
||||||
|
@ -173,5 +173,5 @@ class ClientHolder extends InheritedWidget {
|
||||||
bool updateShouldNotify(covariant ClientHolder oldWidget) =>
|
bool updateShouldNotify(covariant ClientHolder oldWidget) =>
|
||||||
oldWidget.apiClient != apiClient
|
oldWidget.apiClient != apiClient
|
||||||
|| oldWidget.settingsClient != settingsClient
|
|| oldWidget.settingsClient != settingsClient
|
||||||
|| oldWidget.hub != hub;
|
|| oldWidget.messagingClient != messagingClient;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:contacts_plus_plus/apis/message_api.dart';
|
import 'package:contacts_plus_plus/apis/message_api.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: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';
|
||||||
|
@ -29,12 +30,13 @@ enum EventTarget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NeosHub {
|
class MessagingClient {
|
||||||
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";
|
||||||
final ApiClient _apiClient;
|
final ApiClient _apiClient;
|
||||||
|
final Map<String, Friend> _friendsCache = {};
|
||||||
final Map<String, MessageCache> _messageCache = {};
|
final Map<String, MessageCache> _messageCache = {};
|
||||||
final Map<String, Function> _updateListeners = {};
|
final Map<String, Function> _updateListeners = {};
|
||||||
final Logger _logger = Logger("NeosHub");
|
final Logger _logger = Logger("NeosHub");
|
||||||
|
@ -42,7 +44,7 @@ class NeosHub {
|
||||||
WebSocket? _wsChannel;
|
WebSocket? _wsChannel;
|
||||||
bool _isConnecting = false;
|
bool _isConnecting = false;
|
||||||
|
|
||||||
NeosHub({required ApiClient apiClient})
|
MessagingClient({required ApiClient apiClient})
|
||||||
: _apiClient = apiClient {
|
: _apiClient = apiClient {
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
@ -52,7 +54,16 @@ class NeosHub {
|
||||||
_wsChannel!.add(jsonEncode(data)+eofChar);
|
_wsChannel!.add(jsonEncode(data)+eofChar);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<MessageCache> getCache(String userId) async {
|
void updateFriendsCache(List<Friend> friends) {
|
||||||
|
_friendsCache.clear();
|
||||||
|
for (final friend in friends) {
|
||||||
|
_friendsCache[friend.id] = friend;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Friend? getAsFriend(String userId) => _friendsCache[userId];
|
||||||
|
|
||||||
|
Future<MessageCache> getMessageCache(String userId) async {
|
||||||
var cache = _messageCache[userId];
|
var cache = _messageCache[userId];
|
||||||
if (cache == null){
|
if (cache == null){
|
||||||
cache = MessageCache(apiClient: _apiClient, userId: userId);
|
cache = MessageCache(apiClient: _apiClient, userId: userId);
|
||||||
|
@ -168,21 +179,21 @@ class NeosHub {
|
||||||
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 getCache(message.recipientId);
|
final cache = await getMessageCache(message.recipientId);
|
||||||
cache.addMessage(message);
|
cache.addMessage(message);
|
||||||
notifyListener(message.recipientId);
|
notifyListener(message.recipientId);
|
||||||
break;
|
break;
|
||||||
case EventTarget.messageReceived:
|
case EventTarget.messageReceived:
|
||||||
final msg = args[0];
|
final msg = args[0];
|
||||||
final message = Message.fromMap(msg);
|
final message = Message.fromMap(msg);
|
||||||
final cache = await getCache(message.senderId);
|
final cache = await getMessageCache(message.senderId);
|
||||||
cache.addMessage(message);
|
cache.addMessage(message);
|
||||||
notifyListener(message.senderId);
|
notifyListener(message.senderId);
|
||||||
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 getCache(recipientId ?? "");
|
final cache = await getMessageCache(recipientId ?? "");
|
||||||
for (var id in messageIds) {
|
for (var id in messageIds) {
|
||||||
cache.setMessageState(id, MessageState.read);
|
cache.setMessageState(id, MessageState.read);
|
||||||
}
|
}
|
||||||
|
@ -201,7 +212,7 @@ class NeosHub {
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
_sendData(data);
|
_sendData(data);
|
||||||
final cache = await getCache(message.recipientId);
|
final cache = await getMessageCache(message.recipientId);
|
||||||
cache.messages.add(message);
|
cache.messages.add(message);
|
||||||
notifyListener(message.recipientId);
|
notifyListener(message.recipientId);
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
import 'dart:developer';
|
import 'dart:developer';
|
||||||
import 'dart:io' show Platform;
|
import 'dart:io' show Platform;
|
||||||
|
|
||||||
import 'package:contacts_plus_plus/clients/neos_hub.dart';
|
import 'package:contacts_plus_plus/clients/messaging_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/widgets/friends_list.dart';
|
import 'package:contacts_plus_plus/widgets/friends_list.dart';
|
||||||
import 'package:contacts_plus_plus/widgets/login_screen.dart';
|
import 'package:contacts_plus_plus/widgets/login_screen.dart';
|
||||||
|
@ -31,8 +31,8 @@ void main() async {
|
||||||
void callbackDispatcher() {
|
void callbackDispatcher() {
|
||||||
Workmanager().executeTask((String task, Map<String, dynamic>? inputData) async {
|
Workmanager().executeTask((String task, Map<String, dynamic>? inputData) async {
|
||||||
debugPrint("Native called background task: $task"); //simpleTask will be emitted here.
|
debugPrint("Native called background task: $task"); //simpleTask will be emitted here.
|
||||||
if (task == NeosHub.taskName) {
|
if (task == MessagingClient.taskName) {
|
||||||
final unreads = NeosHub.backgroundCheckUnreads(inputData);
|
final unreads = MessagingClient.backgroundCheckUnreads(inputData);
|
||||||
}
|
}
|
||||||
return Future.value(true);
|
return Future.value(true);
|
||||||
});
|
});
|
||||||
|
|
|
@ -85,6 +85,7 @@ class _FriendsListState extends State<FriendsList> {
|
||||||
_autoRefresh = Timer(_autoRefreshDuration, () => setState(() => _refreshFriendsList()));
|
_autoRefresh = Timer(_autoRefreshDuration, () => setState(() => _refreshFriendsList()));
|
||||||
_refreshTimeout?.cancel();
|
_refreshTimeout?.cancel();
|
||||||
_refreshTimeout = Timer(_refreshTimeoutDuration, () {});
|
_refreshTimeout = Timer(_refreshTimeoutDuration, () {});
|
||||||
|
_clientHolder?.messagingClient.updateFriendsCache(friends);
|
||||||
return friends;
|
return friends;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -156,7 +157,7 @@ class _FriendsListState extends State<FriendsList> {
|
||||||
ids: unread.map((e) => e.id).toList(),
|
ids: unread.map((e) => e.id).toList(),
|
||||||
readTime: DateTime.now(),
|
readTime: DateTime.now(),
|
||||||
);
|
);
|
||||||
_clientHolder!.hub.markMessagesRead(readBatch);
|
_clientHolder!.messagingClient.markMessagesRead(readBatch);
|
||||||
}
|
}
|
||||||
setState(() {
|
setState(() {
|
||||||
unread.clear();
|
unread.clear();
|
||||||
|
@ -171,7 +172,10 @@ class _FriendsListState extends State<FriendsList> {
|
||||||
return DefaultErrorWidget(
|
return DefaultErrorWidget(
|
||||||
message: "${snapshot.error}",
|
message: "${snapshot.error}",
|
||||||
onRetry: () {
|
onRetry: () {
|
||||||
_refreshFriendsList();
|
_refreshTimeout?.cancel();
|
||||||
|
setState(() {
|
||||||
|
_refreshFriendsList();
|
||||||
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -47,15 +47,15 @@ class _MessagesListState extends State<MessagesList> {
|
||||||
|
|
||||||
void _loadMessages() {
|
void _loadMessages() {
|
||||||
_messageCacheFutureComplete = false;
|
_messageCacheFutureComplete = false;
|
||||||
_messageCacheFuture = _clientHolder?.hub.getCache(widget.friend.id)
|
_messageCacheFuture = _clientHolder?.messagingClient.getMessageCache(widget.friend.id)
|
||||||
.whenComplete(() => _messageCacheFutureComplete = true);
|
.whenComplete(() => _messageCacheFutureComplete = true);
|
||||||
_clientHolder?.hub.registerListener(
|
_clientHolder?.messagingClient.registerListener(
|
||||||
widget.friend.id, () => setState(() {}));
|
widget.friend.id, () => setState(() {}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_clientHolder?.hub.unregisterListener(widget.friend.id);
|
_clientHolder?.messagingClient.unregisterListener(widget.friend.id);
|
||||||
_messageTextController.dispose();
|
_messageTextController.dispose();
|
||||||
_sessionListScrollController.dispose();
|
_sessionListScrollController.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
|
@ -82,7 +82,7 @@ class _MessagesListState extends State<MessagesList> {
|
||||||
_messageScrollController.position.maxScrollExtent > 0 && _messageCacheFutureComplete) {
|
_messageScrollController.position.maxScrollExtent > 0 && _messageCacheFutureComplete) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_messageCacheFutureComplete = false;
|
_messageCacheFutureComplete = false;
|
||||||
_messageCacheFuture = _clientHolder?.hub.getCache(widget.friend.id)
|
_messageCacheFuture = _clientHolder?.messagingClient.getMessageCache(widget.friend.id)
|
||||||
.then((value) => value.loadOlderMessages()).whenComplete(() => _messageCacheFutureComplete = true);
|
.then((value) => value.loadOlderMessages()).whenComplete(() => _messageCacheFutureComplete = true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -273,7 +273,7 @@ class _MessagesListState extends State<MessagesList> {
|
||||||
sendTime: DateTime.now().toUtc(),
|
sendTime: DateTime.now().toUtc(),
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
_clientHolder!.hub.sendMessage(message);
|
_clientHolder!.messagingClient.sendMessage(message);
|
||||||
_messageTextController.clear();
|
_messageTextController.clear();
|
||||||
setState(() {});
|
setState(() {});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
|
import 'package:contacts_plus_plus/auxiliary.dart';
|
||||||
import 'package:contacts_plus_plus/models/user.dart';
|
import 'package:contacts_plus_plus/models/user.dart';
|
||||||
import 'package:contacts_plus_plus/widgets/generic_avatar.dart';
|
import 'package:contacts_plus_plus/widgets/generic_avatar.dart';
|
||||||
import 'package:flutter/material.dart';
|
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, super.key});
|
const UserListTile({required this.user, required this.isFriend, super.key});
|
||||||
|
|
||||||
final User user;
|
final User user;
|
||||||
|
final bool isFriend;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<UserListTile> createState() => _UserListTileState();
|
State<UserListTile> createState() => _UserListTileState();
|
||||||
|
@ -14,12 +16,12 @@ class UserListTile extends StatefulWidget {
|
||||||
|
|
||||||
class _UserListTileState extends State<UserListTile> {
|
class _UserListTileState extends State<UserListTile> {
|
||||||
final DateFormat _regDateFormat = DateFormat.yMMMMd('en_US');
|
final DateFormat _regDateFormat = DateFormat.yMMMMd('en_US');
|
||||||
late bool _localAdded = widget.user.userProfile != null;
|
late bool _localAdded = widget.isFriend;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return ListTile(
|
return ListTile(
|
||||||
leading: GenericAvatar(imageUri: widget.user.userProfile?.iconUrl ?? "",),
|
leading: GenericAvatar(imageUri: Aux.neosDbToHttp(widget.user.userProfile?.iconUrl),),
|
||||||
title: Text(widget.user.username),
|
title: Text(widget.user.username),
|
||||||
subtitle: Text(_regDateFormat.format(widget.user.registrationDate)),
|
subtitle: Text(_regDateFormat.format(widget.user.registrationDate)),
|
||||||
trailing: IconButton(
|
trailing: IconButton(
|
||||||
|
|
|
@ -68,15 +68,15 @@ class _UserSearchState extends State<UserSearch> {
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (snapshot.hasData) {
|
if (snapshot.hasData) {
|
||||||
final users = (snapshot.data as List<User>);
|
final users = (snapshot.data as List<User>);
|
||||||
|
final mClient = ClientHolder.of(context).messagingClient;
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
itemCount: users.length,
|
itemCount: users.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
return UserListTile(user: users[index]);
|
final user = users[index];
|
||||||
|
return UserListTile(user: user, isFriend: mClient.getAsFriend(user.id) != null,);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
} else if (snapshot.hasError) {
|
} else if (snapshot.hasError) {
|
||||||
FlutterError.reportError(
|
|
||||||
FlutterErrorDetails(exception: snapshot.error!, stack: snapshot.stackTrace));
|
|
||||||
final err = snapshot.error;
|
final err = snapshot.error;
|
||||||
if (err is SearchError) {
|
if (err is SearchError) {
|
||||||
return DefaultErrorWidget(
|
return DefaultErrorWidget(
|
||||||
|
@ -84,6 +84,8 @@ class _UserSearchState extends State<UserSearch> {
|
||||||
iconOverride: err.icon,
|
iconOverride: err.icon,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
FlutterError.reportError(
|
||||||
|
FlutterErrorDetails(exception: snapshot.error!, stack: snapshot.stackTrace));
|
||||||
return DefaultErrorWidget(title: "${snapshot.error}",);
|
return DefaultErrorWidget(title: "${snapshot.error}",);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue