forked from weiran/Hackers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
181 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,6 @@ type_name: | |
identifier_name: | ||
excluded: | ||
- id | ||
- by | ||
- by | ||
allowed_symbols: | ||
- _body |
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 |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// | ||
// LoginView.swift | ||
// Hackers | ||
// | ||
// Created by Weiran Zhang on 04/04/2022. | ||
// Copyright © 2022 Glass Umbrella. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import Swinject | ||
import SwinjectStoryboard | ||
import PromiseKit | ||
|
||
struct LoginView: View { | ||
@State private var username: String = "" | ||
@State private var password: String = "" | ||
@State private var isAuthenticating = false | ||
@State private var showAlert = false | ||
|
||
@Inject private var sessionService: SessionService | ||
|
||
@Environment(\.presentationMode) var presentationMode | ||
|
||
var body: some View { | ||
VStack { | ||
Text("Login to Hacker News") | ||
.font(.largeTitle) | ||
.padding(.bottom, 30) | ||
|
||
TextField("Username", text: $username) | ||
.autocapitalization(.none) | ||
.disableAutocorrection(true) | ||
.textFieldStyle(RoundedTextField()) | ||
.textContentType(.username) | ||
|
||
SecureField("Password", text: $password) | ||
.textFieldStyle(RoundedTextField()) | ||
.textContentType(.password) | ||
|
||
Text("Hackers never stores your password") | ||
.foregroundColor(Color.secondary) | ||
.font(.footnote) | ||
|
||
Button("Login") { | ||
isAuthenticating = true | ||
sessionService.authenticate(username: username, password: password) | ||
.done { _ in | ||
presentationMode.wrappedValue.dismiss() | ||
}.ensure { | ||
NotificationCenter.default.post(name: Notification.Name.refreshRequired, object: nil) | ||
isAuthenticating = false | ||
}.catch { _ in | ||
showAlert = true | ||
password = "" | ||
} | ||
}.buttonStyle(FilledButton()) | ||
.padding(.top, 30) | ||
.disabled(isAuthenticating) | ||
.alert(isPresented: $showAlert) { | ||
Alert( | ||
title: Text("Login Failed"), | ||
message: Text("Error logging into Hacker News, check your username and password.") | ||
) | ||
} | ||
|
||
LabelledDivider(label: "or") | ||
|
||
Link(destination: URL(string: "https://news.ycombinator.com/login")!) { | ||
HStack { | ||
Text("Register on Hacker News") | ||
Image(systemName: "rectangle.portrait.and.arrow.right") | ||
} | ||
.padding() | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct RoundedTextField: TextFieldStyle { | ||
func _body(configuration: TextField<Self._Label>) -> some View { | ||
configuration | ||
.padding(.all, 10) | ||
.overlay( | ||
RoundedRectangle(cornerRadius: 5) | ||
.stroke(Color.secondary, lineWidth: 0.5) | ||
) | ||
.padding(.horizontal, 20) | ||
} | ||
} | ||
|
||
struct FilledButton: ButtonStyle { | ||
func makeBody(configuration: Configuration) -> some View { | ||
configuration | ||
.label | ||
.padding() | ||
.padding(.horizontal, 50) | ||
.frame(maxWidth: .infinity) | ||
.foregroundColor(Color.white) | ||
.background(Color.accentColor) | ||
.cornerRadius(5) | ||
.padding(.horizontal, 20) | ||
} | ||
} | ||
|
||
struct LabelledDivider: View { | ||
|
||
let label: String | ||
let horizontalPadding: CGFloat | ||
let color: Color | ||
|
||
init(label: String, horizontalPadding: CGFloat = 20, color: Color = .gray) { | ||
self.label = label | ||
self.horizontalPadding = horizontalPadding | ||
self.color = color | ||
} | ||
|
||
var body: some View { | ||
HStack { | ||
line | ||
Text(label).foregroundColor(color) | ||
line | ||
} | ||
} | ||
|
||
var line: some View { | ||
VStack { Divider().background(color) }.padding(horizontalPadding) | ||
} | ||
} | ||
|
||
@propertyWrapper | ||
struct Inject<Component> { | ||
let wrappedValue: Component | ||
init() { | ||
self.wrappedValue = Resolver.shared.resolve(Component.self) | ||
} | ||
} | ||
|
||
class Resolver { | ||
static let shared = Resolver() | ||
private let container = buildContainer() | ||
|
||
func resolve<T>(_ type: T.Type) -> T { | ||
container.resolve(T.self)! | ||
} | ||
} | ||
|
||
struct LoginView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
LoginView() | ||
} | ||
} | ||
|
||
func buildContainer() -> Container { | ||
let container = SwinjectStoryboard.defaultContainer | ||
|
||
container.register(SessionService.self) { _ in | ||
return SessionService() | ||
}.inObjectScope(.container) | ||
|
||
return container | ||
} |
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