Skip to content

Commit 34d4c85

Browse files
committed
refactor(settings): migrate to core library and update error handling
- Replace HtDataRepository with DataRepository - Update import paths to use core and data_repository packages - Replace HtHttpException with HttpException - Simplify some widget code and remove redundant packages
1 parent 9252be5 commit 34d4c85

File tree

3 files changed

+34
-83
lines changed

3 files changed

+34
-83
lines changed

lib/settings/bloc/settings_bloc.dart

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import 'dart:async';
22

33
import 'package:bloc/bloc.dart';
4+
import 'package:core/core.dart';
5+
import 'package:data_repository/data_repository.dart';
46
import 'package:equatable/equatable.dart';
5-
import 'package:ht_data_repository/ht_data_repository.dart';
6-
import 'package:ht_shared/ht_shared.dart';
77

88
part 'settings_event.dart';
99
part 'settings_state.dart';
1010

1111
class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
1212
SettingsBloc({
13-
required HtDataRepository<UserAppSettings> userAppSettingsRepository,
13+
required DataRepository<UserAppSettings> userAppSettingsRepository,
1414
}) : _userAppSettingsRepository = userAppSettingsRepository,
1515
super(const SettingsInitial()) {
1616
on<SettingsLoaded>(_onSettingsLoaded);
@@ -22,7 +22,7 @@ class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
2222
on<SettingsLanguageChanged>(_onSettingsLanguageChanged);
2323
}
2424

25-
final HtDataRepository<UserAppSettings> _userAppSettingsRepository;
25+
final DataRepository<UserAppSettings> _userAppSettingsRepository;
2626

