Skip to content

Commit

Permalink
Have bloc handle changing the selected profile, move that state into …
Browse files Browse the repository at this point in the history
…the bloc class.
  • Loading branch information
famousj committed Nov 17, 2023
1 parent 0ffbfd5 commit 475a7d7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
21 changes: 13 additions & 8 deletions example/lib/src/presentation/ui/auth/auth_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ import 'package:polygonid_flutter_sdk_example/utils/secure_storage_keys.dart';

class AuthBloc extends Bloc<AuthEvent, AuthState> {
final PolygonIdSdk _polygonIdSdk;
final NonceUtils _nonceUtils;

AuthBloc(this._polygonIdSdk)
: _nonceUtils = NonceUtils(getIt()),
super(const AuthState.initial()) {
static const SelectedProfile _defaultProfile = SelectedProfile.public;
SelectedProfile selectedProfile = _defaultProfile;

AuthBloc(this._polygonIdSdk) : super(const AuthState.initial()) {
on<ClickScanQrCodeEvent>(_handleClickScanQrCode);
on<ScanQrCodeResponse>(_handleScanQrCodeResponse);
on<ProfileSelectedEvent>(_handleProfileSelected);
}

void _handleProfileSelected(
ProfileSelectedEvent event, Emitter<AuthState> emit) {
selectedProfile = event.profile;
emit(AuthState.profileSelected(event.profile));
}

///
Expand Down Expand Up @@ -61,7 +68,6 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
await _authenticate(
iden3message: iden3message,
privateKey: privateKey,
profile: event.profile,
emit: emit,
);
} catch (error) {
Expand All @@ -73,7 +79,6 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
Future<void> _authenticate({
required Iden3MessageEntity iden3message,
required String privateKey,
required SelectedProfile profile,
required Emitter<AuthState> emit,
}) async {
emit(const AuthState.loading());
Expand All @@ -86,9 +91,9 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
network: envEntity.network);

try {
final BigInt nonce = profile == SelectedProfile.public
final BigInt nonce = selectedProfile == SelectedProfile.public
? GENESIS_PROFILE_NONCE
: await _nonceUtils.getPrivateProfileNonce(
: await NonceUtils(getIt()).getPrivateProfileNonce(
did: did, privateKey: privateKey, from: iden3message.from);
await _polygonIdSdk.iden3comm.authenticate(
message: iden3message,
Expand Down
6 changes: 4 additions & 2 deletions example/lib/src/presentation/ui/auth/auth_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ part 'auth_event.freezed.dart';

@freezed
class AuthEvent with _$AuthEvent {
const factory AuthEvent.profileSelected(SelectedProfile profile) =
ProfileSelectedEvent;
const factory AuthEvent.clickScanQrCode() = ClickScanQrCodeEvent;
const factory AuthEvent.onScanQrCodeResponse(
String? response, SelectedProfile profile) = ScanQrCodeResponse;
const factory AuthEvent.onScanQrCodeResponse(String? response) =
ScanQrCodeResponse;
}
4 changes: 4 additions & 0 deletions example/lib/src/presentation/ui/auth/auth_state.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:polygonid_flutter_sdk/iden3comm/domain/entities/common/iden3_message_entity.dart';
import 'package:polygonid_flutter_sdk_example/src/presentation/ui/common/widgets/profile_radio_button.dart';

part 'auth_state.freezed.dart';

Expand All @@ -9,6 +10,9 @@ class AuthState with _$AuthState {

const factory AuthState.loading() = LoadingAuthState;

const factory AuthState.profileSelected(SelectedProfile profile) =
ProfileSelectedAuthState;

const factory AuthState.navigateToQrCodeScanner() =
NavigateToQrCodeScannerAuthState;

Expand Down
23 changes: 15 additions & 8 deletions example/lib/src/presentation/ui/auth/widgets/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ class AuthScreen extends StatefulWidget {
}

class _AuthScreenState extends State<AuthScreen> {
SelectedProfile _profile = SelectedProfile.public;
void _selectProfile(SelectedProfile profile) {
setState(() => _profile = profile);
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -70,7 +65,7 @@ class _AuthScreenState extends State<AuthScreen> {
],
),
),
Expanded(child: ProfileRadio(_profile, _selectProfile)),
Expanded(child: _buildRadioButtons()),
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Stack(
Expand Down Expand Up @@ -151,8 +146,7 @@ class _AuthScreenState extends State<AuthScreen> {
Future<void> _handleNavigateToQrCodeScannerAuthState() async {
String? qrCodeScanningResult =
await Navigator.pushNamed(context, Routes.qrCodeScannerPath) as String?;
widget._bloc
.add(AuthEvent.onScanQrCodeResponse(qrCodeScanningResult, _profile));
widget._bloc.add(AuthEvent.onScanQrCodeResponse(qrCodeScanningResult));
}

///
Expand Down Expand Up @@ -227,4 +221,17 @@ class _AuthScreenState extends State<AuthScreen> {
},
);
}

///
Widget _buildRadioButtons() {
void _selectProfile(SelectedProfile profile) {
widget._bloc.add(AuthEvent.profileSelected(profile));
}

return BlocBuilder(
bloc: widget._bloc,
builder: (BuildContext context, AuthState state) {
return ProfileRadio(widget._bloc.selectedProfile, _selectProfile);
});
}
}

0 comments on commit 475a7d7

Please sign in to comment.