瀏覽代碼

Merge pull request #112 from ente-io/109_fix_bug

Fix bug in edit code
Neeraj Gupta 2 年之前
父節點
當前提交
08cc772ff1
共有 2 個文件被更改,包括 54 次插入10 次删除
  1. 37 0
      lib/models/code.dart
  2. 17 10
      lib/onboarding/view/setup_enter_secret_key_page.dart

+ 37 - 0
lib/models/code.dart

@@ -27,6 +27,43 @@ class Code {
     this.generatedID,
     this.generatedID,
   });
   });
 
 
+  Code copyWith({
+    String? account,
+    String? issuer,
+    int? digits,
+    int? period,
+    String? secret,
+    Algorithm? algorithm,
+    Type? type,
+  }) {
+    final String updateAccount = account ?? this.account;
+    final String updateIssuer = issuer ?? this.issuer;
+    final int updatedDigits = digits ?? this.digits;
+    final int updatePeriod = period ?? this.period;
+    final String updatedSecret = secret ?? this.secret;
+    final Algorithm updatedAlgo = algorithm ?? this.algorithm;
+    final Type updatedType = type ?? this.type;
+
+    return Code(
+      updateAccount,
+      updateIssuer,
+      updatedDigits,
+      updatePeriod,
+      updatedSecret,
+      updatedAlgo,
+      updatedType,
+      "otpauth://${updatedType.name}/" +
+          updateIssuer +
+          ":" +
+          updateAccount +
+          "?algorithm=${updatedAlgo.name}&digits=$updatedDigits&issuer=" +
+          updateIssuer +
+          "&period=$updatePeriod&secret=" +
+          updatedSecret,
+      generatedID: generatedID,
+    );
+  }
+
   static Code fromAccountAndSecret(
   static Code fromAccountAndSecret(
     String account,
     String account,
     String issuer,
     String issuer,

+ 17 - 10
lib/onboarding/view/setup_enter_secret_key_page.dart

@@ -117,17 +117,24 @@ class _SetupEnterSecretKeyPageState extends State<SetupEnterSecretKeyPage> {
                         return;
                         return;
                       }
                       }
                       try {
                       try {
-                        final code = Code.fromAccountAndSecret(
-                          _accountController.text.trim(),
-                          _issuerController.text.trim(),
-                          _secretController.text.trim().replaceAll(' ', ''),
-                        );
+                        final account = _accountController.text.trim();
+                        final issuer = _issuerController.text.trim();
+                        final secret =
+                            _secretController.text.trim().replaceAll(' ', '');
+                        final Code newCode = widget.code == null
+                            ? Code.fromAccountAndSecret(
+                                account,
+                                issuer,
+                                secret,
+                              )
+                            : widget.code!.copyWith(
+                                account: account,
+                                issuer: issuer,
+                                secret: secret,
+                              );
                         // Verify the validity of the code
                         // Verify the validity of the code
-                        getTotp(code);
-                        if (widget.code != null) {
-                          code.generatedID = widget.code!.generatedID;
-                        }
-                        Navigator.of(context).pop(code);
+                        getTotp(newCode);
+                        Navigator.of(context).pop(newCode);
                       } catch (e) {
                       } catch (e) {
                         _showIncorrectDetailsDialog(context);
                         _showIncorrectDetailsDialog(context);
                       }
                       }