You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
doctor_app_flutter/ios/Runner/MainAppViewController.swift

199 lines
7.7 KiB
Swift

//
// MainAppViewController.swift
// Runner
//
// Created by Zohaib Iqbal Kambrani on 08/06/2021.
// Copyright © 2021 The Chromium Authors. All rights reserved.
//
import Foundation
import AADraggableView
class MainAppViewController: FlutterViewController{
var videoCallContainer:AADraggableView!
var videoCallViewController:VideoCallViewController!
var videoCallFlutterResult:FlutterResult?
var vdoCallViewFloatRectConstraint:[NSLayoutConstraint]!
var vdoCallViewFullConstraint:[NSLayoutConstraint]!
var vdoCallViewFloatCircleConstraint:[NSLayoutConstraint]!
override func viewDidLoad() {
super.viewDidLoad()
initFlutterBridge()
prepareVideoCallView()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
var videoCallChannel:FlutterMethodChannel?
private func initFlutterBridge(){
videoCallChannel = FlutterMethodChannel(name: "Dr.cloudSolution/videoCall", binaryMessenger: binaryMessenger)
videoCallChannel?.setMethodCallHandler({
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
switch call.method {
case "openVideoCall":
self.startVideoCall(result: result, call: call)
default:
result(FlutterMethodNotImplemented)
}
})
}
}
// Video Call Functions
extension MainAppViewController : ICallProtocol{
func prepareVideoCallView(){
videoCallContainer = AADraggableView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))
videoCallContainer.backgroundColor = UIColor.black
videoCallContainer.isHidden = true
videoCallContainer.clipsToBounds = true
videoCallContainer.padding = 20
videoCallContainer.reposition = .edgesOnly
videoCallContainer.isEnabled = false
view.addSubview(videoCallContainer)
setVideoViewConstrints()
NSLayoutConstraint.activate(vdoCallViewFullConstraint)
NSLayoutConstraint.deactivate(vdoCallViewFloatRectConstraint)
ViewEmbedder.embed(
withIdentifier: "videoCall", // Storyboard ID
parent: self,
container: self.videoCallContainer){ vc in
self.videoCallViewController = vc as? VideoCallViewController
}
}
private func showVideo(show:Bool){
UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: {
self.videoCallContainer.isHidden = !show
})
}
private func startVideoCall(result: @escaping FlutterResult, call:FlutterMethodCall) {
videoCallFlutterResult = result
if let arguments = call.arguments as? NSDictionary{
showVideoCallView(true)
videoCallViewController.onRectFloat = { min in
self.rectFloatVideoCallView(min)
if(min){
self.videoCallContainer.repositionIfNeeded()
}
}
videoCallViewController.onCircleFloat = { min in
self.circleFloatVideoCallView(min)
self.videoCallContainer.reposition = min ? .free : .edgesOnly
self.videoCallContainer.repositionIfNeeded()
}
videoCallViewController.onCallConnect = {
self.videoCallChannel?.invokeMethod("onCallConnected",arguments: nil)
}
videoCallViewController.onCallDisconnect = {
self.showVideoCallView(false)
self.videoCallChannel?.invokeMethod("onCallDisconnected",arguments: nil)
}
videoCallViewController.callBack = self
videoCallViewController.start(params: VideoCallRequestParameters(dictionary: arguments))
}
}
private func rectFloatVideoCallView(_ value:Bool){
videoCallContainer.enable(value)
UIView.animate(withDuration: 0.5) {
if(value){
NSLayoutConstraint.deactivate(self.vdoCallViewFullConstraint)
NSLayoutConstraint.deactivate(self.vdoCallViewFloatCircleConstraint)
NSLayoutConstraint.activate(self.vdoCallViewFloatRectConstraint)
}else{
NSLayoutConstraint.deactivate(self.vdoCallViewFloatRectConstraint)
NSLayoutConstraint.deactivate(self.vdoCallViewFloatCircleConstraint)
NSLayoutConstraint.activate(self.vdoCallViewFullConstraint)
}
self.videoCallContainer.layer.cornerRadius = value ? 10 : 0
self.videoCallContainer.layer.borderColor = value ? UIColor.white.cgColor : nil
self.videoCallContainer.layer.borderWidth = value ? 2 : 0
self.view.layoutIfNeeded()
}
}
private func circleFloatVideoCallView(_ value:Bool){
videoCallContainer.enable(value)
UIView.animate(withDuration: 0.5) {
if(value){
NSLayoutConstraint.deactivate(self.vdoCallViewFullConstraint)
NSLayoutConstraint.deactivate(self.vdoCallViewFloatRectConstraint)
NSLayoutConstraint.activate(self.vdoCallViewFloatCircleConstraint)
self.videoCallContainer.layer.cornerRadius = 35
}else{
NSLayoutConstraint.activate(self.vdoCallViewFloatRectConstraint)
NSLayoutConstraint.deactivate(self.vdoCallViewFullConstraint)
NSLayoutConstraint.deactivate(self.vdoCallViewFloatCircleConstraint)
self.videoCallContainer.layer.cornerRadius = 10
}
self.videoCallContainer.layer.borderColor = value ? UIColor.white.cgColor : nil
self.videoCallContainer.layer.borderWidth = value ? 2 : 0
self.view.layoutIfNeeded()
}
}
private func showVideoCallView(_ value:Bool){
UIView.animate(withDuration: 0.5) {
self.videoCallContainer.isHidden = !value
}
}
func sessionDone(res: Any) {
videoCallFlutterResult?(res)
}
func sessionNotResponded(res: Any) {
videoCallFlutterResult?(res)
}
func setVideoViewConstrints(){
videoCallContainer.layer.shadowColor = UIColor.black.cgColor
videoCallContainer.layer.shadowOffset = CGSize(width: 1, height: 1)
videoCallContainer.layer.shadowRadius = 5
let screen = UIScreen.main.bounds
videoCallContainer.translatesAutoresizingMaskIntoConstraints = false
vdoCallViewFullConstraint = [
videoCallContainer.topAnchor.constraint(equalTo: view.topAnchor),
videoCallContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
videoCallContainer.widthAnchor.constraint(equalToConstant: screen.width),
videoCallContainer.heightAnchor.constraint(equalToConstant: screen.height)
]
vdoCallViewFloatRectConstraint = [
videoCallContainer.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
videoCallContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
videoCallContainer.widthAnchor.constraint(equalToConstant: screen.width/3),
videoCallContainer.heightAnchor.constraint(equalToConstant: screen.height/3.5)
]
vdoCallViewFloatCircleConstraint = [
videoCallContainer.topAnchor.constraint(equalTo: view.topAnchor),
videoCallContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
videoCallContainer.widthAnchor.constraint(equalToConstant: 70),
videoCallContainer.heightAnchor.constraint(equalToConstant: 70)
]
}
}