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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for override user interface style #481

Open
wants to merge 4 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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Change Log
All notable changes to this project will be documented in this file.

## 9.0.6

### Features

* Add `UIView` associated type to `Event`, e.g. `willShow(UIView)` so that event listeners can inspect the view.
* Add `Event.id: String?` property so that event listeners can reason about the view's ID.

## 9.0.5

### Fixes

* #482 Fix timing of `KeyboardTrackingView` callbacks.
* #483 KeyboardTrackingView causes a small space under bottom-style view

## 9.0.4

* #471 Xcode 13 issue - Enum cases with associated values cannot be marked potentially unavailable with '@available'
Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ let package = Package(
.iOS("9.0")
],
products: [
.library(name: "SwiftMessages", targets: ["SwiftMessages"])
.library(name: "SwiftMessages", targets: ["SwiftMessages"]),
.library(name: "SwiftMessages-Dynamic", type: .dynamic, targets: ["SwiftMessages"])
],
targets: [
.target(
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ config.preferredStatusBarStyle = .lightContent

// Specify one or more event listeners to respond to show and hide events.
config.eventListeners.append() { event in
if case .didHide = event { print("yep") }
if case .didHide = event {
print("yep id=\(String(describing: event.id)")
}
}

SwiftMessages.show(config: config, view: view)
Expand Down
2 changes: 1 addition & 1 deletion SwiftMessages.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'SwiftMessages'
spec.version = '9.0.4'
spec.version = '9.0.6'
spec.license = { :type => 'MIT' }
spec.homepage = 'https://github.com/SwiftKickMobile/SwiftMessages'
spec.authors = { 'Timothy Moose' => 'tim@swiftkick.it' }
Expand Down
16 changes: 15 additions & 1 deletion SwiftMessages/KeyboardTrackingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ open class KeyboardTrackingView: UIView {
/// The margin to maintain between the keyboard and the top of the view.
@IBInspectable open var topMargin: CGFloat = 0

/// Subclasses can override this to do something before the change.
open func willChange(
change: KeyboardTrackingView.Change,
userInfo: [AnyHashable : Any]
) {}

/// Subclasses can override this to do something after the change.
open func didChange(
change: KeyboardTrackingView.Change,
userInfo: [AnyHashable : Any]
) {}

override public init(frame: CGRect) {
super.init(frame: frame)
postInit()
Expand Down Expand Up @@ -101,11 +113,12 @@ open class KeyboardTrackingView: UIView {
guard !(isPaused || isAutomaticallyPaused),
let userInfo = (notification as NSNotification).userInfo,
let value = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
willChange(change: change, userInfo: userInfo)
delegate?.keyboardTrackingViewWillChange(change: change, userInfo: userInfo)
let keyboardRect = value.cgRectValue
let thisRect = convert(bounds, to: nil)
let newHeight = max(0, thisRect.maxY - keyboardRect.minY) + topMargin
guard heightConstraint.constant != newHeight else { return }
delegate?.keyboardTrackingViewWillChange(change: change, userInfo: userInfo)
animateKeyboardChange(change: change, height: newHeight, userInfo: userInfo)
}

Expand All @@ -115,6 +128,7 @@ open class KeyboardTrackingView: UIView {
let curveNumber = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber {
CATransaction.begin()
CATransaction.setCompletionBlock {
self.didChange(change: change, userInfo: userInfo)
self.delegate?.keyboardTrackingViewDidChange(change: change, userInfo: userInfo)
}
UIView.beginAnimations(nil, context: nil)
Expand Down
11 changes: 10 additions & 1 deletion SwiftMessages/MaskingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ class MaskingView: PassthroughView {
guard let keyboardTrackingView = keyboardTrackingView,
view != keyboardTrackingView,
view != backgroundView else { return }
keyboardTrackingView.topAnchor.constraint(greaterThanOrEqualTo: view.bottomAnchor).with(priority: UILayoutPriority(250)).isActive = true
let offset: CGFloat
if let adjustable = view as? MarginAdjustable {
offset = -adjustable.bounceAnimationOffset
} else {
offset = 0
}
keyboardTrackingView.topAnchor.constraint(
greaterThanOrEqualTo: view.bottomAnchor,
constant: offset
).with(priority: UILayoutPriority(250)).isActive = true
}
}
8 changes: 4 additions & 4 deletions SwiftMessages/Presenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Presenter: NSObject {
func show(completion: @escaping AnimationCompletion) throws {
try presentationContext = getPresentationContext()
install()
self.config.eventListeners.forEach { $0(.willShow) }
self.config.eventListeners.forEach { $0(.willShow(self.view)) }
showAnimation() { completed in
completion(completed)
if completed {
Expand All @@ -128,7 +128,7 @@ class Presenter: NSObject {
} else {
self.showAccessibilityAnnouncement()
}
self.config.eventListeners.forEach { $0(.didShow) }
self.config.eventListeners.forEach { $0(.didShow(self.view)) }
}
}
}
Expand Down Expand Up @@ -181,15 +181,15 @@ class Presenter: NSObject {

func hide(animated: Bool, completion: @escaping AnimationCompletion) {
isHiding = true
self.config.eventListeners.forEach { $0(.willHide) }
self.config.eventListeners.forEach { $0(.willHide(self.view)) }
let context = animationContext()
let action = {
if let viewController = self.presentationContext.viewControllerValue() as? WindowViewController {
viewController.uninstall()
}
self.maskingView.removeFromSuperview()
completion(true)
self.config.eventListeners.forEach { $0(.didHide) }
self.config.eventListeners.forEach { $0(.didHide(self.view)) }
}
guard animated else {
action()
Expand Down
21 changes: 17 additions & 4 deletions SwiftMessages/SwiftMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,23 @@ open class SwiftMessages {
Specifies events in the message lifecycle.
*/
public enum Event {
case willShow
case didShow
case willHide
case didHide
case willShow(UIView)
case didShow(UIView)
case willHide(UIView)
case didHide(UIView)

public var view: UIView {
switch self {
case .willShow(let view): return view
case .didShow(let view): return view
case .willHide(let view): return view
case .didHide(let view): return view
}
}

public var id: String? {
return (view as? Identifiable)?.id
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion SwiftMessages/WindowViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ open class WindowViewController: UIViewController
window.rootViewController = self
window.windowLevel = config.windowLevel ?? UIWindow.Level.normal
if #available(iOS 13, *) {
window.overrideUserInterfaceStyle = config.overrideUserInterfaceStyle
if window.responds(to: #selector(setter: UIView.overrideUserInterfaceStyle)) {
window.setValue(config.overrideUserInterfaceStyle.rawValue, forKeyPath: #keyPath(UIView.overrideUserInterfaceStyle))
}
}
}

Expand Down