@@ -33,6 +33,7 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
3333 _onHomeTextChanged,
3434 transformer: restartableDebounce (const Duration (milliseconds: 500 )),
3535 );
36+ on < HomeTranslatorRemoved > (_onHomeTranslatorRemoved);
3637 }
3738
3839 final HomeRepository homeRepository;
@@ -45,28 +46,28 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
4546 Emitter <HomeState > emit,
4647 ) {
4748 event.textController.clear ();
48- emit (state.empty ());
49+ emit (state.empty (method : HomeMethod .textCleared ));
4950 }
5051
5152 Future <void > _onHomeTextChanged (
5253 HomeTextChanged event,
5354 Emitter <HomeState > emit,
5455 ) async {
5556 try {
56- emit (state.loading ());
57+ emit (state.loading (method : HomeMethod .textChanged ));
5758
5859 final sourceText = event.sourceText.trim ();
5960
6061 if (sourceText.isEmpty) {
61- emit (state.empty ());
62- return ;
63- }
64-
65- // TODO: Tell the user that the translation won't work if there is no
66- // translator enabled
67- if (settingsBloc.state.translatorSettings == null ||
62+ emit (state.empty (method: HomeMethod .textChanged));
63+ } else if (settingsBloc.state.translatorSettings == null ||
6864 settingsBloc.state.translatorSettings! .enabledTranslators.isEmpty) {
69- emit (state.failure (const NoTranslatorEnabledException ()));
65+ emit (
66+ state.failure (
67+ const NoTranslatorEnabledException (),
68+ method: HomeMethod .textChanged,
69+ ),
70+ );
7071 return ;
7172 }
7273
@@ -85,20 +86,56 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
8586 // For now, we register all the translated text even if they
8687 // are one letter apart
8788
88- return state.success (translationDetails);
89+ return state.success (
90+ translationDetails,
91+ method: HomeMethod .textChanged,
92+ );
8993 },
9094 onError: (e, stackTrace) {
9195 log ('error: $e , stackTrace: $stackTrace ' );
9296 // Log error to Sentry
9397 Sentry .captureException (e, stackTrace: stackTrace);
94- return state.failure (Exception (e));
98+ return state.failure (
99+ Exception ('An error has occurred.' ),
100+ method: HomeMethod .textChanged,
101+ );
95102 },
96103 );
97104 } on Exception catch (e) {
98105 log (e.toString ());
99106 unawaited (Sentry .captureException (e));
100- emit (state.failure (e));
107+ emit (
108+ state.failure (
109+ Exception ('An error has occurred.' ),
110+ method: HomeMethod .textChanged,
111+ ),
112+ );
113+ }
114+ }
115+
116+ Future <void > _onHomeTranslatorRemoved (
117+ HomeTranslatorRemoved event,
118+ Emitter <HomeState > emit,
119+ ) async {
120+ // Remove existing translation details for the removed translators
121+ final updatedTranslationDetails = Map .of (state.translationDetails);
122+
123+ if (event.removedTranslators.isEmpty) {
124+ return ;
101125 }
126+
127+ for (final translator in event.removedTranslators) {
128+ updatedTranslationDetails.remove (translator);
129+ }
130+
131+ emit (
132+ state.copyWith (
133+ translationDetails: updatedTranslationDetails,
134+ method: HomeMethod .settingsApiKeyRemoved,
135+ // Deliberately not changing the status so that the UI depends on the
136+ // status of the last translation
137+ ),
138+ );
102139 }
103140}
104141
0 commit comments