Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple SwiftEntryKit instances working in parallel #366

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Source/Infra/EKContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ protocol EntryContentViewDelegate: AnyObject {
func changeToActive(withAttributes attributes: EKAttributes)
func changeToInactive(withAttributes attributes: EKAttributes, pushOut: Bool)
func didFinishDisplaying(entry: EKEntryView, keepWindowActive: Bool, dismissCompletionHandler: SwiftEntryKit.DismissCompletionHandler?)
var safeAreaInsets: UIEdgeInsets { get }
}

class EKContentView: UIView {
Expand Down Expand Up @@ -119,7 +120,7 @@ class EKContentView: UIView {

// Define a spacer to catch top / bottom offsets
var spacerView: UIView!
let safeAreaInsets = EKWindowProvider.safeAreaInsets
let safeAreaInsets = entryDelegate.safeAreaInsets
let overrideSafeArea = attributes.positionConstraints.safeArea.isOverridden

if !overrideSafeArea && safeAreaInsets.hasVerticalInsets && !attributes.position.isCenter {
Expand Down
13 changes: 10 additions & 3 deletions Source/Infra/EKEntryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import UIKit

protocol EntryViewDelegate: AnyObject {
var safeAreaInsets: UIEdgeInsets { get }
}

class EKEntryView: EKStyleView {

struct Content {
Expand All @@ -28,6 +32,8 @@ class EKEntryView: EKStyleView {
}

// MARK: Props

private weak var delegate: EntryViewDelegate!

/** Background view */
private var backgroundView: EKBackgroundView!
Expand All @@ -53,8 +59,9 @@ class EKEntryView: EKStyleView {
}()

// MARK: Setup
init(newEntry content: Content) {
init(newEntry content: Content, delegate: EntryViewDelegate) {
self.content = content
self.delegate = delegate
super.init(frame: UIScreen.main.bounds)
setupContentView()
applyDropShadow()
Expand Down Expand Up @@ -161,9 +168,9 @@ class EKEntryView: EKStyleView {
var bottomInset: CGFloat = 0
switch attributes.position {
case .top:
topInset = -EKWindowProvider.safeAreaInsets.top
topInset = -delegate.safeAreaInsets.top
case .bottom, .center:
bottomInset = EKWindowProvider.safeAreaInsets.bottom
bottomInset = delegate.safeAreaInsets.bottom
}

backgroundView.layoutToSuperview(.top, offset: topInset)
Expand Down
6 changes: 5 additions & 1 deletion Source/Infra/EKRootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import UIKit
protocol EntryPresenterDelegate: AnyObject {
var isResponsiveToTouches: Bool { set get }
func displayPendingEntryOrRollbackWindow(dismissCompletionHandler: SwiftEntryKit.DismissCompletionHandler?)
var safeAreaInsets: UIEdgeInsets { get }
}

class EKRootViewController: UIViewController {
Expand Down Expand Up @@ -213,7 +214,10 @@ extension EKRootViewController {
// MARK: - EntryScrollViewDelegate

extension EKRootViewController: EntryContentViewDelegate {

var safeAreaInsets: UIEdgeInsets {
delegate.safeAreaInsets
}

func didFinishDisplaying(entry: EKEntryView, keepWindowActive: Bool, dismissCompletionHandler: SwiftEntryKit.DismissCompletionHandler?) {
guard !isDisplaying else {
return
Expand Down
4 changes: 2 additions & 2 deletions Source/Infra/EKWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit

class EKWindow: UIWindow {

var isAbleToReceiveTouches = false

init(with rootVC: UIViewController) {
Expand Down Expand Up @@ -37,7 +37,7 @@ class EKWindow: UIWindow {
return super.hitTest(point, with: event)
}

guard let rootVC = EKWindowProvider.shared.rootVC else {
guard let rootVC = rootViewController as? EKRootViewController else {
return nil
}

Expand Down
25 changes: 10 additions & 15 deletions Source/Infra/EKWindowProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@

import UIKit

final class EKWindowProvider: EntryPresenterDelegate {
final class EKWindowProvider: EntryPresenterDelegate, EntryViewDelegate {

/** The artificial safe area insets */
static var safeAreaInsets: UIEdgeInsets {
var safeAreaInsets: UIEdgeInsets {
if #available(iOS 11.0, *) {
return EKWindowProvider.shared.entryWindow?.rootViewController?.view?.safeAreaInsets ?? UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets ?? .zero
return entryWindow?.rootViewController?.view?.safeAreaInsets ?? UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets ?? .zero
} else {
let statusBarMaxY = UIApplication.shared.statusBarFrame.maxY
return UIEdgeInsets(top: statusBarMaxY, left: 0, bottom: 10, right: 0)
}
}

/** Single access point */
static let shared = EKWindowProvider()


/** Current entry window */
var entryWindow: EKWindow!

Expand All @@ -43,7 +40,7 @@ final class EKWindowProvider: EntryPresenterDelegate {
private weak var entryView: EKEntryView!

/** Cannot be instantiated, customized, inherited */
private init() {}
init() {}

var isResponsiveToTouches: Bool {
set {
Expand Down Expand Up @@ -139,13 +136,13 @@ final class EKWindowProvider: EntryPresenterDelegate {

/** Display a view using attributes */
func display(view: UIView, using attributes: EKAttributes, presentInsideKeyWindow: Bool, rollbackWindow: SwiftEntryKit.RollbackWindow) {
let entryView = EKEntryView(newEntry: .init(view: view, attributes: attributes))
let entryView = EKEntryView(newEntry: .init(view: view, attributes: attributes), delegate: self)
display(entryView: entryView, using: attributes, presentInsideKeyWindow: presentInsideKeyWindow, rollbackWindow: rollbackWindow)
}

/** Display a view controller using attributes */
func display(viewController: UIViewController, using attributes: EKAttributes, presentInsideKeyWindow: Bool, rollbackWindow: SwiftEntryKit.RollbackWindow) {
let entryView = EKEntryView(newEntry: .init(viewController: viewController, attributes: attributes))
let entryView = EKEntryView(newEntry: .init(viewController: viewController, attributes: attributes), delegate: self)
display(entryView: entryView, using: attributes, presentInsideKeyWindow: presentInsideKeyWindow, rollbackWindow: rollbackWindow)
}

Expand All @@ -159,12 +156,10 @@ final class EKWindowProvider: EntryPresenterDelegate {
switch rollbackWindow! {
case .main:
if let mainRollbackWindow = mainRollbackWindow {
mainRollbackWindow.makeKeyAndVisible()
} else {
UIApplication.shared.keyWindow?.makeKeyAndVisible()
mainRollbackWindow.becomeKey()
}
case .custom(window: let window):
window.makeKeyAndVisible()
window.becomeKey()
}
}

Expand Down