Skip to content

Commit

Permalink
Fixed alert layering issue. Fixed function scoping. Added background …
Browse files Browse the repository at this point in the history
…observer. Updated docs
  • Loading branch information
ArtSabintsev committed Jan 6, 2019
1 parent 6c8a47f commit cde489c
Show file tree
Hide file tree
Showing 72 changed files with 445 additions and 237 deletions.
6 changes: 3 additions & 3 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
window?.makeKeyAndVisible()

/// - Warning:
/// Siren should ONLY be placed in UIApplication.didFinishLaunchingWithOptionsand only after the `window?.makeKeyAndVisible()` call.
/// Siren should ONLY be placed in UIApplication.didFinishLaunchingWithOptions and only after the `window?.makeKeyAndVisible()` call.
/// Siren initializes a listener on `didBecomeActiveNotification` to perform version checks.

// defaultExample()
defaultExampleUsingCompletionHandler()
// defaultExampleUsingCompletionHandler()
// minimalCustomizationPresentationExample()
// forceLocalizationCustomizationPresentationExample()
// customMessagingPresentationExample()
// annoyingRuleExample()
annoyingRuleExample()
// hyperCriticalRulesExample()
// updateSpecificRulesExample()
// customAlertRulesExample()
Expand Down
2 changes: 1 addition & 1 deletion Siren.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
# Version
s.version = "4.0.0"
s.version = "4.0.1"
s.swift_version = '4.2'

# Meta
Expand Down
17 changes: 12 additions & 5 deletions Sources/Managers/PresentationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public struct PresentationManager {
let updateButtonTitle: String

/// The instance of the `UIAlertController` used to present the update alert.
private var alertController: UIAlertController?
var alertController: UIAlertController?

/// The `UIWindow` instance that presents the `SirenViewController`.
private var updaterWindow: UIWindow {
Expand Down Expand Up @@ -134,11 +134,12 @@ extension PresentationManager {
handler?(.unknown)
}

// If the alertType is .none, an alert will not be presneted.
// If the alertType is .none, an alert will not be presented.
// If the `updaterWindow` is not hidden, than an alert is already presented.
// The latter prevents `UIAlertControllers` from appearing on top of each other.
if rules.alertType != .none && updaterWindow.isHidden {
alertController?.show(window: updaterWindow)

}
}

Expand All @@ -156,7 +157,7 @@ extension PresentationManager {
}

let action = UIAlertAction(title: title, style: .default) { _ in
self.alertController?.hide(window: self.updaterWindow)
self.cleanUpAlertController()
Siren.shared.launchAppStore()

handler?(.appStore)
Expand All @@ -180,7 +181,7 @@ extension PresentationManager {
}

let action = UIAlertAction(title: title, style: .default) { _ in
self.alertController?.hide(window: self.updaterWindow)
self.cleanUpAlertController()
UserDefaults.shouldPerformVersionCheckOnSubsequentLaunch = true

handler?(.nextTime)
Expand Down Expand Up @@ -208,12 +209,18 @@ extension PresentationManager {
UserDefaults.storedSkippedVersion = currentAppStoreVersion
UserDefaults.standard.synchronize()

self.alertController?.hide(window: self.updaterWindow)
self.cleanUpAlertController()

handler?(.skip)
return
}

return action
}

/// Removes the `alertController` from memory.
private func cleanUpAlertController() {
alertController?.hide(window: self.updaterWindow)
alertController?.dismiss(animated: false, completion: nil)
}
}
45 changes: 33 additions & 12 deletions Sources/Siren.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public final class Siren: NSObject {
/// The Siren singleton. The main point of entry to the Siren library.
public static let shared = Siren()

/// The debug flag, which is disabled by default.
/// When enabled, a stream of `print()` statements are logged to your console when a version check is performed.
public lazy var debugEnabled: Bool = false

/// The manager that controls the App Store API that is
/// used to fetch the latest version of the app.
///
Expand Down Expand Up @@ -47,6 +43,9 @@ public final class Siren: NSObject {
/// The retained `NotificationCenter` observer that listens for `UIApplication.didBecomeActiveNotification` notifications.
var didBecomeActiveObserver: NSObjectProtocol?

/// The retained `NotificationCenter` observer that listens for `UIApplication.didEnterBackgroundNotification` notifications.
var didEnterBackgroundObserver: NSObjectProtocol?

/// The last date that an alert was presented to the user.
private var alertPresentationDate: Date?

Expand All @@ -62,12 +61,12 @@ public final class Siren: NSObject {
}
}

// MARK: - Public Functionality
// MARK: - Public API Interface

public extension Siren {
/// This method executes the Siren version checking and alert presentation flow.
///
///
/// - Parameter handler:
/// - Parameter handler: Returns the metadata around a successful version check and interaction with the update modal or it returns nil.
func wail(completion handler: ResultsHandler? = nil) {
resultsHandler = handler
addObservers()
Expand All @@ -94,10 +93,10 @@ public extension Siren {
}
}

// MARK: - Private Functionality
// MARK: - Version Check and Alert Presentation Flow

extension Siren {
/// Initiates the uni-directional version checking flow.
private extension Siren {
/// Initiates the unidirectional version checking flow.
func performVersionCheck() {
alertPresentationDate = UserDefaults.alertPresentationDate
apiManager.performVersionCheckRequest { [weak self] (lookupModel, error) in
Expand Down Expand Up @@ -221,10 +220,19 @@ extension Siren {
self.resultsHandler?(results, nil)
}
}
}

/// Add an observer that listens for app launching/relaunching
/// (e.g., calls to `UIApplication`'s `didBecomeActive` function).
// MARK: - Observers

private extension Siren {
/// Add app state observers
func addObservers() {
addForegroundObserver()
addBackgroundObserver()
}

/// Adds an observer that listens for app launching/relaunching.
func addForegroundObserver() {
guard didBecomeActiveObserver == nil else { return }
didBecomeActiveObserver = NotificationCenter
.default
Expand All @@ -235,4 +243,17 @@ extension Siren {
self.performVersionCheck()
}
}

/// Adds an observer that listens for when the app is sent to the background.
func addBackgroundObserver() {
guard didEnterBackgroundObserver == nil else { return }
didEnterBackgroundObserver = NotificationCenter
.default
.addObserver(forName: UIApplication.didEnterBackgroundNotification,
object: nil,
queue: nil) { [weak self] _ in
guard let self = self else { return }
self.presentationManager.alertController?.dismiss(animated: true, completion: nil)
}
}
}
2 changes: 1 addition & 1 deletion docs/Classes.html
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2018 <a class="link" href="https://github.com/ArtSabintsev/Siren" target="_blank" rel="external">Arthur Ariel Sabintsev</a>. All rights reserved. (Last updated: 2018-12-26)</p>
<p>&copy; 2019 <a class="link" href="https://github.com/ArtSabintsev/Siren" target="_blank" rel="external">Arthur Ariel Sabintsev</a>. All rights reserved. (Last updated: 2019-01-05)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.4</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
Loading

0 comments on commit cde489c

Please sign in to comment.