From 263a245a69d882879773c96c232839792d9710ff Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Thu, 27 Aug 2020 17:29:10 +0200 Subject: [PATCH] Use TutorialCommons, and host data in the AppDelegate --- .../project.pbxproj | 13 ++++++ .../ChatRoomTutorial/AppDelegate.swift | 3 +- .../ChatRoomTutorial/ChatRoomExample.swift | 41 ++++--------------- .../ChatRoomTutorial/ContentView.swift | 6 +-- .../ChatRoomTutorial/SceneDelegate.swift | 9 ++-- 5 files changed, 30 insertions(+), 42 deletions(-) diff --git a/swift/ChatRoomTutorial/ChatRoomTutorial.xcodeproj/project.pbxproj b/swift/ChatRoomTutorial/ChatRoomTutorial.xcodeproj/project.pbxproj index 09c6b2f..0ffd783 100644 --- a/swift/ChatRoomTutorial/ChatRoomTutorial.xcodeproj/project.pbxproj +++ b/swift/ChatRoomTutorial/ChatRoomTutorial.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 66B14F6A24D945B30041952F /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 66B14F6924D945B30041952F /* Preview Assets.xcassets */; }; 66B14F6D24D945B30041952F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 66B14F6B24D945B30041952F /* LaunchScreen.storyboard */; }; 66B14F7524D94A8B0041952F /* ChatRoomExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66B14F7424D94A8B0041952F /* ChatRoomExample.swift */; }; + 66E0886524F7D9FD00399387 /* commons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66E0886424F7D9FD00399387 /* commons.swift */; }; F6D7663413A00C7D71C9256A /* Pods_ChatRoomTutorial.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA7659F911FB1269A70609BD /* Pods_ChatRoomTutorial.framework */; }; /* End PBXBuildFile section */ @@ -29,6 +30,7 @@ 66B14F6C24D945B30041952F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 66B14F6E24D945B30041952F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 66B14F7424D94A8B0041952F /* ChatRoomExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatRoomExample.swift; sourceTree = ""; }; + 66E0886424F7D9FD00399387 /* commons.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = commons.swift; sourceTree = ""; }; DA7659F911FB1269A70609BD /* Pods_ChatRoomTutorial.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ChatRoomTutorial.framework; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -47,6 +49,7 @@ 66B14F5424D945B10041952F = { isa = PBXGroup; children = ( + 66E0886324F7D9E200399387 /* TutorialCommons */, 66B14F5F24D945B10041952F /* ChatRoomTutorial */, 66B14F5E24D945B10041952F /* Products */, 7803BAE55D29633EC66FF288 /* Pods */, @@ -85,6 +88,15 @@ path = "Preview Content"; sourceTree = ""; }; + 66E0886324F7D9E200399387 /* TutorialCommons */ = { + isa = PBXGroup; + children = ( + 66E0886424F7D9FD00399387 /* commons.swift */, + ); + name = TutorialCommons; + path = ../TutorialCommons; + sourceTree = ""; + }; 7803BAE55D29633EC66FF288 /* Pods */ = { isa = PBXGroup; children = ( @@ -220,6 +232,7 @@ 66B14F6124D945B10041952F /* AppDelegate.swift in Sources */, 66B14F7524D94A8B0041952F /* ChatRoomExample.swift in Sources */, 66B14F6324D945B10041952F /* SceneDelegate.swift in Sources */, + 66E0886524F7D9FD00399387 /* commons.swift in Sources */, 66B14F6524D945B10041952F /* ContentView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/swift/ChatRoomTutorial/ChatRoomTutorial/AppDelegate.swift b/swift/ChatRoomTutorial/ChatRoomTutorial/AppDelegate.swift index 4339eb4..8a402a5 100644 --- a/swift/ChatRoomTutorial/ChatRoomTutorial/AppDelegate.swift +++ b/swift/ChatRoomTutorial/ChatRoomTutorial/AppDelegate.swift @@ -7,11 +7,12 @@ // import UIKit +import SwiftUI @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - + @ObservedObject var tutorialContext = ChatRoomExampleContext() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. diff --git a/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift b/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift index 8acb2ef..3212ac7 100644 --- a/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift +++ b/swift/ChatRoomTutorial/ChatRoomTutorial/ChatRoomExample.swift @@ -6,8 +6,6 @@ // Copyright © 2020 BelledonneCommunications. All rights reserved. import linphonesw -import SwiftUI - enum ChatroomExampleState { @@ -33,9 +31,7 @@ class ChatRoomExampleContext : ObservableObject @Published var coreVersion: String = Core.getVersion /*------------ Logs related variables ------------------------*/ - var log : LoggingService? - var logManager : LinphoneLoggingServiceManager? - @Published var logsEnabled : Bool = true + var loggingUnit = LoggingUnit() /*--- Variable shared between Basic and FlexiSip chatrooms ---- -------- "A" always initiates the chat, "B" answers --------*/ @@ -72,17 +68,8 @@ class ChatRoomExampleContext : ObservableObject mCoreChatDelegate.tutorialContext = self mRegistrationConfirmDelegate.tutorialContext = self - let factory = Factory.Instance // Instanciate - - logManager = LinphoneLoggingServiceManager() - logManager!.tutorialContext = self; - log = LoggingService.Instance - log!.addDelegate(delegate: logManager!) - log!.logLevel = LogLevel.Debug - factory.enableLogCollection(state: LogCollectionState.Enabled) - // Initialize Linphone Core - try? mCore = factory.createCore(configPath: "", factoryConfigPath: "", systemContext: nil) + try? mCore = Factory.Instance.createCore(configPath: "", factoryConfigPath: "", systemContext: nil) // main loop for receiving notifications and doing background linphonecore work: mCore.autoIterateEnabled = true @@ -141,9 +128,9 @@ class ChatRoomExampleContext : ObservableObject if (isBasicChatroom && mBasicChatRoom == nil) { chatParams.backend = ChatRoomBackend.Basic + mBasicChatRoom = try mCore.createChatRoom(params: chatParams , localAddr: mProxyConfigA.contact! - , subject: "Basic ChatRoom" , participants: chatDest) // Basic chatroom do not require setup time basicChatRoomState = ChatroomExampleState.Started @@ -156,7 +143,6 @@ class ChatRoomExampleContext : ObservableObject chatParams.groupEnabled = false mChatRoomA = try mCore.createChatRoom(params: chatParams , localAddr: mProxyConfigA.contact! - , subject: "Flexisip ChatRoom" , participants: chatDest) mChatRoomA!.addDelegate(delegate: mChatRoomDelegate) // Flexisip chatroom requires a setup time. The delegate will set the state to started when it is ready. @@ -206,24 +192,11 @@ class ChatRoomExampleContext : ObservableObject } - -class LinphoneLoggingServiceManager: LoggingServiceDelegate { - - var tutorialContext : ChatRoomExampleContext! - - override func onLogMessageWritten(logService: LoggingService, domain: String, lev: LogLevel, message: String) { - if (tutorialContext.logsEnabled) - { - print("Logging service log: \(message)s\n") - } - } -} - class LinphoneRegistrationConfirmDelegate: CoreDelegate { var tutorialContext : ChatRoomExampleContext! - override func onRegistrationStateChanged(lc: Core, cfg: ProxyConfig, cstate: RegistrationState, message: String?) { + override func onRegistrationStateChanged(core lc: Core, proxyConfig cfg: ProxyConfig, state cstate: RegistrationState, message: String?) { print("New registration state \(cstate) for user id \( String(describing: cfg.identityAddress?.asString()))\n") if (cstate == RegistrationState.Ok) { @@ -245,7 +218,7 @@ class LinphoneRegistrationConfirmDelegate: CoreDelegate { class LinphoneCoreChatDelegate: CoreDelegate { var tutorialContext : ChatRoomExampleContext! - override func onMessageReceived(lc: Core, room: ChatRoom, message: ChatMessage) { + override func onMessageReceived(core lc: Core, chatRoom room: ChatRoom, message: ChatMessage) { if (tutorialContext.mChatRoomB == nil) { @@ -262,7 +235,7 @@ class LinphoneChatRoomStateTracker: ChatRoomDelegate { var tutorialContext : ChatRoomExampleContext! - override func onStateChanged(cr: ChatRoom, newState: ChatRoom.State) { + override func onStateChanged(chatRoom cr: ChatRoom, newState: ChatRoom.State) { if (newState == ChatRoom.State.Created) { print("ChatRoomTrace - Chatroom ready to start") @@ -279,7 +252,7 @@ class LinphoneChatRoomStateTracker: ChatRoomDelegate { } class LinphoneChatMessageTracker: ChatMessageDelegate { - override func onMsgStateChanged(msg: ChatMessage, state: ChatMessage.State) { + override func onMsgStateChanged(message msg: ChatMessage, state: ChatMessage.State) { print("MessageTrace - msg state changed: \(state)\n") } } diff --git a/swift/ChatRoomTutorial/ChatRoomTutorial/ContentView.swift b/swift/ChatRoomTutorial/ChatRoomTutorial/ContentView.swift index 9835457..24c75f0 100644 --- a/swift/ChatRoomTutorial/ChatRoomTutorial/ContentView.swift +++ b/swift/ChatRoomTutorial/ChatRoomTutorial/ContentView.swift @@ -10,7 +10,7 @@ import SwiftUI struct ContentView: View { - @ObservedObject var tutorialContext = ChatRoomExampleContext() + @ObservedObject var tutorialContext : ChatRoomExampleContext var body: some View { @@ -87,7 +87,7 @@ struct ContentView: View { } Group { Spacer() - Toggle(isOn: $tutorialContext.logsEnabled) { + Toggle(isOn: $tutorialContext.loggingUnit.logsEnabled.value) { Text("Logs collection") .multilineTextAlignment(.trailing) } @@ -99,6 +99,6 @@ struct ContentView: View { struct ContentView_Previews: PreviewProvider { static var previews: some View { - ContentView() + ContentView(tutorialContext: ChatRoomExampleContext()) } } diff --git a/swift/ChatRoomTutorial/ChatRoomTutorial/SceneDelegate.swift b/swift/ChatRoomTutorial/ChatRoomTutorial/SceneDelegate.swift index 2d10a67..6a43393 100644 --- a/swift/ChatRoomTutorial/ChatRoomTutorial/SceneDelegate.swift +++ b/swift/ChatRoomTutorial/ChatRoomTutorial/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 {