Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
Merculiar committed Sep 20, 2024
1 parent 5ead4e3 commit c53e4e0
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 220 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
import 'package:dfunc/dfunc.dart';
import 'package:flutter/material.dart';

import '../../../di.dart';
import '../../../l10n/l10n.dart';
import '../../../ui/app_bar.dart';
import '../../../ui/colors.dart';
import '../../../ui/dialogs.dart';
import '../../../ui/loader.dart';
import '../../../ui/page_spacer_wrapper.dart';
import '../../../ui/text_field.dart';
import '../../../ui/theme.dart';
import '../../profile/service/update_profile.dart';
import '../models/country.dart';

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

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

static Future<Country?> push(
BuildContext context, {
Country? initial,
NavigatorState? navigator,
}) =>
(navigator ?? Navigator.of(context, rootNavigator: true))
.pushAndRemoveUntil<Country>(
PageRouteBuilder(
pageBuilder: (context, _, __) => CountryPickerScreen(
initial: initial,
shouldUpdateCountry: false,
),
transitionDuration: Duration.zero,
),
F,
)
.then((country) => country);

final Country? initial;
final bool shouldUpdateCountry;

@override
Widget build(BuildContext context) => CpTheme.dark(
Expand All @@ -23,15 +66,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,
shouldUpdateCountry: shouldUpdateCountry,
),
),
),
);
}

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

final Country? initial;
final bool shouldUpdateCountry;

@override
State<_Content> createState() => _ContentState();
Expand All @@ -46,6 +95,24 @@ class _ContentState extends State<_Content> {

final _countries = Country.all;

Future<void> _updateCountry(Country country) => 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,
),
);

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -143,7 +210,14 @@ class _ContentState extends State<_Content> {
),
selectedColor: Colors.white,
shape: selected ? const StadiumBorder() : null,
onTap: () => Navigator.pop(context, country),
onTap: () async {
if (widget.shouldUpdateCountry) {
await _updateCountry(country);
}

if (!context.mounted) return;
Navigator.pop(context, country);
},
),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ class CountryPicker extends StatelessWidget {
child: ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
onTap: () async {
final Country? updated = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CountryPickerScreen(initial: country),
),
);
final Country? updated =
await CountryPickerScreen.push(context, initial: country);

if (context.mounted && updated != null) {
onSubmitted(updated);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
import 'dart:async';

import 'package:flutter/material.dart';

import '../../../di.dart';
import '../../country_picker/screens/country_picker_screen.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,
);

if (context.mounted) {
unawaited(Future.microtask(() => onConfirmed()));
}
} else {
ViewRecoveryPhraseScreen.open(
context,
navigator: navigator,
onConfirmed: () => OnboardingProfileScreen.open(
context,
navigator: navigator,
onConfirmed: onConfirmed,
),
onConfirmed: () async {
await CountryPickerScreen.open(
context,
navigator: navigator,
);

if (context.mounted) {
unawaited(Future.microtask(() => onConfirmed()));
}
},
);
}
}
Expand Down

This file was deleted.

Loading

0 comments on commit c53e4e0

Please sign in to comment.