1
+ import 'dart:async' ;
2
+
1
3
import 'package:bloc/bloc.dart' ;
2
4
import 'package:equatable/equatable.dart' ;
3
- import 'package:ht_authentication_repository/ht_authentication_repository.dart' ;
5
+ import 'package:ht_auth_repository/ht_auth_repository.dart' ;
6
+ import 'package:ht_data_repository/ht_data_repository.dart' ;
7
+ import 'package:ht_shared/ht_shared.dart'
8
+ show HtHttpException, User, UserContentPreferences;
4
9
5
10
part 'account_event.dart' ;
6
11
part 'account_state.dart' ;
@@ -10,41 +15,77 @@ part 'account_state.dart';
10
15
/// {@endtemplate}
11
16
class AccountBloc extends Bloc <AccountEvent , AccountState > {
12
17
/// {@macro account_bloc}
13
- AccountBloc ({required HtAuthenticationRepository authenticationRepository})
14
- : _authenticationRepository = authenticationRepository,
15
- super (const AccountState ()) {
16
- on < AccountLogoutRequested > (_onLogoutRequested);
18
+ AccountBloc ({
19
+ required HtAuthRepository authenticationRepository,
20
+ required HtDataRepository <UserContentPreferences >
21
+ userContentPreferencesRepository,
22
+ }) : _authenticationRepository = authenticationRepository,
23
+ _userContentPreferencesRepository = userContentPreferencesRepository,
24
+ super (const AccountState ()) {
25
+ // Listen to authentication state changes from the repository
26
+ _authenticationRepository.authStateChanges.listen (
27
+ (user) => add (_AccountUserChanged (user: user)),
28
+ );
29
+
30
+ on < _AccountUserChanged > (_onAccountUserChanged);
31
+ on < AccountLoadContentPreferencesRequested > (
32
+ _onAccountLoadContentPreferencesRequested,
33
+ );
17
34
// Handlers for AccountSettingsNavigationRequested and
18
35
// AccountBackupNavigationRequested are typically handled in the UI layer
19
36
// (e.g., BlocListener navigating) or could emit specific states if needed.
20
- // For now, we only need the logout logic here.
21
37
}
22
38
23
- final HtAuthenticationRepository _authenticationRepository;
39
+ final HtAuthRepository _authenticationRepository;
40
+ final HtDataRepository <UserContentPreferences >
41
+ _userContentPreferencesRepository;
42
+
43
+ /// Handles [_AccountUserChanged] events.
44
+ ///
45
+ /// Updates the state with the current user and triggers loading
46
+ /// of user preferences if the user is authenticated.
47
+ Future <void > _onAccountUserChanged (
48
+ _AccountUserChanged event,
49
+ Emitter <AccountState > emit,
50
+ ) async {
51
+ emit (state.copyWith (user: event.user));
52
+ if (event.user != null ) {
53
+ // User is authenticated, load preferences
54
+ add (AccountLoadContentPreferencesRequested (userId: event.user! .id));
55
+ } else {
56
+ // User is unauthenticated, clear preferences
57
+ emit (state.copyWith ());
58
+ }
59
+ }
24
60
25
- /// Handles the [AccountLogoutRequested] event .
61
+ /// Handles [AccountLoadContentPreferencesRequested] events .
26
62
///
27
- /// Attempts to sign out the user using the [HtAuthenticationRepository] .
28
- /// Emits [AccountStatus.loading] before the operation and updates to
29
- /// [AccountStatus.success] or [AccountStatus.failure] based on the outcome.
30
- Future <void > _onLogoutRequested (
31
- AccountLogoutRequested event,
63
+ /// Attempts to load the user's content preferences.
64
+ Future <void > _onAccountLoadContentPreferencesRequested (
65
+ AccountLoadContentPreferencesRequested event,
32
66
Emitter <AccountState > emit,
33
67
) async {
34
68
emit (state.copyWith (status: AccountStatus .loading));
35
69
try {
36
- await _authenticationRepository.signOut ();
37
- // No need to emit success here. The AppBloc listening to the
38
- // repository's user stream will handle the global state change
39
- // and trigger the necessary UI updates/redirects.
40
- // We can emit an initial state again if needed for this BLoC's
41
- // local state.
42
- emit (state.copyWith (status: AccountStatus .initial));
70
+ final preferences = await _userContentPreferencesRepository.read (
71
+ id: event.userId,
72
+ userId: event.userId, // Preferences are user-scoped
73
+ );
74
+ emit (
75
+ state.copyWith (status: AccountStatus .success, preferences: preferences),
76
+ );
77
+ } on HtHttpException catch (e) {
78
+ emit (
79
+ state.copyWith (
80
+ status: AccountStatus .failure,
81
+ errorMessage: 'Failed to load preferences: ${e .message }' ,
82
+ ),
83
+ );
43
84
} catch (e) {
44
85
emit (
45
86
state.copyWith (
46
87
status: AccountStatus .failure,
47
- errorMessage: 'Logout failed : $e ' ,
88
+ errorMessage: 'An unexpected error occurred : $e ' ,
48
89
),
49
90
);
50
91
}
0 commit comments