-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from les-crepes/159-afficher-les-chats-du-dié…
…téticien 159 afficher les chats du diététicien
- Loading branch information
Showing
17 changed files
with
399 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import 'dart:collection'; | ||
import 'dart:developer'; | ||
|
||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:pdg_app/api/firebase_message.dart'; | ||
import 'package:sorted_list/sorted_list.dart'; | ||
|
||
import '../api/firebase_user.dart'; | ||
import '../api/imessage.dart'; | ||
import '../api/iuser.dart'; | ||
import '../model/message.dart'; | ||
import '../model/user.dart'; | ||
import 'auth_provider.dart'; | ||
|
||
typedef Uid = String; | ||
|
||
class ChatProvider extends ChangeNotifier { | ||
/// Getting the instance of the AuthProvider class. | ||
final AuthProvider _auth; | ||
final IMessage _messageApi = FirebaseMessage(FirebaseFirestore.instance); | ||
final IUser _userApi = FirebaseUser(FirebaseFirestore.instance); | ||
|
||
/// The list of messages using a [LinkedHashMap] to keep the order of the messages. | ||
final Map<User, SortedList<Message>> _messages = {}; | ||
|
||
ChatProvider(AuthProvider authProvider) : _auth = authProvider; | ||
|
||
Map<User, SortedList<Message>> get messages => UnmodifiableMapView(_messages); | ||
|
||
/// Returning an unmodifiable list view of the messages. | ||
UnmodifiableListView<Message> getMessagesWithUid(String uid) { | ||
final messages = _messages[uid]; | ||
if (messages != null) { | ||
return UnmodifiableListView(messages); | ||
} | ||
return UnmodifiableListView([]); | ||
} | ||
|
||
UnmodifiableListView<MapEntry<User, Message>> getLastMessageOfEachUser() { | ||
final result = | ||
_messages.entries.where((element) => element.value.isNotEmpty).map((e) { | ||
MapEntry<User, Message> entry = MapEntry(e.key, e.value.last); | ||
return entry; | ||
}).toList(); | ||
return UnmodifiableListView(result); | ||
} | ||
|
||
/// Fetching all the messages from the database. | ||
Future<void> fetchAllMessages() async { | ||
List<User> users = []; | ||
|
||
if (_auth.isAdmin) { | ||
final clients = await _userApi.getDietitianClient(_auth.userUid); | ||
users.addAll(clients); | ||
} else { | ||
final diet = await _userApi.readDietitianOfClient(_auth.userUid); | ||
if (diet != null) { | ||
users.add(diet); | ||
} | ||
} | ||
final List<Future<List<Message>?>> functions = []; | ||
for (final user in users) { | ||
functions.add(_messageApi.readConversation(_auth.userUid, user.uid)); | ||
} | ||
|
||
final futureResult = await Future.wait(functions); | ||
|
||
int index = 0; | ||
for (final conv in futureResult) { | ||
final sortedList = | ||
SortedList<Message>((a, b) => a.time.compareTo(b.time)); | ||
|
||
sortedList.addAll(conv!); | ||
|
||
_messages[users[index]] = sortedList; | ||
index++; | ||
} | ||
log(_messages.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'package:auto_route/auto_route.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:get_it/get_it.dart'; | ||
import 'package:pdg_app/provider/auth_provider.dart'; | ||
import 'package:pdg_app/provider/chat_provider.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
class ChatRouterPage extends StatelessWidget { | ||
const ChatRouterPage({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ChangeNotifierProvider( | ||
create: (context) => ChatProvider(GetIt.I.get<AuthProvider>()), | ||
builder: (context, child) { | ||
return FutureBuilder( | ||
future: context.read<ChatProvider>().fetchAllMessages(), | ||
builder: (context, snapshot) => | ||
snapshot.connectionState == ConnectionState.done | ||
? const AutoRouter() | ||
: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
crossAxisAlignment: CrossAxisAlignment.center, | ||
children: const [CircularProgressIndicator()]), | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.