Renamed LinphoneCoreHolder to LinphoneTutorialContext. Instead of using a singleton, pass the context to delegate when required.

This commit is contained in:
QuentinArguillere
2020-07-29 16:17:18 +02:00
parent d1a7ccc4a5
commit 00dcfd490f

View File

@@ -28,18 +28,12 @@ func toString(tutorialState state : ChatroomTutorialState) -> String
} }
class LinphoneCoreHolder : ObservableObject class LinphoneTutorialContext : ObservableObject
{ {
public static let instance = LinphoneCoreHolder()
var mCore: Core! var mCore: Core!
var proxy_cfg: ProxyConfig! var proxy_cfg: ProxyConfig!
var call: Call! var call: Call!
let mRegistrationTracer = LinphoneRegistrationTracker()
let mPhoneStateTracer = LinconePhoneStateTracker()
let mChatRoomDelegate = LinphoneChatRoomStateTracker()
let mChatMessageDelegate = LinphoneChatMessageTracker()
var mChatRoom : ChatRoom? var mChatRoom : ChatRoom?
var mChatMessage : ChatMessage? var mChatMessage : ChatMessage?
@@ -49,6 +43,10 @@ class LinphoneCoreHolder : ObservableObject
var proxy_cfg_A : ProxyConfig! var proxy_cfg_A : ProxyConfig!
var proxy_cfg_B : ProxyConfig! var proxy_cfg_B : ProxyConfig!
let mRegistrationTracer = LinphoneRegistrationTracker()
let mPhoneStateTracer = LinconePhoneStateTracker()
let mChatRoomDelegate = LinphoneChatRoomStateTracker()
let mChatMessageDelegate = LinphoneChatMessageTracker()
@Published var coreVersion: String = Core.getVersion @Published var coreVersion: String = Core.getVersion
@Published var callRunning : Bool = false @Published var callRunning : Bool = false
@@ -64,8 +62,11 @@ class LinphoneCoreHolder : ObservableObject
@Published var proxyConfigBRegistered : Bool = false @Published var proxyConfigBRegistered : Bool = false
private init() init()
{ {
mRegistrationTracer.tutorialContext = self
mChatRoomDelegate.tutorialContext = self
let factory = Factory.Instance // Instanciate let factory = Factory.Instance // Instanciate
// set logsEnabled to false to disable logs collection // set logsEnabled to false to disable logs collection
@@ -102,12 +103,12 @@ class LinphoneCoreHolder : ObservableObject
try proxy_cfg.setServeraddr(newValue: server_addr) try proxy_cfg.setServeraddr(newValue: server_addr)
proxy_cfg.registerEnabled = true proxy_cfg.registerEnabled = true
proxy_cfg.conferenceFactoryUri = fUri proxy_cfg.conferenceFactoryUri = fUri
try mCore.addProxyConfig(config: proxy_cfg)
if ( mCore.defaultProxyConfig == nil) if ( mCore.defaultProxyConfig == nil)
{ {
mCore.defaultProxyConfig = proxy_cfg // set to default proxy // IMPORTANT : default proxy config setting MUST be done AFTER adding the config to the core !
mCore.defaultProxyConfig = proxy_cfg
} }
try mCore.addProxyConfig(config: proxy_cfg)
return proxy_cfg return proxy_cfg
} catch { } catch {
@@ -165,10 +166,10 @@ class LinphoneCoreHolder : ObservableObject
func virtualChatRoom() func virtualChatRoom()
{ {
proxy_cfg_A = createProxyConfigAndRegister(identity : "sip:peche5@sip.linphone.org", password : "peche5", factoryUri: "sip:conference-factory@sip.linphone.org")! proxy_cfg_A = createProxyConfigAndRegister(identity : "sip:peche5@sip.linphone.org", password : "peche5", factoryUri: "sip:conference-factory@sip.linphone.org")!
mCore.defaultProxyConfig = proxy_cfg_A // set to default proxy
proxy_cfg_B = createProxyConfigAndRegister(identity : proxy_cfg_B = createProxyConfigAndRegister(identity :
"sip:arguillq@sip.linphone.org", password : "078zUVlK", factoryUri: "sip:conference-factory@sip.linphone.org")! "sip:arguillq@sip.linphone.org", password : "078zUVlK", factoryUri: "sip:conference-factory@sip.linphone.org")!
DispatchQueue.global(qos: .userInitiated).async { DispatchQueue.global(qos: .userInitiated).async {
while(!self.proxyConfigARegistered || !self.proxyConfigBRegistered){ while(!self.proxyConfigARegistered || !self.proxyConfigBRegistered){
usleep(1000000) usleep(1000000)
@@ -198,7 +199,7 @@ class LinphoneCoreHolder : ObservableObject
struct ContentView: View { struct ContentView: View {
@ObservedObject var coreHolder = LinphoneCoreHolder.instance @ObservedObject var tutorialContext = LinphoneTutorialContext()
var body: some View { var body: some View {
@@ -207,17 +208,17 @@ struct ContentView: View {
HStack { HStack {
Text("Identity :") Text("Identity :")
.font(.title) .font(.title)
TextField("", text : $coreHolder.id) TextField("", text : $tutorialContext.id)
.textFieldStyle(RoundedBorderTextFieldStyle()) .textFieldStyle(RoundedBorderTextFieldStyle())
} }
HStack { HStack {
Text("Password :") Text("Password :")
.font(.title) .font(.title)
TextField("", text : $coreHolder.passwd) TextField("", text : $tutorialContext.passwd)
.textFieldStyle(RoundedBorderTextFieldStyle()) .textFieldStyle(RoundedBorderTextFieldStyle())
} }
HStack { HStack {
Button(action: self.coreHolder.registrationExample) Button(action: tutorialContext.registrationExample)
{ {
Text("Login") Text("Login")
.font(.largeTitle) .font(.largeTitle)
@@ -225,17 +226,17 @@ struct ContentView: View {
.frame(width: 100.0, height: 50.0) .frame(width: 100.0, height: 50.0)
.background(Color.gray) .background(Color.gray)
} }
Text(coreHolder.loggedIn ? "Registered" : "") Text(tutorialContext.loggedIn ? "Registered" : "")
} }
} }
Spacer() Spacer()
VStack(spacing: 0.0) { VStack(spacing: 0.0) {
Text("Call destination :") Text("Call destination :")
.font(.largeTitle) .font(.largeTitle)
TextField("", text : $coreHolder.dest) TextField("", text : $tutorialContext.dest)
.textFieldStyle(RoundedBorderTextFieldStyle()) .textFieldStyle(RoundedBorderTextFieldStyle())
HStack { HStack {
Button(action: self.coreHolder.startOutgoingCallExample) Button(action: tutorialContext.startOutgoingCallExample)
{ {
Text("Call") Text("Call")
.font(.largeTitle) .font(.largeTitle)
@@ -244,7 +245,7 @@ struct ContentView: View {
.background(Color.green) .background(Color.green)
} }
.padding(.trailing, 30.0) .padding(.trailing, 30.0)
Button(action: coreHolder.stopOutgoingCallExample) { Button(action: tutorialContext.stopOutgoingCallExample) {
Text("Stop Call") Text("Stop Call")
.font(.largeTitle) .font(.largeTitle)
.foregroundColor(Color.white) .foregroundColor(Color.white)
@@ -255,14 +256,14 @@ struct ContentView: View {
.padding(.top, 15.0) .padding(.top, 15.0)
HStack { HStack {
Text("Call State : ") Text("Call State : ")
Text(coreHolder.callRunning ? "Ongoing" : "Stopped") Text(tutorialContext.callRunning ? "Ongoing" : "Stopped")
.foregroundColor(coreHolder.callRunning ? Color.green : Color.black) .foregroundColor(tutorialContext.callRunning ? Color.green : Color.black)
} }
.padding(.top, 5.0) .padding(.top, 5.0)
} }
Spacer() Spacer()
Group { Group {
Button(action: self.coreHolder.virtualChatRoom) Button(action: tutorialContext.virtualChatRoom)
{ {
Text("Simulate Chat") Text("Simulate Chat")
.font(.largeTitle) .font(.largeTitle)
@@ -272,12 +273,12 @@ struct ContentView: View {
} }
HStack { HStack {
Text("Chatroom state : ") Text("Chatroom state : ")
Text(toString(tutorialState: coreHolder.chatroomTutorialState)) Text(toString(tutorialState: tutorialContext.chatroomTutorialState))
.foregroundColor((coreHolder.chatroomTutorialState == ChatroomTutorialState.Started) ? Color.green : Color.black) .foregroundColor((tutorialContext.chatroomTutorialState == ChatroomTutorialState.Started) ? Color.green : Color.black)
}.padding(.top, 2.0) }.padding(.top, 2.0)
} }
Spacer() Spacer()
Text("Hello, Linphone, Core Version is \n \(coreHolder.coreVersion)") Text("Hello, Linphone, Core Version is \n \(tutorialContext.coreVersion)")
} }
.padding() .padding()
} }
@@ -290,6 +291,9 @@ class LinphoneLoggingServiceManager: LoggingServiceDelegate {
} }
class LinphoneRegistrationTracker: CoreDelegate { class LinphoneRegistrationTracker: CoreDelegate {
var tutorialContext : LinphoneTutorialContext?
override func onRegistrationStateChanged(lc: Core, cfg: ProxyConfig, cstate: RegistrationState, message: String?) { override func onRegistrationStateChanged(lc: Core, cfg: ProxyConfig, cstate: RegistrationState, message: String?) {
print("New registration state \(cstate) for user id \( String(describing: cfg.identityAddress?.asString()))\n") print("New registration state \(cstate) for user id \( String(describing: cfg.identityAddress?.asString()))\n")
if (cstate == RegistrationState.Ok) if (cstate == RegistrationState.Ok)
@@ -298,11 +302,11 @@ class LinphoneRegistrationTracker: CoreDelegate {
{ {
if (cfgIdentity.asString() == "sip:peche5@sip.linphone.org") if (cfgIdentity.asString() == "sip:peche5@sip.linphone.org")
{ {
LinphoneCoreHolder.instance.proxyConfigARegistered = true tutorialContext!.proxyConfigARegistered = true
} }
else if (cfgIdentity.asString() == "sip:arguillq@sip.linphone.org") else if (cfgIdentity.asString() == "sip:arguillq@sip.linphone.org")
{ {
LinphoneCoreHolder.instance.proxyConfigBRegistered = true tutorialContext!.proxyConfigBRegistered = true
} }
} }
} }
@@ -331,11 +335,14 @@ class LinconePhoneStateTracker: CoreDelegate {
} }
class LinphoneChatRoomStateTracker: ChatRoomDelegate { class LinphoneChatRoomStateTracker: ChatRoomDelegate {
var tutorialContext : LinphoneTutorialContext?
override func onStateChanged(cr: ChatRoom, newState: ChatRoom.State) { override func onStateChanged(cr: ChatRoom, newState: ChatRoom.State) {
if (newState == ChatRoom.State.Created) if (newState == ChatRoom.State.Created)
{ {
print("ChatRoomTrace - Chatroom ready to start") print("ChatRoomTrace - Chatroom ready to start")
LinphoneCoreHolder.instance.chatroomTutorialState = ChatroomTutorialState.Started tutorialContext!.chatroomTutorialState = ChatroomTutorialState.Started
} }
} }
} }