Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Jun 8, 2020
1 parent 1939349 commit 5124f0e
Show file tree
Hide file tree
Showing 14 changed files with 1,027 additions and 424 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1150"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2A3CEE822486B4EA00C69C3C"
BuildableName = "CombineSchedulers.app"
BlueprintName = "CombineSchedulers"
ReferencedContainer = "container:CombineSchedulers.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2A3CEE982486B4EC00C69C3C"
BuildableName = "CombineSchedulersTests.xctest"
BlueprintName = "CombineSchedulersTests"
ReferencedContainer = "container:CombineSchedulers.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2A3CEE822486B4EA00C69C3C"
BuildableName = "CombineSchedulers.app"
BlueprintName = "CombineSchedulers"
ReferencedContainer = "container:CombineSchedulers.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2A3CEE822486B4EA00C69C3C"
BuildableName = "CombineSchedulers.app"
BlueprintName = "CombineSchedulers"
ReferencedContainer = "container:CombineSchedulers.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// AppDelegate.swift
// CombineSchedulers
//
// Created by Point-Free on 5/29/20.
// Created by Point-Free on 6/2/20.
// Copyright © 2020 Point-Free. All rights reserved.
//

Expand Down
Original file line number Diff line number Diff line change
@@ -1,91 +1,149 @@
import Combine
import SwiftUI

func validationMessage<P: Publisher>(
forPassword password: P
) -> AnyPublisher<String, Never> where P.Output == String, P.Failure == Never {

password.map {
$0.count < 5 ? "Password is too short 👎"
: $0.count > 20 ? "Password is too long 👎"
: "Password is good 👍"
}
.eraseToAnyPublisher()

}


class RegisterViewModel: ObservableObject {
struct Alert: Identifiable {
var title: String
var id: String { self.title }
}

@Published var email = ""
@Published var errorAlert: Alert?
@Published var isRegistered = false
@Published var isRegisterRequestInFlight = false
@Published var password = ""
@Published var isLoginSuccessful = false
@Published var passwordValidationMessage = ""

var cancellables: Set<AnyCancellable> = []
let register: (String, String) -> AnyPublisher<(data: Data, response: URLResponse), URLError>

let login: (String, String) -> AnyPublisher<Bool, Never>
var cancellables: Set<AnyCancellable> = []

init(
login: @escaping (String, String) -> AnyPublisher<Bool, Never>
register: @escaping (String, String) -> AnyPublisher<(data: Data, response: URLResponse), URLError>,
validatePassword: @escaping (String) -> AnyPublisher<(data: Data, response: URLResponse), URLError>
) {
self.login = login
self.register = register

self.$password
.map {
$0.count < 5 ? "Password is too short 👎"
: $0.count > 20 ? "Password is too long 👎"
: "Password is good 👍"
}
.sink { self.passwordValidationMessage = $0 }
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
// .debounce(for: .milliseconds(300), scheduler: ImmediateScheduler.shared)
.flatMap { password in
password.isEmpty
? Just("").eraseToAnyPublisher()
: validatePassword(password)
.receive(on: DispatchQueue.main)
// .receive(on: ImmediateScheduler.shared)
.map { data, _ in
String(decoding: data, as: UTF8.self)
}
.replaceError(with: "Could not validate password.")
.eraseToAnyPublisher()
}
.sink { [weak self] in self?.passwordValidationMessage = $0 }
.store(in: &self.cancellables)
}

func loginButtonTapped() {
self.login(self.email, self.password)
func registerButtonTapped() {
self.isRegisterRequestInFlight = true
self.register(self.email, self.password)
.receive(on: DispatchQueue.main)
.sink { self.isLoginSuccessful = $0 }
// .receive(on: ImmediateScheduler.shared)
.map { data, _ in
Bool(String(decoding: data, as: UTF8.self)) ?? false
}
.replaceError(with: false)
.sink {
self.isRegistered = $0
self.isRegisterRequestInFlight = false
if !$0 {
self.errorAlert = Alert(title: "Failed to register. Please try again.")
}
}
.store(in: &self.cancellables)
}
}

func registerRequest(
email: String,
password: String
) -> AnyPublisher<(data: Data, response: URLResponse), URLError> {
var components = URLComponents(string: "https://www.pointfree.co/register")!
components.queryItems = [
URLQueryItem(name: "email", value: email),
URLQueryItem(name: "password", value: password)
]

return URLSession.shared
.dataTaskPublisher(for: components.url!)
.eraseToAnyPublisher()
}


//class PasswordViewModel: ObservableObject {
// @Published var password: String = ""
//
// //let passwordValidationMessage: AnyPublisher<String, Never>
// @Published var passwordValidationMessage = ""
//
// func validatePassword() {
// validationMessage(forPassword: Just(self.password))
// .sink { self.passwordValidationMessage = $0 }
// }
//}

struct ContentView: View {
// @State var password: String = ""
@ObservedObject var viewModel = RegisterViewModel(
login: { _, _ in Just(true).eraseToAnyPublisher() }
)
@ObservedObject var viewModel: RegisterViewModel

var body: some View {
Form {
if self.viewModel.isLoginSuccessful {
Text("Logged in! Welcome!")
NavigationView {
if self.viewModel.isRegistered {
Text("Welcome!")
} else {
TextField("Email", text: self.$viewModel.email)
TextField("Password", text: self.$viewModel.password)

Text(self.viewModel.passwordValidationMessage)

Button("Login") { self.viewModel.loginButtonTapped() }
Form {
Section(header: Text("Email")) {
TextField(
"[email protected]",
text: self.$viewModel.email
)
}

Section(header: Text("Password")) {
TextField(
"Password",
text: self.$viewModel.password
)
if !self.viewModel.passwordValidationMessage.isEmpty {
Text(self.viewModel.passwordValidationMessage)
}
}

if self.viewModel.isRegisterRequestInFlight {
Text("Registering...")
} else {
Button("Register") { self.viewModel.registerButtonTapped() }
}
}
.navigationBarTitle("Register")
.alert(item: self.$viewModel.errorAlert) { errorAlert in
Alert(title: Text(errorAlert.title))
}
}
}
}
}

func mockValidate(password: String) -> AnyPublisher<(data: Data, response: URLResponse), URLError> {
let message = password.count < 5 ? "Password is too short 👎"
: password.count > 20 ? "Password is too long 👎"
: "Password is good 👍"
return Just((Data(message.utf8), URLResponse()))
.setFailureType(to: URLError.self)
.eraseToAnyPublisher()
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
ContentView(
viewModel: RegisterViewModel(
register: { _, _ in
Just((Data("false".utf8), URLResponse()))
.setFailureType(to: URLError.self)
.delay(for: 1, scheduler: DispatchQueue.main)
.eraseToAnyPublisher()
},
validatePassword: {
mockValidate(password: $0)
.delay(for: 0.5, scheduler: DispatchQueue.main)
.eraseToAnyPublisher()
})
)
}
}
Original file line number Diff line number Diff line change
@@ -1,64 +1,24 @@
//
// SceneDelegate.swift
// CombineSchedulers
//
// Created by Point-Free on 5/29/20.
// Copyright © 2020 Point-Free. All rights reserved.
//

import UIKit
import SwiftUI
import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
let contentView = ContentView(
viewModel: RegisterViewModel(
register: registerRequest(email:password:),
validatePassword: mockValidate(password:)
)
)

// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}

func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
}

func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}

func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}

func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}

func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}


}

Loading

0 comments on commit 5124f0e

Please sign in to comment.