Created
June 20, 2016 20:46
-
-
Save doapp-grady/1940e1e9626974a7045ef01ffbdc2a32 to your computer and use it in GitHub Desktop.
This file contains 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
import UIKit | |
struct NMAdLocation { | |
var categoryId: String | |
var categoryName: String | |
var subcategoryId: String | |
var subcategoryName: String | |
var articleUrl: String? | |
init(categoryId: String, categoryName: String, subcategoryId: String, subcategoryName: String, articleUrl: String? = nil) { | |
self.categoryId = categoryId | |
self.categoryName = categoryName | |
self.subcategoryId = subcategoryId | |
self.subcategoryName = subcategoryName | |
self.articleUrl = articleUrl | |
} | |
} | |
class AdController { | |
static let sharedInstance = AdController() | |
/// Downloads and saves the ad setting JSON | |
func requestAdSettings(withCompletion completion: (success: Bool) -> Void) { | |
} | |
func generateAdRequesterWithLocation(location: NMAdLocation, adType: DAPAdType) -> AdRequester { | |
let providers = providersForAdType(adType, withLocation: location) | |
return AdRequester(providers: providers) | |
} | |
private func providersForAdType(adType: DAPAdType, withLocation location: NMAdLocation) -> [AdProvider] { | |
//Look at config to determine which providers are relevent to this request | |
//Inject appropriate advice to each provider | |
return [AdProvider]() //TODO | |
} | |
private func adAdviceForLocation(location: NMAdLocation) -> AdAdvice { | |
return AdAdvice(adLocation: location, lat: 0, long: 0) //TODO | |
} | |
} | |
protocol AdRequestListener { | |
func adRequestSucceeded(adType: DAPAdType) | |
func adRequestFailed(error: NSError) | |
} | |
class AdRequester { | |
private let providers: [AdProvider] | |
var delegate: AdRequestListener? | |
let adViewContainer: UIView | |
init(providers: [AdProvider]) { | |
self.providers = providers | |
self.adViewContainer = UIView() //Each Ad Provider should make its own view | |
} | |
func beginAdRequest() { | |
//Set up timer if we are refreshable | |
requestAd() | |
} | |
private func requestAd() { | |
for provider in providers { | |
provider.requestAd() | |
} | |
} | |
} | |
class BannerAdRequester: AdRequester { | |
} | |
class AdProvider { | |
private let advice: AdAdvice | |
init(advice: AdAdvice) { | |
self.advice = advice | |
} | |
func requestAd() { | |
} | |
} | |
class DFPAdProvider: AdProvider { | |
} | |
struct AdAdvice { | |
var adLocation: NMAdLocation | |
var lat: Float | |
var long: Float | |
} | |
class SampleViewController: UIViewController, AdRequestListener { | |
private var bannerAdRequester: AdRequester? | |
override func viewDidLoad() { | |
let location = NMAdLocation(categoryId: "0", categoryName: "0", subcategoryId: "Sample", subcategoryName: "Sample") | |
let bannerAdRequester = AdController.sharedInstance.generateAdRequesterWithLocation(location, adType: DAPAdType.MobileBanner) | |
self.bannerAdRequester = bannerAdRequester | |
view.insertSubview(bannerAdRequester.adViewContainer, atIndex: 0) | |
//Hook up adViewContainer into our layout | |
bannerAdRequester.delegate = self | |
bannerAdRequester.beginAdRequest() | |
} | |
func adRequestSucceeded(adType: DAPAdType) { | |
//Move bannerAdRequester.adViewContainer on to the screen | |
} | |
func adRequestFailed(error: NSError) { | |
//Move bannerAdRequester.adViewContainer off of the screen | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment