From 559ab319ae6d5ffd83b0b9635907dab3f8a43213 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 21 Mar 2019 16:28:35 +0200 Subject: [PATCH] Client API behavior (#163) * refactored code --- .../PrebidDemo/BannerController.swift | 8 +- .../PrebidMobile/AdUnits/AdUnit.swift | 74 ++++++++++++------- .../PrebidMobile/Dispatcher.swift | 28 ++++--- .../AdUnitTests/BannerAdUnitTests.swift | 2 +- .../AdUnitTests/InterstItialAdUnitTests.swift | 2 +- .../FetchingLogictests/DispatcherTests.swift | 10 +-- 6 files changed, 73 insertions(+), 51 deletions(-) diff --git a/example/Swift/PrebidDemo/PrebidDemo/BannerController.swift b/example/Swift/PrebidDemo/PrebidDemo/BannerController.swift index 8ca188a55..54f5838c6 100644 --- a/example/Swift/PrebidDemo/PrebidDemo/BannerController.swift +++ b/example/Swift/PrebidDemo/PrebidDemo/BannerController.swift @@ -32,6 +32,7 @@ class BannerController: UIViewController, GADBannerViewDelegate, MPAdViewDelegat let request = DFPRequest() var dfpBanner: DFPBannerView! + var bannerUnit: BannerAdUnit! var mopubBanner: MPAdView? @@ -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){ @@ -72,9 +72,9 @@ class BannerController: UIViewController, GADBannerViewDelegate, MPAdViewDelegat 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) } } diff --git a/src/PrebidMobile/PrebidMobile/AdUnits/AdUnit.swift b/src/PrebidMobile/PrebidMobile/AdUnits/AdUnit.swift index d88261986..b36927732 100644 --- a/src/PrebidMobile/PrebidMobile/AdUnits/AdUnit.swift +++ b/src/PrebidMobile/PrebidMobile/AdUnits/AdUnit.swift @@ -24,14 +24,12 @@ import ObjectiveC.runtime var identifier:String - var timerClass:Dispatcher? - - var refreshTime:Double? = 0.0 + var dispatcher: Dispatcher? private var customKeywords = [String: Array]() //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 isInitialFetchDemandCallMade: Bool = false private var adServerObject:AnyObject? @@ -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) { @@ -72,15 +68,12 @@ 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 timeOutSignalSent = false self.closure = completion @@ -165,20 +158,22 @@ 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(); } } @@ -186,7 +181,7 @@ import ObjectiveC.runtime * This method stops the auto refresh of demand */ public func stopAutoRefresh(){ - timerClass!.stop() + stopDispatcher() } func refreshDemand(){ @@ -196,4 +191,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 + } + } diff --git a/src/PrebidMobile/PrebidMobile/Dispatcher.swift b/src/PrebidMobile/PrebidMobile/Dispatcher.swift index 24a28c65b..1d0a772a3 100644 --- a/src/PrebidMobile/PrebidMobile/Dispatcher.swift +++ b/src/PrebidMobile/PrebidMobile/Dispatcher.swift @@ -23,24 +23,28 @@ class Dispatcher:NSObject { var timer : Timer? - weak var delegate: DispatcherDelegate? + var delegate: DispatcherDelegate! - var repeatInSeconds:Double! = 0 + var repeatInSeconds:Double = 0 - init(withDelegate:DispatcherDelegate) { - super.init() + init(withDelegate:DispatcherDelegate, autoRefreshMillies:Double) { + + //timer takes values in seconds... + repeatInSeconds = autoRefreshMillies/1000 delegate = withDelegate + + super.init() + } - func start(autoRefreshMillies:Double) { - - if(self.timer != nil){ - self.timer?.invalidate(); - self.timer = nil; - } + open func invalidate() { + stop() + delegate = nil + } + + func start() { - //timer takes values in seconds... - repeatInSeconds = autoRefreshMillies/1000 + stop() self.timer = Timer.scheduledTimer(timeInterval: repeatInSeconds, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true) diff --git a/src/PrebidMobile/PrebidMobileTests/AdUnitTests/BannerAdUnitTests.swift b/src/PrebidMobile/PrebidMobileTests/AdUnitTests/BannerAdUnitTests.swift index f93a6b9b7..f10a9a7e7 100644 --- a/src/PrebidMobile/PrebidMobileTests/AdUnitTests/BannerAdUnitTests.swift +++ b/src/PrebidMobile/PrebidMobileTests/AdUnitTests/BannerAdUnitTests.swift @@ -31,7 +31,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() diff --git a/src/PrebidMobile/PrebidMobileTests/AdUnitTests/InterstItialAdUnitTests.swift b/src/PrebidMobile/PrebidMobileTests/AdUnitTests/InterstItialAdUnitTests.swift index 2ffbdec80..50e5b1a76 100644 --- a/src/PrebidMobile/PrebidMobileTests/AdUnitTests/InterstItialAdUnitTests.swift +++ b/src/PrebidMobile/PrebidMobileTests/AdUnitTests/InterstItialAdUnitTests.swift @@ -30,7 +30,7 @@ class InterstItialAdUnitTests: XCTestCase { { let adUnit = InterstitialAdUnit(configId: Constants.configID1) XCTAssertTrue(adUnit.prebidConfigId == Constants.configID1) - XCTAssertTrue(0 == adUnit.refreshTime) + XCTAssertNil(adUnit.dispatcher) } func testSetUserKeyword() diff --git a/src/PrebidMobile/PrebidMobileTests/FetchingLogictests/DispatcherTests.swift b/src/PrebidMobile/PrebidMobileTests/FetchingLogictests/DispatcherTests.swift index 309eebddc..bd56a9b3b 100644 --- a/src/PrebidMobile/PrebidMobileTests/FetchingLogictests/DispatcherTests.swift +++ b/src/PrebidMobile/PrebidMobileTests/FetchingLogictests/DispatcherTests.swift @@ -33,15 +33,15 @@ class DispatcherTests: XCTestCase, DispatcherDelegate { func testDispatcherIsNotNil() { - let dispatcher = Dispatcher(withDelegate: self) + 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) + 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)") @@ -50,8 +50,8 @@ class DispatcherTests: XCTestCase, DispatcherDelegate { func testStopDispatcher() { - let dispatcher = Dispatcher(withDelegate: self) - dispatcher.start(autoRefreshMillies: 10.0) + let dispatcher = Dispatcher(withDelegate: self, autoRefreshMillies: 10.0) + dispatcher.start() XCTAssertNotNil(dispatcher.timer) XCTAssertTrue(dispatcher.timer!.isValid) dispatcher.stop()