Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search implementation #132

Merged
merged 6 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions lib/bootstrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:fluttercon/features/home/cubit/home_cubits.dart';
import 'package:fluttercon/features/sessions/cubit/bookmark_session_cubit.dart';
import 'package:fluttercon/features/sessions/cubit/fetch_grouped_sessions_cubit.dart';
import 'package:fluttercon/firebase_options.dart';
import 'package:fluttercon/search/cubit/search_cubit.dart';

class AppBlocObserver extends BlocObserver {
const AppBlocObserver();
Expand Down Expand Up @@ -146,6 +147,9 @@ Future<void> bootstrap(FutureOr<Widget> Function() builder) async {
hiveRepository: getIt(),
),
),
BlocProvider<SearchCubit>(
create: (_) => getIt<SearchCubit>(),
),
],
NdegwaJulius marked this conversation as resolved.
Show resolved Hide resolved
child: await builder(),
),
Expand Down
48 changes: 48 additions & 0 deletions lib/common/repository/db_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,46 @@ import 'package:path_provider/path_provider.dart' as path_provider;

@singleton
class DBRepository {
Future<List<LocalSession>> searchSessions(String query) async {
return localDB.localSessions
.where()
.filter()
.titleContains(query, caseSensitive: false)
.or()
.descriptionContains(query, caseSensitive: false)
.findAll();
}

Future<List<LocalSpeaker>> searchSpeakers(String query) async {
return localDB.localSpeakers
.where()
.filter()
.nameContains(query, caseSensitive: false)
.or()
.biographyContains(query, caseSensitive: false)
.or()
.taglineContains(query, caseSensitive: false)
.findAll();
}

Future<List<LocalSponsor>> searchSponsors(String query) async {
return localDB.localSponsors
.where()
.filter()
.nameContains(query, caseSensitive: false)
.or()
.taglineContains(query, caseSensitive: false)
.findAll();
}

Future<List<LocalOrganiser>> searchOrganisers(String query) async {
return localDB.localOrganisers
.where()
.filter()
.nameContains(query, caseSensitive: false)
.findAll();
}

NdegwaJulius marked this conversation as resolved.
Show resolved Hide resolved
Future<Isar> init() async {
final dir = await path_provider.getApplicationDocumentsDirectory();
return Isar.open(
Expand Down Expand Up @@ -235,6 +275,7 @@ class DBRepository {
BookmarkStatus? bookmarkStatus,
String? sessionLevel,
String? sessionType,
String? query,
NdegwaJulius marked this conversation as resolved.
Show resolved Hide resolved
}) async {
return localDB.localSessions
.where()
Expand All @@ -252,6 +293,13 @@ class DBRepository {
sessionType != null,
(q) => q.sessionFormatEqualTo(sessionType!),
)
.optional(
query != null,
(q) => q
.titleContains(query!, caseSensitive: false)
.or()
.descriptionContains(query, caseSensitive: false),
)
.findAll();
}

Expand Down
6 changes: 4 additions & 2 deletions lib/features/home/ui/home_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:fluttercon/common/utils/constants/app_assets.dart';
import 'package:fluttercon/common/utils/misc.dart';
Expand All @@ -8,6 +7,7 @@ import 'package:fluttercon/features/home/widgets/sessions_card.dart';
import 'package:fluttercon/features/home/widgets/speaker_home_card.dart';
import 'package:fluttercon/features/home/widgets/sponsors_card.dart';
import 'package:fluttercon/l10n/l10n.dart';
import 'package:fluttercon/search/ui/widgets/search_bar.dart';

class HomeScreen extends StatefulWidget {
const HomeScreen({super.key, this.switchTab});
Expand All @@ -33,7 +33,7 @@ class _HomeScreenState extends State<HomeScreen> {
const SizedBox(height: 15),
Align(
alignment: Alignment.centerLeft,
child: AutoSizeText(
child: Text(
l10n.welcomeToFlutterCon,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
Expand All @@ -43,6 +43,8 @@ class _HomeScreenState extends State<HomeScreen> {
),
),
const SizedBox(height: 15),
const SearchBarWidget(),
const SizedBox(height: 15),
ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.asset(AppAssets.droidconBanner),
Expand Down
78 changes: 78 additions & 0 deletions lib/search/cubit/search_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttercon/common/data/models/failure.dart';
import 'package:fluttercon/common/repository/db_repository.dart';
import 'package:fluttercon/search/cubit/search_state.dart';
import 'package:fluttercon/search/models/search_result.dart';
import 'package:injectable/injectable.dart';

@injectable
NdegwaJulius marked this conversation as resolved.
Show resolved Hide resolved
class SearchCubit extends Cubit<SearchState> {
NdegwaJulius marked this conversation as resolved.
Show resolved Hide resolved
SearchCubit(
this._dbRepository,
) : super(const SearchState.initial());

final DBRepository _dbRepository;
NdegwaJulius marked this conversation as resolved.
Show resolved Hide resolved

Future<void> search(String query) async {
emit(const SearchState.loading());
try {
final sessions = await _dbRepository.searchSessions(query);
final speakers = await _dbRepository.searchSpeakers(query);
final sponsors = await _dbRepository.searchSponsors(query);
final organizers = await _dbRepository.searchOrganisers(query);

final results = [
...sessions.map(
(session) => SearchResult(
id: session.serverId.toString(),
title: session.title,
subtitle: 'Session',
imageUrl: session.sessionImage,
type: SearchResultType.session,
session: session,
),
),
...speakers.map(
(speaker) => SearchResult(
id: speaker.id.toString(),
title: speaker.name,
subtitle: speaker.tagline ?? '',
imageUrl: speaker.avatar,
type: SearchResultType.speaker,
speaker: speaker,
),
),
...sponsors.map(
(sponsor) => SearchResult(
id: sponsor.id.toString(),
title: sponsor.name,
subtitle: 'Sponsor',
imageUrl: sponsor.logo,
type: SearchResultType.sponsor,
sponsor: sponsor,
),
),
...organizers.map(
(organizer) => SearchResult(
id: organizer.id.toString(),
NdegwaJulius marked this conversation as resolved.
Show resolved Hide resolved
title: organizer.name,
subtitle: 'Organizer',
imageUrl: organizer.logo,
type: SearchResultType.organizer,
organizer: organizer,
),
),
];

emit(SearchState.loaded(results: results));
} on Failure catch (e) {
emit(SearchState.error(message: e.message));
} catch (e) {
emit(SearchState.error(message: e.toString()));
}
}

void clearSearch() {
emit(const SearchState.initial());
}
}
12 changes: 12 additions & 0 deletions lib/search/cubit/search_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:fluttercon/search/models/search_result.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'search_state.freezed.dart';

@freezed
class SearchState with _$SearchState {
const factory SearchState.initial() = SearchInitial;
const factory SearchState.loading() = SearchLoading;
const factory SearchState.loaded({required List<SearchResult> results}) =
SearchLoaded;
const factory SearchState.error({required String message}) = SearchError;
}
24 changes: 24 additions & 0 deletions lib/search/models/search_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:fluttercon/common/data/models/local/local_organiser.dart';
import 'package:fluttercon/common/data/models/local/local_session.dart';
import 'package:fluttercon/common/data/models/local/local_speaker.dart';
import 'package:fluttercon/common/data/models/local/local_sponsor.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'search_result.freezed.dart';

enum SearchResultType { session, speaker, sponsor, organizer }
NdegwaJulius marked this conversation as resolved.
Show resolved Hide resolved

@freezed
class SearchResult with _$SearchResult {
const factory SearchResult({
required String id,
required String title,
required String subtitle,
required String imageUrl,
required SearchResultType type,
LocalSession? session,
LocalSpeaker? speaker,
LocalSponsor? sponsor,
NdegwaJulius marked this conversation as resolved.
Show resolved Hide resolved
LocalOrganiser? organizer,
NdegwaJulius marked this conversation as resolved.
Show resolved Hide resolved
}) = _SearchResult;
}
Loading
Loading