Advertisement
andreyminin

Untitled

Mar 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.53 KB | None | 0 0
  1. class CommunicationManager: CommunicatorDelegate {
  2.  
  3.   static let shared = CommunicationManager() // Making singleton
  4.   var multiPeerCommunicator: MultiPeerCommunicator!
  5.   var delegate: ManagerDelegate! // Delegate to talk to ViewController
  6.  
  7.   private init() {
  8.     self.multiPeerCommunicator = MultiPeerCommunicator()  //Setting up the instance of MultiPeerCommunicator
  9.     self.multiPeerCommunicator.delegate = self  // Setting up the delegate
  10.   }
  11.  
  12.   var listOfBlabbers: [String : Blabber] = [:]
  13.  
  14.   func didFoundUser(userID: String, userName: String?) {
  15.     // If Blabber already exsists, just making him online:
  16.     if let newBlabber = listOfBlabbers[userID] {
  17.       newBlabber.online = true
  18.     } else {
  19.       // If not adding him to the list:
  20.       let newBlabber = Blabber(id: userID, name: userName)
  21.       newBlabber.online = true
  22.       listOfBlabbers[userID] = newBlabber
  23.     }
  24.     DispatchQueue.main.async {
  25.       self.delegate.globalUpdate()
  26.     }
  27.   }
  28.  
  29.   func didLostUser(userID: String) {
  30.     if let newBlabber = listOfBlabbers[userID] {
  31.       newBlabber.online = false
  32.       listOfBlabbers.removeValue(forKey: userID)
  33.     }
  34.     DispatchQueue.main.async {
  35.       self.delegate.globalUpdate()
  36.     }
  37.   }
  38.  
  39.   func failedToStartBrowsingForUsers(error: Error) {
  40.     print(error.localizedDescription)
  41.   }
  42.  
  43.   func failedToStartAdvertisingForUsers(error: Error) {
  44.     print(error.localizedDescription)
  45.   }
  46.  
  47.   func didReceiveMessage(text: String, fromUser: String, toUser: String) {
  48.  
  49.  
  50.   }
  51.  
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement