Skip to content

Commit

Permalink
Quick and dirty new Login UI
Browse files Browse the repository at this point in the history
  • Loading branch information
weiran committed Apr 4, 2022
1 parent 77841b4 commit 2942e66
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ type_name:
identifier_name:
excluded:
- id
- by
- by
allowed_symbols:
- _body
161 changes: 161 additions & 0 deletions App/Settings/LoginView.swift
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
}
7 changes: 6 additions & 1 deletion App/Settings/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import UIKit
import SwiftUI
import SafariServices
import MessageUI
import PromiseKit
Expand Down Expand Up @@ -144,7 +145,11 @@ extension SettingsViewController {
}

private func login() {
authenticationUIService?.showAuthentication()
let loginView = LoginView()
let loginHC = UIHostingController(rootView: loginView)
present(loginHC, animated: true)

//authenticationUIService?.showAuthentication()
}
}

Expand Down
18 changes: 11 additions & 7 deletions Hackers.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
2481BD61247C11AE0088CA2B /* HackersKit+CommentVoting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2481BD60247C11AE0088CA2B /* HackersKit+CommentVoting.swift */; };
2481BD63247C12540088CA2B /* HNScraperShim.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2481BD62247C12540088CA2B /* HNScraperShim.swift */; };
2481BD65247C1B380088CA2B /* HackersKit+PostVoting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2481BD64247C1B380088CA2B /* HackersKit+PostVoting.swift */; };
2486D7D527FAEBFC007504C6 /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2486D7D427FAEBFC007504C6 /* LoginView.swift */; };
24993C55229C2BD400AF1891 /* AuthenticationUIService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24993C54229C2BD400AF1891 /* AuthenticationUIService.swift */; };
24993C57229C768700AF1891 /* AuthenticationBulletinPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24993C56229C768700AF1891 /* AuthenticationBulletinPage.swift */; };
249A7E32194374550059C079 /* CommentTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 249A7E31194374550059C079 /* CommentTableViewCell.swift */; };
Expand Down Expand Up @@ -160,6 +161,7 @@
2481BD60247C11AE0088CA2B /* HackersKit+CommentVoting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HackersKit+CommentVoting.swift"; sourceTree = "<group>"; };
2481BD62247C12540088CA2B /* HNScraperShim.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HNScraperShim.swift; sourceTree = "<group>"; };
2481BD64247C1B380088CA2B /* HackersKit+PostVoting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HackersKit+PostVoting.swift"; sourceTree = "<group>"; };
2486D7D427FAEBFC007504C6 /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
24993C54229C2BD400AF1891 /* AuthenticationUIService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationUIService.swift; sourceTree = "<group>"; };
24993C56229C768700AF1891 /* AuthenticationBulletinPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationBulletinPage.swift; sourceTree = "<group>"; };
249A7E31194374550059C079 /* CommentTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommentTableViewCell.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -359,6 +361,7 @@
2464D21A209E1FA100C7F8FC /* SettingsViewController.swift */,
2464D21C209E208800C7F8FC /* SettingsTableViewCell.swift */,
2464D21E209E27FE00C7F8FC /* UserDefaultsExtensions.swift */,
2486D7D427FAEBFC007504C6 /* LoginView.swift */,
);
path = Settings;
sourceTree = "<group>";
Expand Down Expand Up @@ -745,7 +748,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "buildNumber=$(xcodebuild -showBuildSettings -project Hackers.xcodeproj | pcregrep -o1 \"PROJECT_VERSION = ([0-9a-f\\-]+)\")\nmarketingVersion=$(xcodebuild -showBuildSettings -project Hackers.xcodeproj | pcregrep -o1 \"MARKETING_VERSION = ([0-9a-f\\-.]+)\")\n\n/usr/libexec/PlistBuddy -c \"Set :CFBundleVersion $buildNumber\" \"$SRCROOT/Extensions/HackersShareExtension/Info.plist\"\n/usr/libexec/PlistBuddy -c \"Set :CFBundleShortVersionString $marketingVersion\" \"$SRCROOT/Extensions/HackersShareExtension/Info.plist\"\n";
shellScript = "if [ $ENABLE_PREVIEWS == \"NO\" ]\nthen\n buildNumber=$(xcodebuild -showBuildSettings -project Hackers.xcodeproj | pcregrep -o1 \"PROJECT_VERSION = ([0-9a-f\\-]+)\")\n marketingVersion=$(xcodebuild -showBuildSettings -project Hackers.xcodeproj | pcregrep -o1 \"MARKETING_VERSION = ([0-9a-f\\-.]+)\")\n /usr/libexec/PlistBuddy -c \"Set :CFBundleVersion $buildNumber\" \"$SRCROOT/Extensions/HackersShareExtension/Info.plist\"\n /usr/libexec/PlistBuddy -c \"Set :CFBundleShortVersionString $marketingVersion\" \"$SRCROOT/Extensions/HackersShareExtension/Info.plist\"\nelse\n echo \"Skipping the script because of preview mode\"\nfi\n";
};
2419D03E248AC2CA00740184 /* Update Version Numbers */ = {
isa = PBXShellScriptBuildPhase;
Expand All @@ -763,25 +766,25 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "buildNumber=$(xcodebuild -showBuildSettings -project Hackers.xcodeproj | pcregrep -o1 \"PROJECT_VERSION = ([0-9a-f\\-]+)\")\nmarketingVersion=$(xcodebuild -showBuildSettings -project Hackers.xcodeproj | pcregrep -o1 \"MARKETING_VERSION = ([0-9a-f\\-.]+)\")\n\n/usr/libexec/PlistBuddy -c \"Set :CFBundleVersion $buildNumber\" \"$SRCROOT/Extensions/HackersActionExtension/Info.plist\"\n/usr/libexec/PlistBuddy -c \"Set :CFBundleShortVersionString $marketingVersion\" \"$SRCROOT/Extensions/HackersActionExtension/Info.plist\"\n";
shellScript = "if [ $ENABLE_PREVIEWS == \"NO\" ]\nthen\n buildNumber=$(xcodebuild -showBuildSettings -project Hackers.xcodeproj | pcregrep -o1 \"PROJECT_VERSION = ([0-9a-f\\-]+)\")\n marketingVersion=$(xcodebuild -showBuildSettings -project Hackers.xcodeproj | pcregrep -o1 \"MARKETING_VERSION = ([0-9a-f\\-.]+)\")\n /usr/libexec/PlistBuddy -c \"Set :CFBundleVersion $buildNumber\" \"$SRCROOT/Extensions/HackersActionExtension/Info.plist\"\n /usr/libexec/PlistBuddy -c \"Set :CFBundleShortVersionString $marketingVersion\" \"$SRCROOT/Extensions/HackersActionExtension/Info.plist\"\nelse\n echo \"Skipping the script because of preview mode\"\nfi\n";
};
244DE6BB1E599EB6005B441E /* TODO as Warnings */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
buildActionMask = 8;
files = (
);
inputPaths = (
);
name = "TODO as Warnings";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
runOnlyForDeploymentPostprocessing = 1;
shellPath = /bin/sh;
shellScript = "KEYWORDS=\"TODO:|FIXME:|\\?\\?\\?:|\\!\\!\\!:\"\nfind \".\" \\( -name \"*.h\" -or -name \"*.m\" -or -name \"*.swift\" \\) -not -path \"./Pods/*\" -print0 | \n xargs -0 egrep --with-filename --line-number --only-matching \"($KEYWORDS).*\\$\" | \n perl -p -e \"s/($KEYWORDS)/ warning: \\$1/\"\n";
shellScript = "KEYWORDS=\"TODO:|FIXME:|\\?\\?\\?:|\\!\\!\\!:\"\nfind \".\" \\( -name \"*.h\" -or -name \"*.m\" -or -name \"*.swift\" \\) -not -path \"./Pods/*\" -print0 | \nxargs -0 egrep --with-filename --line-number --only-matching \"($KEYWORDS).*\\$\" | \nperl -p -e \"s/($KEYWORDS)/ warning: \\$1/\"\n";
};
249B8F8022A29B2E002A9EC5 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
buildActionMask = 12;
files = (
);
inputFileListPaths = (
Expand All @@ -794,7 +797,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/SwiftLint/swiftlint\"\n";
shellScript = "if [ $ENABLE_PREVIEWS == \"NO\" ]\nthen\n \"${PODS_ROOT}/SwiftLint/swiftlint\"\nelse\n echo \"Skipping the script because of preview mode\"\nfi\n";
};
32F4D363AD3C2A95650B70E3 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
Expand Down Expand Up @@ -934,6 +937,7 @@
2454B9041F655426005A98C7 /* EmptyViewController.swift in Sources */,
2464D21D209E208800C7F8FC /* SettingsTableViewCell.swift in Sources */,
24D1846124B396BD0017E4F4 /* UIViewController+Deselection.swift in Sources */,
2486D7D527FAEBFC007504C6 /* LoginView.swift in Sources */,
24D811A51FF90A58000951C3 /* TouchableTextView.swift in Sources */,
24B982DD22BE65E2000D8913 /* SettingsView.swift in Sources */,
24CEAA9B1F8916970050DEDF /* UIViewExtensions.swift in Sources */,
Expand Down

0 comments on commit 2942e66

Please sign in to comment.