Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/bounswe/bounswe2023group6
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
otaliptus committed Dec 25, 2023
2 parents 011028b + 596d613 commit abe4029
Show file tree
Hide file tree
Showing 5 changed files with 425 additions and 28 deletions.
39 changes: 39 additions & 0 deletions app/mobile/lib/data/models/dto/search/search_all_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:mobile/data/models/dto/base_dto_object.dart';
import 'package:mobile/data/models/game_model.dart';
import 'package:mobile/data/models/lfg_model.dart';
import 'package:mobile/data/models/post_model.dart';

class SearchAllResponse extends BaseDTOObject<SearchAllResponse> {
List<Post>? posts;
List<Game>? games;
List<LFG>? lfgs;

SearchAllResponse({
this.posts,
this.games,
this.lfgs,
});

@override
void validate() {
}

// TODO: add lfgs
factory SearchAllResponse.fromJson(Map<String, dynamic> json) => SearchAllResponse(
posts: List<Post>.from(json["postResults"].map((x) => Post.fromJson(x))),
games: List<Game>.from(json["gameResults"].map((x) => Game.fromJson(x))),
// lfgs: List<LFG>.from(json["lfgResults"].map((x) => LFG.fromJson(x))),
);

@override
Map<String, dynamic> toJson() => {
"posts": List<dynamic>.from(posts!.map((x) => x.toJson())),
"games": List<dynamic>.from(games!.map((x) => x.toJson())),
// "lfgs": List<dynamic>.from(lfgs!.map((x) => x.toJson())),
};

@override
SearchAllResponse fromJson(Map<String, dynamic> json) {
return SearchAllResponse.fromJson(json);
}
}
111 changes: 111 additions & 0 deletions app/mobile/lib/data/services/search_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import 'package:mobile/data/models/content_model.dart';
import 'package:mobile/data/models/dto/content/multiple_content_dto_response.dart';
import 'package:mobile/data/models/dto/empty_response.dart';
import 'package:mobile/data/models/dto/game/multiple_game_dto_response.dart';
import 'package:mobile/data/models/dto/search/search_all_response.dart';
import 'package:mobile/data/models/game_model.dart';
import 'package:mobile/data/models/lfg_model.dart';
import 'package:mobile/data/models/post_model.dart';
import 'package:mobile/data/models/service_response.dart';
import 'package:mobile/data/services/base_service.dart';
import 'package:mobile/data/services/game_service.dart';
import 'package:mobile/data/services/lfg_service.dart';

class SearchService {
BaseNetworkService service = BaseNetworkService();

SearchService._init();
static final SearchService _instance = SearchService._init();

factory SearchService() => _instance;
static SearchService get instance => _instance;

static const String _searchAll = '/search/all';
static const String _searchPosts = '/search/posts';
static const String _searchGames = '/search/games';
static const String _searchUsers = '/search/lfgs';

Future<Map<String, List<dynamic>>> searchAll(String query) async {
ServiceResponse<SearchAllResponse> response =
await service.sendRequestSafe<EmptyResponse, SearchAllResponse>(
"$_searchAll?query=$query",
null,
SearchAllResponse(),
'GET',
);

if (response.success) {
List<Post> posts = response.responseConverted!.posts!;
List<Game> games = response.responseConverted!.games!;
// TODO: add lfgs later
// List<LFG> lfgs = response.responseConverted!.lfgs!;
List<LFG> lfgs = [];

return {
'posts': posts,
'games': games,
'lfgs': lfgs,
};
} else {
throw Exception('Failed to load posts ${response.errorMessage}');
}
}

Future<List<Post>> searchPosts(String query) async {
ServiceResponse<MultipleContentAsDTO> response =
await service.sendRequestSafe<EmptyResponse, MultipleContentAsDTO>(
"$_searchPosts?query=$query",
null,
MultipleContentAsDTO(),
'GET',
);

if (response.success) {
List<Post> posts = response.responseConverted!.posts!
.map((e) => e.content! as Post)
.toList();
return posts;
} else {
throw Exception('Failed to load posts ${response.errorMessage}');
}
}

Future<List<Game>> searchGames(String query) async {
ServiceResponse<MultipleGameAsDTO> response =
await service.sendRequestSafe<EmptyResponse, MultipleGameAsDTO>(
"$_searchGames?query=$query",
null,
MultipleGameAsDTO(),
'GET',
);

if (response.success) {
List<Game> games =
response.responseConverted!.games!.map((e) => e.game!).toList();

return games;
} else {
throw Exception('Failed to load posts ${response.errorMessage}');
}
}

Future<List<LFG>> searchLFGs(String query) async {
ServiceResponse<MultipleContentAsDTO> response =
await service.sendRequestSafe<EmptyResponse, MultipleContentAsDTO>(
"$_searchUsers?query=$query",
null,
MultipleContentAsDTO(),
'GET',
);

if (response.success) {
List<LFG> lfgs = response.responseConverted!.posts!
.map((e) => e.content! as LFG)
.toList();

return lfgs;
} else {
throw Exception('Failed to load posts ${response.errorMessage}');
}
}
}
54 changes: 26 additions & 28 deletions app/mobile/lib/presentation/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,35 @@ class _HomePageState extends State<HomePage> {
List<Game> games = snapshot.data![1];
return ListView(
children: [
SizedBox(height: 10,),
SearchAnchor(
builder: (BuildContext context, SearchController controller) {
return SearchBar(
controller: controller,
padding: const MaterialStatePropertyAll<EdgeInsets>(
EdgeInsets.symmetric(horizontal: 16.0)),
const SizedBox(height: 10,),
Container(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: InkWell(
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
child: Row(
children: [
Expanded(
child: AbsorbPointer(
child: TextField(
decoration: InputDecoration(
hintText: 'Search',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
),
const SizedBox(width: 10),
],
),
onTap: () {
controller.openView();
},
onChanged: (_) {
controller.openView();
Navigator.of(context).pushNamed('/search');
},
leading: const Icon(Icons.search),
);
}, suggestionsBuilder:
(BuildContext context, SearchController controller) {
return List<ListTile>.generate(5, (int index) {
final String item = '';
return ListTile(
title: Text(item),
onTap: () {
setState(() {
controller.closeView(item);
});
},
);
});
},
),
),
SizedBox(height: 15,),
const SizedBox(height: 15,),
Card(
margin:
const EdgeInsets.symmetric(horizontal: 2, vertical: 4),
Expand Down
3 changes: 3 additions & 0 deletions app/mobile/lib/presentation/pages/navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:mobile/presentation/pages/login_page.dart';
import 'package:mobile/presentation/pages/post/post_create_page.dart';
import 'package:mobile/presentation/pages/post/post_page.dart';
import 'package:mobile/presentation/pages/profile_page.dart';
import 'package:mobile/presentation/widgets/searchbar_widget.dart';

class CustomNavigator extends StatefulWidget {
CustomNavigator({super.key, required this.defaultPage});
Expand Down Expand Up @@ -43,6 +44,8 @@ class _CustomNavigatorState extends State<CustomNavigator> {
return const LFGPageCreate();
case "/group":
return GroupPage();
case "/search":
return const SearchWidget();
case "/login":
return LoginPage();
default:
Expand Down
Loading

0 comments on commit abe4029

Please sign in to comment.