mirror of
https://github.com/Shchoholiev/shopping-assistant-mobile-client.git
synced 2025-04-04 16:49:37 +00:00
add normal messages
This commit is contained in:
parent
0393a70f88
commit
7b3963fad7
@ -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<ServerSentEvent>();
|
||||
|
||||
Stream<ServerSentEvent> get sseStream => _sseController.stream;
|
||||
|
||||
Future<void> initializeAuthenticationService() async {
|
||||
await _authenticationService.initialize();
|
||||
}
|
||||
|
||||
Future<void> startPersonalWishlist(String message, Function(Message) handleSSEMessage) async {
|
||||
Future<void> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<ChatScreen> {
|
||||
|
||||
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<ChatScreen> {
|
||||
|
||||
Future<void> _startPersonalWishlist(String message) async {
|
||||
await _searchService.initializeAuthenticationService();
|
||||
await _searchService.startPersonalWishlist(message, _handleSSEMessage);
|
||||
await _searchService.startPersonalWishlist(message);
|
||||
}
|
||||
|
||||
Future<void> _sendMessageToAPI(String message) async {
|
||||
await _searchService.startPersonalWishlist(message, _handleSSEMessage);
|
||||
await _searchService.startPersonalWishlist(message);
|
||||
|
||||
setState(() {
|
||||
messages.add(Message(text: message, isUser: true));
|
||||
|
Loading…
Reference in New Issue
Block a user