Skip to content

Commit

Permalink
AuthWindowController: Wiring MagicLink Notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
jleandroperez committed Jun 19, 2024
1 parent bf1d840 commit 60c003a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
57 changes: 57 additions & 0 deletions Simplenote/AuthWindowController.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation
import AppKit
import SwiftUI


// MARK: - AuthWindowController
//
Expand All @@ -18,6 +20,10 @@ class AuthWindowController: NSWindowController, SPAuthenticationInterface {
}

// MARK: - Initializer

deinit {
stopListeningToNotifications()
}

init() {
let window = NSWindow(contentViewController: authViewController)
Expand All @@ -29,9 +35,60 @@ class AuthWindowController: NSWindowController, SPAuthenticationInterface {
window.backgroundColor = .white

super.init(window: window)
startListeningToNotifications()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}


// MARK: - Notifications
//
extension AuthWindowController {

func startListeningToNotifications() {
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(displayAuthenticationInProgress), name: .magicLinkAuthWillStart, object: nil)
}

func stopListeningToNotifications() {
NotificationCenter.default.removeObserver(self)
}

@objc
func displayAuthenticationInProgress(_ sender: Notification) {
switchToMagicLinkConfirmationUI()
}
}


// MARK: - User Interface
//
extension AuthWindowController {

func switchToAuthenticationUI() {
guard let window else {
return
}

let authViewController = AuthViewController()
authViewController.authenticator = authenticator
window.transition(to: authViewController)
}

func switchToMagicLinkConfirmationUI() {
guard let window else {
return
}

var rootView = MagicLinkConfirmationView()
rootView.onDismissRequest = { [weak self] in
self?.switchToAuthenticationUI()
}

let hostingController = NSHostingController(rootView: rootView)
window.switchContentViewController(to: hostingController)
}
}
10 changes: 9 additions & 1 deletion Simplenote/MagicLinkConfirmationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ struct MagicLinkConfirmationView: View {
SwiftUI.Button(action: switchToAuthenticationUI) {
Text("Accept")
.fontWeight(.bold)
.cornerRadius(5)
.foregroundStyle(Color(nsColor: .simplenoteBrandColor))
.onHover { inside in
if inside {
NSCursor.pointingHand.set()
} else {
NSCursor.arrow.set()
}
}
}
.buttonStyle(PlainButtonStyle())
}

func switchToAuthenticationUI() {
Expand Down

0 comments on commit 60c003a

Please sign in to comment.