Created
December 16, 2016 09:06
-
-
Save omayib/6164ed8d4ce11ca9b1a7794e947963f9 to your computer and use it in GitHub Desktop.
an implementation of strategy pattern on swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
//lets to trya strategy pattern! | |
protocol QCallKit{ | |
func dial() | |
func endCall() | |
func accept() | |
func decline() | |
} | |
protocol QCallKitDelegate{ | |
func conversationStarted(message: String) | |
func conversationClossed(message: String) | |
//other delegate here... | |
} | |
/*================== Agora and friends =================*/ | |
class Agora : QCallKit { | |
private var delegate: QCallKitDelegate! | |
init(withDelegate: QCallKitDelegate) { | |
self.delegate = withDelegate | |
} | |
func dial(){ | |
print("dialing using agora") | |
//other procedure here... | |
//assume the dialing is succeed and the conversation begin. | |
delegate.conversationStarted(message: "agora conversation is started") | |
} | |
func endCall(){ | |
print("endCall using agora") | |
//long time conversation happen. now the user close the phone | |
//alot of procedure using agora to close the call. | |
//lets assume endCall is succed! | |
delegate.conversationClossed(message: "agora conversation is closed") | |
} | |
func accept(){ | |
print("accept using agora") | |
} | |
func decline(){ | |
print("decline using agora") | |
} | |
func iHateThisFunction(){ | |
} | |
// other agora functions... | |
} | |
class AgoraHelper{ | |
//.... | |
} | |
/*================== ToxBox and friends =================*/ | |
class TokBox : QCallKit { | |
private var delegate: QCallKitDelegate! | |
init(withDelegate: QCallKitDelegate) { | |
self.delegate = withDelegate | |
} | |
func dial(){ | |
print("dialing using TokBox") | |
//other procedure here... | |
//assume the dialing is succeed and the conversation begin. | |
delegate.conversationStarted(message: "TokBox conversation is started") | |
} | |
func endCall(){ | |
print("endCall using TokBox") | |
//long time conversation happen. now the user close the phone | |
//alot of procedure using TokBox to close the call. | |
//lets assume endCall is succed! | |
delegate.conversationClossed(message: "agora conversation is closed") | |
} | |
func accept(){ | |
print("accept using TokBox") | |
} | |
func decline(){ | |
print("decline using TokBox") | |
} | |
func ruwerFunction(){ | |
} | |
// other TokBox functions... | |
} | |
class TokBoxHelper{ | |
//.... | |
} | |
/*================== OtherCallLibrary and friends =================*/ | |
class OtherCallLibrary : QCallKit { | |
private var delegate: QCallKitDelegate! | |
init(withDelegate: QCallKitDelegate) { | |
self.delegate = withDelegate | |
} | |
func dial(){ | |
print("dialing using OtherCallLibrary") | |
//other procedure here... | |
//assume the dialing is succeed and the conversation begin. | |
delegate.conversationStarted(message: "OtherCallLibrary conversation is started") | |
} | |
func endCall(){ | |
print("endCall using OtherCallLibrary") | |
//long time conversation happen. now the user close the phone | |
//alot of procedure using OtherCallLibrary to close the call. | |
//lets assume endCall is succed! | |
delegate.conversationClossed(message: "OtherCallLibrary conversation is started") | |
} | |
func accept(){ | |
print("accept using OtherCallLibrary") | |
} | |
func decline(){ | |
print("decline using OtherCallLibrary") | |
} | |
func wtfFunc(){ | |
} | |
// other OtherCallLibrary functions... | |
} | |
class OtherCallLibraryHelper{ | |
// | |
} | |
/*============== packaging all into CallSDK ==================*/ | |
class CallSDK{ | |
private var callKitDelegate: QCallKitDelegate | |
private var callKit: QCallKit | |
init(withDelegate:QCallKitDelegate) { | |
self.callKitDelegate = withDelegate | |
self.callKit = OtherCallLibrary(withDelegate: callKitDelegate) | |
} | |
func dialNow(userId: Int){ | |
self.callKit.dial() | |
} | |
func closeConversation(){ | |
self.callKit.endCall() | |
} | |
func reject(){ | |
self.callKit.decline() | |
} | |
func answer(){ | |
self.callKit.accept() | |
} | |
} | |
/*============== lets see the implementation =================*/ | |
class CallVC: UIViewController, QCallKitDelegate { | |
var callSDK: CallSDK! | |
override func viewDidLoad() { | |
callSDK = CallSDK(withDelegate: self) | |
} | |
func callButtonDidTap(){ | |
callSDK.dialNow(userId: 123) | |
} | |
func rejectButtonDidTap(){ | |
callSDK.reject() | |
} | |
func answerButtonDidTap(){ | |
callSDK.answer() | |
} | |
func closeButtonDidTap(){ | |
callSDK.closeConversation() | |
} | |
// MARK : thoose function come from QCallKitDelegate | |
func conversationStarted(message: String){ | |
print("from call vc - \(message)") | |
} | |
func conversationClossed(message: String){ | |
print("from call vc - \(message)") | |
} | |
} | |
let callVC = CallVC() | |
callVC.viewDidLoad() | |
// mulai menelpon | |
callVC.callButtonDidTap() | |
callVC.closeButtonDidTap() | |
// menjawab telpon | |
// menutup telpon | |
// mereject telpon |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment