Skip to content

Commit

Permalink
feat: onboarding flow changes (#1584)
Browse files Browse the repository at this point in the history
  • Loading branch information
Merculiar authored Oct 10, 2024
1 parent b33e7b8 commit 026ee63
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 227 deletions.
6 changes: 0 additions & 6 deletions packages/espressocash_app/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ class _EspressoCashAppState extends State<EspressoCashApp> {
OnboardingFlowScreen.open(
context,
navigator: _navigator.currentState,
onConfirmed: () {
AuthenticatedFlowScreen.open(
context,
navigator: _navigator.currentState,
);
},
);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dfunc/dfunc.dart';
import 'package:flutter/material.dart';

import '../../../l10n/l10n.dart';
Expand All @@ -8,13 +9,52 @@ import '../../../ui/text_field.dart';
import '../../../ui/theme.dart';
import '../models/country.dart';

typedef CountryOnTap = Future<void> Function(
Country updatedCountry,
BuildContext context,
);

class CountryPickerScreen extends StatelessWidget {
const CountryPickerScreen({
super.key,
this.initial,
this.onTap,
});

static Future<void> open(
BuildContext context, {
Country? initial,
CountryOnTap? onTap,
NavigatorState? navigator,
}) =>
(navigator ?? Navigator.of(context, rootNavigator: true))
.pushAndRemoveUntil<Country>(
PageRouteBuilder(
pageBuilder: (context, _, __) => CountryPickerScreen(
initial: initial,
onTap: onTap,
),
transitionDuration: Duration.zero,
),
F,
);

static Future<void> push(
BuildContext context, {
Country? initial,
CountryOnTap? onTap,
}) =>
Navigator.of(context).push<void>(
MaterialPageRoute(
builder: (context) => CountryPickerScreen(
initial: initial,
onTap: onTap,
),
),
);

final Country? initial;
final CountryOnTap? onTap;

@override
Widget build(BuildContext context) => CpTheme.dark(
Expand All @@ -23,15 +63,21 @@ class CountryPickerScreen extends StatelessWidget {
appBar: CpAppBar(
title: Text(context.l10n.selectCountryTitle.toUpperCase()),
),
body: _Wrapper(child: _Content(initial: initial)),
body: _Wrapper(
child: _Content(
initial: initial,
onTap: onTap,
),
),
),
);
}

class _Content extends StatefulWidget {
const _Content({this.initial});
const _Content({this.initial, required this.onTap});

final Country? initial;
final CountryOnTap? onTap;

@override
State<_Content> createState() => _ContentState();
Expand Down Expand Up @@ -143,7 +189,13 @@ class _ContentState extends State<_Content> {
),
selectedColor: Colors.white,
shape: selected ? const StadiumBorder() : null,
onTap: () => Navigator.pop(context, country),
onTap: () async {
await widget.onTap
?.let((onTap) => onTap(country, context));

if (!context.mounted) return;
Navigator.pop(context);
},
),
);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dfunc/dfunc.dart';
import 'package:flutter/material.dart';

import '../../../l10n/l10n.dart';
Expand Down Expand Up @@ -25,15 +26,15 @@ class CountryPicker extends StatelessWidget {
child: ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
onTap: () async {
final Country? updated = await Navigator.push(
Country? country;
await CountryPickerScreen.push(
context,
MaterialPageRoute(
builder: (context) => CountryPickerScreen(initial: country),
),
initial: country,
onTap: (updated, _) async => country = updated,
);

if (context.mounted && updated != null) {
onSubmitted(updated);
if (context.mounted) {
country?.let(onSubmitted);
}
},
title: Text(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,78 @@
import 'dart:async';

import 'package:dfunc/dfunc.dart';
import 'package:flutter/material.dart';

import '../../../di.dart';
import '../../../l10n/l10n.dart';
import '../../../ui/dialogs.dart';
import '../../../ui/loader.dart';
import '../../authenticated/screens/authenticated_flow_screen.dart';
import '../../country_picker/models/country.dart';
import '../../country_picker/screens/country_picker_screen.dart';
import '../../profile/service/update_profile.dart';
import '../data/onboarding_repository.dart';
import 'profile_screen.dart';
import 'view_recovery_phrase_screen.dart';

class OnboardingFlowScreen {
static void open(
static Future<void> open(
BuildContext context, {
required VoidCallback onConfirmed,
NavigatorState? navigator,
}) {
}) async {
final hasConfirmedPassphrase =
sl<OnboardingRepository>().hasConfirmedPassphrase;

if (hasConfirmedPassphrase) {
OnboardingProfileScreen.open(
await CountryPickerScreen.open(
context,
navigator: navigator,
onConfirmed: onConfirmed,
onTap: _updateCountry,
);

if (context.mounted) {
AuthenticatedFlowScreen.open(
context,
navigator: navigator,
);
}
} else {
ViewRecoveryPhraseScreen.open(
context,
navigator: navigator,
onConfirmed: () => OnboardingProfileScreen.open(
context,
navigator: navigator,
onConfirmed: onConfirmed,
),
onConfirmed: () async {
await CountryPickerScreen.open(
context,
navigator: navigator,
onTap: _updateCountry,
);

if (context.mounted) {
AuthenticatedFlowScreen.open(
context,
navigator: navigator,
);
}
},
);
}
}
}

Future<void> _updateCountry(Country country, BuildContext context) =>
runWithLoader(
context,
() async {
await sl<UpdateProfile>()
.call(
countryCode: country.code,
)
.foldAsync((e) => throw e, ignore);

if (!context.mounted) return;
},
onError: (error) => showErrorDialog(
context,
context.l10n.lblProfileUpdateFailed,
error,
),
);
Loading

0 comments on commit 026ee63

Please sign in to comment.