From 7eac7fb9fb54197b6e3e2ec42d9fc26015535e02 Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Thu, 14 Dec 2023 21:37:14 +0200 Subject: [PATCH 1/3] add ability to leave messages by pressing the button every time you open New Chat tab it really opens NEW chat --- lib/main.dart | 4 +-- lib/network/search_service.dart | 2 +- lib/screens/chat.dart | 45 +++++++++++++++++++++++++-------- lib/screens/wishlists.dart | 2 +- pubspec.lock | 26 +++++++++---------- 5 files changed, 51 insertions(+), 28 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 5be5953..6b06b96 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -18,7 +18,7 @@ class MyApp extends StatefulWidget { static List _widgetOptions = [ WishlistsScreen(), - ChatScreen(wishlistId: '', wishlistName: 'New Chat',), + ChatScreen(wishlistId: '', wishlistName: 'New Chat', openedFromBottomBar: true), Text(''), ]; @@ -139,4 +139,4 @@ class _MyAppState extends State { // // .then((result) => print(jsonEncode(result))); // // sleep(Duration(milliseconds: 100)); // // } -// \ No newline at end of file +// diff --git a/lib/network/search_service.dart b/lib/network/search_service.dart index 2197852..f59bdcd 100644 --- a/lib/network/search_service.dart +++ b/lib/network/search_service.dart @@ -142,4 +142,4 @@ class SearchService { } return []; } -} \ No newline at end of file +} diff --git a/lib/screens/chat.dart b/lib/screens/chat.dart index 95f8c46..6eec81b 100644 --- a/lib/screens/chat.dart +++ b/lib/screens/chat.dart @@ -65,8 +65,9 @@ class MessageBubble extends StatelessWidget { class ChatScreen extends StatefulWidget { String wishlistId; String wishlistName; + bool openedFromBottomBar; - ChatScreen({Key? key, required this.wishlistId, required this.wishlistName}) : super(key: key); + ChatScreen({Key? key, required this.wishlistId, required this.wishlistName, required this.openedFromBottomBar}) : super(key: key); @override State createState() => ChatScreenState(); @@ -74,18 +75,22 @@ class ChatScreen extends StatefulWidget { class ChatScreenState extends State { var logger = Logger(); - final SearchService _searchService = SearchService(); + SearchService _searchService = SearchService(); List messages = []; - final TextEditingController _messageController = TextEditingController(); + TextEditingController _messageController = TextEditingController(); + bool showBackButton = false; bool buttonsVisible = true; bool isSendButtonEnabled = false; bool showButtonsContainer = true; bool isWaitingForResponse = false; - final ScrollController _scrollController = ScrollController(); + ScrollController _scrollController = ScrollController(); late Widget appBarTitle; void initState() { super.initState(); + if (widget.openedFromBottomBar) { + _resetState(); + } appBarTitle = Text('New Chat', style: TextStyle(fontSize: 18.0)); _searchService.sseStream.listen((event) { _handleSSEMessage(Message(text: '${event.data}')); @@ -94,11 +99,27 @@ class ChatScreenState extends State { if(!widget.wishlistId.isEmpty) { _loadPreviousMessages(); + showBackButton = true; showButtonsContainer = false; buttonsVisible = false; } } + void _resetState() { + widget.wishlistId = ''; + widget.wishlistName = ''; + _searchService = SearchService(); + messages = []; + _messageController = TextEditingController(); + showBackButton = false; + buttonsVisible = true; + isSendButtonEnabled = false; + showButtonsContainer = true; + isWaitingForResponse = false; + _scrollController = ScrollController(); + appBarTitle = const Text('New Chat', style: TextStyle(fontSize: 18.0)); + } + Future _loadPreviousMessages() async { final pageNumber = 1; final pageSize = 200; @@ -239,12 +260,14 @@ class ChatScreenState extends State { appBar: AppBar( title: appBarTitle, centerTitle: true, - leading: IconButton( - icon: Icon(Icons.arrow_back), - onPressed: () { - print('Back button pressed'); - }, - ), + leading: showBackButton + ? IconButton( + icon: Icon(Icons.arrow_back), + onPressed: () { + Navigator.of(context).pop(); + }, + ) + : null, ), body: Column( children: [ @@ -416,4 +439,4 @@ class ChatScreenState extends State { ), ); } -} \ No newline at end of file +} diff --git a/lib/screens/wishlists.dart b/lib/screens/wishlists.dart index ea8e434..1cdf178 100644 --- a/lib/screens/wishlists.dart +++ b/lib/screens/wishlists.dart @@ -218,7 +218,7 @@ class _WishlistItemState extends State { Navigator.push( context, MaterialPageRoute( - builder: (context) => ChatScreen(wishlistId: widget._wishlist.id, wishlistName: widget._wishlist.name), + builder: (context) => ChatScreen(wishlistId: widget._wishlist.id, wishlistName: widget._wishlist.name, openedFromBottomBar: false), ), ); }, diff --git a/pubspec.lock b/pubspec.lock index c20e2af..86567a6 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -45,10 +45,10 @@ packages: dependency: transitive description: name: collection - sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted - version: "1.17.2" + version: "1.18.0" connectivity_plus: dependency: transitive description: @@ -308,10 +308,10 @@ packages: dependency: transitive description: name: meta - sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.10.0" nm: dependency: transitive description: @@ -497,18 +497,18 @@ packages: dependency: transitive description: name: stack_trace - sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.11.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" string_scanner: dependency: transitive description: @@ -529,10 +529,10 @@ packages: dependency: transitive description: name: test_api - sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" + sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" url: "https://pub.dev" source: hosted - version: "0.6.0" + version: "0.6.1" typed_data: dependency: transitive description: @@ -649,10 +649,10 @@ packages: dependency: transitive description: name: web - sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10 + sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152 url: "https://pub.dev" source: hosted - version: "0.1.4-beta" + version: "0.3.0" web_socket_channel: dependency: transitive description: @@ -694,5 +694,5 @@ packages: source: hosted version: "3.1.2" sdks: - dart: ">=3.1.4 <4.0.0" + dart: ">=3.2.0-194.0.dev <4.0.0" flutter: ">=3.13.0" From 84268619fd45352ce66d790fd999499da88c3849 Mon Sep 17 00:00:00 2001 From: stasex Date: Fri, 15 Dec 2023 14:41:05 +0200 Subject: [PATCH 2/3] added suggestions display --- lib/network/search_service.dart | 2 +- lib/screens/chat.dart | 79 ++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/lib/network/search_service.dart b/lib/network/search_service.dart index 2197852..697ab5f 100644 --- a/lib/network/search_service.dart +++ b/lib/network/search_service.dart @@ -31,7 +31,7 @@ class SearchService { } bool checkerForSuggestion() { - return type == SearchEventType.product; + return type == SearchEventType.suggestion; } String? wishlistId; diff --git a/lib/screens/chat.dart b/lib/screens/chat.dart index 95f8c46..e6384ad 100644 --- a/lib/screens/chat.dart +++ b/lib/screens/chat.dart @@ -76,6 +76,7 @@ class ChatScreenState extends State { var logger = Logger(); final SearchService _searchService = SearchService(); List messages = []; + List suggestions = []; final TextEditingController _messageController = TextEditingController(); bool buttonsVisible = true; bool isSendButtonEnabled = false; @@ -129,7 +130,11 @@ class ChatScreenState extends State { message.isSuggestion = _searchService.checkerForSuggestion(); bool checker = false; logger.d("Product status: ${message.isProduct}"); - if (lastMessage != null && lastMessage.role != "User" && message.role != "User") { + logger.d("Suggestion status: ${message.isSuggestion}"); + if(message.isSuggestion){ + suggestions.add(message.text); + } + if (lastMessage != null && lastMessage.role != "User" && message.role != "User" && !message.isSuggestion && message.text != "/n") { final updatedMessage = Message( text: "${lastMessage.text}${message.text}", role: "Application", @@ -137,7 +142,9 @@ class ChatScreenState extends State { messages.removeLast(); messages.add(updatedMessage); } else { - messages.add(message); + if(!message.isSuggestion && message.text != "/n"){ + messages.add(message); + } } }); setState(() { @@ -205,6 +212,13 @@ class ChatScreenState extends State { _scrollToBottom(); } + void _handleSuggestion(String suggestion) { + _messageController.text = suggestion; + _sendMessage(); + suggestions.clear(); + } + + void _scrollToBottom() { _scrollController.animateTo( _scrollController.position.maxScrollExtent, @@ -233,6 +247,46 @@ class ChatScreenState extends State { ); } + Widget _generateSuggestionButtons() { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8), + child: Text( + 'Several possible options', + style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.grey), + ), + ), + SingleChildScrollView( + scrollDirection: Axis.horizontal, + child: Row( + children: suggestions.map((suggestion) { + return Padding( + padding: EdgeInsets.symmetric(horizontal: 8, vertical: 5), + child: ElevatedButton( + onPressed: () { + _handleSuggestion(suggestion); + }, + style: ElevatedButton.styleFrom( + padding: EdgeInsets.symmetric(horizontal: 30, vertical: 16), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(10), + ), + primary: Colors.white, + onPrimary: Colors.blue, + side: BorderSide(color: Colors.blue, width: 2.0), + ), + child: Text(suggestion, style: TextStyle(color: Colors.black)), + ), + ); + }).toList(), + ), + ), + ], + ); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -366,24 +420,8 @@ class ChatScreenState extends State { color: Colors.blue, size: 25.0, ), - if (messages.any((message) => message.isSuggestion)) - Container( - padding: EdgeInsets.all(8.0), - color: Colors.grey[300], - child: Row( - children: [ - Icon(Icons.lightbulb), - SizedBox(width: 8.0), - Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: messages - .where((message) => message.isSuggestion) - .map((message) => Text(message.text)) - .toList(), - ), - ], - ), - ), + if (suggestions.isNotEmpty) + _generateSuggestionButtons(), Container( margin: const EdgeInsets.all(8.0), child: Row( @@ -403,6 +441,7 @@ class ChatScreenState extends State { contentPadding: EdgeInsets.symmetric(vertical: 20.0), ), ), + ), ), IconButton( From a47a5ba490a735d7ae36a826485d545cbb35457e Mon Sep 17 00:00:00 2001 From: stasex Date: Fri, 15 Dec 2023 15:20:05 +0200 Subject: [PATCH 3/3] deleted display "\n" --- lib/screens/chat.dart | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/screens/chat.dart b/lib/screens/chat.dart index e6384ad..fd78bc3 100644 --- a/lib/screens/chat.dart +++ b/lib/screens/chat.dart @@ -128,25 +128,30 @@ class ChatScreenState extends State { final lastMessage = messages.isNotEmpty ? messages.last : null; message.isProduct = _searchService.checkerForProduct(); message.isSuggestion = _searchService.checkerForSuggestion(); - bool checker = false; - logger.d("Product status: ${message.isProduct}"); - logger.d("Suggestion status: ${message.isSuggestion}"); if(message.isSuggestion){ suggestions.add(message.text); } - if (lastMessage != null && lastMessage.role != "User" && message.role != "User" && !message.isSuggestion && message.text != "/n") { + logger.d("Product status: ${message.isProduct}"); + logger.d("Suggestion status: ${message.isSuggestion}"); + logger.d("Message text: ${message.text}"); + if (lastMessage != null && lastMessage.role != "User" && message.role != "User" && !message.isSuggestion) { + String fullMessageText = lastMessage.text + message.text; + fullMessageText = fullMessageText.replaceAll("\\n", ""); + logger.d("fullMessageText: $fullMessageText"); final updatedMessage = Message( - text: "${lastMessage.text}${message.text}", + text: fullMessageText, role: "Application", isProduct: message.isProduct); messages.removeLast(); messages.add(updatedMessage); } else { - if(!message.isSuggestion && message.text != "/n"){ - messages.add(message); + String messageText = message.text.replaceAll("\\n", ""); + if (!message.isSuggestion) { + messages.add(Message(text: messageText, role: message.role, isProduct: message.isProduct, isSuggestion: message.isSuggestion)); } } }); + setState(() { isWaitingForResponse = false; }); @@ -209,6 +214,7 @@ class ChatScreenState extends State { } _messageController.clear(); + suggestions.clear(); _scrollToBottom(); }