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';
|
|
|
|
import 'package:dartz/dartz.dart';
|
|
|
|
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(),
|
|
|
|
successOrFail: none(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
cancelInputSecret: () async {
|
|
|
|
await getIt<AuthService>().signOut();
|
|
|
|
emit(
|
|
|
|
state.copyWith(
|
|
|
|
successOrFail: none(),
|
|
|
|
isSignOut: true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
didFinishCheck: (Either<Unit, FlowyError> result) {
|
|
|
|
result.fold(
|
|
|
|
(unit) {
|
|
|
|
emit(
|
|
|
|
state.copyWith(
|
|
|
|
loadingState: const LoadingState.loading(),
|
|
|
|
successOrFail: Some(result),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(err) {
|
|
|
|
emit(
|
|
|
|
state.copyWith(
|
|
|
|
loadingState: LoadingState.finish(right(err)),
|
|
|
|
successOrFail: Some(result),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
Either<Unit, FlowyError> result,
|
|
|
|
) = _DidFinishCheck;
|
|
|
|
const factory EncryptSecretEvent.cancelInputSecret() = _CancelInputSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
|
|
|
class EncryptSecretState with _$EncryptSecretState {
|
|
|
|
const factory EncryptSecretState({
|
|
|
|
required Option<Either<Unit, FlowyError>> successOrFail,
|
|
|
|
required bool isSignOut,
|
|
|
|
LoadingState? loadingState,
|
|
|
|
}) = _EncryptSecretState;
|
|
|
|
|
|
|
|
factory EncryptSecretState.initial() => EncryptSecretState(
|
|
|
|
successOrFail: none(),
|
|
|
|
isSignOut: false,
|
|
|
|
);
|
|
|
|
}
|