Skip to content

Commit 7332d21

Browse files
committed
feat: dictionary for word lookups
1 parent 02b856a commit 7332d21

File tree

15 files changed

+123640
-123307
lines changed

15 files changed

+123640
-123307
lines changed

assets/dict.csv

Lines changed: 123300 additions & 123300 deletions
Large diffs are not rendered by default.

lib/app/view/app.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
44
import 'package:qack/gen/fonts.gen.dart';
55
import 'package:qack/l10n/arb/app_localizations.dart';
66
import 'package:qack/layout/device_info_setter.dart';
7+
import 'package:qack/presentation/dictionary/bloc/dictionary_bloc.dart';
78
import 'package:qack/presentation/history/bloc/history_bloc.dart';
89
import 'package:qack/presentation/history/repositories/repositories.dart';
910
import 'package:qack/presentation/home/bloc/home_bloc.dart';
@@ -22,6 +23,7 @@ class App extends StatelessWidget {
2223
required this.secureStorage,
2324
required this.settingsBloc,
2425
required this.historyBloc,
26+
required this.dictionaryBloc,
2527
required this.wordOfTheDayCubit,
2628
required this.appDatabase,
2729
super.key,
@@ -30,6 +32,7 @@ class App extends StatelessWidget {
3032
final FlutterSecureStorage secureStorage;
3133
final SettingsBloc settingsBloc;
3234
final HistoryBloc historyBloc;
35+
final DictionaryBloc dictionaryBloc;
3336
final WordOfTheDayCubit wordOfTheDayCubit;
3437
final AppDatabase appDatabase;
3538

@@ -50,6 +53,7 @@ class App extends StatelessWidget {
5053
child: _App(
5154
settingsBloc: settingsBloc,
5255
historyBloc: historyBloc,
56+
dictionaryBloc: dictionaryBloc,
5357
wordOfTheDayCubit: wordOfTheDayCubit,
5458
),
5559
);
@@ -60,10 +64,12 @@ class _App extends StatelessWidget {
6064
const _App({
6165
required this.settingsBloc,
6266
required this.historyBloc,
67+
required this.dictionaryBloc,
6368
required this.wordOfTheDayCubit,
6469
});
6570
final SettingsBloc settingsBloc;
6671
final HistoryBloc historyBloc;
72+
final DictionaryBloc dictionaryBloc;
6773
final WordOfTheDayCubit wordOfTheDayCubit;
6874

6975
@override
@@ -81,6 +87,9 @@ class _App extends StatelessWidget {
8187
BlocProvider(
8288
create: (context) => historyBloc,
8389
),
90+
BlocProvider(
91+
create: (context) => dictionaryBloc,
92+
),
8493
BlocProvider(create: (context) => wordOfTheDayCubit),
8594
BlocProvider(create: (context) => BottomNavigationBarCubit()),
8695
],

lib/bootstrap.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
77
import 'package:get_it/get_it.dart';
88
import 'package:http_client/http_client.dart';
99
import 'package:qack/constants/key_name.dart';
10+
import 'package:qack/presentation/dictionary/bloc/dictionary_bloc.dart';
11+
import 'package:qack/presentation/dictionary/repositories/repositories.dart';
1012
import 'package:qack/presentation/history/bloc/history_bloc.dart';
1113
import 'package:qack/presentation/history/repositories/repositories.dart';
1214
import 'package:qack/presentation/home/cubit/word_of_the_day_cubit.dart';
@@ -43,6 +45,7 @@ Future<void> bootstrap(
4345
FlutterSecureStorage secureStorage,
4446
SettingsBloc settingsBloc,
4547
HistoryBloc historyBloc,
48+
DictionaryBloc dictionaryBloc,
4649
WordOfTheDayCubit wordOfTheDayCubit,
4750
AppDatabase appDatabase,
4851
) builder,
@@ -102,6 +105,11 @@ Future<void> bootstrap(
102105
HistoryRepository(appDatabase: appDatabase),
103106
)..add(const HistoryFetched());
104107

108+
// Initialize dictionary bloc
109+
final dictionaryBloc =
110+
DictionaryBloc(DictionaryRepository(appDatabase: appDatabase))
111+
..add(DictionaryFetched());
112+
105113
// Initialize word of the day cubit
106114
final wordOfTheDayCubit = WordOfTheDayCubit(
107115
const WordOfTheDayRepository(),
@@ -121,6 +129,7 @@ Future<void> bootstrap(
121129
secureStorage,
122130
settingsBloc,
123131
historyBloc,
132+
dictionaryBloc,
124133
wordOfTheDayCubit,
125134
appDatabase,
126135
),

lib/main_development.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
33
import 'package:qack/app/view/app.dart';
44
import 'package:qack/bootstrap.dart';
55
import 'package:qack/constants/key_name.dart';
6+
import 'package:qack/presentation/dictionary/bloc/dictionary_bloc.dart';
67
import 'package:qack/presentation/history/bloc/history_bloc.dart';
78
import 'package:qack/presentation/home/cubit/word_of_the_day_cubit.dart';
89
import 'package:qack/presentation/settings/bloc/settings_bloc.dart';
@@ -36,13 +37,15 @@ Future<void> main() async {
3637
FlutterSecureStorage secureStorage,
3738
SettingsBloc settingsBloc,
3839
HistoryBloc historyBloc,
40+
DictionaryBloc dictionaryBloc,
3941
WordOfTheDayCubit wordOfTheDayCubit,
4042
AppDatabase appDatabase,
4143
) async {
4244
return App(
4345
secureStorage: secureStorage,
4446
settingsBloc: settingsBloc,
4547
historyBloc: historyBloc,
48+
dictionaryBloc: dictionaryBloc,
4649
wordOfTheDayCubit: wordOfTheDayCubit,
4750
appDatabase: appDatabase,
4851
);

lib/main_production.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
33
import 'package:qack/app/view/app.dart';
44
import 'package:qack/bootstrap.dart';
55
import 'package:qack/constants/constants.dart';
6+
import 'package:qack/presentation/dictionary/bloc/dictionary_bloc.dart';
67
import 'package:qack/presentation/history/bloc/history_bloc.dart';
78
import 'package:qack/presentation/home/cubit/word_of_the_day_cubit.dart';
89
import 'package:qack/presentation/settings/bloc/settings_bloc.dart';
@@ -35,13 +36,15 @@ Future<void> main() async {
3536
FlutterSecureStorage secureStorage,
3637
SettingsBloc settingsBloc,
3738
HistoryBloc historyBloc,
39+
DictionaryBloc dictionaryBloc,
3840
WordOfTheDayCubit wordOfTheDayCubit,
3941
AppDatabase appDatabase,
4042
) async {
4143
return App(
4244
secureStorage: secureStorage,
4345
settingsBloc: settingsBloc,
4446
historyBloc: historyBloc,
47+
dictionaryBloc: dictionaryBloc,
4548
wordOfTheDayCubit: wordOfTheDayCubit,
4649
appDatabase: appDatabase,
4750
);

lib/main_staging.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
33
import 'package:qack/app/view/app.dart';
44
import 'package:qack/bootstrap.dart';
55
import 'package:qack/constants/constants.dart';
6+
import 'package:qack/presentation/dictionary/bloc/dictionary_bloc.dart';
67
import 'package:qack/presentation/history/bloc/history_bloc.dart';
78
import 'package:qack/presentation/home/cubit/word_of_the_day_cubit.dart';
89
import 'package:qack/presentation/settings/bloc/settings_bloc.dart';
@@ -35,13 +36,15 @@ Future<void> main() async {
3536
FlutterSecureStorage secureStorage,
3637
SettingsBloc settingsBloc,
3738
HistoryBloc historyBloc,
39+
DictionaryBloc dictionaryBloc,
3840
WordOfTheDayCubit wordOfTheDayCubit,
3941
AppDatabase appDatabase,
4042
) async {
4143
return App(
4244
secureStorage: secureStorage,
4345
settingsBloc: settingsBloc,
4446
historyBloc: historyBloc,
47+
dictionaryBloc: dictionaryBloc,
4548
wordOfTheDayCubit: wordOfTheDayCubit,
4649
appDatabase: appDatabase,
4750
);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import 'package:bloc/bloc.dart';
2+
import 'package:equatable/equatable.dart';
3+
import 'package:freezed_annotation/freezed_annotation.dart';
4+
import 'package:qack/presentation/dictionary/repositories/repositories.dart';
5+
import 'package:qack/utils/database/database.dart';
6+
import 'package:sentry_flutter/sentry_flutter.dart';
7+
8+
part 'dictionary_event.dart';
9+
part 'dictionary_state.dart';
10+
11+
class DictionaryBloc extends Bloc<DictionaryEvent, DictionaryState> {
12+
DictionaryBloc(this.repository) : super(const DictionaryState()) {
13+
on<DictionaryFetched>(_onDictionaryFetched);
14+
on<DictionaryQueried>(_onDictionaryQueried);
15+
}
16+
final DictionaryRepository repository;
17+
18+
Future<void> _onDictionaryFetched(
19+
DictionaryFetched event,
20+
Emitter<DictionaryState> emit,
21+
) async {
22+
try {
23+
final entries = await repository.fetchDictionaryEntries();
24+
25+
emit(
26+
state.copyWith(
27+
method: DictionaryMethod.fetchDictionary,
28+
status: DictionaryStatus.success,
29+
dictionaryEntries: entries,
30+
),
31+
);
32+
} on Exception catch (e) {
33+
emit(
34+
state.copyWith(
35+
method: DictionaryMethod.fetchDictionary,
36+
status: DictionaryStatus.failure,
37+
),
38+
);
39+
40+
await Sentry.captureException(
41+
e,
42+
stackTrace: StackTrace.current,
43+
);
44+
}
45+
}
46+
47+
Future<void> _onDictionaryQueried(
48+
DictionaryQueried event,
49+
Emitter<DictionaryState> emit,
50+
) async {
51+
try {
52+
final entries = await repository.queryDictionaryEntries(event.query);
53+
54+
emit(
55+
state.copyWith(
56+
method: DictionaryMethod.queryDictionary,
57+
status: DictionaryStatus.success,
58+
dictionaryEntries: entries,
59+
),
60+
);
61+
} on Exception catch (e) {
62+
emit(
63+
state.copyWith(
64+
method: DictionaryMethod.queryDictionary,
65+
status: DictionaryStatus.failure,
66+
),
67+
);
68+
69+
await Sentry.captureException(
70+
e,
71+
stackTrace: StackTrace.current,
72+
);
73+
}
74+
}
75+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
part of 'dictionary_bloc.dart';
2+
3+
class DictionaryEvent extends Equatable {
4+
@override
5+
List<Object?> get props => [];
6+
}
7+
8+
class DictionaryFetched extends DictionaryEvent {
9+
@override
10+
List<Object?> get props => [];
11+
}
12+
13+
class DictionaryQueried extends DictionaryEvent {
14+
DictionaryQueried(this.query);
15+
final String query;
16+
@override
17+
List<Object?> get props => [query];
18+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
part of 'dictionary_bloc.dart';
2+
3+
enum DictionaryMethod {
4+
initial,
5+
fetchDictionary,
6+
queryDictionary,
7+
}
8+
9+
enum DictionaryStatus {
10+
initial,
11+
loading,
12+
success,
13+
failure,
14+
}
15+
16+
class DictionaryState extends Equatable {
17+
const DictionaryState({
18+
this.method = DictionaryMethod.initial,
19+
this.status = DictionaryStatus.initial,
20+
this.dictionaryEntries = const [],
21+
});
22+
23+
final DictionaryMethod method;
24+
final DictionaryStatus status;
25+
final List<DictionaryData> dictionaryEntries;
26+
27+
DictionaryState copyWith({
28+
DictionaryMethod? method,
29+
DictionaryStatus? status,
30+
List<DictionaryData>? dictionaryEntries,
31+
}) {
32+
return DictionaryState(
33+
method: method ?? this.method,
34+
status: status ?? this.status,
35+
dictionaryEntries: dictionaryEntries ?? this.dictionaryEntries,
36+
);
37+
}
38+
39+
@override
40+
List<Object?> get props => [method, status, dictionaryEntries];
41+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export 'dictionary_list_tile.dart';

0 commit comments

Comments
 (0)