Advertisement
Guest User

collectionViewLayout

a guest
Feb 28th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.36 KB | None | 0 0
  1. import UIKit
  2.  
  3. import UIKit
  4. import AgoraRtcEngineKit
  5. import collection_view_layouts
  6.  
  7. class ViewController: UIViewController {
  8.    
  9.     @IBOutlet weak var collectionView: UICollectionView! {
  10.         didSet {
  11.             let contentFlowLayout: ContentDynamicLayout = FacebookStyleFlowLayout()
  12.            
  13.             contentFlowLayout.delegate = self
  14.             contentFlowLayout.contentPadding = ItemsPadding(horizontal: 10, vertical: 10)
  15.             contentFlowLayout.cellsPadding = ItemsPadding(horizontal: 8, vertical: 8)
  16.             contentFlowLayout.contentAlign = .left
  17.            
  18.             collectionView.collectionViewLayout = contentFlowLayout
  19.         }
  20.     }
  21.    
  22.     var agoraKit: AgoraRtcEngineKit!
  23.    
  24.     var uIds: [Int64] = [] {
  25.         didSet {
  26.             cellsSizes.removeAll()
  27.             (0..<uIds.count).forEach({ _ in cellsSizes.append(CGSize(width: collectionView.bounds.width/2, height: 300)) })
  28.         }
  29.     }
  30.    
  31.     var cellsSizes: [CGSize] = []
  32.    
  33.     override func viewDidLoad() {
  34.         super.viewDidLoad()
  35.        
  36.         initializeAgoraEngine()
  37.         setupVideo()
  38.         setChannelProfile()
  39.     }
  40.    
  41.     override func viewDidAppear(_ animated: Bool) {
  42.         super.viewDidAppear(animated)
  43.         showJoinAlert()
  44.     }
  45.    
  46.     func showJoinAlert() {
  47.         let alertController = UIAlertController(title: nil, message: "Ready to join channel.", preferredStyle: .alert)
  48.        
  49.         let action = UIAlertAction(title: "Join", style: .destructive) { (action:UIAlertAction) in
  50.             self.joinChannel()
  51.         }
  52.         alertController.addAction(action)
  53.         present(alertController, animated: true, completion: nil)
  54.     }
  55.    
  56.     func initializeAgoraEngine() {
  57.         agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: AppID, delegate: self)
  58.     }
  59.    
  60.     func setChannelProfile() {
  61.         agoraKit.setChannelProfile(.communication)
  62.     }
  63.    
  64.     func setupVideo() {
  65.         agoraKit.enableVideo()
  66.         agoraKit.setVideoEncoderConfiguration(
  67.             AgoraVideoEncoderConfiguration(size: AgoraVideoDimension640x360,
  68.                                            frameRate: .fps15,
  69.                                            bitrate: AgoraVideoBitrateStandard,
  70.                                            orientationMode: .adaptative)
  71.         )
  72.     }
  73.    
  74.     func joinChannel() {
  75.         agoraKit.setDefaultAudioRouteToSpeakerphone(true)
  76.        
  77.         guard let uid = (1...100).randomElement() else { return }
  78.        
  79.         agoraKit.joinChannel(byToken: nil,
  80.                              channelId: "DemoChannel",
  81.                              info: nil,
  82.                              uid: UInt(uid)) { [weak self] (sid, uid, elapsed) -> Void in
  83.                                
  84.                                 guard let _self = self else { return }
  85.                                 DispatchQueue.main.async {
  86.                                     _self.uIds.append(Int64(uid))
  87.                                     _self.collectionView.reloadData()
  88.                                 }
  89.                                
  90.         }
  91.         UIApplication.shared.isIdleTimerDisabled = true
  92.     }
  93.    
  94.     @IBAction func didClickHangUpButton(_ sender: UIButton) {
  95.         leaveChannel()
  96.     }
  97.    
  98.     func leaveChannel() {
  99.         agoraKit.leaveChannel(nil)
  100.         UIApplication.shared.isIdleTimerDisabled = false
  101.     }
  102.    
  103.     @IBAction func didClickMuteButton(_ sender: UIButton) {
  104.         sender.isSelected = !sender.isSelected
  105.         agoraKit.muteLocalAudioStream(sender.isSelected)
  106.     }
  107. }
  108.  
  109. extension ViewController: AgoraRtcEngineDelegate {
  110.    
  111.     func rtcEngine(_ engine: AgoraRtcEngineKit, firstRemoteVideoDecodedOfUid uid:UInt, size:CGSize, elapsed:Int) {
  112.        
  113.         uIds.append(Int64(uid))
  114.         collectionView.reloadData()
  115.     }
  116.    
  117.     internal func rtcEngine(_ engine: AgoraRtcEngineKit, didOfflineOfUid uid:UInt, reason:AgoraUserOfflineReason) {
  118.        
  119.         if let index = uIds.firstIndex(where: { $0 == uid }) {
  120.             uIds.remove(at: index)
  121.             collectionView.reloadData()
  122.         }
  123.     }
  124.    
  125.     func rtcEngine(_ engine: AgoraRtcEngineKit, didVideoMuted muted:Bool, byUid:UInt) {
  126.         //        remoteVideoMutedIndicator.isHidden = !muted
  127.     }
  128. }
  129.  
  130. extension ViewController: UICollectionViewDataSource {
  131.    
  132.     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  133.         print("Returning ", uIds.count)
  134.         return uIds.count
  135.     }
  136.    
  137.     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  138.        
  139.         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
  140.        
  141.         let uid = uIds[indexPath.row]
  142.        
  143.         let videoCanvas = AgoraRtcVideoCanvas()
  144.         videoCanvas.uid = UInt(uid)
  145.         videoCanvas.view = cell.contentView
  146.         videoCanvas.renderMode = .hidden
  147.        
  148.         if indexPath.row == 0 {
  149.             agoraKit.setupLocalVideo(videoCanvas)
  150.         } else {
  151.             agoraKit.setupRemoteVideo(videoCanvas)
  152.         }
  153.        
  154.         return cell
  155.     }
  156. }
  157.  
  158. extension ViewController: ContentDynamicLayoutDelegate {
  159.    
  160.     func cellSize(indexPath: IndexPath) -> CGSize {
  161.         return cellsSizes[indexPath.row]
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement