diff --git a/lib/network/search_service.dart b/lib/network/search_service.dart index b692f17..e3b0320 100644 --- a/lib/network/search_service.dart +++ b/lib/network/search_service.dart @@ -1,6 +1,10 @@ // search_service.dart +import 'dart:async'; + import 'package:graphql_flutter/graphql_flutter.dart'; +import '../models/enums/search_event_type.dart'; +import '../models/server_sent_event.dart'; import '../network/api_client.dart'; import '../network/authentication_service.dart'; import '../screens/chat.dart'; @@ -17,11 +21,15 @@ class SearchService { final AuthenticationService _authenticationService = AuthenticationService(); final ApiClient client = ApiClient(); + final _sseController = StreamController(); + + Stream get sseStream => _sseController.stream; + Future initializeAuthenticationService() async { await _authenticationService.initialize(); } - Future startPersonalWishlist(String message, Function(Message) handleSSEMessage) async { + Future startPersonalWishlist(String message) async { await _authenticationService.initialize(); final options = MutationOptions( @@ -40,10 +48,16 @@ class SearchService { {'text': message}, ); + StringBuffer fullMessage = StringBuffer(); // Використовуємо StringBuffer для зберігання повідомлення + await for (final chunk in sseStream) { - print('${chunk.event}: ${chunk.data}'); - handleSSEMessage(Message(text: '${chunk.event}: ${chunk.data}')); + fullMessage.write(chunk.data); // Додаємо чанк до повідомлення } + + final cleanedMessage = fullMessage.toString().replaceAll('"', ''); + + final event = ServerSentEvent(SearchEventType.message, cleanedMessage.toString().trim()); + _sseController.add(event); } } } diff --git a/lib/screens/chat.dart b/lib/screens/chat.dart index 865463c..f75790a 100644 --- a/lib/screens/chat.dart +++ b/lib/screens/chat.dart @@ -24,7 +24,7 @@ class MessageBubble extends StatelessWidget { margin: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(16.0), decoration: BoxDecoration( - color: isOutgoing ? Colors.blue : Colors.white, + color: isOutgoing ? Colors.blue : Colors.white38, borderRadius: BorderRadius.circular(10.0), ), child: Text( @@ -50,6 +50,14 @@ class ChatScreenState extends State { String wishlistId = ''; + @override + void initState() { + super.initState(); + _searchService.sseStream.listen((event) { + _handleSSEMessage(Message(text: '${event.event}: ${event.data}')); + }); + } + void _handleSSEMessage(Message message) { setState(() { messages.add(message); @@ -58,11 +66,11 @@ class ChatScreenState extends State { Future _startPersonalWishlist(String message) async { await _searchService.initializeAuthenticationService(); - await _searchService.startPersonalWishlist(message, _handleSSEMessage); + await _searchService.startPersonalWishlist(message); } Future _sendMessageToAPI(String message) async { - await _searchService.startPersonalWishlist(message, _handleSSEMessage); + await _searchService.startPersonalWishlist(message); setState(() { messages.add(Message(text: message, isUser: true));