|
| 1 | +import 'dart:async'; |
| 2 | + |
1 | 3 | import 'package:bloc/bloc.dart';
|
2 | 4 | import 'package:equatable/equatable.dart';
|
| 5 | +import 'package:ht_data_repository/ht_data_repository.dart'; |
| 6 | +import 'package:ht_shared/ht_shared.dart'; |
3 | 7 |
|
4 | 8 | part 'settings_event.dart';
|
5 | 9 | part 'settings_state.dart';
|
6 | 10 |
|
7 | 11 | class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
|
8 |
| - SettingsBloc() : super(SettingsInitial()) { |
9 |
| - on<SettingsEvent>((event, emit) { |
10 |
| - // TODO: implement event handler |
11 |
| - }); |
| 12 | + SettingsBloc({ |
| 13 | + required HtDataRepository<UserAppSettings> userAppSettingsRepository, |
| 14 | + }) : _userAppSettingsRepository = userAppSettingsRepository, |
| 15 | + super(SettingsInitial()) { |
| 16 | + on<SettingsLoaded>(_onSettingsLoaded); |
| 17 | + on<SettingsBaseThemeChanged>(_onSettingsBaseThemeChanged); |
| 18 | + on<SettingsAccentThemeChanged>(_onSettingsAccentThemeChanged); |
| 19 | + on<SettingsFontFamilyChanged>(_onSettingsFontFamilyChanged); |
| 20 | + on<SettingsTextScaleFactorChanged>(_onSettingsTextScaleFactorChanged); |
| 21 | + on<SettingsFontWeightChanged>(_onSettingsFontWeightChanged); |
| 22 | + on<SettingsLanguageChanged>(_onSettingsLanguageChanged); |
| 23 | + } |
| 24 | + |
| 25 | + final HtDataRepository<UserAppSettings> _userAppSettingsRepository; |
| 26 | + |
| 27 | + Future<void> _onSettingsLoaded( |
| 28 | + SettingsLoaded event, |
| 29 | + Emitter<SettingsState> emit, |
| 30 | + ) async { |
| 31 | + emit(SettingsLoadInProgress()); |
| 32 | + try { |
| 33 | + // Assuming a fixed ID for user settings, or fetching based on current user |
| 34 | + final userAppSettings = |
| 35 | + await _userAppSettingsRepository.read(id: event.userId!); |
| 36 | + emit(SettingsLoadSuccess(userAppSettings: userAppSettings)); |
| 37 | + } on NotFoundException { |
| 38 | + final defaultSettings = UserAppSettings(id: event.userId!); |
| 39 | + await _userAppSettingsRepository.create(item: defaultSettings); |
| 40 | + emit(SettingsLoadSuccess(userAppSettings: defaultSettings)); |
| 41 | + } on HtHttpException catch (e) { |
| 42 | + emit(SettingsLoadFailure(e.message)); |
| 43 | + } catch (e) { |
| 44 | + emit(SettingsLoadFailure('An unexpected error occurred: $e')); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + Future<void> _updateSettings( |
| 49 | + UserAppSettings updatedSettings, |
| 50 | + Emitter<SettingsState> emit, |
| 51 | + ) async { |
| 52 | + emit(SettingsUpdateInProgress(userAppSettings: updatedSettings)); |
| 53 | + try { |
| 54 | + final result = await _userAppSettingsRepository.update( |
| 55 | + id: updatedSettings.id, |
| 56 | + item: updatedSettings, |
| 57 | + ); |
| 58 | + emit(SettingsUpdateSuccess(userAppSettings: result)); |
| 59 | + } on HtHttpException catch (e) { |
| 60 | + emit(SettingsUpdateFailure(e.message)); |
| 61 | + } catch (e) { |
| 62 | + emit(SettingsUpdateFailure('An unexpected error occurred: $e')); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + Future<void> _onSettingsBaseThemeChanged( |
| 67 | + SettingsBaseThemeChanged event, |
| 68 | + Emitter<SettingsState> emit, |
| 69 | + ) async { |
| 70 | + if (state is SettingsLoadSuccess) { |
| 71 | + final currentSettings = (state as SettingsLoadSuccess).userAppSettings; |
| 72 | + final updatedSettings = currentSettings.copyWith( |
| 73 | + displaySettings: |
| 74 | + currentSettings.displaySettings.copyWith(baseTheme: event.baseTheme), |
| 75 | + ); |
| 76 | + await _updateSettings(updatedSettings, emit); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + Future<void> _onSettingsAccentThemeChanged( |
| 81 | + SettingsAccentThemeChanged event, |
| 82 | + Emitter<SettingsState> emit, |
| 83 | + ) async { |
| 84 | + if (state is SettingsLoadSuccess) { |
| 85 | + final currentSettings = (state as SettingsLoadSuccess).userAppSettings; |
| 86 | + final updatedSettings = currentSettings.copyWith( |
| 87 | + displaySettings: currentSettings.displaySettings |
| 88 | + .copyWith(accentTheme: event.accentTheme), |
| 89 | + ); |
| 90 | + await _updateSettings(updatedSettings, emit); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + Future<void> _onSettingsFontFamilyChanged( |
| 95 | + SettingsFontFamilyChanged event, |
| 96 | + Emitter<SettingsState> emit, |
| 97 | + ) async { |
| 98 | + if (state is SettingsLoadSuccess) { |
| 99 | + final currentSettings = (state as SettingsLoadSuccess).userAppSettings; |
| 100 | + final updatedSettings = currentSettings.copyWith( |
| 101 | + displaySettings: currentSettings.displaySettings |
| 102 | + .copyWith(fontFamily: event.fontFamily), |
| 103 | + ); |
| 104 | + await _updateSettings(updatedSettings, emit); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + Future<void> _onSettingsTextScaleFactorChanged( |
| 109 | + SettingsTextScaleFactorChanged event, |
| 110 | + Emitter<SettingsState> emit, |
| 111 | + ) async { |
| 112 | + if (state is SettingsLoadSuccess) { |
| 113 | + final currentSettings = (state as SettingsLoadSuccess).userAppSettings; |
| 114 | + final updatedSettings = currentSettings.copyWith( |
| 115 | + displaySettings: currentSettings.displaySettings |
| 116 | + .copyWith(textScaleFactor: event.textScaleFactor), |
| 117 | + ); |
| 118 | + await _updateSettings(updatedSettings, emit); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + Future<void> _onSettingsFontWeightChanged( |
| 123 | + SettingsFontWeightChanged event, |
| 124 | + Emitter<SettingsState> emit, |
| 125 | + ) async { |
| 126 | + if (state is SettingsLoadSuccess) { |
| 127 | + final currentSettings = (state as SettingsLoadSuccess).userAppSettings; |
| 128 | + final updatedSettings = currentSettings.copyWith( |
| 129 | + displaySettings: currentSettings.displaySettings |
| 130 | + .copyWith(fontWeight: event.fontWeight), |
| 131 | + ); |
| 132 | + await _updateSettings(updatedSettings, emit); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + Future<void> _onSettingsLanguageChanged( |
| 137 | + SettingsLanguageChanged event, |
| 138 | + Emitter<SettingsState> emit, |
| 139 | + ) async { |
| 140 | + if (state is SettingsLoadSuccess) { |
| 141 | + final currentSettings = (state as SettingsLoadSuccess).userAppSettings; |
| 142 | + final updatedSettings = |
| 143 | + currentSettings.copyWith(language: event.language); |
| 144 | + await _updateSettings(updatedSettings, emit); |
| 145 | + } |
12 | 146 | }
|
13 | 147 | }
|
0 commit comments