forked from firka/student-legacy
added almost everything to provider
This commit is contained in:
parent
7f9c22bdcd
commit
871c93050c
@ -12,14 +12,19 @@ import 'package:provider/provider.dart';
|
|||||||
|
|
||||||
class MessageProvider with ChangeNotifier {
|
class MessageProvider with ChangeNotifier {
|
||||||
late List<Message> _messages;
|
late List<Message> _messages;
|
||||||
|
late List<SendRecipient> _recipients;
|
||||||
late BuildContext _context;
|
late BuildContext _context;
|
||||||
|
|
||||||
List<Message> get messages => _messages;
|
List<Message> get messages => _messages;
|
||||||
|
List<SendRecipient> get recipients => _recipients;
|
||||||
|
|
||||||
MessageProvider({
|
MessageProvider({
|
||||||
List<Message> initialMessages = const [],
|
List<Message> initialMessages = const [],
|
||||||
|
List<SendRecipient> initialRecipients = const [],
|
||||||
required BuildContext context,
|
required BuildContext context,
|
||||||
}) {
|
}) {
|
||||||
_messages = List.castFrom(initialMessages);
|
_messages = List.castFrom(initialMessages);
|
||||||
|
_recipients = List.castFrom(initialRecipients);
|
||||||
_context = context;
|
_context = context;
|
||||||
|
|
||||||
if (_messages.isEmpty) restore();
|
if (_messages.isEmpty) restore();
|
||||||
@ -93,6 +98,21 @@ class MessageProvider with ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// restore recipients
|
||||||
|
Future<void> restoreRecipients() async {
|
||||||
|
String? userId = Provider.of<UserProvider>(_context, listen: false).id;
|
||||||
|
|
||||||
|
// Load messages from the database
|
||||||
|
if (userId != null) {
|
||||||
|
var dbRecipients =
|
||||||
|
await Provider.of<DatabaseProvider>(_context, listen: false)
|
||||||
|
.userQuery
|
||||||
|
.getRecipients(userId: userId);
|
||||||
|
_recipients = dbRecipients;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// fetch recipients
|
// fetch recipients
|
||||||
Future<void> fetchRecipients() async {
|
Future<void> fetchRecipients() async {
|
||||||
Map<AddresseeType, SendRecipientType> addressable = {};
|
Map<AddresseeType, SendRecipientType> addressable = {};
|
||||||
@ -161,13 +181,15 @@ class MessageProvider with ChangeNotifier {
|
|||||||
|
|
||||||
// store recipients
|
// store recipients
|
||||||
Future<void> storeRecipients(List<SendRecipient> recipients) async {
|
Future<void> storeRecipients(List<SendRecipient> recipients) async {
|
||||||
|
_recipients.addAll(recipients);
|
||||||
|
|
||||||
User? user = Provider.of<UserProvider>(_context, listen: false).user;
|
User? user = Provider.of<UserProvider>(_context, listen: false).user;
|
||||||
if (user == null) throw "Cannot store Recipients for User null";
|
if (user == null) throw "Cannot store Recipients for User null";
|
||||||
|
|
||||||
String userId = user.id;
|
String userId = user.id;
|
||||||
await Provider.of<DatabaseProvider>(_context, listen: false)
|
await Provider.of<DatabaseProvider>(_context, listen: false)
|
||||||
.userStore
|
.userStore
|
||||||
.storeMessages(_messages, userId: userId);
|
.storeRecipients(_recipients, userId: userId);
|
||||||
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user