Refactor message cache handling

This commit is contained in:
Nutcake 2023-05-02 10:04:54 +02:00
parent 1f945234cf
commit 8417a29ffe
6 changed files with 121 additions and 167 deletions

View file

@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:contacts_plus_plus/neos_hub.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
@ -124,9 +125,13 @@ class ApiClient {
class ClientHolder extends InheritedWidget { class ClientHolder extends InheritedWidget {
final ApiClient client; final ApiClient client;
late final NeosHub hub;
ClientHolder({super.key, required AuthenticationData authenticationData, required super.child}) ClientHolder({super.key, required AuthenticationData authenticationData, required super.child})
: client = ApiClient(authenticationData: authenticationData); : client = ApiClient(authenticationData: authenticationData) {
hub = NeosHub(apiClient: client);
}
static ClientHolder? maybeOf(BuildContext context) { static ClientHolder? maybeOf(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<ClientHolder>(); return context.dependOnInheritedWidgetOfExactType<ClientHolder>();

View file

@ -53,3 +53,13 @@ class Aux {
return fullUri; return fullUri;
} }
} }
extension Unique<E, Id> on List<E> {
List<E> unique([Id Function(E element)? id, bool inplace = true]) {
final ids = <Id>{};
var list = inplace ? this : List<E>.from(this);
list.retainWhere((x) => ids.add(id != null ? id(x) : x as Id));
return list;
}
}

View file

@ -1,5 +1,3 @@
import 'package:contacts_plus_plus/models/message.dart';
import 'package:contacts_plus_plus/neos_hub.dart';
import 'package:contacts_plus_plus/widgets/home_screen.dart'; import 'package:contacts_plus_plus/widgets/home_screen.dart';
import 'package:contacts_plus_plus/widgets/login_screen.dart'; import 'package:contacts_plus_plus/widgets/login_screen.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -20,14 +18,10 @@ class ContactsPlusPlus extends StatefulWidget {
class _ContactsPlusPlusState extends State<ContactsPlusPlus> { class _ContactsPlusPlusState extends State<ContactsPlusPlus> {
final Typography _typography = Typography.material2021(platform: TargetPlatform.android); final Typography _typography = Typography.material2021(platform: TargetPlatform.android);
AuthenticationData _authData = AuthenticationData.unauthenticated(); AuthenticationData _authData = AuthenticationData.unauthenticated();
final Map<String, MessageCache> _messageCache = {};
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return HubHolder( return ClientHolder(
messageCache: _messageCache,
authenticationData: _authData,
child: ClientHolder(
authenticationData: _authData, authenticationData: _authData,
child: MaterialApp( child: MaterialApp(
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
@ -49,7 +43,6 @@ class _ContactsPlusPlusState extends State<ContactsPlusPlus> {
}, },
), ),
), ),
),
); );
} }
} }

View file

