Skip to content

Commit

Permalink
Reset digits and period values
Browse files Browse the repository at this point in the history
  • Loading branch information
elibon99 committed Jan 16, 2025
1 parent 22b1b8c commit 83f3f2b
Showing 1 changed file with 49 additions and 52 deletions.
101 changes: 49 additions & 52 deletions lib/oath/views/add_account_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import 'dart:async';
import 'dart:io';
import 'dart:math';

import 'package:flutter/material.dart';
Expand Down Expand Up @@ -79,7 +80,6 @@ class _OathAddAccountPageState extends ConsumerState<OathAddAccountPage>
final _accountFocus = FocusNode();
final _secretFocus = FocusNode();
final _periodController = TextEditingController(text: '$defaultPeriod');
late AnimationController _animationController;
UserInteractionController? _promptController;
Uri? _otpauthUri;
bool _touch = false;
Expand All @@ -106,15 +106,13 @@ class _OathAddAccountPageState extends ConsumerState<OathAddAccountPage>
_issuerFocus.dispose();
_accountFocus.dispose();
_secretFocus.dispose();
_animationController.dispose();
super.dispose();
}

@override
void initState() {
super.initState();
_animationController = AnimationController(
duration: const Duration(milliseconds: 3000), vsync: this);

final cred = widget.credentialData;
if (cred != null) {
_loadCredentialData(cred);
Expand Down Expand Up @@ -345,58 +343,63 @@ class _OathAddAccountPageState extends ConsumerState<OathAddAccountPage>
_hashAlgorithm = defaultHashAlgorithm;
_digits = defaultDigits;
_counter = defaultCounter;
_periodValues = [20, 30, 45, 60];
_digitsValues = [6, 8];
_dataLoaded = false;
});
}

void handleQrData(String? qrData, WithContext withContext) async {
final withContext = ref.read(withContextProvider);
await withContext(
(context) async {
if (qrData != null) {
try {
final creds = CredentialData.fromUri(Uri.parse(qrData));

if (creds.length == 1) {
_loadCredentialData(creds[0]);
setState(() {
_qrScanSuccess = true;
});
} else {
Navigator.of(context).pop();
await handleUri(context, widget.credentials, qrData,
widget.devicePath, widget.state, l10n);
return;
void scanQrCode({File? file}) async {
final qrScanner = ref.read(qrScannerProvider);
if (qrScanner != null) {
final withContext = ref.read(withContextProvider);
setState(() {
_scanning = true;
});
String? qrData;
if (file != null) {
// Scan QR code from file
qrData = await handleQrFile(file, context, withContext, qrScanner);
} else {
qrData = await qrScanner.scanQr();
}
await withContext(
(context) async {
if (qrData != null) {
try {
final creds = CredentialData.fromUri(Uri.parse(qrData));

if (creds.length == 1) {
_loadCredentialData(creds[0]);
setState(() {
_qrScanSuccess = true;
});
} else {
Navigator.of(context).pop();
await handleUri(context, widget.credentials, qrData,
widget.devicePath, widget.state, l10n);
return;
}
} catch (_) {
showMessage(context, l10n.l_invalid_qr);
}
} catch (_) {
showMessage(context, l10n.l_invalid_qr);
} else {
showMessage(context, l10n.l_qr_not_found);
}
} else {
showMessage(context, l10n.l_qr_not_found);
}
setState(() {
_scanning = false;
});
},
);
setState(() {
_scanning = false;
});
},
);
}
}

final qrScanner = ref.read(qrScannerProvider);
final withContext = ref.read(withContextProvider);

final theme = Theme.of(context);
final colorScheme = theme.colorScheme;

return FileDropTarget(
onFileDropped: (file) async {
if (qrScanner != null) {
setState(() {
_scanning = true;
});
final qrData =
await handleQrFile(file, context, withContext, qrScanner);
handleQrData(qrData, withContext);
}
onFileDropped: (file) {
scanQrCode(file: file);
},
overlay: FileDropOverlay(
title: l10n.s_add_account,
Expand Down Expand Up @@ -450,21 +453,15 @@ class _OathAddAccountPageState extends ConsumerState<OathAddAccountPage>
? l10n.l_qr_scanned
: l10n.s_qr_scan,
),
onPressed: () async {
onPressed: () {
if (_qrScanSuccess) {
clearCredentialData();
setState(() {
_qrScanSuccess = false;
});
return;
}
if (qrScanner != null) {
setState(() {
_scanning = true;
});
final qrData = await qrScanner.scanQr();
handleQrData(qrData, withContext);
}
scanQrCode();
},
),
ActionChip(
Expand Down

0 comments on commit 83f3f2b

Please sign in to comment.