-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite LoginScreen as a SwiftUIScreen
- Loading branch information
1 parent
c7d51b6
commit 2fbfc5e
Showing
2 changed files
with
30 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,128 +14,43 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import Workflow | ||
import WorkflowUI | ||
import SwiftUI | ||
import WorkflowSwiftUI | ||
|
||
struct LoginScreen: Screen { | ||
struct LoginScreen: SwiftUIScreen { | ||
var title: String | ||
var email: String | ||
var onEmailChanged: (String) -> Void | ||
var password: String | ||
var onPasswordChanged: (String) -> Void | ||
var onLoginTapped: () -> Void | ||
|
||
func viewControllerDescription(environment: ViewEnvironment) -> ViewControllerDescription { | ||
return ViewControllerDescription( | ||
environment: environment, | ||
build: { LoginViewController() }, | ||
update: { $0.update(with: self) } | ||
) | ||
} | ||
} | ||
|
||
private final class LoginViewController: UIViewController { | ||
private let welcomeLabel: UILabel = UILabel(frame: .zero) | ||
private let emailField: UITextField = UITextField(frame: .zero) | ||
private let passwordField: UITextField = UITextField(frame: .zero) | ||
private let button: UIButton = UIButton(frame: .zero) | ||
private var onEmailChanged: (String) -> Void = { _ in } | ||
private var onPasswordChanged: (String) -> Void = { _ in } | ||
private var onLoginTapped: () -> Void = {} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
view.backgroundColor = .white | ||
|
||
welcomeLabel.textAlignment = .center | ||
|
||
emailField.placeholder = "[email protected]" | ||
emailField.autocapitalizationType = .none | ||
emailField.autocorrectionType = .no | ||
emailField.textContentType = .emailAddress | ||
emailField.backgroundColor = UIColor(white: 0.92, alpha: 1.0) | ||
emailField.addTarget(self, action: #selector(textDidChange(sender:)), for: .editingChanged) | ||
|
||
passwordField.placeholder = "password" | ||
passwordField.isSecureTextEntry = true | ||
passwordField.backgroundColor = UIColor(white: 0.92, alpha: 1.0) | ||
passwordField.addTarget(self, action: #selector(textDidChange(sender:)), for: .editingChanged) | ||
|
||
button.backgroundColor = UIColor(red: 41 / 255, green: 150 / 255, blue: 204 / 255, alpha: 1.0) | ||
button.setTitle("Login", for: .normal) | ||
button.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside) | ||
|
||
view.addSubview(welcomeLabel) | ||
view.addSubview(emailField) | ||
view.addSubview(passwordField) | ||
view.addSubview(button) | ||
} | ||
|
||
override func viewDidLayoutSubviews() { | ||
super.viewDidLayoutSubviews() | ||
|
||
let inset: CGFloat = 12.0 | ||
let height: CGFloat = 44.0 | ||
var yOffset = (view.bounds.size.height - (3 * height + inset)) / 2.0 | ||
|
||
welcomeLabel.frame = CGRect( | ||
x: view.bounds.origin.x, | ||
y: view.bounds.origin.y, | ||
width: view.bounds.size.width, | ||
height: yOffset | ||
) | ||
|
||
emailField.frame = CGRect( | ||
x: view.bounds.origin.x, | ||
y: yOffset, | ||
width: view.bounds.size.width, | ||
height: height | ||
) | ||
.insetBy(dx: inset, dy: 0.0) | ||
|
||
yOffset += height + inset | ||
|
||
passwordField.frame = CGRect( | ||
x: view.bounds.origin.x, | ||
y: yOffset, | ||
width: view.bounds.size.width, | ||
height: height | ||
) | ||
.insetBy(dx: inset, dy: 0.0) | ||
|
||
yOffset += height + inset | ||
|
||
button.frame = CGRect( | ||
x: view.bounds.origin.x, | ||
y: yOffset, | ||
width: view.bounds.size.width, | ||
height: height | ||
) | ||
.insetBy(dx: inset, dy: 0.0) | ||
} | ||
|
||
func update(with screen: LoginScreen) { | ||
welcomeLabel.text = screen.title | ||
emailField.text = screen.email | ||
passwordField.text = screen.password | ||
onEmailChanged = screen.onEmailChanged | ||
onPasswordChanged = screen.onPasswordChanged | ||
onLoginTapped = screen.onLoginTapped | ||
} | ||
|
||
@objc private func textDidChange(sender: UITextField) { | ||
guard let text = sender.text else { | ||
return | ||
static func makeView(model: ObservableValue<LoginScreen>) -> some View { | ||
VStack(spacing: 16) { | ||
Text(model.title) | ||
|
||
TextField( | ||
"[email protected]", | ||
text: model.binding( | ||
get: \.email, | ||
set: \.onEmailChanged | ||
) | ||
) | ||
.autocapitalization(.none) | ||
.autocorrectionDisabled() | ||
.textContentType(.emailAddress) | ||
|
||
SecureField( | ||
"password", | ||
text: model.binding( | ||
get: \.password, | ||
set: \.onPasswordChanged | ||
), | ||
onCommit: model.onLoginTapped | ||
) | ||
|
||
Button("Login", action: model.onLoginTapped) | ||
} | ||
if sender == emailField { | ||
onEmailChanged(text) | ||
} else if sender == passwordField { | ||
onPasswordChanged(text) | ||
} | ||
} | ||
|
||
@objc private func buttonTapped(sender: UIButton) { | ||
onLoginTapped() | ||
.frame(maxWidth: 400) | ||
} | ||
} |