From 49437a01f432255a326999a0bbec0755e47f4849 Mon Sep 17 00:00:00 2001 From: "Ali A. Hilal" Date: Fri, 18 Jun 2021 19:03:21 +0300 Subject: [PATCH] [fix] Fix The startup time interval being ignored --- HeraSDK.podspec | 2 +- Sources/Ads/Banner.swift | 2 +- Sources/Ads/HeraAd.swift | 2 +- Sources/Ads/Interstitial.swift | 2 +- Sources/Core/Hera.swift | 8 +++-- Sources/Core/HeraError.swift | 54 +++++++++++++++++----------------- 6 files changed, 36 insertions(+), 34 deletions(-) diff --git a/HeraSDK.podspec b/HeraSDK.podspec index f9a471c..fab8fbd 100644 --- a/HeraSDK.podspec +++ b/HeraSDK.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'HeraSDK' - s.version = '2.0.1' + s.version = '2.0.2' s.summary = 'An abstraction layer used to manage Ads.' s.description = <<-DESC diff --git a/Sources/Ads/Banner.swift b/Sources/Ads/Banner.swift index e9c7398..ae4b660 100644 --- a/Sources/Ads/Banner.swift +++ b/Sources/Ads/Banner.swift @@ -14,5 +14,5 @@ final class Banner: HeraAd { // NOTE: The banner always refreshes itself based on specific // time intervals so we need to take this into considration. - var lastShowingDate = Date() + var lastShowingDate: Date? } diff --git a/Sources/Ads/HeraAd.swift b/Sources/Ads/HeraAd.swift index 6a9b942..7c1e0b8 100644 --- a/Sources/Ads/HeraAd.swift +++ b/Sources/Ads/HeraAd.swift @@ -17,5 +17,5 @@ protocol HeraAd { var lastLoadingDate: Date? { get set } /// The date represents when the ad is last showed. - var lastShowingDate: Date { get set } + var lastShowingDate: Date? { get set } } diff --git a/Sources/Ads/Interstitial.swift b/Sources/Ads/Interstitial.swift index f827abd..1b73b8b 100644 --- a/Sources/Ads/Interstitial.swift +++ b/Sources/Ads/Interstitial.swift @@ -11,5 +11,5 @@ import Foundation final class Interstitial: HeraAd { var state: AdState = .hidden var lastLoadingDate: Date? - var lastShowingDate = Date() + var lastShowingDate: Date? } diff --git a/Sources/Core/Hera.swift b/Sources/Core/Hera.swift index 428ecca..4438fcd 100644 --- a/Sources/Core/Hera.swift +++ b/Sources/Core/Hera.swift @@ -160,7 +160,8 @@ public final class Hera { Logger.log(.debug, "Will show ad of type \(type) and action \(action)") guard let adType = config.actions[action]?.type, type == adType else { - self.notifiyObserver { $0?.heraDidFailToShowAd(for: action, adType: type, error: HeraError.actionDoesNotMatch) } + let avaliableActions = config.actions.map({ $0.key }).joined(separator: ", ") + self.notifiyObserver { $0?.heraDidFailToShowAd(for: action, adType: type, error: HeraError.actionDoesNotMatch(action: action, availableActions: avaliableActions)) } return } @@ -306,7 +307,7 @@ private extension Hera { throw HeraError.outOfTimeInterval } - let date = adContainer.ad(ofType: type).lastShowingDate + guard let date = adContainer.ad(ofType: type).lastShowingDate else { return } if Date().timeIntervalSince(date) <= config.adInterval { throw HeraError.betweenTwoAdsInterval } @@ -322,7 +323,8 @@ private extension Hera { } guard let adId = config.actions[action]?.unitID else { - throw HeraError.actionDoesNotMatch + let avaliableActions = config.actions.map({ $0.key }).joined(separator: ", ") + throw HeraError.actionDoesNotMatch(action: action, availableActions: avaliableActions) } try self.checkTimeInterval(from: config, andAction: action, type: adType) diff --git a/Sources/Core/HeraError.swift b/Sources/Core/HeraError.swift index e28a50a..e0aa0ba 100644 --- a/Sources/Core/HeraError.swift +++ b/Sources/Core/HeraError.swift @@ -9,58 +9,58 @@ import Foundation /// A type encloses all the possible errors thrown by the SDK. public enum HeraError: Swift.Error { - case falidToLoad + case falidToLoad(action: String) case invalidData - case outOfTimeInterval + case outOfTimeInterval case betweenTwoAdsInterval - case actionDoesNotMatch - case anotherOperationInProgress - case notCongiguredProperly + case actionDoesNotMatch(action: String, availableActions: String) + case anotherOperationInProgress + case notCongiguredProperly case wrongAppID - case anotherAdIsBeingShown + case anotherAdIsBeingShown case wrongPresenterType - case viewDoesNotHaveVisibleUIWindow + case viewDoesNotHaveVisibleUIWindow case nilBanner } extension HeraError: CustomStringConvertible, LocalizedError { - public var description: String { - switch self { - case .falidToLoad: - return "Failed to load the add" + public var description: String { + switch self { + case .falidToLoad(let action): + return "Failed to load the add for action: \(action)" case .invalidData: - return "Invalid data, can;t be decoded!" + return "Invalid data, can't be decoded!" - case .outOfTimeInterval: - return "Cant show the add at the moment because the time interval since startup is less than the configured." + case .outOfTimeInterval: + return "Cant show the add at the moment because the time interval since the app startup is less than the configured. Please wait" - case .actionDoesNotMatch: - return "The provided action doesn't any of the fetched actions. Please make sure you provided the right action name (case-sensetive)." + case .actionDoesNotMatch(let action, let fethcedActions): + return "The provided action: (\(action) doesn't any of the fetched actions \(fethcedActions). Please make sure you provided the right action name (case-sensetive)." - case .anotherOperationInProgress: + case .anotherOperationInProgress: return "Trying to load ad while another load operation is in progress. Please wait!" - case .notCongiguredProperly: return "The Manager is misconfigured, make sure you have called both initialize(apiKey: environment:) and setUserProperties(_:) with the right parameters consequently." + case .notCongiguredProperly: return "The fetched configs are invalid." case .betweenTwoAdsInterval: return "Cant show the add at the moment because the time interval since the last shown ad is less than the configured." case .wrongAppID: return "The provider ID is incorrect." - - case .anotherAdIsBeingShown: - return "Can show add for now because another ad is being shown 🔫" + + case .anotherAdIsBeingShown: + return "Can show add for now because another ad is being shown 🔫" case .wrongPresenterType: return "You are either trying to present interstial on a UIView or presenting banner on UIViewController. Make sure you have used the right presenter type" - case .viewDoesNotHaveVisibleUIWindow: - return "View does not have visible UIWindow" + case .viewDoesNotHaveVisibleUIWindow: + return "View does not have visible UIWindow" case .nilBanner: return "Banner is deallocated. Please load the banner before trying to show it." } - } + } - public var errorDescription: String? { - description - } + public var errorDescription: String? { + description + } }