Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cat-voices): registration seed phrase step #897

Merged
merged 17 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion catalyst_voices/lib/dependency/dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class Dependencies extends DependencyProvider {
)
..registerLazySingleton<SessionBloc>(SessionBloc.new)
// Factory will rebuild it each time needed
..registerFactory<RegistrationBloc>(RegistrationBloc.new);
..registerFactory<RegistrationCubit>(RegistrationCubit.new);
}

void _registerRepositories() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,13 @@ class _Navigation extends StatelessWidget {
children: [
Expanded(
child: VoicesBackButton(
onTap: () {
RegistrationBloc.of(context).add(const PreviousStepEvent());
},
onTap: () => RegistrationCubit.of(context).previousStep(),
),
),
const SizedBox(width: 10),
Expanded(
child: VoicesNextButton(
onTap: () {
RegistrationBloc.of(context).add(const NextStepEvent());
},
onTap: () => RegistrationCubit.of(context).nextStep(),
),
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,45 @@ import 'package:catalyst_voices_localization/catalyst_voices_localization.dart';
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:flutter/material.dart';

class SeedPhrasePanel extends StatelessWidget {
class SeedPhrasePanel extends StatefulWidget {
final SeedPhrase? seedPhrase;
final bool isStoreSeedPhraseConfirmed;
final bool isNextEnabled;

const SeedPhrasePanel({
super.key,
required this.seedPhrase,
this.seedPhrase,
required this.isStoreSeedPhraseConfirmed,
required this.isNextEnabled,
});

@override
State<SeedPhrasePanel> createState() => _SeedPhrasePanelState();
}

class _SeedPhrasePanelState extends State<SeedPhrasePanel> {
@override
void initState() {
RegistrationCubit.of(context).buildSeedPhrase();
super.initState();
damian-molinski marked this conversation as resolved.
Show resolved Hide resolved
}

@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
child: _Body(
words: seedPhrase?.mnemonicWords ?? [],
words: widget.seedPhrase?.mnemonicWords,
onDownloadTap: _downloadSeedPhrase,
),
),
const SizedBox(height: 10),
_SeedPhraseStoredConfirmation(isConfirmed: isStoreSeedPhraseConfirmed),
_SeedPhraseStoredConfirmation(
isConfirmed: widget.isStoreSeedPhraseConfirmed,
),
const SizedBox(height: 10),
_Navigation(isNextEnabled: isNextEnabled),
_Navigation(isNextEnabled: widget.isNextEnabled),
],
);
}
Expand All @@ -38,10 +51,34 @@ class SeedPhrasePanel extends StatelessWidget {
}

class _Body extends StatelessWidget {
final List<String> words;
final List<String>? words;
final VoidCallback? onDownloadTap;

const _Body({
this.words,
this.onDownloadTap,
});

@override
Widget build(BuildContext context) {
final words = this.words;

if (words == null || words.isEmpty) {
return const Center(child: VoicesCircularProgressIndicator());
} else {
return _SeedPhraseWords(
words: words,
onDownloadTap: onDownloadTap,
);
}
}
}

class _SeedPhraseWords extends StatelessWidget {
final List<String> words;
final VoidCallback? onDownloadTap;

const _SeedPhraseWords({
required this.words,
this.onDownloadTap,
});
Expand Down Expand Up @@ -79,8 +116,7 @@ class _SeedPhraseStoredConfirmation extends StatelessWidget {
value: isConfirmed,
label: Text(context.l10n.createKeychainSeedPhraseStoreConfirmation),
onChanged: (value) {
final event = SeedPhraseStoreConfirmationEvent(value: value);
RegistrationBloc.of(context).add(event);
RegistrationCubit.of(context).confirmSeedPhraseStored(confirmed: value);
},
);
}
Expand All @@ -99,16 +135,14 @@ class _Navigation extends StatelessWidget {
children: [
Expanded(
child: VoicesBackButton(
onTap: () {
RegistrationBloc.of(context).add(const PreviousStepEvent());
},
onTap: () => RegistrationCubit.of(context).previousStep(),
),
),
const SizedBox(width: 10),
Expanded(
child: VoicesNextButton(
onTap: isNextEnabled
? () => RegistrationBloc.of(context).add(const NextStepEvent())
? () => RegistrationCubit.of(context).nextStep
damian-molinski marked this conversation as resolved.
Show resolved Hide resolved
: null,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ class SplashPanel extends StatelessWidget {
const Spacer(),
VoicesFilledButton(
child: Text(context.l10n.accountCreationSplashNextButton),
onTap: () {
RegistrationBloc.of(context).add(const NextStepEvent());
},
onTap: () => RegistrationCubit.of(context).nextStep(),
),
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ class GetStartedPanel extends StatelessWidget {
key: ValueKey(type),
type: type,
onTap: () {
final event = CreateAccountTypeEvent(type: type);
RegistrationBloc.of(context).add(event);
switch (type) {
case CreateAccountType.createNew:
RegistrationCubit.of(context).createNewKeychain();
case CreateAccountType.recover:
RegistrationCubit.of(context).recoverKeychain();
}
},
);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class RegistrationDialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => Dependencies.instance.get<RegistrationBloc>(),
child: BlocBuilder<RegistrationBloc, RegistrationState>(
create: (context) => Dependencies.instance.get<RegistrationCubit>(),
child: BlocBuilder<RegistrationCubit, RegistrationState>(
builder: (context, state) => _RegistrationDialog(state: state),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RegistrationInfoPanel extends StatelessWidget {

@override
Widget build(BuildContext context) {
return BlocBuilder<RegistrationBloc, RegistrationState>(
return BlocBuilder<RegistrationCubit, RegistrationState>(
builder: (context, state) {
final headerStrings = _buildHeaderStrings(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class IntroPanel extends StatelessWidget {
VoicesFilledButton(
leading: VoicesAssets.icons.wallet.buildIcon(),
onTap: () {
RegistrationBloc.of(context).add(const NextStepEvent());
RegistrationCubit.of(context).nextStep();
},
child: Text(context.l10n.chooseCardanoWallet),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class RbacTransactionPanel extends StatelessWidget {
VoicesFilledButton(
leading: VoicesAssets.icons.wallet.buildIcon(),
onTap: () {
RegistrationBloc.of(context).add(const PreviousStepEvent());
RegistrationCubit.of(context).previousStep();
},
child: const Text('Previous'),
),
const SizedBox(height: 12),
VoicesFilledButton(
leading: VoicesAssets.icons.wallet.buildIcon(),
onTap: () {
RegistrationBloc.of(context).add(const NextStepEvent());
RegistrationCubit.of(context).nextStep();
},
child: const Text('Next'),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class RolesChooserPanel extends StatelessWidget {
VoicesFilledButton(
leading: VoicesAssets.icons.wallet.buildIcon(),
onTap: () {
RegistrationBloc.of(context).add(const PreviousStepEvent());
RegistrationCubit.of(context).previousStep();
},
child: const Text('Previous'),
),
const SizedBox(height: 12),
VoicesFilledButton(
leading: VoicesAssets.icons.wallet.buildIcon(),
onTap: () {
RegistrationBloc.of(context).add(const NextStepEvent());
RegistrationCubit.of(context).nextStep();
},
child: const Text('Next'),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class RolesSummaryPanel extends StatelessWidget {
VoicesFilledButton(
leading: VoicesAssets.icons.wallet.buildIcon(),
onTap: () {
RegistrationBloc.of(context).add(const PreviousStepEvent());
RegistrationCubit.of(context).previousStep();
},
child: const Text('Previous'),
),
const SizedBox(height: 12),
VoicesFilledButton(
leading: VoicesAssets.icons.wallet.buildIcon(),
onTap: () {
RegistrationBloc.of(context).add(const NextStepEvent());
RegistrationCubit.of(context).nextStep();
},
child: const Text('Next'),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class _SelectWalletPanelState extends State<SelectWalletPanel> {
const SizedBox(height: 24),
VoicesBackButton(
onTap: () {
RegistrationBloc.of(context).add(const PreviousStepEvent());
RegistrationCubit.of(context).previousStep();
},
),
const SizedBox(height: 10),
Expand All @@ -64,8 +64,7 @@ class _SelectWalletPanelState extends State<SelectWalletPanel> {
}

void _sendRefreshEvent() {
const event = RefreshCardanoWalletsEvent();
RegistrationBloc.of(context).add(event);
RegistrationCubit.of(context).refreshCardanoWallets();
}
}

Expand Down Expand Up @@ -105,7 +104,7 @@ class _WalletsList extends StatelessWidget {
iconSrc: wallet.icon,
name: Text(wallet.name),
onTap: () {
RegistrationBloc.of(context).add(const NextStepEvent());
RegistrationCubit.of(context).nextStep();
},
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class WalletDetailsPanel extends StatelessWidget {
VoicesFilledButton(
leading: VoicesAssets.icons.wallet.buildIcon(),
onTap: () {
RegistrationBloc.of(context).add(const PreviousStepEvent());
RegistrationCubit.of(context).previousStep();
},
child: const Text('Previous'),
),
const SizedBox(height: 12),
VoicesFilledButton(
leading: VoicesAssets.icons.wallet.buildIcon(),
onTap: () {
RegistrationBloc.of(context).add(const NextStepEvent());
RegistrationCubit.of(context).nextStep();
},
child: const Text('Next'),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
// ignore_for_file: one_member_abstracts
// ignore_for_file: avoid_positional_boolean_parameters

import 'package:catalyst_voices_blocs/catalyst_voices_blocs.dart';
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

final class KeychainCreationController with ChangeNotifier {
// ignore: prefer_final_fields
SeedPhrase _seedPhrase = SeedPhrase();
final class KeychainCreationCubit extends Cubit<CreateKeychain> {
CreateKeychainStage _stage;
SeedPhrase? _seedPhrase;
bool _isSeedPhraseStoredConfirmed = false;

KeychainCreationController();
KeychainCreationCubit()
: _stage = CreateKeychainStage.splash,
super(const CreateKeychain());

void handleEvent(KeychainCreationEvent event) {
switch (event) {
case SeedPhraseStoreConfirmationEvent():
if (_isSeedPhraseStoredConfirmed != event.value) {
_isSeedPhraseStoredConfirmed = event.value;
notifyListeners();
}
void changeStage(CreateKeychainStage newValue) {
if (_stage != newValue) {
_stage = newValue;
emit(_buildState());
}
}

CreateKeychain buildState(CreateKeychainStage stage) {
return CreateKeychain(
stage: stage,
seedPhraseState: SeedPhraseState(
seedPhrase: _seedPhrase,
isStoredConfirmed: _isSeedPhraseStoredConfirmed,
),
);
void buildSeedPhrase() {
_seedPhrase = SeedPhrase();
emit(_buildState());
}

void ensureSeedPhraseCreated() {
if (_seedPhrase == null) {
buildSeedPhrase();
}
}

void setSeedPhraseStoredConfirmed(bool newValue) {
if (_isSeedPhraseStoredConfirmed != newValue) {
_isSeedPhraseStoredConfirmed = newValue;
emit(_buildState());
}
}

CreateKeychainStep? nextStep(CreateKeychainStage stage) {
final currentStageIndex = CreateKeychainStage.values.indexOf(stage);
CreateKeychainStep? nextStep() {
final currentStageIndex = CreateKeychainStage.values.indexOf(_stage);
final isLast = currentStageIndex == CreateKeychainStage.values.length - 1;
if (isLast) {
return null;
Expand All @@ -43,8 +49,8 @@ final class KeychainCreationController with ChangeNotifier {
return CreateKeychainStep(stage: nextStage);
}

CreateKeychainStep? previousStep(CreateKeychainStage stage) {
final currentStageIndex = CreateKeychainStage.values.indexOf(stage);
CreateKeychainStep? previousStep() {
final currentStageIndex = CreateKeychainStage.values.indexOf(_stage);
final isFirst = currentStageIndex == 0;
if (isFirst) {
return null;
Expand All @@ -53,4 +59,14 @@ final class KeychainCreationController with ChangeNotifier {
final previousStage = CreateKeychainStage.values[currentStageIndex - 1];
return CreateKeychainStep(stage: previousStage);
}

CreateKeychain _buildState() {
return CreateKeychain(
stage: _stage,
seedPhraseState: SeedPhraseState(
seedPhrase: _seedPhrase,
isStoredConfirmed: _isSeedPhraseStoredConfirmed,
),
);
}
}
Loading
Loading