From fea7a91bb839edf2e7d6946a2f93e31f78113a08 Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Wed, 1 Nov 2017 16:55:21 -0400
Subject: [PATCH] accounts: allow passwordless account creation and device
 linking

- Allows the user to create an account, link to an existing
  account, and generate account link PINs  without supplying a
  password.

Change-Id: I41216d55bb44bab5f92fd096d483f639502e4a42
Reviewed-by: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
---
 .../Features/Me/LinkNewDeviceViewController.swift    |  7 +------
 .../CreateAccount/CreateAccountViewModel.swift       | 12 ++++++++----
 .../Walkthrough/LinkDevice/LinkDeviceViewModel.swift | 11 ++++-------
 3 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/Ring/Ring/Features/Me/LinkNewDeviceViewController.swift b/Ring/Ring/Features/Me/LinkNewDeviceViewController.swift
index 9cd7cea81..5778a590b 100644
--- a/Ring/Ring/Features/Me/LinkNewDeviceViewController.swift
+++ b/Ring/Ring/Features/Me/LinkNewDeviceViewController.swift
@@ -62,12 +62,6 @@ class LinkNewDeviceViewController: UIViewController, StoryboardBased, ViewModelB
             .bind(to: self.pinLabel.rx.isVisible)
             .addDisposableTo(self.disposeBag)
 
-        passwordField.rx.text
-            .map({!$0!.isEmpty})
-            .shareReplay(1)
-            .bind(to: okButton.rx.isEnabled)
-            .addDisposableTo(self.disposeBag)
-
         self.viewModel.observableState
             .observeOn(MainScheduler.instance)
             .subscribe(onNext: { [weak self] (state) in
@@ -115,3 +109,4 @@ class LinkNewDeviceViewController: UIViewController, StoryboardBased, ViewModelB
         self.explanationMessage.text = self.viewModel.explanationMessage
     }
 }
+
diff --git a/Ring/Ring/Features/Walkthrough/CreateAccount/CreateAccountViewModel.swift b/Ring/Ring/Features/Walkthrough/CreateAccount/CreateAccountViewModel.swift
index 196e4f3a2..5709dc862 100644
--- a/Ring/Ring/Features/Walkthrough/CreateAccount/CreateAccountViewModel.swift
+++ b/Ring/Ring/Features/Walkthrough/CreateAccount/CreateAccountViewModel.swift
@@ -170,6 +170,10 @@ class CreateAccountViewModel: Stateable, ViewModel {
     lazy var passwordValidationState: Observable<PasswordValidationState> = {
         return Observable.combineLatest(self.password.asObservable(), self.confirmPassword.asObservable())
         { (password: String, confirmPassword: String) -> PasswordValidationState in
+            if password.isEmpty && confirmPassword.isEmpty {
+                return .validated
+            }
+
             if password.characters.count < 6 {
                 return .error(message: L10n.Createaccount.passwordCharactersNumberError)
             }
@@ -186,17 +190,19 @@ class CreateAccountViewModel: Stateable, ViewModel {
         return Observable.combineLatest(self.passwordValidationState.asObservable(),
                                         self.usernameValidationState.asObservable(),
                                         self.registerUsername.asObservable(),
+                                        self.username.asObservable(),
                                         self.createState,
                                         resultSelector:
             { ( passwordValidationState: PasswordValidationState,
                 usernameValidationState: UsernameValidationState,
                 registerUsername: Bool,
+                username: String,
                 creationState: AccountCreationState) -> Bool in
 
                 var canAsk = true
 
                 if registerUsername {
-                    canAsk = canAsk && usernameValidationState.isAvailable
+                    canAsk = canAsk && usernameValidationState.isAvailable && !username.isEmpty
                 }
 
                 canAsk = canAsk && passwordValidationState.isValidated
@@ -220,13 +226,10 @@ class CreateAccountViewModel: Stateable, ViewModel {
             switch status {
             case .lookingUp:
                 self?.usernameValidationState.value = .lookingForAvailibility(message: L10n.Createaccount.lookingForUsernameAvailability)
-                break
             case .invalid:
                 self?.usernameValidationState.value = .invalid(message: L10n.Createaccount.invalidUsername)
-                break
             case .alreadyTaken:
                 self?.usernameValidationState.value = .unavailable(message: L10n.Createaccount.usernameAlreadyTaken)
-                break
             default:
                 self?.usernameValidationState.value = .available
             }
@@ -277,3 +280,4 @@ class CreateAccountViewModel: Stateable, ViewModel {
                                            password: self.password.value)
     }
 }
+
diff --git a/Ring/Ring/Features/Walkthrough/LinkDevice/LinkDeviceViewModel.swift b/Ring/Ring/Features/Walkthrough/LinkDevice/LinkDeviceViewModel.swift
index 8364598fa..753aa4979 100644
--- a/Ring/Ring/Features/Walkthrough/LinkDevice/LinkDeviceViewModel.swift
+++ b/Ring/Ring/Features/Walkthrough/LinkDevice/LinkDeviceViewModel.swift
@@ -35,13 +35,9 @@ class LinkDeviceViewModel: Stateable, ViewModel {
     }()
 
     lazy var linkButtonEnabledState: Observable<Bool>  = {
-        return Observable.combineLatest(self.password.asObservable(),
-                                        self.pin.asObservable()) {(password, pin) -> Bool in
-                                            if !password.isEmpty && !pin.isEmpty {
-                                                return true
-                                            }
-                                            return false
-        }
+        return self.pin.asObservable().map({ pin in
+            return !pin.isEmpty
+        })
     }()
 
     let pin = Variable<String>("")
@@ -76,3 +72,4 @@ class LinkDeviceViewModel: Stateable, ViewModel {
                                               password: self.password.value)
     }
 }
+
-- 
GitLab