Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Commit

Permalink
Support for multiple *different* windows. Mostly
Browse files Browse the repository at this point in the history
So, in practice, what we need to do here, is to create a NSUserActivity
and, in there, store the payload (i.e. as userInfo) that the new window
should receive in order to set up. However, currently, due to bugs in
Catalina, these payloads seem to not always arrive. So in order to
still support something, this feature currently loads a random entry
if the payload is not there.
  • Loading branch information
terhechte committed Jul 1, 2019
1 parent fcddeb8 commit 3985ea5
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Nerau.xcodeproj/project.pbxproj
Expand Up @@ -15,6 +15,7 @@
3B10AC5E22B8F20200D835A4 /* TrainController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B10AC5D22B8F20200D835A4 /* TrainController.swift */; };
3B10AC6022B8F3E600D835A4 /* TrainViewNormal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B10AC5F22B8F3E600D835A4 /* TrainViewNormal.swift */; };
4D0E3C1C22C6081E00603C58 /* SettingsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D0E3C1B22C6081E00603C58 /* SettingsController.swift */; };
4D25947322CAA1D6001A29BF /* ResultSceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D25947222CAA1D6001A29BF /* ResultSceneDelegate.swift */; };
4D3DEB5622BCF3E400C30B91 /* ListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D3DEB5522BCF3E400C30B91 /* ListController.swift */; };
4D3DEB5C22BCF85600C30B91 /* SidebarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D3DEB5B22BCF85600C30B91 /* SidebarController.swift */; };
4D3DEB5E22BD096200C30B91 /* DetailListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D3DEB5D22BD096200C30B91 /* DetailListController.swift */; };
Expand Down Expand Up @@ -94,6 +95,7 @@
3B10AC5F22B8F3E600D835A4 /* TrainViewNormal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrainViewNormal.swift; sourceTree = "<group>"; };
4D0E3BD522C3539600603C58 /* Nerau-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Nerau-Bridging-Header.h"; sourceTree = "<group>"; };
4D0E3C1B22C6081E00603C58 /* SettingsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsController.swift; sourceTree = "<group>"; };
4D25947222CAA1D6001A29BF /* ResultSceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResultSceneDelegate.swift; sourceTree = "<group>"; };
4D3DEB5522BCF3E400C30B91 /* ListController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListController.swift; sourceTree = "<group>"; };
4D3DEB5B22BCF85600C30B91 /* SidebarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarController.swift; sourceTree = "<group>"; };
4D3DEB5D22BD096200C30B91 /* DetailListController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailListController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -189,6 +191,7 @@
3B10AC5722B8EEDB00D835A4 /* Info.plist */,
4DC772E022C34BBF00EDB6B2 /* SceneDelegate.swift */,
4D0E3BD522C3539600603C58 /* Nerau-Bridging-Header.h */,
4D25947222CAA1D6001A29BF /* ResultSceneDelegate.swift */,
);
path = Nerau;
sourceTree = "<group>";
Expand Down Expand Up @@ -428,6 +431,7 @@
files = (
4D0E3C1C22C6081E00603C58 /* SettingsController.swift in Sources */,
4D3DEB6022BD0F6900C30B91 /* Extensions.swift in Sources */,
4D25947322CAA1D6001A29BF /* ResultSceneDelegate.swift in Sources */,
3B10AC4E22B8EED800D835A4 /* TrainingViewController.swift in Sources */,
3B10AC6022B8F3E600D835A4 /* TrainViewNormal.swift in Sources */,
4D4F64AF22BFF4A700A81C7A /* MacSplitViewController.swift in Sources */,
Expand Down
43 changes: 38 additions & 5 deletions Nerau/AppDelegate.swift
Expand Up @@ -9,8 +9,18 @@
import UIKit
import NerauModel

let ResultOpenDetailActivityType = "com.stylemac.nerau.openResult"
let ResultOpenTrainResultKey = "result"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

private var nextSceneName: String? = nil
private var nextSceneResult: [String: TrainResult] = [:]

func sceneResult(for key: String) -> TrainResult? {
return nextSceneResult.removeValue(forKey: key)
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if Database.shared.numberOfTrainResults() == 0 {
Expand Down Expand Up @@ -42,6 +52,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

func application(_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions) -> UISceneConfiguration {
defer {
nextSceneName = nil
}
return UISceneConfiguration(name: nextSceneName ?? "Default Configuration", sessionRole: connectingSceneSession.role)
}

// MARK: Menu Actions

Expand All @@ -58,11 +77,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func openResult(result: TrainResult) {
let window = UIApplication.shared.keyWindow
guard let wrappingController = window?.rootViewController?.storyboard?.instantiateViewController(identifier: "ResultWrapper") as? ResultWrappingController
else { return }
wrappingController.result = result
window?.rootViewController?.present(wrappingController, animated: true, completion: nil)

/// Open a new window with
let newWindowExample = true
if newWindowExample {
let uuid = UUID().uuidString
nextSceneResult[uuid] = result
let userActivity = NSUserActivity(activityType: ResultOpenDetailActivityType)
userActivity.userInfo = [ResultOpenTrainResultKey: uuid]
nextSceneName = "Result Configuration"
UIApplication.shared.requestSceneSessionActivation(nil, userActivity: userActivity, options: nil) { (e) in
print("error", e)
}
} else {
let window = UIApplication.shared.keyWindow
guard let wrappingController = window?.rootViewController?.storyboard?.instantiateViewController(identifier: "ResultWrapper") as? ResultWrappingController
else { return }
wrappingController.result = result
window?.rootViewController?.present(wrappingController, animated: true, completion: nil)
}
}

func startTraining(configuration: TrainConfiguration?) {
Expand Down
14 changes: 14 additions & 0 deletions Nerau/Info.plist
Expand Up @@ -22,6 +22,10 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSUserActivityTypes</key>
<array>
<string>com.stylemac.nerau.openResult</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
Expand Down Expand Up @@ -53,6 +57,16 @@
<key>UISceneStoryboardFile</key>
<string>Main</string>
</dict>
<dict>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UISceneConfigurationName</key>
<string>Result Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).ResultSceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Result</string>
</dict>
</array>
</dict>
</dict>
Expand Down
13 changes: 11 additions & 2 deletions Nerau/Result/ResultController.swift
Expand Up @@ -66,7 +66,12 @@ final class ResultController: UIViewController {
static let SomethingTouchBarIdentifier = NSTouchBarItem.Identifier(rawValue: "ToggleStar")
#endif

public var result: TrainResult?
public var result: TrainResult? {
didSet {
listContainer?.dataPoints = (result?.durationPoints.map { $0.1 }) ?? []
hoverView?.trainResult = result
}
}

@IBOutlet weak var containerViewBottom: NSLayoutConstraint!
@IBOutlet weak var bottomToolbar: UIToolbar!
Expand All @@ -77,6 +82,8 @@ final class ResultController: UIViewController {
@IBOutlet weak var hoverView: ResultHoverView!
@IBOutlet weak var valueLabel: UILabel!

private weak var listContainer: ResultTimeIntervalListController?

override func viewDidLoad() {
super.viewDidLoad()
#if targetEnvironment(UIKitForMac)
Expand All @@ -99,8 +106,10 @@ final class ResultController: UIViewController {
#endif

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let points = result?.durationPoints else { return }
if segue.identifier == "resultList" {
(segue.destination as? ResultTimeIntervalListController)?.dataPoints = result!.durationPoints.map { $0.1 }
listContainer = segue.destination as? ResultTimeIntervalListController
(segue.destination as? ResultTimeIntervalListController)?.dataPoints = points.map { $0.1 }
}
}

Expand Down
72 changes: 72 additions & 0 deletions Nerau/ResultSceneDelegate.swift
@@ -0,0 +1,72 @@
import UIKit
import NerauModel

class ResultSceneDelegate: 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).
guard let _ = (scene as? UIWindowScene) else { return }

// just for show, load a default result
let defaultResult = Database.shared.trainResults(selection: Database.Selection(false)).first!
(window?.rootViewController as? ResultController)?.result = decodeUserActivity(from: connectionOptions, session: session) ?? defaultResult
}

private func decodeUserActivity(from options: UIScene.ConnectionOptions, session: UISceneSession) -> TrainResult? {
// Pull together the information for setting up the new window
guard let userActivity = options.userActivities.first ?? session.stateRestorationActivity else {
print("Could not setup scene, missing result")
return nil
}

guard userActivity.activityType == ResultOpenDetailActivityType else {
print("Invalid activity type")
return nil
}

guard let resultKey = userActivity.userInfo?[ResultOpenTrainResultKey] as? String else {
print("Missing train result")
return nil
}

guard let result = (UIApplication.shared.delegate as? AppDelegate)?.sceneResult(for: resultKey) else {
print("Invalid Key")
return nil
}

return result
}

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.
}
}

0 comments on commit 3985ea5

Please sign in to comment.