From 637353467c9b8576749bc556f90252aa9ed712e8 Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Thu, 27 Aug 2020 14:06:10 +0200 Subject: [PATCH] Move data hosting from the view to the AppDelegate --- swift/CallTutorial/CallTutorial/AppDelegate.swift | 3 +++ swift/CallTutorial/CallTutorial/ContentView.swift | 4 ++-- swift/CallTutorial/CallTutorial/SceneDelegate.swift | 4 +++- swift/LoginTutorial/LoginTutorial/AppDelegate.swift | 5 +++-- swift/LoginTutorial/LoginTutorial/ContentView.swift | 4 ++-- swift/LoginTutorial/LoginTutorial/SceneDelegate.swift | 9 +++++---- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/swift/CallTutorial/CallTutorial/AppDelegate.swift b/swift/CallTutorial/CallTutorial/AppDelegate.swift index 8d7f368..c63133f 100644 --- a/swift/CallTutorial/CallTutorial/AppDelegate.swift +++ b/swift/CallTutorial/CallTutorial/AppDelegate.swift @@ -7,9 +7,12 @@ // import UIKit +import SwiftUI @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { + + @ObservedObject var tutorialContext = CallExampleContext() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. diff --git a/swift/CallTutorial/CallTutorial/ContentView.swift b/swift/CallTutorial/CallTutorial/ContentView.swift index fb302c8..a584361 100644 --- a/swift/CallTutorial/CallTutorial/ContentView.swift +++ b/swift/CallTutorial/CallTutorial/ContentView.swift @@ -10,7 +10,7 @@ import SwiftUI struct ContentView: View { - @ObservedObject var tutorialContext = CallExampleContext() + @ObservedObject var tutorialContext : CallExampleContext func getCallButtonText() -> String { if (tutorialContext.callRunning) { @@ -144,6 +144,6 @@ struct ContentView: View { struct ContentView_Previews: PreviewProvider { static var previews: some View { - ContentView() + ContentView(tutorialContext: CallExampleContext()) } } diff --git a/swift/CallTutorial/CallTutorial/SceneDelegate.swift b/swift/CallTutorial/CallTutorial/SceneDelegate.swift index 7db0041..2902b39 100644 --- a/swift/CallTutorial/CallTutorial/SceneDelegate.swift +++ b/swift/CallTutorial/CallTutorial/SceneDelegate.swift @@ -18,8 +18,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { // 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). + let delegate = UIApplication.shared.delegate as! AppDelegate + // Create the SwiftUI view that provides the window contents. - let contentView = ContentView() + let contentView = ContentView(tutorialContext: delegate.tutorialContext) // Use a UIHostingController as window root view controller. if let windowScene = scene as? UIWindowScene { diff --git a/swift/LoginTutorial/LoginTutorial/AppDelegate.swift b/swift/LoginTutorial/LoginTutorial/AppDelegate.swift index 8d50e6a..920836e 100644 --- a/swift/LoginTutorial/LoginTutorial/AppDelegate.swift +++ b/swift/LoginTutorial/LoginTutorial/AppDelegate.swift @@ -7,11 +7,12 @@ // import UIKit +import SwiftUI @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - - + + @ObservedObject var tutorialContext = LoginTutorialContext() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. diff --git a/swift/LoginTutorial/LoginTutorial/ContentView.swift b/swift/LoginTutorial/LoginTutorial/ContentView.swift index 846b9a1..9f525c0 100644 --- a/swift/LoginTutorial/LoginTutorial/ContentView.swift +++ b/swift/LoginTutorial/LoginTutorial/ContentView.swift @@ -10,7 +10,7 @@ import SwiftUI struct ContentView: View { - @ObservedObject var tutorialContext = LoginTutorialContext() + @ObservedObject var tutorialContext : LoginTutorialContext var body: some View { @@ -71,6 +71,6 @@ struct ContentView: View { struct ContentView_Previews: PreviewProvider { static var previews: some View { - ContentView() + ContentView(tutorialContext: LoginTutorialContext()) } } diff --git a/swift/LoginTutorial/LoginTutorial/SceneDelegate.swift b/swift/LoginTutorial/LoginTutorial/SceneDelegate.swift index 1211e9c..e90febe 100644 --- a/swift/LoginTutorial/LoginTutorial/SceneDelegate.swift +++ b/swift/LoginTutorial/LoginTutorial/SceneDelegate.swift @@ -13,14 +13,15 @@ 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 delegate = UIApplication.shared.delegate as! AppDelegate + + // Create the SwiftUI view that provides the window contents. + let contentView = ContentView(tutorialContext: delegate.tutorialContext) // Use a UIHostingController as window root view controller. if let windowScene = scene as? UIWindowScene {