Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions mobile/lib/controllers/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ class ChatController extends ChangeNotifier {
}

Future<void> queryUsers(String term) async {
if (term.trim().isEmpty) {
final String cleanTerm = term.trim();

if (cleanTerm.isEmpty) {
contactSearchResults.clear();
notifyListeners();
return;
}

if (cleanTerm.length < 3) {
contactSearchResults.clear();
notifyListeners();
return;
Expand All @@ -97,7 +105,7 @@ class ChatController extends ChangeNotifier {
notifyListeners();

try {
final res = await _api.searchUsers(term);
final res = await _api.searchUsers(cleanTerm);
if (res.data == null) {
contactSearchResults = [];
} else if (res.data is List) {
Expand Down
31 changes: 26 additions & 5 deletions mobile/lib/pages/new_chat_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class _NewChatPageState extends State<NewChatPage> {
autofocus: true,
style: const TextStyle(color: Colors.black87),
decoration: const InputDecoration(
hintText: "Enter exact username...",
hintText: "Enter minimum 3 characters...",
hintStyle: TextStyle(color: Colors.black38),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black26),
Expand All @@ -73,13 +73,22 @@ class _NewChatPageState extends State<NewChatPage> {
),
onPressed: () {
final text = inputController.text.trim();
if (text.isNotEmpty) {
if (text.length >= 3) {
context.read<ChatController>().queryUsers(text);
setState(() {
_searchController.text = text;
});
Navigator.pop(context);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"Search query must be at least 3 characters.",
),
backgroundColor: Colors.redAccent,
),
);
}
Navigator.pop(context);
},
child: const Text("Search", style: TextStyle(color: Colors.white)),
),
Expand Down Expand Up @@ -115,7 +124,9 @@ class _NewChatPageState extends State<NewChatPage> {
@override
Widget build(BuildContext context) {
final chatState = context.watch<ChatController>();
final bool isSearching = _searchController.text.trim().isNotEmpty;
final String searchInput = _searchController.text.trim();
final bool isSearching = searchInput.isNotEmpty;
final bool hasValidQueryLength = searchInput.length >= 3;

return Scaffold(
backgroundColor: Colors.white,
Expand Down Expand Up @@ -172,7 +183,17 @@ class _NewChatPageState extends State<NewChatPage> {
),
Expanded(
child: isSearching
? (chatState.isSearchLoading
? (!hasValidQueryLength
? const Center(
child: Text(
"Type at least 3 characters to search...",
style: TextStyle(
color: Colors.black45,
fontSize: 14,
),
),
)
: chatState.isSearchLoading
? const Center(
child: CircularProgressIndicator(
color: Color(0xFF1890FF),
Expand Down
Loading