Skip to content

Commit

Permalink
Clean up code (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Inder Dhir authored Jun 25, 2021
1 parent 6d89e48 commit 703bf3f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 84 deletions.
38 changes: 17 additions & 21 deletions package/Runtime/Plugins/iOS/AdView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,22 @@ class AdView: UIView {
parentView.addSubview(self)

if #available(iOS 11.0, *) {
self.translatesAutoresizingMaskIntoConstraints = false

var constraints = [self.widthAnchor.constraint(equalToConstant: self.frame.width),
self.heightAnchor.constraint(equalToConstant: self.frame.height)]

constraints.append(contentsOf: [self.bottomAnchor .constraint(equalTo: parentView.safeAreaLayoutGuide.bottomAnchor)])
constraints.append(contentsOf: [self.centerXAnchor .constraint(equalTo: parentView.safeAreaLayoutGuide.centerXAnchor)])

NSLayoutConstraint.activate(constraints)
translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
widthAnchor.constraint(equalToConstant: frame.width),
heightAnchor.constraint(equalToConstant: frame.height),
bottomAnchor.constraint(equalTo: parentView.safeAreaLayoutGuide.bottomAnchor),
centerXAnchor.constraint(equalTo: parentView.safeAreaLayoutGuide.centerXAnchor)
])
} else {
var rect = self.frame

let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height

rect.origin.x = screenWidth / 2 - rect.size.width / 2
rect.origin.y = screenHeight - rect.size.height
self.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin]
self.frame = rect
let ogFrame = frame
autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin]
frame = CGRect(
x: UIScreen.main.bounds.size.width / 2 - ogFrame.size.width / 2,
y: UIScreen.main.bounds.size.height - ogFrame.size.height,
width: ogFrame.width,
height: ogFrame.height
)
}
}

}
}
}
40 changes: 20 additions & 20 deletions package/Runtime/Plugins/iOS/NimbusBinding.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,43 @@ void _initializeSDKWithPublisher(const char* publisher,
const char* apikey,
bool enableSDKInTestMode,
bool enableUnityLogs) {
NSString* publishString = GetStringParam(publisher);
NSString* apiKeyString = GetStringParam(apikey);

[NimbusManager initializeNimbusSDKWithPublisher: publishString
apiKey: apiKeyString
[NimbusManager initializeNimbusSDKWithPublisher: GetStringParam(publisher)
apiKey: GetStringParam(apikey)
enableSDKInTestMode: enableSDKInTestMode
enableUnityLogs: enableUnityLogs];
}

void _showBannerAd(int adUnitInstanceId, const char* position, float bannerFloor) {
NSString* positionString = GetStringParam(position);
[[NimbusManager nimbusManagerForAdUnityInstanceId:adUnitInstanceId] showBannerAdWithPosition:positionString
bannerFloor:bannerFloor];
[
[NimbusManager nimbusManagerForAdUnityInstanceId:adUnitInstanceId]
showBannerAdWithPosition:GetStringParam(position) bannerFloor:bannerFloor
];
}

void _showInterstitialAd(int adUnitInstanceId, const char* position, float bannerFloor, float videoFloor, double closeButtonDelay) {
NSString* positionString = GetStringParam(position);
[[NimbusManager nimbusManagerForAdUnityInstanceId:adUnitInstanceId] showInterstitialAdWithPosition:positionString
bannerFloor:bannerFloor
videoFloor:videoFloor
closeButtonDelay:closeButtonDelay];
[
[NimbusManager nimbusManagerForAdUnityInstanceId:adUnitInstanceId]
showInterstitialAdWithPosition:GetStringParam(position)
bannerFloor:bannerFloor
videoFloor:videoFloor
closeButtonDelay:closeButtonDelay
];
}

void _showRewardedVideoAd(int adUnitInstanceId, const char* position, float videoFloor, double closeButtonDelay) {
NSString* positionString = GetStringParam(position);
[[NimbusManager nimbusManagerForAdUnityInstanceId:adUnitInstanceId] showRewardedVideoAdWithPosition:positionString
videoFloor:videoFloor
closeButtonDelay:closeButtonDelay];
[
[NimbusManager nimbusManagerForAdUnityInstanceId:adUnitInstanceId]
showRewardedVideoAdWithPosition:GetStringParam(position)
videoFloor:videoFloor
closeButtonDelay:closeButtonDelay
];
}

void _setGDPRConsentString(const char* consent) {
NSString* consentString = GetStringParam(consent);
[NimbusManager setGDPRConsentStringWithConsent: consentString];
[NimbusManager setGDPRConsentStringWithConsent: GetStringParam(consent)];
}

void _destroyAd(int adUnitInstanceId) {
[[NimbusManager nimbusManagerForAdUnityInstanceId:adUnitInstanceId] destroyExistingAd];
}

}
81 changes: 38 additions & 43 deletions package/Runtime/Plugins/iOS/NimbusManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@ import NimbusKit
private static var managerDictionary: [Int: NimbusManager] = [:]

