Skip to content

Commit

Permalink
Merge pull request #51 from zenangst/improve/using-view-controllers-w…
Browse files Browse the repository at this point in the history
…ith-nibs

Improve using Vaccine with nibs/xibs
  • Loading branch information
zenangst authored Nov 5, 2018
2 parents 1d97413 + 571cf20 commit ddfca5a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
osx_image: xcode10
osx_image: xcode10.1
language: objective-c

script:
Expand Down
12 changes: 11 additions & 1 deletion Source/Shared/Injection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public class Injection {

static func viewControllerWasInjected(_ viewController: ViewController,
in notification: Notification) -> Bool {

if objectWasInjected(self, in: notification) { return true }
guard let object = object(from: notification) else {
return false
Expand All @@ -214,11 +215,20 @@ public class Injection {
shouldRespondToInjection = object.classForCoder == viewController.classForCoder
}


let objectName = "\(object.classForCoder)".lowercased()

/// Do a dirty match on the class name.
if !shouldRespondToInjection {
shouldRespondToInjection = "\(object.classForCoder)".lowercased().contains("viewcontroller")
shouldRespondToInjection = objectName.contains("viewcontroller")
}

if !shouldRespondToInjection, objectName.contains("cell") {
let allSubviews = viewController.view.subviewsRecursive()
shouldRespondToInjection = !allSubviews.filter({ $0 is TableView || $0 is CollectionView }).isEmpty
}


return shouldRespondToInjection
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Shared/View+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension View {
/// Recursively gather all subviews into a single collection.
///
/// - Returns: A collection of views.
private func subviewsRecursive() -> [View] {
func subviewsRecursive() -> [View] {
return subviews + subviews.flatMap { $0.subviewsRecursive() }
}

Expand Down
19 changes: 0 additions & 19 deletions Source/Shared/ViewController+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,6 @@ extension ViewController {
#endif


#if !os(macOS)
public static func _swizzleViewControllers() {
#if DEBUG
DispatchQueue.once(token: "com.zenangst.Vaccine.\(#function)") {
let originalSelector = #selector(loadView)
let swizzledSelector = #selector(vaccine_loadView)
Swizzling.swizzle(ViewController.self,
originalSelector: originalSelector,
swizzledSelector: swizzledSelector)
}
#endif
}

@objc func vaccine_loadView() {
vaccine_loadView()
Injection.addViewController(self)
}
#else
public static func _swizzleViewControllers() {
#if DEBUG
DispatchQueue.once(token: "com.zenangst.Vaccine.\(#function)") {
Expand All @@ -57,5 +39,4 @@ extension ViewController {
}
self.vaccine_setView(view)
}
#endif
}
37 changes: 32 additions & 5 deletions Source/iOS+tvOS/UIViewController+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ import UIKit
snapshot.mask = maskView
view.window?.addSubview(snapshot)
let oldScrollViews = indexScrollViews()
resetViewControllerState()
rebuildViewContorllerState()

if nibName == nil {
resetViewControllerState()
rebuildViewControllerState()
} else {
rebuildNibViewControllerState()
}

syncOldScrollViews(oldScrollViews, with: indexScrollViews())
UIView.animate(withDuration: 0.25, delay: 0.0, options: options, animations: {
snapshot.alpha = 0.0
Expand All @@ -43,16 +49,37 @@ import UIKit
} else {
let scrollViews = indexScrollViews()
lockScreenUpdates(!scrollViews.isEmpty)
resetViewControllerState()
rebuildViewContorllerState()

if nibName == nil {
resetViewControllerState()
rebuildViewControllerState()
} else {
rebuildNibViewControllerState()
}

unlockScreenUpdates(!scrollViews.isEmpty, scrollViews: scrollViews)
}
}

private func rebuildNibViewControllerState() {
let subviews = view.subviewsRecursive()

for case let tableView as UITableView in subviews {
tableView.reloadData()
}

for case let collectionView as UICollectionView in subviews {
collectionView.reloadData()
}

viewWillAppear(false)
viewDidAppear(false)
}

/// Will invoke `viewDidLoad` to run view controllers setup operations.
/// In addition, it will force all subview to layout and collection & table views
/// to reload. This is to make sure that we are displaying the latest changes.
private func rebuildViewContorllerState() {
private func rebuildViewControllerState() {
viewDidLoad()
view.subviews.forEach { view in
view.setNeedsLayout()
Expand Down
2 changes: 1 addition & 1 deletion Vaccine.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "Vaccine"
s.summary = "Make your apps immune to recompile-disease."
s.version = "0.13.4"
s.version = "0.14.0"
s.homepage = "https://github.com/zenangst/Vaccine"
s.license = 'MIT'
s.author = { "Christoffer Winterkvist" => "christoffer@winterkvist.com" }
Expand Down

0 comments on commit ddfca5a

Please sign in to comment.