|
| 1 | +import 'package:bloc/bloc.dart'; |
| 2 | +import 'package:equatable/equatable.dart'; |
| 3 | +import 'package:ht_data_repository/ht_data_repository.dart'; |
| 4 | +import 'package:ht_shared/ht_shared.dart'; // Use AppConfig from ht_shared |
| 5 | + |
| 6 | +part 'app_configuration_event.dart'; |
| 7 | +part 'app_configuration_state.dart'; |
| 8 | + |
| 9 | +class AppConfigurationBloc |
| 10 | + extends Bloc<AppConfigurationEvent, AppConfigurationState> { |
| 11 | + AppConfigurationBloc({ |
| 12 | + required HtDataRepository<AppConfig> appConfigRepository, |
| 13 | + }) : _appConfigRepository = appConfigRepository, |
| 14 | + super(const AppConfigurationState()) { |
| 15 | + on<AppConfigurationLoaded>(_onAppConfigurationLoaded); |
| 16 | + on<AppConfigurationUpdated>(_onAppConfigurationUpdated); |
| 17 | + on<AppConfigurationFieldChanged>(_onAppConfigurationFieldChanged); |
| 18 | + } |
| 19 | + |
| 20 | + final HtDataRepository<AppConfig> _appConfigRepository; |
| 21 | + |
| 22 | + Future<void> _onAppConfigurationLoaded( |
| 23 | + AppConfigurationLoaded event, |
| 24 | + Emitter<AppConfigurationState> emit, |
| 25 | + ) async { |
| 26 | + emit(state.copyWith(status: AppConfigurationStatus.loading)); |
| 27 | + try { |
| 28 | + final appConfig = await _appConfigRepository.read(id: 'app_config'); |
| 29 | + emit( |
| 30 | + state.copyWith( |
| 31 | + status: AppConfigurationStatus.success, |
| 32 | + appConfig: appConfig, |
| 33 | + isDirty: false, |
| 34 | + ), |
| 35 | + ); |
| 36 | + } on HtHttpException catch (e) { |
| 37 | + emit( |
| 38 | + state.copyWith( |
| 39 | + status: AppConfigurationStatus.failure, |
| 40 | + errorMessage: e.message, |
| 41 | + ), |
| 42 | + ); |
| 43 | + } catch (e) { |
| 44 | + emit( |
| 45 | + state.copyWith( |
| 46 | + status: AppConfigurationStatus.failure, |
| 47 | + errorMessage: 'An unknown error occurred: $e', |
| 48 | + ), |
| 49 | + ); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Future<void> _onAppConfigurationUpdated( |
| 54 | + AppConfigurationUpdated event, |
| 55 | + Emitter<AppConfigurationState> emit, |
| 56 | + ) async { |
| 57 | + emit(state.copyWith(status: AppConfigurationStatus.loading)); |
| 58 | + try { |
| 59 | + final updatedConfig = await _appConfigRepository.update( |
| 60 | + id: event.appConfig.id, |
| 61 | + item: event.appConfig, |
| 62 | + ); |
| 63 | + emit( |
| 64 | + state.copyWith( |
| 65 | + status: AppConfigurationStatus.success, |
| 66 | + appConfig: updatedConfig, |
| 67 | + isDirty: false, |
| 68 | + ), |
| 69 | + ); |
| 70 | + } on HtHttpException catch (e) { |
| 71 | + emit( |
| 72 | + state.copyWith( |
| 73 | + status: AppConfigurationStatus.failure, |
| 74 | + errorMessage: e.message, |
| 75 | + ), |
| 76 | + ); |
| 77 | + } catch (e) { |
| 78 | + emit( |
| 79 | + state.copyWith( |
| 80 | + status: AppConfigurationStatus.failure, |
| 81 | + errorMessage: 'An unknown error occurred: $e', |
| 82 | + ), |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + void _onAppConfigurationFieldChanged( |
| 88 | + AppConfigurationFieldChanged event, |
| 89 | + Emitter<AppConfigurationState> emit, |
| 90 | + ) { |
| 91 | + emit( |
| 92 | + state.copyWith( |
| 93 | + appConfig: event.appConfig, |
| 94 | + isDirty: true, |
| 95 | + clearErrorMessage: true, // Clear any previous error messages |
| 96 | + ), |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
0 commit comments