private let adUnitInstanceId: Int

private var nimbusAdManager: NimbusAdManager?
private var adController: AdController?

private var adView: AdView?

// MARK: - Class Functions

@objc public class func initializeNimbusSDK(publisher: String,
apiKey: String,
enableSDKInTestMode: Bool,
enableUnityLogs: Bool) {
@objc public class func initializeNimbusSDK(
publisher: String,
apiKey: String,
enableSDKInTestMode: Bool,
enableUnityLogs: Bool
) {
Nimbus.shared.initialize(publisher: publisher, apiKey: apiKey)

Nimbus.shared.logLevel = enableUnityLogs ? .debug : .off
Nimbus.shared.testMode = enableSDKInTestMode

Nimbus.shared.renderers = [
.forAuctionType(.static): NimbusStaticAdRenderer(),
.forAuctionType(.video): NimbusVideoAdRenderer()
Expand All @@ -50,7 +48,7 @@ import NimbusKit
@objc public class func setGDPRConsentString(consent: String) {
var user = NimbusRequestManager.user ?? NimbusUser()
user.configureGdprConsent(didConsent: true, consentString: consent)
NimbusRequestManager.user = user
NimbusAdManager.user = user
}

// MARK: - Private Functions
Expand All @@ -60,7 +58,7 @@ import NimbusKit
}

private func unityViewController() -> UIViewController? {
return UIApplication.shared.windows.first { $0.isKeyWindow }?.rootViewController
UIApplication.shared.windows.first { $0.isKeyWindow }?.rootViewController
}

// MARK: - Public Functions
Expand All @@ -70,22 +68,24 @@ import NimbusKit

let adFormat = NimbusAdFormat.banner320x50
let adPosition = NimbusPosition.footer

let request = NimbusRequest.forBannerAd(position: position,
format: adFormat,
adPosition: adPosition)
let request = NimbusRequest.forBannerAd(
position: position,
format: adFormat,
adPosition: adPosition
)
request.impressions[0].bidFloor = bannerFloor

let view = AdView(bannerFormat: adFormat)
self.adView = view

adView = view
view.attachToView(parentView: viewController.view, position: adPosition)

nimbusAdManager = NimbusAdManager()
nimbusAdManager?.delegate = self
nimbusAdManager?.showAd(request: request,
container: view,
adPresentingViewController: viewController)
nimbusAdManager?.showAd(
request: request,
container: view,
adPresentingViewController: viewController
)
}

@objc public func showInterstitialAd(position: String, bannerFloor: Float, videoFloor: Float, closeButtonDelay: Double) {
Expand All @@ -95,7 +95,8 @@ import NimbusKit
request.impressions[0].banner?.bidFloor = bannerFloor
request.impressions[0].video?.bidFloor = videoFloor

let adFormat = UIDevice.current.orientation.isLandscape ? NimbusAdFormat.interstitialLandscape : NimbusAdFormat.interstitialPortrait
let adFormat = UIDevice.current.orientation.isLandscape ?
NimbusAdFormat.interstitialLandscape : NimbusAdFormat.interstitialPortrait
let banner = NimbusBanner(
width: adFormat.width,
height: adFormat.height,
Expand All @@ -106,29 +107,29 @@ import NimbusKit
var impression = request.impressions[0]
impression.video?.companionAds = [banner]
request.impressions[0] = impression

(Nimbus.shared.renderers[.forAuctionType(.video)] as? NimbusVideoAdRenderer)?.showMuteButton = false // false by default


nimbusAdManager = NimbusAdManager()
nimbusAdManager?.delegate = self
nimbusAdManager?.showBlockingAd(request: request,
closeButtonDelay: closeButtonDelay,
adPresentingViewController: viewController)
nimbusAdManager?.showBlockingAd(
request: request,
closeButtonDelay: closeButtonDelay,
adPresentingViewController: viewController
)
}

@objc public func showRewardedVideoAd(position: String, videoFloor: Float, closeButtonDelay: Double) {
guard let viewController = unityViewController() else { return }

let request = NimbusRequest.forVideoAd(position: position)
request.impressions[0].video?.bidFloor = videoFloor

(Nimbus.shared.renderers[.forAuctionType(.video)] as? NimbusVideoAdRenderer)?.showMuteButton = false // false by default


nimbusAdManager = NimbusAdManager()
nimbusAdManager?.delegate = self
nimbusAdManager?.showRewardedAd(request: request,
closeButtonDelay: closeButtonDelay,
adPresentingViewController: viewController)
nimbusAdManager?.showRewardedAd(
request: request,
closeButtonDelay: closeButtonDelay,
adPresentingViewController: viewController
)
}

@objc public func destroyExistingAd() {
Expand Down Expand Up @@ -157,7 +158,6 @@ extension NimbusManager: NimbusAdManagerDelegate {
"network": ad.network,
"placementId": ad.placementId ?? ""
]

UnityBinding.sendMessage(methodName: "OnAdResponse", params: params)
}

Expand All @@ -166,7 +166,6 @@ extension NimbusManager: NimbusAdManagerDelegate {
"adUnitInstanceID": adUnitInstanceId,
"errorMessage": error.localizedDescription
]

UnityBinding.sendMessage(methodName: "OnError", params: params)
destroyExistingAd()
}
Expand All @@ -175,10 +174,7 @@ extension NimbusManager: NimbusAdManagerDelegate {
self.adController = controller
self.adController?.delegate = self

let params: [String: Any] = [
"adUnitInstanceID": adUnitInstanceId
]

let params: [String: Any] = ["adUnitInstanceID": adUnitInstanceId]
UnityBinding.sendMessage(methodName: "OnAdResponse", params: params)
}

Expand All @@ -189,9 +185,9 @@ extension NimbusManager: NimbusAdManagerDelegate {
extension NimbusManager: AdControllerDelegate {

public func didReceiveNimbusEvent(controller: AdController, event: NimbusEvent) {
var method = "OnAdEvent", eventName = ""
let eventName: String
switch event {
case .loaded, .loadedCompanionAd(width: _, height: _), .firstQuartile, .midpoint, .thirdQuartile:
case .loaded, .loadedCompanionAd, .firstQuartile, .midpoint, .thirdQuartile:
return // Unity doesn't handle these events
case .impression:
eventName = "IMPRESSION"
Expand All @@ -210,12 +206,12 @@ extension NimbusManager: AdControllerDelegate {
print("Ad Event not sent: \(event)")
return
}

let params: [String: Any] = [
"adUnitInstanceID": adUnitInstanceId,
"eventName": eventName
]
UnityBinding.sendMessage(methodName: method, params: params)
UnityBinding.sendMessage(methodName: "OnAdEvent", params: params)
}

/// Received an error for the ad
Expand All @@ -224,7 +220,6 @@ extension NimbusManager: AdControllerDelegate {
"adUnitInstanceID": adUnitInstanceId,
"errorMessage": error.localizedDescription
]

UnityBinding.sendMessage(methodName: "OnError", params: params)
destroyExistingAd()
}
Expand Down

0 comments on commit 703bf3f

Please sign in to comment.