Skip to content

Commit 6171249

Browse files
authored
Merge pull request #3 from headlines-toolkit/fix_and_ui_refactor_for_app_configuration_feature
Fix and UI refactor for app configuration feature
2 parents 50d5536 + e36e57f commit 6171249

File tree

6 files changed

+992
-359
lines changed

6 files changed

+992
-359
lines changed

lib/app/view/app.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:go_router/go_router.dart';
77
import 'package:ht_auth_repository/ht_auth_repository.dart';
88
import 'package:ht_dashboard/app/bloc/app_bloc.dart';
99
import 'package:ht_dashboard/app/config/app_environment.dart';
10+
import 'package:ht_dashboard/app_configuration/bloc/app_configuration_bloc.dart';
1011
import 'package:ht_dashboard/authentication/bloc/authentication_bloc.dart';
1112
import 'package:ht_dashboard/l10n/app_localizations.dart';
1213
import 'package:ht_dashboard/router/router.dart';
@@ -81,6 +82,11 @@ class App extends StatelessWidget {
8182
authenticationRepository: context.read<HtAuthRepository>(),
8283
),
8384
),
85+
BlocProvider(
86+
create: (context) => AppConfigurationBloc(
87+
appConfigRepository: context.read<HtDataRepository<AppConfig>>(),
88+
),
89+
),
8490
],
8591
child: _AppView(
8692
htAuthenticationRepository: _htAuthenticationRepository,

lib/app_configuration/bloc/app_configuration_bloc.dart

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ class AppConfigurationBloc
1111
AppConfigurationBloc({
1212
required HtDataRepository<AppConfig> appConfigRepository,
1313
}) : _appConfigRepository = appConfigRepository,
14-
super(const AppConfigurationState()) {
14+
super(
15+
const AppConfigurationState(),
16+
) {
1517
on<AppConfigurationLoaded>(_onAppConfigurationLoaded);
1618
on<AppConfigurationUpdated>(_onAppConfigurationUpdated);
1719
on<AppConfigurationFieldChanged>(_onAppConfigurationFieldChanged);
20+
on<AppConfigurationDiscarded>(_onAppConfigurationDiscarded);
1821
}
1922

2023
final HtDataRepository<AppConfig> _appConfigRepository;
@@ -30,7 +33,10 @@ class AppConfigurationBloc
3033
state.copyWith(
3134
status: AppConfigurationStatus.success,
3235
appConfig: appConfig,
36+
originalAppConfig: appConfig, // Store the original config
3337
isDirty: false,
38+
clearShowSaveSuccess:
39+
true, // Clear any previous success snackbar flag
3440
),
3541
);
3642
} on HtHttpException catch (e) {
@@ -64,7 +70,9 @@ class AppConfigurationBloc
6470
state.copyWith(
6571
status: AppConfigurationStatus.success,
6672
appConfig: updatedConfig,
73+
originalAppConfig: updatedConfig, // Update original config on save
6774
isDirty: false,
75+
showSaveSuccess: true, // Set flag to show success snackbar
6876
),
6977
);
7078
} on HtHttpException catch (e) {
@@ -93,6 +101,21 @@ class AppConfigurationBloc
93101
appConfig: event.appConfig,
94102
isDirty: true,
95103
clearErrorMessage: true, // Clear any previous error messages
104+
clearShowSaveSuccess: true, // Clear success snackbar on field change
105+
),
106+
);
107+
}
108+
109+
void _onAppConfigurationDiscarded(
110+
AppConfigurationDiscarded event,
111+
Emitter<AppConfigurationState> emit,
112+
) {
113+
emit(
114+
state.copyWith(
115+
appConfig: state.originalAppConfig, // Revert to original config
116+
isDirty: false,
117+
clearErrorMessage: true, // Clear any previous error messages
118+
clearShowSaveSuccess: true, // Clear success snackbar
96119
),
97120
);
98121
}

lib/app_configuration/bloc/app_configuration_event.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class AppConfigurationUpdated extends AppConfigurationEvent {
3333
List<Object?> get props => [appConfig];
3434
}
3535

36+
/// {@template app_configuration_discarded}
37+
/// Event to discard any unsaved changes to the application configuration.
38+
/// {@endtemplate}
39+
class AppConfigurationDiscarded extends AppConfigurationEvent {
40+
/// {@macro app_configuration_discarded}
41+
const AppConfigurationDiscarded();
42+
}
43+
3644
/// {@template app_configuration_field_changed}
3745
/// Event to notify that a field in the application configuration has changed.
3846
///

lib/app_configuration/bloc/app_configuration_state.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ class AppConfigurationState extends Equatable {
2323
const AppConfigurationState({
2424
this.status = AppConfigurationStatus.initial,
2525
this.appConfig,
26+
this.originalAppConfig,
2627
this.errorMessage,
2728
this.isDirty = false,
29+
this.showSaveSuccess = false,
2830
});
2931

3032
/// The current status of the application configuration.
@@ -33,35 +35,50 @@ class AppConfigurationState extends Equatable {
3335
/// The loaded or updated application configuration.
3436
final AppConfig? appConfig;
3537

38+
/// The original application configuration loaded from the backend.
39+
final AppConfig? originalAppConfig;
40+
3641
/// An error message if an operation failed.
3742
final String? errorMessage;
3843

3944
/// Indicates if there are unsaved changes to the configuration.
4045
final bool isDirty;
4146

47+
/// Indicates if a save operation was successful and a snackbar should be shown.
48+
final bool showSaveSuccess;
49+
4250
/// Creates a copy of the current state with updated values.
4351
AppConfigurationState copyWith({
4452
AppConfigurationStatus? status,
4553
AppConfig? appConfig,
54+
AppConfig? originalAppConfig,
4655
String? errorMessage,
4756
bool? isDirty,
4857
bool clearErrorMessage = false,
58+
bool? showSaveSuccess,
59+
bool clearShowSaveSuccess = false,
4960
}) {
5061
return AppConfigurationState(
5162
status: status ?? this.status,
5263
appConfig: appConfig ?? this.appConfig,
64+
originalAppConfig: originalAppConfig ?? this.originalAppConfig,
5365
errorMessage: clearErrorMessage
5466
? null
5567
: errorMessage ?? this.errorMessage,
5668
isDirty: isDirty ?? this.isDirty,
69+
showSaveSuccess: clearShowSaveSuccess
70+
? false
71+
: showSaveSuccess ?? this.showSaveSuccess,
5772
);
5873
}
5974

6075
@override
6176
List<Object?> get props => [
6277
status,
6378
appConfig,
79+
originalAppConfig,
6480
errorMessage,
6581
isDirty,
82+
showSaveSuccess,
6683
];
6784
}

0 commit comments

Comments
 (0)