-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
263 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
enum SearchLoadStatus { | ||
initial, | ||
loading, | ||
loaded, | ||
error, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:study_match/core/enums/search_load_status.dart'; | ||
import 'package:study_match/search/services/search_service.dart'; | ||
|
||
part 'search_state.dart'; | ||
|
||
class SearchCubit extends Cubit<SearchState> { | ||
final _searchService = SearchService.instance; | ||
|
||
SearchCubit() : super(const SearchState([], SearchLoadStatus.initial)); | ||
|
||
Future<void> searchUsers(String query) async { | ||
emit(const SearchState([], SearchLoadStatus.loading)); | ||
if (query.isEmpty) { | ||
emit(const SearchState([], SearchLoadStatus.initial)); | ||
return; | ||
} | ||
final users = await _searchService.searchUsers(query); | ||
emit(SearchState(users, SearchLoadStatus.loaded)); | ||
} | ||
|
||
void clearSearch() { | ||
emit(const SearchState([], SearchLoadStatus.initial)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
part of 'search_cubit.dart'; | ||
|
||
class SearchState extends Equatable { | ||
final List<(String, String)> users; | ||
final SearchLoadStatus status; | ||
|
||
const SearchState(this.users, this.status); | ||
|
||
@override | ||
List<Object> get props => [users, status]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,81 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:study_match/core/enums/search_load_status.dart'; | ||
import 'package:study_match/profile/profile_view.dart'; | ||
import 'package:study_match/search/bloc/cubit/search_cubit.dart'; | ||
|
||
class SearchScreen extends StatelessWidget { | ||
const SearchScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const Scaffold( | ||
body: Center( | ||
child: Text('Search Screen 🔎'), | ||
), | ||
return BlocProvider( | ||
create: (context) => SearchCubit(), | ||
child: const SearchPage(), | ||
); | ||
} | ||
} | ||
|
||
class SearchPage extends StatelessWidget { | ||
const SearchPage({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return BlocBuilder<SearchCubit, SearchState>( | ||
builder: (context, state) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Row( | ||
children: [ | ||
Expanded( | ||
child: TextField( | ||
decoration: const InputDecoration( | ||
hintText: 'Search for a user', | ||
), | ||
onChanged: (value) { | ||
context.read<SearchCubit>().searchUsers(value); | ||
}, | ||
), | ||
), | ||
IconButton( | ||
icon: const Icon(Icons.clear), | ||
onPressed: () { | ||
context.read<SearchCubit>().clearSearch(); | ||
}, | ||
), | ||
], | ||
), | ||
), | ||
body: _showSearchResults(context, state), | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
Widget _showSearchResults(BuildContext context, SearchState state) { | ||
if (state.status == SearchLoadStatus.loading) { | ||
return const Center(child: CircularProgressIndicator()); | ||
} | ||
|
||
if (state.status == SearchLoadStatus.loaded) { | ||
return ListView.builder( | ||
itemCount: state.users.length, | ||
itemBuilder: (context, index) { | ||
final user = state.users[index]; | ||
return ListTile( | ||
title: Text(user.$2), | ||
onTap: () => Navigator.of(context).push( | ||
MaterialPageRoute( | ||
builder: (context) => ProfileView(userID: user.$1), | ||
), | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
|
||
return const Center( | ||
child: Text('Your search results will appear here!'), | ||
); // | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'package:supabase_flutter/supabase_flutter.dart'; | ||
|
||
class SearchService { | ||
final supabase = Supabase.instance.client; | ||
SearchService._(); | ||
|
||
Future<List<(String, String)>> searchUsers(String query) async { | ||
final response = await supabase | ||
.from('profiles') | ||
.select('id, username') | ||
.ilike('username', '%$query%'); | ||
|
||
if (response == []) return []; | ||
|
||
final data = response as List<dynamic>; | ||
final users = data.map((e) { | ||
final userData = e as Map<String, dynamic>; | ||
return (userData['id'] as String, userData['username'] as String); | ||
}).toList(); | ||
|
||
return users; | ||
} | ||
|
||
static final SearchService instance = SearchService._(); | ||
} |
Oops, something went wrong.