@ -1,6 +1,9 @@
import 'dart:async'; import 'dart:async';
import 'dart:developer'; import 'dart:developer';
import 'package:contacts_plus_plus/api_client.dart';
import 'package:contacts_plus_plus/apis/message_api.dart';
import 'package:contacts_plus_plus/auxiliary.dart';
import 'package:contacts_plus_plus/config.dart'; import 'package:contacts_plus_plus/config.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
@ -95,10 +98,54 @@ class Message extends Comparable {
} }
class MessageCache { class MessageCache {
final List<Message> _messages; final List<Message> _messages = [];
final ApiClient _apiClient;
final String _userId;
Future? currentOperation;
List<Message> get messages => _messages; List<Message> get messages => _messages;
MessageCache({required List<Message> messages}) MessageCache({required ApiClient apiClient, required String userId}) : _apiClient = apiClient, _userId = userId;
: _messages = messages;
/// Adds a message to the cache, ensuring integrity of the message cache.
/// Returns true if the message was inserted into the cache and false if an existing message was overwritten.
bool addMessage(Message message) {
final existingIdx = _messages.indexWhere((element) => element.id == message.id);
if (existingIdx == -1) {
_messages.add(message);
_ensureIntegrity();
} else {
_messages[existingIdx] = message;
_messages.sort(); // Overwriting can't create duplicates, so we just need to sort.
}
return existingIdx == -1;
}
/// Sets the state of an existing message by id.
/// Returns true if a message with the specified id exists and was modified and false if the message doesn't exist.
bool setMessageState(String messageId, MessageState state) {
final messageIdx = _messages.indexWhere((element) => element.id == messageId);
if (messageIdx == -1) return false;
_messages[messageIdx] = _messages[messageIdx].copyWith(state: state);
return true;
}
Future<void> loadOlderMessages() async {
final oldest = _messages.first;
final olderMessages = await MessageApi.getUserMessages(_apiClient, userId: _userId, fromTime: oldest.sendTime);
_messages.insertAll(0, olderMessages);
_ensureIntegrity();
}
Future<void> loadInitialMessages() async {
final messages = await MessageApi.getUserMessages(_apiClient, userId: _userId);
_messages.clear();
_messages.addAll(messages);
_ensureIntegrity();
}
void _ensureIntegrity() {
_messages.sort();
_messages.unique((element) => element.id);
}
} }

View file

@ -1,7 +1,6 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:developer'; import 'dart:developer';
import 'package:contacts_plus_plus/models/authentication_data.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
@ -32,30 +31,34 @@ enum EventTarget {
class NeosHub { class NeosHub {
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";
final AuthenticationData _authenticationData; final ApiClient _apiClient;
final Map<String, MessageCache> _messageCache; final Map<String, MessageCache> _messageCache = {};
final Map<String, Function> _updateListeners = {}; final Map<String, Function> _updateListeners = {};
WebSocketChannel? _wsChannel; WebSocketChannel? _wsChannel;
NeosHub({required AuthenticationData authenticationData, required Map<String, MessageCache> messageCache}) NeosHub({required ApiClient apiClient})
: _authenticationData = authenticationData, _messageCache = messageCache { : _apiClient = apiClient {
start(); start();
} }
MessageCache? getCache(String index) => _messageCache[index]; Future<MessageCache> getCache(String userId) async {
var cache = _messageCache[userId];
void setCache(String index, List<Message> messages) { if (cache == null){
_messageCache[index] = MessageCache(messages: messages); cache = MessageCache(apiClient: _apiClient, userId: userId);
await cache.loadInitialMessages();
_messageCache[userId] = cache;
}
return cache;
} }
Future<void> start() async { Future<void> start() async {
if (!_authenticationData.isAuthenticated) { if (!_apiClient.isAuthenticated) {
log("Hub not authenticated."); log("Hub not authenticated.");
return; return;
} }
final response = await http.post( final response = await http.post(
Uri.parse("${Config.neosHubUrl}/negotiate"), Uri.parse("${Config.neosHubUrl}/negotiate"),
headers: _authenticationData.authorizationHeader, headers: _apiClient.authorizationHeader,
); );
ApiClient.checkResponse(response); ApiClient.checkResponse(response);
@ -94,7 +97,7 @@ class NeosHub {
} }
} }
void _handleMessageEvent(body) { void _handleMessageEvent(body) async {
final target = EventTarget.parse(body["target"]); final target = EventTarget.parse(body["target"]);
final args = body["arguments"]; final args = body["arguments"];
switch (target) { switch (target) {
@ -104,49 +107,30 @@ 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);
var cache = getCache(message.recipientId); final cache = await getCache(message.recipientId);
if (cache == null) { cache.addMessage(message);
setCache(message.recipientId, [message]);
} else {
// Possible race condition
final existingIndex = cache.messages.indexWhere((element) => element.id == message.id);
if (existingIndex == -1) {
cache.messages.add(message);
} else {
cache.messages[existingIndex] = message;
}
cache.messages.sort();
}
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);
var cache = getCache(message.senderId); final cache = await getCache(message.senderId);
if (cache == null) { cache.addMessage(message);
setCache(message.senderId, [message]);
} else {
cache.messages.add(message);
cache.messages.sort();
}
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 = getCache(recipientId ?? ""); final cache = await getCache(recipientId ?? "");
if (cache == null) return;
for (var id in messageIds) { for (var id in messageIds) {
final idx = cache.messages.indexWhere((element) => element.id == id); cache.setMessageState(id, MessageState.read);
if (idx == -1) continue;
cache.messages[idx] = cache.messages[idx].copyWith(state: MessageState.read);
} }
notifyListener(recipientId); notifyListener(recipientId);
break; break;
} }
} }
void sendMessage(Message message) { void sendMessage(Message message) async {
if (_wsChannel == null) throw "Neos Hub is not connected"; if (_wsChannel == null) throw "Neos Hub is not connected";
final msgBody = message.toMap(); final msgBody = message.toMap();
final data = { final data = {
@ -156,35 +140,9 @@ class NeosHub {
msgBody msgBody
], ],
}; };
_wsChannel!.sink.add(jsonEncode(data)+eofChar); final cache = await getCache(message.recipientId);
var cache = _messageCache[message.recipientId];
if (cache == null) {
setCache(message.recipientId, [message]);
cache = getCache(message.recipientId);
} else {
cache.messages.add(message); cache.messages.add(message);
} _wsChannel!.sink.add(jsonEncode(data)+eofChar);
notifyListener(message.recipientId); notifyListener(message.recipientId);
} }
} }
class HubHolder extends InheritedWidget {
HubHolder({super.key, required AuthenticationData authenticationData, required Map<String, MessageCache> messageCache, required super.child})
: hub = NeosHub(authenticationData: authenticationData, messageCache: messageCache);
final NeosHub hub;
static HubHolder? maybeOf(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<HubHolder>();
}
static HubHolder of(BuildContext context) {
final HubHolder? result = maybeOf(context);
assert(result != null, 'No HubHolder found in context');
return result!;
}
@override
bool updateShouldNotify(covariant HubHolder oldWidget) => hub._authenticationData != oldWidget.hub._authenticationData
|| hub._messageCache != oldWidget.hub._messageCache;
}

