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

improve phone number verification counter #846

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Changes from 2 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
119 changes: 90 additions & 29 deletions app/lib/screens/identity_verification_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@ class _IdentityVerificationScreenState
Timer? emailTimer;
ValueNotifier<int> countdownNotifier = ValueNotifier(-1);

int phoneCountdown = 120;
Timer? phoneTimer;
ValueNotifier<int> phoneCountdownNotifier = ValueNotifier(-1);

void startOrResumePhoneCountdown() {
int currentTime = DateTime.now().millisecondsSinceEpoch;
int lockedUntil =
Globals().smsSentOn + (Globals().smsMinutesCoolDown * 60 * 1000);
int timeLeft = ((lockedUntil - currentTime) / 1000).round();

if (timeLeft > 0) {
phoneCountdownNotifier.value = timeLeft;

phoneTimer?.cancel();
phoneTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
int remainingTime =
((lockedUntil - DateTime.now().millisecondsSinceEpoch) / 1000)
.round();

if (remainingTime > 0) {
phoneCountdownNotifier.value = remainingTime;
} else {
phoneCountdownNotifier.value = -1;
timer.cancel();
}
});
} else {
phoneCountdownNotifier.value = -1;
}
}

void startOrResumeEmailCountdown({bool startNew = false}) {
int currentTime = DateTime.now().millisecondsSinceEpoch;
int lockedUntil =
Expand Down Expand Up @@ -123,6 +154,10 @@ class _IdentityVerificationScreenState
if (mounted) {
setState(() {
phoneVerified = Globals().phoneVerified.value;
if (phoneVerified){
phoneCountdownNotifier.value = -1;
phoneTimer?.cancel();
}
Globals().smsSentOn = 0;
});
}
Expand Down Expand Up @@ -155,22 +190,28 @@ class _IdentityVerificationScreenState
checkPhoneStatus();
getUserValues();
startOrResumeEmailCountdown();
startOrResumePhoneCountdown();
}

@override
void dispose() {
emailTimer?.cancel();
phoneTimer?.cancel();
phoneCountdownNotifier.dispose();
countdownNotifier.dispose();
super.dispose();
}

checkPhoneStatus() {
if (Globals().smsSentOn + (Globals().smsMinutesCoolDown * 60 * 1000) >
DateTime.now().millisecondsSinceEpoch) {
int currentTime = DateTime.now().millisecondsSinceEpoch;
int lockedUntil =
Globals().smsSentOn + (Globals().smsMinutesCoolDown * 60 * 1000);

if (lockedUntil > currentTime) {
return Globals().hidePhoneButton.value = true;
} else if (phoneCountdownNotifier.value <= 0) {
return Globals().hidePhoneButton.value = false;
}

return Globals().hidePhoneButton.value = false;
}

void getUserValues() {
Expand Down Expand Up @@ -924,7 +965,7 @@ class _IdentityVerificationScreenState
return _changeEmailDialog(false);
}

if (step == 2) {
if (step == 2 && phoneCountdownNotifier.value == -1) {
if (Globals().hidePhoneButton.value == true) {
return;
}
Expand Down Expand Up @@ -1035,29 +1076,50 @@ class _IdentityVerificationScreenState
step == 2 &&
Globals().hidePhoneButton.value ==
true
? Row(
children: <Widget>[
Text(
'SMS sent, retry in ${calculateMinutes()} minute${calculateMinutes() == '1' ? '' : 's'}',
overflow: TextOverflow.clip,
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(
fontWeight:
FontWeight.bold,
color: Theme.of(context)
.colorScheme
.warning),
)
],
)
? Row(children: <Widget>[
ValueListenableBuilder<int>(
valueListenable:
phoneCountdownNotifier,
builder: (context, remainingTime,
child) {
if (remainingTime > 0) {
String formattedTime;
if (remainingTime >= 60) {
int minutes = remainingTime ~/
60;
int seconds = remainingTime %
60;
formattedTime =
'${minutes}m ${seconds}s';
} else {
formattedTime =
'${remainingTime}s';
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we export the logic here in an outer function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


return Text(
'SMS sent, retry in $formattedTime',
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(
fontWeight:
FontWeight.bold,
color: Theme.of(context)
.colorScheme
.warning,
),
);
}
return Container();
},
)
])
: Container(),
]))),
Globals().hidePhoneButton.value == true && step == 2
? Container()
: ValueListenableBuilder(
valueListenable: countdownNotifier,
valueListenable: step == 1 ? countdownNotifier : phoneCountdownNotifier,
builder: (context, countdownValue, child) {
return Padding(
padding: const EdgeInsets.only(left: 12),
Expand Down Expand Up @@ -1106,13 +1168,11 @@ class _IdentityVerificationScreenState
int currentTime = DateTime.now().millisecondsSinceEpoch;
int lockedUntil =
Globals().smsSentOn + (Globals().smsMinutesCoolDown * 60 * 1000);
String difference =
((lockedUntil - currentTime) / 1000 / 60).round().toString();
int remainingTime = ((lockedUntil - currentTime) / 1000).round();

if (int.parse(difference) >= 0) {
return difference;
if (remainingTime > 0) {
return (remainingTime / 60).ceil().toString();
}

return '0';
}

Expand Down Expand Up @@ -1876,10 +1936,10 @@ class _IdentityVerificationScreenState
FlutterPkid client = await getPkidClient();
client.setPKidDoc('phone', json.encode({'phone': phone}));

startPhoneNumberCounter();
return;
} else {
PhoneAlertDialogState().sendPhoneVerification();
startPhoneNumberCounter();
return;
}
}
Expand Down Expand Up @@ -1938,6 +1998,7 @@ class _IdentityVerificationScreenState
Globals().hidePhoneButton.value = true;
Globals().smsSentOn = DateTime.now().millisecondsSinceEpoch;

startOrResumePhoneCountdown();
phoneSendDialog(context);
}

Expand Down
Loading