| 
									
										
										
										
											2024-01-05 17:30:54 +08:00
										 |  |  | import 'package:appflowy/plugins/database/application/defines.dart'; | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  | import 'package:appflowy/startup/startup.dart'; | 
					
						
							|  |  |  | import 'package:appflowy_backend/dispatch/dispatch.dart'; | 
					
						
							|  |  |  | import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; | 
					
						
							|  |  |  | import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart'; | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  | import 'package:appflowy_result/appflowy_result.dart'; | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  | import 'package:flutter_bloc/flutter_bloc.dart'; | 
					
						
							|  |  |  | import 'package:freezed_annotation/freezed_annotation.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import 'auth/auth_service.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | part 'encrypt_secret_bloc.freezed.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class EncryptSecretBloc extends Bloc<EncryptSecretEvent, EncryptSecretState> { | 
					
						
							|  |  |  |   EncryptSecretBloc({required this.user}) | 
					
						
							|  |  |  |       : super(EncryptSecretState.initial()) { | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |     _dispatch(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final UserProfilePB user; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void _dispatch() { | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |     on<EncryptSecretEvent>((event, emit) async { | 
					
						
							|  |  |  |       await event.when( | 
					
						
							|  |  |  |         setEncryptSecret: (secret) async { | 
					
						
							|  |  |  |           if (isLoading()) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           final payload = UserSecretPB.create() | 
					
						
							|  |  |  |             ..encryptionSecret = secret | 
					
						
							|  |  |  |             ..encryptionSign = user.encryptionSign | 
					
						
							|  |  |  |             ..encryptionType = user.encryptionType | 
					
						
							|  |  |  |             ..userId = user.id; | 
					
						
							| 
									
										
										
										
											2024-01-29 10:26:45 +08:00
										 |  |  |           final result = await UserEventSetEncryptionSecret(payload).send(); | 
					
						
							|  |  |  |           if (!isClosed) { | 
					
						
							|  |  |  |             add(EncryptSecretEvent.didFinishCheck(result)); | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |           emit( | 
					
						
							|  |  |  |             state.copyWith( | 
					
						
							|  |  |  |               loadingState: const LoadingState.loading(), | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |               successOrFail: null, | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |             ), | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         cancelInputSecret: () async { | 
					
						
							|  |  |  |           await getIt<AuthService>().signOut(); | 
					
						
							|  |  |  |           emit( | 
					
						
							|  |  |  |             state.copyWith( | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |               successOrFail: null, | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |               isSignOut: true, | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |         didFinishCheck: (result) { | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |           result.fold( | 
					
						
							|  |  |  |             (unit) { | 
					
						
							|  |  |  |               emit( | 
					
						
							|  |  |  |                 state.copyWith( | 
					
						
							|  |  |  |                   loadingState: const LoadingState.loading(), | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |                   successOrFail: result, | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |                 ), | 
					
						
							|  |  |  |               ); | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             (err) { | 
					
						
							|  |  |  |               emit( | 
					
						
							|  |  |  |                 state.copyWith( | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |                   loadingState: LoadingState.finish(FlowyResult.failure(err)), | 
					
						
							|  |  |  |                   successOrFail: result, | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |                 ), | 
					
						
							|  |  |  |               ); | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bool isLoading() { | 
					
						
							|  |  |  |     final loadingState = state.loadingState; | 
					
						
							|  |  |  |     if (loadingState != null) { | 
					
						
							| 
									
										
										
										
											2024-01-11 09:44:33 +08:00
										 |  |  |       return loadingState.when( | 
					
						
							|  |  |  |         loading: () => true, | 
					
						
							|  |  |  |         finish: (_) => false, | 
					
						
							|  |  |  |         idle: () => false, | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @freezed | 
					
						
							|  |  |  | class EncryptSecretEvent with _$EncryptSecretEvent { | 
					
						
							|  |  |  |   const factory EncryptSecretEvent.setEncryptSecret(String secret) = | 
					
						
							|  |  |  |       _SetEncryptSecret; | 
					
						
							|  |  |  |   const factory EncryptSecretEvent.didFinishCheck( | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |     FlowyResult<void, FlowyError> result, | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |   ) = _DidFinishCheck; | 
					
						
							|  |  |  |   const factory EncryptSecretEvent.cancelInputSecret() = _CancelInputSecret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @freezed | 
					
						
							|  |  |  | class EncryptSecretState with _$EncryptSecretState { | 
					
						
							|  |  |  |   const factory EncryptSecretState({ | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |     required FlowyResult<void, FlowyError>? successOrFail, | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |     required bool isSignOut, | 
					
						
							|  |  |  |     LoadingState? loadingState, | 
					
						
							|  |  |  |   }) = _EncryptSecretState; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   factory EncryptSecretState.initial() => const EncryptSecretState( | 
					
						
							|  |  |  |         successOrFail: null, | 
					
						
							| 
									
										
										
										
											2023-08-17 23:46:39 +08:00
										 |  |  |         isSignOut: false, | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | } |