fix for resend, cooldown and a few small fixes
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
class SignupConfirmationArgs {
|
||||
final String? email;
|
||||
final String? password;
|
||||
|
||||
const SignupConfirmationArgs({this.email});
|
||||
const SignupConfirmationArgs({this.email, this.password});
|
||||
}
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'package:pshared/provider/account.dart';
|
||||
import 'package:pweb/models/resend/action_result.dart';
|
||||
import 'package:pweb/models/resend/avaliability.dart';
|
||||
|
||||
import 'package:pweb/utils/snackbar.dart';
|
||||
import 'package:pweb/utils/error/snackbar.dart';
|
||||
import 'package:pweb/utils/cooldown_format.dart';
|
||||
import 'package:pweb/controllers/signup/confirmation_card.dart';
|
||||
import 'package:pweb/widgets/resend_link.dart';
|
||||
import 'package:pweb/widgets/vspacer.dart';
|
||||
|
||||
import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
@@ -15,7 +18,6 @@ import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||
part 'state.dart';
|
||||
part 'content.dart';
|
||||
part 'badge.dart';
|
||||
part 'resend_button.dart';
|
||||
|
||||
|
||||
class SignupConfirmationCard extends StatefulWidget {
|
||||
|
||||
@@ -63,11 +63,11 @@ class _SignupConfirmationContent extends StatelessWidget {
|
||||
runSpacing: 12,
|
||||
alignment: WrapAlignment.center,
|
||||
children: [
|
||||
_SignupConfirmationResendButton(
|
||||
canResend: canResend,
|
||||
isResending: isResending,
|
||||
resendLabel: resendLabel,
|
||||
onResend: onResend,
|
||||
ResendLink(
|
||||
label: resendLabel,
|
||||
onPressed: onResend,
|
||||
isDisabled: !canResend,
|
||||
isLoading: isResending,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
part of 'card.dart';
|
||||
|
||||
|
||||
class _SignupConfirmationResendButton extends StatelessWidget {
|
||||
final bool canResend;
|
||||
final bool isResending;
|
||||
final String resendLabel;
|
||||
final VoidCallback onResend;
|
||||
|
||||
const _SignupConfirmationResendButton({
|
||||
required this.canResend,
|
||||
required this.isResending,
|
||||
required this.resendLabel,
|
||||
required this.onResend,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return ElevatedButton.icon(
|
||||
onPressed: canResend ? onResend : null,
|
||||
icon: isResending
|
||||
? SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: theme.colorScheme.onPrimary,
|
||||
),
|
||||
)
|
||||
: const Icon(Icons.mark_email_read_outlined),
|
||||
label: Text(resendLabel),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,61 +3,45 @@ part of 'card.dart';
|
||||
|
||||
class _SignupConfirmationCardState extends State<SignupConfirmationCard> {
|
||||
static const int _defaultCooldownSeconds = 60;
|
||||
|
||||
Timer? _cooldownTimer;
|
||||
int _cooldownRemainingSeconds = 0;
|
||||
bool _isResending = false;
|
||||
late final SignupConfirmationCardController _controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_startCooldown(_defaultCooldownSeconds);
|
||||
_controller = SignupConfirmationCardController(
|
||||
accountProvider: context.read<AccountProvider>(),
|
||||
defaultCooldown: const Duration(seconds: _defaultCooldownSeconds),
|
||||
);
|
||||
_controller.initialize(email: widget.email);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_cooldownTimer?.cancel();
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool get _isCooldownActive => _cooldownRemainingSeconds > 0;
|
||||
|
||||
void _startCooldown(int seconds) {
|
||||
_cooldownTimer?.cancel();
|
||||
if (seconds <= 0) {
|
||||
setState(() => _cooldownRemainingSeconds = 0);
|
||||
return;
|
||||
@override
|
||||
void didUpdateWidget(covariant SignupConfirmationCard oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.email != widget.email) {
|
||||
_controller.updateEmail(widget.email);
|
||||
}
|
||||
setState(() => _cooldownRemainingSeconds = seconds);
|
||||
_cooldownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
if (!mounted) {
|
||||
timer.cancel();
|
||||
return;
|
||||
}
|
||||
if (_cooldownRemainingSeconds <= 1) {
|
||||
timer.cancel();
|
||||
setState(() => _cooldownRemainingSeconds = 0);
|
||||
return;
|
||||
}
|
||||
setState(() => _cooldownRemainingSeconds -= 1);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _resendVerificationEmail() async {
|
||||
final email = widget.email?.trim();
|
||||
final locs = AppLocalizations.of(context)!;
|
||||
if (email == null || email.isEmpty) {
|
||||
notifyUser(context, locs.errorEmailMissing);
|
||||
return;
|
||||
}
|
||||
if (_isResending || _isCooldownActive) return;
|
||||
|
||||
setState(() => _isResending = true);
|
||||
try {
|
||||
await context.read<AccountProvider>().resendVerificationEmail(email);
|
||||
final result = await _controller.resendVerificationEmail();
|
||||
if (!mounted) return;
|
||||
notifyUser(context, locs.signupConfirmationResent(email));
|
||||
_startCooldown(_defaultCooldownSeconds);
|
||||
if (result == ResendActionResult.missingEmail) {
|
||||
notifyUser(context, locs.errorEmailMissing);
|
||||
return;
|
||||
}
|
||||
if (result == ResendActionResult.sent) {
|
||||
final email = widget.email?.trim() ?? '';
|
||||
notifyUser(context, locs.signupConfirmationResent(email));
|
||||
}
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
postNotifyUserOfErrorX(
|
||||
@@ -65,22 +49,9 @@ class _SignupConfirmationCardState extends State<SignupConfirmationCard> {
|
||||
errorSituation: locs.signupConfirmationResendError,
|
||||
exception: e,
|
||||
);
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _isResending = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _formatCooldown(int seconds) {
|
||||
final minutes = seconds ~/ 60;
|
||||
final remainingSeconds = seconds % 60;
|
||||
if (minutes > 0) {
|
||||
return '$minutes:${remainingSeconds.toString().padLeft(2, '0')}';
|
||||
}
|
||||
return remainingSeconds.toString();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
@@ -89,34 +60,44 @@ class _SignupConfirmationCardState extends State<SignupConfirmationCard> {
|
||||
final description = (email != null && email.isNotEmpty)
|
||||
? locs.signupConfirmationDescription(email)
|
||||
: locs.signupConfirmationDescriptionNoEmail;
|
||||
final canResend = !_isResending && !_isCooldownActive && email != null && email.isNotEmpty;
|
||||
final resendLabel = _isCooldownActive
|
||||
? locs.signupConfirmationResendCooldown(_formatCooldown(_cooldownRemainingSeconds))
|
||||
: locs.signupConfirmationResend;
|
||||
|
||||
final content = _SignupConfirmationContent(
|
||||
email: email,
|
||||
description: description,
|
||||
canResend: canResend,
|
||||
resendLabel: resendLabel,
|
||||
isResending: _isResending,
|
||||
onResend: _resendVerificationEmail,
|
||||
);
|
||||
return AnimatedBuilder(
|
||||
animation: _controller,
|
||||
builder: (context, _) {
|
||||
final availability = _controller.resendAvailability;
|
||||
final canResend = availability == ResendAvailability.available;
|
||||
final isResending = availability == ResendAvailability.resending;
|
||||
final resendLabel = availability == ResendAvailability.cooldown
|
||||
? locs.signupConfirmationResendCooldown(
|
||||
formatCooldownSeconds(_controller.cooldownRemainingSeconds),
|
||||
)
|
||||
: locs.signupConfirmationResend;
|
||||
|
||||
if (widget.isEmbedded) return content;
|
||||
final content = _SignupConfirmationContent(
|
||||
email: email,
|
||||
description: description,
|
||||
canResend: canResend,
|
||||
resendLabel: resendLabel,
|
||||
isResending: isResending,
|
||||
onResend: _resendVerificationEmail,
|
||||
);
|
||||
|
||||
return Card(
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
side: BorderSide(
|
||||
color: theme.dividerColor.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(28),
|
||||
child: content,
|
||||
),
|
||||
if (widget.isEmbedded) return content;
|
||||
|
||||
return Card(
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
side: BorderSide(
|
||||
color: theme.dividerColor.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(28),
|
||||
child: content,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,106 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'package:pshared/provider/account.dart';
|
||||
|
||||
import 'package:pweb/app/router/pages.dart';
|
||||
import 'package:pweb/pages/login/app_bar.dart';
|
||||
import 'package:pweb/pages/signup/confirmation/card/card.dart';
|
||||
import 'package:pweb/pages/signup/confirmation/login_prompt.dart';
|
||||
import 'package:pweb/controllers/signup/confirmation.dart';
|
||||
import 'package:pweb/pages/with_footer.dart';
|
||||
import 'package:pweb/widgets/vspacer.dart';
|
||||
|
||||
|
||||
class SignUpConfirmationPage extends StatefulWidget {
|
||||
final String? email;
|
||||
final String? password;
|
||||
|
||||
const SignUpConfirmationPage({super.key, this.email});
|
||||
const SignUpConfirmationPage({
|
||||
super.key,
|
||||
this.email,
|
||||
this.password,
|
||||
});
|
||||
|
||||
@override
|
||||
State<SignUpConfirmationPage> createState() => _SignUpConfirmationPageState();
|
||||
}
|
||||
|
||||
class _SignUpConfirmationPageState extends State<SignUpConfirmationPage> {
|
||||
late final SignupConfirmationController _controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = SignupConfirmationController(
|
||||
accountProvider: context.read<AccountProvider>(),
|
||||
)..addListener(_handleAuthorizationStatus);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _startPolling());
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.removeListener(_handleAuthorizationStatus);
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _startPolling() {
|
||||
if (!mounted) return;
|
||||
final email = widget.email?.trim();
|
||||
final password = widget.password;
|
||||
if (email == null || email.isEmpty || password == null || password.isEmpty) {
|
||||
return;
|
||||
}
|
||||
_controller.startPolling(
|
||||
email: email,
|
||||
password: password,
|
||||
locale: Localizations.localeOf(context).toLanguageTag(),
|
||||
);
|
||||
}
|
||||
|
||||
void _handleAuthorizationStatus() {
|
||||
if (!_controller.isAuthorized || !mounted) return;
|
||||
navigateAndReplace(context, Pages.login);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final email = widget.email?.trim();
|
||||
final width = MediaQuery.of(context).size.width;
|
||||
final isWide = width >= 980;
|
||||
|
||||
return PageWithFooter(
|
||||
appBar: const LoginAppBar(),
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final isWide = constraints.maxWidth >= 980;
|
||||
|
||||
return ListView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 32),
|
||||
children: [
|
||||
Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(maxWidth: isWide ? 980 : 720),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Card(
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
side: BorderSide(
|
||||
color: Theme.of(context).dividerColor.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(28),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
SignupConfirmationCard(
|
||||
email: email,
|
||||
isEmbedded: true,
|
||||
),
|
||||
const VSpacer(multiplier: 2),
|
||||
Divider(
|
||||
height: 1,
|
||||
color: Theme.of(context).dividerColor.withValues(alpha: 0.6),
|
||||
),
|
||||
const VSpacer(multiplier: 2),
|
||||
const SignupConfirmationLoginPrompt(isEmbedded: true),
|
||||
],
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 32),
|
||||
child: Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(maxWidth: isWide ? 980 : 720),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Card(
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
side: BorderSide(
|
||||
color: Theme.of(context).dividerColor.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(28),
|
||||
child:
|
||||
SignupConfirmationCard(
|
||||
email: email,
|
||||
isEmbedded: true,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user