View file

@ -1,14 +1,11 @@
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
import 'package:contacts_plus_plus/api_client.dart'; import 'package:contacts_plus_plus/api_client.dart';
import 'package:contacts_plus_plus/apis/message_api.dart';
import 'package:contacts_plus_plus/auxiliary.dart'; import 'package:contacts_plus_plus/auxiliary.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/neos_hub.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:flutter/services.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
class Messages extends StatefulWidget { class Messages extends StatefulWidget {
@ -21,54 +18,34 @@ class Messages extends StatefulWidget {
} }
class _MessagesState extends State<Messages> { class _MessagesState extends State<Messages> {
Future<Iterable<Message>>? _messagesFuture; Future<MessageCache>? _messageCacheFuture;
final TextEditingController _messageTextController = TextEditingController(); final TextEditingController _messageTextController = TextEditingController();
final ScrollController _sessionListScrollController = ScrollController(); final ScrollController _sessionListScrollController = ScrollController();
ClientHolder? _clientHolder; ClientHolder? _clientHolder;
HubHolder? _cacheHolder;
bool _isSendable = false; bool _isSendable = false;
bool _showSessionListChevron = false; bool _showSessionListChevron = false;
double get _shevronOpacity => _showSessionListChevron ? 1.0 : 0.0; double get _shevronOpacity => _showSessionListChevron ? 1.0 : 0.0;
void _loadMessages() {
final cache = _cacheHolder?.hub.getCache(widget.friend.id);
if (cache != null) {
_messagesFuture = Future(() => cache.messages);
} else {
_messagesFuture = MessageApi.getUserMessages(
_clientHolder!.client, userId: widget.friend.id)
..then((value) {
final list = value.toList();
list.sort();
_cacheHolder?.hub.setCache(widget.friend.id, list);
_cacheHolder?.hub.registerListener(
widget.friend.id, () => setState(() {}));
return list;
});
}
}
@override @override
void didChangeDependencies() { void didChangeDependencies() {
super.didChangeDependencies(); super.didChangeDependencies();
final clientHolder = ClientHolder.of(context); final clientHolder = ClientHolder.of(context);
bool dirty = false;
if (_clientHolder != clientHolder) { if (_clientHolder != clientHolder) {
_clientHolder = clientHolder; _clientHolder = clientHolder;
dirty = true;
} }
final cacheHolder = HubHolder.of(context); _loadMessages();
if (_cacheHolder != cacheHolder) {
_cacheHolder = cacheHolder;
dirty = true;
} }
if (dirty) _loadMessages();
void _loadMessages() {
_messageCacheFuture = _clientHolder?.hub.getCache(widget.friend.id);
_clientHolder?.hub.registerListener(
widget.friend.id, () => setState(() {}));
} }
@override @override
void dispose() { void dispose() {
_cacheHolder?.hub.unregisterListener(widget.friend.id); _clientHolder?.hub.unregisterListener(widget.friend.id);
_messageTextController.dispose(); _messageTextController.dispose();
_sessionListScrollController.dispose(); _sessionListScrollController.dispose();
super.dispose(); super.dispose();
@ -107,52 +84,19 @@ class _MessagesState extends State<Messages> {
title: Text(widget.friend.username), title: Text(widget.friend.username),
scrolledUnderElevation: 0.0, scrolledUnderElevation: 0.0,
backgroundColor: appBarColor, backgroundColor: appBarColor,
/*bottom: sessions.isEmpty ? null : PreferredSize(
preferredSize: Size.fromHeight(_headerHeight),
child: Column(
children: sessions.getRange(0, _headerExpanded ? sessions.length : 1).map((e) => Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: GenericAvatar(imageUri: Aux.neosDbToHttp(e.thumbnail),),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(e.name),
Text("${e.sessionUsers.length} users active"),
],
),
),
const Spacer(),
if (sessions.length > 1) TextButton(onPressed: (){
setState(() {
_headerExpanded = !_headerExpanded;
});
}, child: Text("+${sessions.length-1}"),)
],
)).toList(),
),
),*/
), ),
body: Stack( body: Stack(
children: [ children: [
FutureBuilder( FutureBuilder(
future: _messagesFuture, future: _messageCacheFuture,
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.hasData) { if (snapshot.hasData) {
final data = _cacheHolder?.hub final cache = snapshot.data as MessageCache;
.getCache(widget.friend.id)
?.messages ?? [];
return ListView.builder( return ListView.builder(
reverse: true, reverse: true,
itemCount: data.length, itemCount: cache.messages.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
final entry = data.elementAt(index); final entry = cache.messages[index];
return entry.senderId == apiClient.userId return entry.senderId == apiClient.userId
? MyMessageBubble(message: entry) ? MyMessageBubble(message: entry)
: OtherMessageBubble(message: entry); : OtherMessageBubble(message: entry);
@ -298,7 +242,7 @@ class _MessagesState extends State<Messages> {
padding: const EdgeInsets.only(left: 8, right: 4.0), padding: const EdgeInsets.only(left: 8, right: 4.0),
child: IconButton( child: IconButton(
splashRadius: 24, splashRadius: 24,
onPressed: _isSendable ? () async { onPressed: _isSendable && _clientHolder != null ? () async {
setState(() { setState(() {
_isSendable = false; _isSendable = false;
}); });
@ -311,10 +255,7 @@ class _MessagesState extends State<Messages> {
sendTime: DateTime.now().toUtc(), sendTime: DateTime.now().toUtc(),
); );
try { try {
if (_cacheHolder == null) { _clientHolder!.hub.sendMessage(message);
throw "Hub not connected.";
}
_cacheHolder!.hub.sendMessage(message);
_messageTextController.clear(); _messageTextController.clear();
setState(() {}); setState(() {});
} catch (e) { } catch (e) {