2727
Future<void> _onSettingsLoaded(
2828
SettingsLoaded event,
@@ -54,10 +54,8 @@ class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
5454
);
5555
await _userAppSettingsRepository.create(item: defaultSettings);
5656
emit(SettingsLoadSuccess(userAppSettings: defaultSettings));
57-
} on HtHttpException catch (e) {
58-
emit(
59-
SettingsLoadFailure(e, userAppSettings: state.userAppSettings),
60-
);
57+
} on HttpException catch (e) {
58+
emit(SettingsLoadFailure(e, userAppSettings: state.userAppSettings));
6159
} catch (e) {
6260
emit(
6361
SettingsLoadFailure(
@@ -79,13 +77,8 @@ class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
7977
item: updatedSettings,
8078
);
8179
emit(SettingsUpdateSuccess(userAppSettings: result));
82-
} on HtHttpException catch (e) {
83-
emit(
84-
SettingsUpdateFailure(
85-
e,
86-
userAppSettings: state.userAppSettings,
87-
),
88-
);
80+
} on HttpException catch (e) {
81+
emit(SettingsUpdateFailure(e, userAppSettings: state.userAppSettings));
8982
} catch (e) {
9083
emit(
9184
SettingsUpdateFailure(

lib/settings/bloc/settings_state.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ final class SettingsLoadFailure extends SettingsState {
4242
const SettingsLoadFailure(this.exception, {super.userAppSettings});
4343

4444
/// The error exception describing the failure.
45-
final HtHttpException exception;
45+
final HttpException exception;
4646

4747
@override
4848
List<Object?> get props => [exception, userAppSettings];
@@ -72,7 +72,7 @@ final class SettingsUpdateFailure extends SettingsState {
7272
const SettingsUpdateFailure(this.exception, {super.userAppSettings});
7373

7474
/// The error exception describing the failure.
75-
final HtHttpException exception;
75+
final HttpException exception;
7676

7777
@override
7878
List<Object?> get props => [exception, userAppSettings];

lib/settings/view/settings_page.dart

Lines changed: 24 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import 'package:core/core.dart';
2+
import 'package:data_repository/data_repository.dart';
13
import 'package:flutter/material.dart';
24
import 'package:flutter_bloc/flutter_bloc.dart';
3-
import 'package:ht_dashboard/app/bloc/app_bloc.dart';
4-
import 'package:ht_dashboard/l10n/app_localizations.dart';
5-
import 'package:ht_dashboard/l10n/l10n.dart';
6-
import 'package:ht_dashboard/settings/bloc/settings_bloc.dart';
7-
import 'package:ht_data_repository/ht_data_repository.dart';
8-
import 'package:ht_shared/ht_shared.dart';
9-
import 'package:ht_ui_kit/ht_ui_kit.dart';
5+
import 'package:flutter_news_app_web_dashboard_full_source_code/app/bloc/app_bloc.dart';
6+
import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/app_localizations.dart';
7+
import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/l10n.dart';
8+
import 'package:flutter_news_app_web_dashboard_full_source_code/settings/bloc/settings_bloc.dart';
9+
import 'package:ui_kit/ui_kit.dart';
1010

1111
/// {@template settings_page}
1212
/// A page for user settings, allowing customization of theme and language.
@@ -20,7 +20,7 @@ class SettingsPage extends StatelessWidget {
2020
return BlocProvider(
2121
create: (context) => SettingsBloc(
2222
userAppSettingsRepository: context
23-
.read<HtDataRepository<UserAppSettings>>(),
23+
.read<DataRepository<UserAppSettings>>(),
2424
)..add(SettingsLoaded(userId: context.read<AppBloc>().state.user?.id)),
2525
child: const _SettingsView(),
2626
);
@@ -62,9 +62,7 @@ class _SettingsView extends StatelessWidget {
6262
ScaffoldMessenger.of(context)
6363
..hideCurrentSnackBar()
6464
..showSnackBar(
65-
SnackBar(
66-
content: Text(l10n.settingsSavedSuccessfully),
67-
),
65+
SnackBar(content: Text(l10n.settingsSavedSuccessfully)),
6866
);
6967
// Trigger AppBloc to reload settings for immediate UI update
7068
if (state.userAppSettings != null) {
@@ -77,9 +75,7 @@ class _SettingsView extends StatelessWidget {
7775
..hideCurrentSnackBar()
7876
..showSnackBar(
7977
SnackBar(
80-
content: Text(
81-
state.exception.toFriendlyMessage(context),
82-
),
78+
content: Text(state.exception.toFriendlyMessage(context)),
8379
),
8480
);
8581
}
@@ -89,9 +85,7 @@ class _SettingsView extends StatelessWidget {
8985
state is! SettingsLoadInProgress) {
9086
// If settings are null and not loading, try to load them
9187
context.read<SettingsBloc>().add(
92-
SettingsLoaded(
93-
userId: context.read<AppBloc>().state.user?.id,
94-
),
88+
SettingsLoaded(userId: context.read<AppBloc>().state.user?.id),
9589
);
9690
}
9791

@@ -138,9 +132,7 @@ class _SettingsView extends StatelessWidget {
138132
onChanged: (value) {
139133
if (value != null) {
140134
context.read<SettingsBloc>().add(
141-
SettingsBaseThemeChanged(
142-
value,
143-
),
135+
SettingsBaseThemeChanged(value),
144136
);
145137
}
146138
},
@@ -149,10 +141,7 @@ class _SettingsView extends StatelessWidget {
149141
(theme) => DropdownMenuItem(
150142
value: theme,
151143
child: Text(
152-
_getAppBaseThemeName(
153-
theme,
154-
l10n,
155-
),
144+
_getAppBaseThemeName(theme, l10n),
156145
),
157146
),
158147
)
@@ -169,9 +158,7 @@ class _SettingsView extends StatelessWidget {
169158
onChanged: (value) {
170159
if (value != null) {
171160
context.read<SettingsBloc>().add(
172-
SettingsAccentThemeChanged(
173-
value,
174-
),
161+
SettingsAccentThemeChanged(value),
175162
);
176163
}
177164
},
@@ -180,10 +167,7 @@ class _SettingsView extends StatelessWidget {
180167
(theme) => DropdownMenuItem(
181168
value: theme,
182169
child: Text(
183-
_getAppAccentThemeName(
184-
theme,
185-
l10n,
186-
),
170+
_getAppAccentThemeName(theme, l10n),
187171
),
188172
),
189173
)
@@ -207,22 +191,15 @@ class _SettingsView extends StatelessWidget {
207191
onChanged: (value) {
208192
if (value != null) {
209193
context.read<SettingsBloc>().add(
210-
SettingsFontFamilyChanged(
211-
value,
212-
),
194+
SettingsFontFamilyChanged(value),
213195
);
214196
}
215197
},
216198
items: _supportedFontFamilies
217199
.map(
218200
(font) => DropdownMenuItem(
219201
value: font,
220-
child: Text(
221-
_getFontFamilyName(
222-
font,
223-
l10n,
224-
),
225-
),
202+
child: Text(_getFontFamilyName(font, l10n)),
226203
),
227204
)
228205
.toList(),
@@ -239,9 +216,7 @@ class _SettingsView extends StatelessWidget {
239216
onChanged: (value) {
240217
if (value != null) {
241218
context.read<SettingsBloc>().add(
242-
SettingsTextScaleFactorChanged(
243-
value,
244-
),
219+
SettingsTextScaleFactorChanged(value),
245220
);
246221
}
247222
},
@@ -250,10 +225,7 @@ class _SettingsView extends StatelessWidget {
250225
(scale) => DropdownMenuItem(
251226
value: scale,
252227
child: Text(
253-
_getAppTextScaleFactorName(
254-
scale,
255-
l10n,
256-
),
228+
_getAppTextScaleFactorName(scale, l10n),
257229
),
258230
),
259231
)
@@ -270,9 +242,7 @@ class _SettingsView extends StatelessWidget {
270242
onChanged: (value) {
271243
if (value != null) {
272244
context.read<SettingsBloc>().add(
273-
SettingsFontWeightChanged(
274-
value,
275-
),
245+
SettingsFontWeightChanged(value),
276246
);
277247
}
278248
},
@@ -281,10 +251,7 @@ class _SettingsView extends StatelessWidget {
281251
(weight) => DropdownMenuItem(
282252
value: weight,
283253
child: Text(
284-
_getAppFontWeightName(
285-
weight,
286-
l10n,
287-
),
254+
_getAppFontWeightName(weight, l10n),
288255
),
289256
),
290257
)
@@ -338,10 +305,7 @@ class _SettingsView extends StatelessWidget {
338305
child: Column(
339306
crossAxisAlignment: CrossAxisAlignment.start,
340307
children: [
341-
Text(
342-
title,
343-
style: Theme.of(context).textTheme.titleMedium,
344-
),
308+
Text(title, style: Theme.of(context).textTheme.titleMedium),
345309
const SizedBox(height: AppSpacing.xs),
346310
Text(
347311
description,
@@ -467,10 +431,7 @@ class _LanguageSelectionList extends StatelessWidget {
467431
style: Theme.of(context).textTheme.titleMedium,
468432
),
469433
trailing: isSelected
470-
? Icon(
471-
Icons.check,
472-
color: Theme.of(context).colorScheme.primary,
473-
)
434+
? Icon(Icons.check, color: Theme.of(context).colorScheme.primary)
474435
: null,
475436
onTap: () {
476437
if (!isSelected) {
@@ -495,8 +456,5 @@ class _LanguageSelectionList extends StatelessWidget {
495456
}
496457
}
497458

498-
static const List<AppLanguage> _supportedLanguages = [
499-
'en',
500-
'ar',
501-
];
459+
static const List<AppLanguage> _supportedLanguages = ['en', 'ar'];
502460
}

0 commit comments

Comments
 (0)