Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Jacquemin (Rubicon) authored Mar 22, 2019
2 parents c33750c + 559ab31 commit b16ad9f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 76 deletions.
10 changes: 5 additions & 5 deletions example/Swift/PrebidDemo/PrebidDemo/BannerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class BannerController: UIViewController, GADBannerViewDelegate, MPAdViewDelegat
let request = DFPRequest()

var dfpBanner: DFPBannerView!

var bannerUnit: BannerAdUnit!

var mopubBanner: MPAdView?
Expand All @@ -58,8 +59,7 @@ class BannerController: UIViewController, GADBannerViewDelegate, MPAdViewDelegat

override func viewDidDisappear(_ animated: Bool) {
// important to remove the time instance
bannerUnit.stopAutoRefresh()
bannerUnit = nil
bannerUnit?.stopAutoRefresh()
}

func loadDFPBanner(bannerUnit: AdUnit) {
Expand All @@ -71,10 +71,10 @@ class BannerController: UIViewController, GADBannerViewDelegate, MPAdViewDelegat
dfpBanner.backgroundColor = .red
appBannerView.addSubview(dfpBanner)
request.testDevices = [ kGADSimulatorID, "cc7ca766f86b43ab6cdc92bed424069b"]

bannerUnit.fetchDemand(adObject: self.request) { (ResultCode) in
bannerUnit.fetchDemand(adObject:self.request) { [weak self] (ResultCode) in
print("Prebid demand fetch for DFP \(ResultCode.name())")
self.dfpBanner!.load(self.request)
self?.dfpBanner!.load(self?.request)
}
}

Expand Down
95 changes: 57 additions & 38 deletions src/PrebidMobile/PrebidMobile/AdUnits/AdUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,20 @@ import ObjectiveC.runtime
@objcMembers public class AdUnit: NSObject, DispatcherDelegate {

var prebidConfigId: String! = ""

var adSizes = [CGSize] ()

var identifier: String

var timerClass: Dispatcher?

var refreshTime: Double? = 0.0

private var customKeywords = [String: [String]]()


var adSizes = Array<CGSize> ()

var identifier:String

var dispatcher: Dispatcher?

private var customKeywords = [String: Array<String>]()

//This flag is set to check if the refresh needs to be made though the user has not invoked the fetch demand after initialization
private var isInitialCallMade: Bool! = false

private var adServerObject: AnyObject?

private var isInitialFetchDemandCallMade: Bool = false
private var adServerObject:AnyObject?
private var closure: (ResultCode) -> Void

//notification flag set to check if the prebid response is received within the specified time
Expand All @@ -49,8 +47,6 @@ import ObjectiveC.runtime
adSizes.append(size)
identifier = UUID.init().uuidString
super.init()

timerClass = Dispatcher.init(withDelegate: self)
}

dynamic public func fetchDemand(adObject: AnyObject, completion: @escaping(_ result: ResultCode) -> Void) {
Expand All @@ -72,13 +68,10 @@ import ObjectiveC.runtime
completion(ResultCode.prebidInvalidAccountId)
return
}
if (isInitialCallMade == false) {
//the publisher called the fetch demand 1st fire the timer
isInitialCallMade = true
//start the timer only if the refresh timer is valided & set
if (refreshTime! > 0.0) {
self.timerClass?.start(autoRefreshMillies: refreshTime!)
}

if !isInitialFetchDemandCallMade {
isInitialFetchDemandCallMade = true
startDispatcher()
}

didReceiveResponse = false
Expand Down Expand Up @@ -163,28 +156,31 @@ import ObjectiveC.runtime

/**
* This method allows to set the auto refresh period for the demand
*
* - Parameter time: refresh time interval
*/
public func setAutoRefreshMillis(time: Double) {
if (time >= .PB_MIN_RefreshTime) {
//Stop the old refresh & start a new timer
if (refreshTime! > 0.0 && isInitialCallMade == true) {
timerClass!.stop()
refreshTime = time
timerClass!.start(autoRefreshMillies: refreshTime!)

} else {
refreshTime = time
}
} else {
Log.error("auto refresh not set as the refresh time is less than to 30 seconds")
public func setAutoRefreshMillis(time:Double) {

stopDispatcher()

guard time >= .PB_MIN_RefreshTime else {
Log.error("auto refresh not set as the refresh time is less than to \(.PB_MIN_RefreshTime as Double) seconds")
return
}

initDispatcher(refreshTime: time)

if isInitialFetchDemandCallMade {
startDispatcher();
}
}

/**
* This method stops the auto refresh of demand
*/
public func stopAutoRefresh() {
timerClass!.stop()
public func stopAutoRefresh(){
stopDispatcher()
}

func refreshDemand() {
Expand All @@ -194,4 +190,27 @@ import ObjectiveC.runtime

}

func initDispatcher(refreshTime: Double) {
self.dispatcher = Dispatcher.init(withDelegate:self, autoRefreshMillies: refreshTime)
}

func startDispatcher() {
guard let dispatcher = self.dispatcher else {
Log.verbose("Dispatcher is nil")
return
}

dispatcher.start()
}

func stopDispatcher() {
guard let dispatcher = self.dispatcher else {
Log.verbose("Dispatcher is nil")
return
}

dispatcher.stop()
self.dispatcher = nil
}

}
45 changes: 24 additions & 21 deletions src/PrebidMobile/PrebidMobile/Dispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,32 @@ protocol DispatcherDelegate: class {
func refreshDemand()
}

class Dispatcher: NSObject {

var timer: Timer?

weak var delegate: DispatcherDelegate?

var repeatInSeconds: Double! = 0

init(withDelegate: DispatcherDelegate) {
super.init()
delegate = withDelegate
}

func start(autoRefreshMillies: Double) {

if (self.timer != nil) {
self.timer?.invalidate()
self.timer = nil
}

class Dispatcher:NSObject {

var timer : Timer?

var delegate: DispatcherDelegate!

var repeatInSeconds:Double = 0

init(withDelegate:DispatcherDelegate, autoRefreshMillies:Double) {

//timer takes values in seconds...
repeatInSeconds = autoRefreshMillies/1000

delegate = withDelegate

super.init()
}

open func invalidate() {
stop()
delegate = nil
}

func start() {

stop()

self.timer = Timer.scheduledTimer(timeInterval: repeatInSeconds, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)

RunLoop.main.add(self.timer!, forMode: .commonModes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BannerAdUnitTests: XCTestCase {
let adUnit = BannerAdUnit(configId: Constants.configID1, size: CGSize(width: Constants.width2, height: Constants.height2))
XCTAssertTrue(1 == adUnit.adSizes.count)
XCTAssertTrue(adUnit.prebidConfigId == Constants.configID1)
XCTAssertTrue(0 == adUnit.refreshTime)
XCTAssertNil(adUnit.dispatcher)
}

func testBannerAdUnitAddSize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class InterstItialAdUnitTests: XCTestCase {
func testInterstitialAdUnitCreation() {
let adUnit = InterstitialAdUnit(configId: Constants.configID1)
XCTAssertTrue(adUnit.prebidConfigId == Constants.configID1)
XCTAssertTrue(0 == adUnit.refreshTime)
XCTAssertNil(adUnit.dispatcher)
}

func testSetUserKeyword() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,27 @@ class DispatcherTests: XCTestCase, DispatcherDelegate {
loadSuccesfulException = nil
}

func testDispatcherIsNotNil() {
let dispatcher = Dispatcher(withDelegate: self)
func testDispatcherIsNotNil()
{
let dispatcher = Dispatcher(withDelegate: self, autoRefreshMillies: 0.0)
XCTAssertNotNil(dispatcher)
XCTAssertNil(dispatcher.timer)
}

func testStartDispatcherWithRefreshMiliseconds() {
let dispatcher = Dispatcher(withDelegate: self)
dispatcher.start(autoRefreshMillies: 10.0)

func testStartDispatcherWithRefreshMiliseconds()
{
let dispatcher = Dispatcher(withDelegate: self, autoRefreshMillies: 10.0)
dispatcher.start()
XCTAssertNotNil(dispatcher.timer)
XCTAssertEqual(dispatcher.timer?.timeInterval, TimeInterval(10.0/1000))
loadSuccesfulException = expectation(description: "\(#function)")
waitForExpectations(timeout: timeoutForRequest, handler: nil)
}

func testStopDispatcher() {
let dispatcher = Dispatcher(withDelegate: self)
dispatcher.start(autoRefreshMillies: 10.0)

func testStopDispatcher()
{
let dispatcher = Dispatcher(withDelegate: self, autoRefreshMillies: 10.0)
dispatcher.start()
XCTAssertNotNil(dispatcher.timer)
XCTAssertTrue(dispatcher.timer!.isValid)
dispatcher.stop()
Expand Down

0 comments on commit b16ad9f

Please sign in to comment.