Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): more cleanup #6

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ Listen to the `streamReceived` event. It will return a `view` with the videos. Y
import TiVonage from 'ti.vonage';

const API_KEY = ''; // Get from https://tokbox.com/developer/
const SESSION_ID = '; // Get from https://tokbox.com/developer/tools/playground/
const SESSION_ID = ''; // Get from https://tokbox.com/developer/tools/playground/
const TOKEN = ''; // Get from https://tokbox.com/developer/tools/playground/
const AUDIO_ONLY = false;

function onOpen() {
// iOS requires some privacy permissions first
Expand Down Expand Up @@ -117,6 +118,7 @@ function onClickConnect() {
TiVonage.apiKey = API_KEY;
TiVonage.sessionId = SESSION_ID;
TiVonage.token = TOKEN;
TiVonage.audioOnly = AUDIO_ONLY;

TiVonage.connect();
}
Expand Down
88 changes: 46 additions & 42 deletions ios/Classes/TiVonageModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class TiVonageModule: TiModule {

var subscriber: OTSubscriber?

var apiKey: String?
var _apiKey: String?

var sessionId: String?
var _sessionId: String?

var token: String?
var _token: String?

var audioOnly: Bool = false
var _audioOnly: Bool = false

func moduleGUID() -> String {
return "8669e6e4-ff3a-4a19-b85a-ead686c4c18c"
Expand All @@ -43,7 +43,7 @@ class TiVonageModule: TiModule {

@objc(connect:)
func connect(unused: Any?) {
guard let apiKey = apiKey, let sessionId = sessionId, let token = token else {
guard let apiKey = _apiKey, let sessionId = _sessionId, let token = _token else {
NSLog("[ERROR] Missing apiKey, sessionId or token property! Please set before calling \"connect()\"")
return
}
Expand Down Expand Up @@ -71,46 +71,48 @@ class TiVonageModule: TiModule {

@objc(setApiKey:)
func setApiKey(apiKey: String) {
self.apiKey = apiKey
_apiKey = apiKey
replaceValue(apiKey, forKey: "apiKey", notification: false)
}

@objc(apiKey:)
func apiKey(unused: Any?) -> String? {
return apiKey
@objc(apiKey)
func apiKey() -> String? {
return _apiKey
}

@objc(setSessionId:)
func setSessionId(sessionId: String) {
self.sessionId = sessionId
_sessionId = sessionId
replaceValue(sessionId, forKey: "sessionId", notification: false)
}

@objc(sessionId:)
func sessionId(unused: Any?) -> String? {
return sessionId
@objc(sessionId)
func sessionId() -> String? {
return _sessionId
}

@objc(setToken:)
func setToken(token: String) {
self.token = token
_token = token
replaceValue(token, forKey: "token", notification: false)
}

@objc(token:)
func token(unused: Any?) -> String? {
return token
@objc(token)
func token() -> String? {
return _token
}

@objc(setAudioOnly:)
func setAudioOnly(audioOnly: Bool) {
self.audioOnly = audioOnly
replaceValue(audioOnly, forKey: "audioOnly", notification: false)
func setAudioOnly(audioOnly: Any) {
if let audioOnly = audioOnly as? Bool {
_audioOnly = audioOnly
replaceValue(audioOnly, forKey: "audioOnly", notification: false)
}
}

@objc(audioOnly:)
func audioOnly(unused: Any?) -> Bool {
return audioOnly
@objc(audioOnly)
func audioOnly() -> Any {
return _audioOnly
}
}

Expand All @@ -129,7 +131,7 @@ extension TiVonageModule : OTSessionDelegate {
func sessionDidConnect(_ session: OTSession) {
let settings = OTPublisherSettings()
settings.name = UIDevice.current.name
settings.videoTrack = !audioOnly;
settings.videoTrack = !_audioOnly;

guard let publisher = OTPublisher(delegate: self, settings: settings) else {
return
Expand All @@ -142,20 +144,21 @@ extension TiVonageModule : OTSessionDelegate {
print(error!)
return
}

var event: [String: Any] = [
"userType": "published"
]

guard let publisherView = publisher.view else {
fireEvent("streamReceived", with: event)
return
}
let screenBounds = UIScreen.main.bounds
publisherView.frame = CGRect(x: 0, y: 0, width: 512, height: 512)

let viewProxy = TiVonageVideoProxy()._init(withPageContext: pageContext,
let viewProxy = TiVonageVideoProxy()._init(withPageContext: executionContext,
videoView: publisherView)

let event: [String: Any] = [
"view": viewProxy!,
"userType": "published"
]
event["view"] = viewProxy!

fireEvent("streamReceived", with: event)
}
Expand All @@ -181,28 +184,29 @@ extension TiVonageModule : OTSessionDelegate {
return
}

var event: [String: Any] = [
"userType": "subscriber",
"streamId": stream.streamId,
"connectionData": stream.connection.data ?? "",
"connectionId": stream.connection.connectionId,
"connectionCreationTime": stream.connection.creationTime
]

guard let subscriberView = subscriber.view else {
fireEvent("streamReceived", with: event)
return
}
subscriberView.frame = UIScreen.main.bounds

let viewProxy = TiVonageVideoProxy()._init(withPageContext: pageContext,
videoView: subscriberView)

let event: [String: Any] = [
"view": viewProxy!,
"userType": "subscriber",
"streamId": stream.streamId,
"connectionData": stream.connection.data ?? "",
"connectionId": stream.connection.connectionId,
"connectionCreationTime": stream.connection.creationTime
]

event["view"] = viewProxy
fireEvent("streamReceived", with: event)
}

func session(_ session: OTSession, streamDestroyed stream: OTStream) {
// MARK: Also fire the "streamDestroyed" event here?
fireEvent("streamDropped", with: ["type": "subscriber", "streamId": stream.streamId ])
}
}

Expand All @@ -219,11 +223,11 @@ extension TiVonageModule : OTPublisherDelegate {
}

func publisher(_ publisher: OTPublisherKit, streamCreated stream: OTStream) {
fireEvent("streamCreated")
fireEvent("streamCreated", with: ["streamId": stream.streamId ])
}

func publisher(_ publisher: OTPublisherKit, streamDestroyed stream: OTStream) {
fireEvent("streamDestroyed")
fireEvent("streamDestroyed", with: ["type": "publisher", "streamId": stream.streamId ])
}
}

Expand Down
13 changes: 8 additions & 5 deletions ios/Classes/TiVonageVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ import OpenTok
@objc(TiVonageVideo)
public class TiVonageVideo : TiUIView {

public var videoView: UIView?
public var videoView: UIView? = nil {
didSet {
if let videoView = videoView {
self.frame = videoView.bounds
addSubview(videoView)
}
}
}

public override func frameSizeChanged(_ frame: CGRect, bounds: CGRect) {
super.frameSizeChanged(frame, bounds: bounds)

if let videoView = videoView {
if self.subviews.isEmpty {
self.addSubview(videoView)
}

TiUtils.setView(videoView, positionRect: bounds)
}
}
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/TiVonageVideoProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TiVonageVideoProxy : TiViewProxy {
public func _init(withPageContext context: TiEvaluator!, videoView: UIView) -> Self! {
super._init(withPageContext: context)

self.publisherView.videoView = publisherView
self.publisherView.videoView = videoView

return self
}
Expand Down