145 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Swift
		
	
	
		
		
			
		
	
	
			145 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Swift
		
	
	
|  | // InterstitialAdEngine.swift | ||
|  | // Base class for interstitial ad engines | ||
|  | // Corresponds to InterstitialAdEngine.kt in Android implementation | ||
|  | 
 | ||
|  | import Foundation | ||
|  | import UIKit | ||
|  | 
 | ||
|  | public class InterstitialAdEngine: RelayAdEngine { | ||
|  |      | ||
|  |     private(set) var idleState: Idle! | ||
|  |     private(set) var loadingState: Loading! | ||
|  |     private(set) var loadedState: Loaded! | ||
|  |     private(set) var showingState: Showing! | ||
|  |      | ||
|  |     public var currentStateIdentifier: AdState.Identifier { | ||
|  |         return (currentState as? AdState)?.stateId ?? AdState.Identifier(name: currentState?.name ?? "unknown") | ||
|  |     } | ||
|  |      | ||
|  |     public override init(viewController: UIViewController, id: Int, adType: AdType, strategyName: String) { | ||
|  |         super.init(viewController: viewController, id: id, adType: .Interstitial, strategyName: strategyName) | ||
|  |     } | ||
|  |      | ||
|  |     public func initialize() { | ||
|  |         idleState = createIdleState() | ||
|  |         loadingState = createLoadingState() | ||
|  |         loadedState = createLoadedState() | ||
|  |         showingState = createShowingState() | ||
|  |          | ||
|  |         addState(idleState) | ||
|  |         addState(loadingState) | ||
|  |         addState(loadedState) | ||
|  |         addState(showingState) | ||
|  |         setInitialState(idleState) | ||
|  |         start() | ||
|  |     } | ||
|  |      | ||
|  |     // State classes | ||
|  |     public class AdState: State { | ||
|  |         public let stateId: Identifier | ||
|  |          | ||
|  |         public init(name: String, stateId: Identifier) { | ||
|  |             self.stateId = stateId | ||
|  |             super.init() | ||
|  |         } | ||
|  |          | ||
|  |         public override var name: String { | ||
|  |             return stateId.name | ||
|  |         } | ||
|  |          | ||
|  |         public class Identifier: Equatable { | ||
|  |             public let name: String | ||
|  |              | ||
|  |             public init(name: String) { | ||
|  |                 self.name = name | ||
|  |             } | ||
|  |              | ||
|  |             // Pre-defined identifiers | ||
|  |             public static let IDLE = Identifier(name: "idle") | ||
|  |             public static let LOADING = Identifier(name: "loading") | ||
|  |             public static let LOADED = Identifier(name: "loaded") | ||
|  |             public static let SHOWING = Identifier(name: "showing") | ||
|  |              | ||
|  |             public static func == (lhs: Identifier, rhs: Identifier) -> Bool { | ||
|  |                 return lhs.name == rhs.name | ||
|  |             } | ||
|  |         } | ||
|  |     } | ||
|  |      | ||
|  |     public class Idle: AdState { | ||
|  |         public init() { | ||
|  |             super.init(name: "[IDLE]", stateId: Identifier.IDLE) | ||
|  |         } | ||
|  |     } | ||
|  |      | ||
|  |     public class Loading: AdState { | ||
|  |         public init() { | ||
|  |             super.init(name: "[LOADING]", stateId: Identifier.LOADING) | ||
|  |         } | ||
|  |     } | ||
|  |      | ||
|  |     public class Loaded: AdState { | ||
|  |         public init() { | ||
|  |             super.init(name: "[LOADED]", stateId: Identifier.LOADED) | ||
|  |         } | ||
|  |     } | ||
|  |      | ||
|  |     public class Showing: AdState { | ||
|  |         public init() { | ||
|  |             super.init(name: "[SHOWING]", stateId: Identifier.SHOWING) | ||
|  |         } | ||
|  |     } | ||
|  |      | ||
|  |     // Factory methods for creating the states | ||
|  |     open func createIdleState() -> Idle { | ||
|  |         return Idle() | ||
|  |     } | ||
|  |      | ||
|  |     open func createLoadingState() -> Loading { | ||
|  |         return Loading() | ||
|  |     } | ||
|  |      | ||
|  |     open func createLoadedState() -> Loaded { | ||
|  |         return Loaded() | ||
|  |     } | ||
|  |      | ||
|  |     open func createShowingState() -> Showing { | ||
|  |         return Showing() | ||
|  |     } | ||
|  |      | ||
|  |     // Abstract methods to be implemented by subclasses | ||
|  |     internal func requestLoad() { | ||
|  |         fatalError("Subclass must implement") | ||
|  |     } | ||
|  |      | ||
|  |     internal func requestShow(request: InterstitialShowRequest?) { | ||
|  |         fatalError("Subclass must implement") | ||
|  |     } | ||
|  |      | ||
|  |     internal func requestDestroy() { | ||
|  |         fatalError("Subclass must implement") | ||
|  |     } | ||
|  |      | ||
|  |     // Public API methods | ||
|  |     public func show(_ request: InterstitialShowRequest? = nil) -> Bool { | ||
|  |         if isCurrent(loadedState) { | ||
|  |             requestShow(request: request) | ||
|  |             return true | ||
|  |         } | ||
|  |         return false | ||
|  |     } | ||
|  |      | ||
|  |     public func load() -> Bool { | ||
|  |         if isCurrent(idleState) { | ||
|  |             requestLoad() | ||
|  |             return true | ||
|  |         } | ||
|  |         return false | ||
|  |     } | ||
|  |      | ||
|  |     public func destroy() -> Bool { | ||
|  |         requestDestroy() | ||
|  |         return true | ||
|  |     } | ||
|  | } |