diff --git a/filcnaplo_kreta_api/lib/providers/message_provider.dart b/filcnaplo_kreta_api/lib/providers/message_provider.dart index ace46d6..cafccde 100644 --- a/filcnaplo_kreta_api/lib/providers/message_provider.dart +++ b/filcnaplo_kreta_api/lib/providers/message_provider.dart @@ -12,14 +12,19 @@ import 'package:provider/provider.dart'; class MessageProvider with ChangeNotifier { late List _messages; + late List _recipients; late BuildContext _context; + List get messages => _messages; + List get recipients => _recipients; MessageProvider({ List initialMessages = const [], + List initialRecipients = const [], required BuildContext context, }) { _messages = List.castFrom(initialMessages); + _recipients = List.castFrom(initialRecipients); _context = context; if (_messages.isEmpty) restore(); @@ -93,6 +98,21 @@ class MessageProvider with ChangeNotifier { notifyListeners(); } + // restore recipients + Future restoreRecipients() async { + String? userId = Provider.of(_context, listen: false).id; + + // Load messages from the database + if (userId != null) { + var dbRecipients = + await Provider.of(_context, listen: false) + .userQuery + .getRecipients(userId: userId); + _recipients = dbRecipients; + notifyListeners(); + } + } + // fetch recipients Future fetchRecipients() async { Map addressable = {}; @@ -161,13 +181,15 @@ class MessageProvider with ChangeNotifier { // store recipients Future storeRecipients(List recipients) async { + _recipients.addAll(recipients); + User? user = Provider.of(_context, listen: false).user; if (user == null) throw "Cannot store Recipients for User null"; String userId = user.id; await Provider.of(_context, listen: false) .userStore - .storeMessages(_messages, userId: userId); + .storeRecipients(_recipients, userId: userId); notifyListeners(); }