feat(chat): add chat lazy loading, send message and websocket init for notifications and new chat messages

This commit is contained in:
alex
2024-07-05 22:28:03 +03:00
parent 7514a9b75f
commit 5f88f49c71
11 changed files with 216 additions and 113 deletions

View File

@@ -9,6 +9,38 @@ class InitializationCubit extends Cubit<InitializationState> {
InitializationCubit({required this.chatRepository}) : super(const InitializationState.loading());
Future<void> subscribeNotifications(int userId, PusherChannelsClient client) async {
final privateNotificationChannel = await client.privateChannel(
'private-message-events.$userId',
authorizationDelegate: EndpointAuthorizableChannelTokenAuthorizationDelegate.forPrivateChannel(
authorizationEndpoint: Uri.parse('https://mobile-stage.eatvio.ru/broadcasting/auth'),
headers: {
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
},
),
);
privateNotificationChannel.subscribe();
privateNotificationChannel.bindToAll().listen((event) {});
}
Future<void> subscribeChatMessages(int userId, PusherChannelsClient client) async {
final privateMessagesChannel = await client.privateChannel(
'private-new-chat-messages.$userId',
authorizationDelegate: EndpointAuthorizableChannelTokenAuthorizationDelegate.forPrivateChannel(
authorizationEndpoint: Uri.parse('https://mobile-stage.eatvio.ru/broadcasting/auth'),
headers: {
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
},
),
);
privateMessagesChannel.subscribe();
privateMessagesChannel.bindToAll().listen((event) {});
}
Future<void> runInitialization() async {
try {
await TokenStorage.haveTokenInStorage();
@@ -40,6 +72,10 @@ class InitializationCubit extends Cubit<InitializationState> {
),
);
await client.connect();
await subscribeNotifications(wsUrl.userId, client);
await subscribeChatMessages(wsUrl.userId, client);
emit(InitializationState.success(
client: client,
));