fix for resend, cooldown and a few small fixes
This commit is contained in:
@@ -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,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user