Skip to content

Commit

Permalink
Add UIWindow extensions closes #477 (#494)
Browse files Browse the repository at this point in the history
* #closes #477

* Fix tests
  • Loading branch information
omaralbeik authored and guykogus committed Jun 5, 2018
1 parent faf7e4e commit 20ca06c
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 6 deletions.
8 changes: 2 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S
### Added
- **SKNode**:
- Added `descendants` method to get an array of all descendants of an SKNode. [#490](https://github.com/SwifterSwift/SwifterSwift/pull/490) by [oliviabrown9](https://github.com/oliviabrown9).

- **Comparable**:
- Added `isBetween(min:max:)` and `clamped(min:max:)` to confirm a value is between bounds or limit it between bounds. [#466](https://github.com/SwifterSwift/SwifterSwift/pull/466) by [freak4pc](https://github.com/freak4pc).

- **UIScrollView**:
- Added `snapshot` method to get a full snapshot of a rendered scroll view. [#457](https://github.com/SwifterSwift/SwifterSwift/pull/457) by [aliamcami](https://github.com/aliamcami).

- **UIGestureRecognizer**:
- Added `removeFromView()` method to remove recognizer from the view the recognizer is attached to. [#456](https://github.com/SwifterSwift/SwifterSwift/pull/456) by [mmdock](https://github.com/mmdock)

- **Character**:
- Added `randomAlphanumeric()` method to generate a random alphanumeric Character. [#462](https://github.com/SwifterSwift/SwifterSwift/pull/462) by [oliviabrown9](https://github.com/oliviabrown9)

- **UITextView**:
- Added `wrapToContent()` method which will remove insets, offsets, paddings which lies within UITextView's `bounds` and `contenSize`. [#458](https://github.com/SwifterSwift/SwifterSwift/pull/458) by [ratulSharker](https://github.com/ratulSharker)

- **URL**
- Added `deletingAllPathComponents()` and `deleteAllPathComponents()` to delete all path components from a URL. [#441](https://github.com/SwifterSwift/SwifterSwift/pull/441) by [setoelkahfi](https://github.com/setoelkahfi).
- Added `queryValue(for:)` to get the value of a query key from a URL. [#467](https://github.com/SwifterSwift/SwifterSwift/pull/467) by [jdisho](https://github.com/jdisho).
Expand All @@ -32,6 +26,8 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S

- **Optional**:
- Added `isNilOrEmpty` property to check whether an optional is nil or empty collection.
- **UIWindow**:
- Added `switchRootViewController` method to switch root view controller with animation. [#494](https://github.com/SwifterSwift/SwifterSwift/pull/494) by [omaralbeik](https://github.com/omaralbeik).

- **Sequence**
- added `containsDuplicates()` to check whether a sequence contains duplicates. [#496](https://github.com/SwifterSwift/SwifterSwift/pull/496) by [@vyax](https://github.com/vyax).
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ let package = Package(
<li><a href="https://github.com/SwifterSwift/SwifterSwift/tree/master/Sources/Extensions/UIKit/UITextViewExtensions.swift"><code>UITextView extensions</code></a></li>
<li><a href="https://github.com/SwifterSwift/SwifterSwift/tree/master/Sources/Extensions/UIKit/UIViewControllerExtensions.swift"><code>UIViewController extensions</code></a></li>
<li><a href="https://github.com/SwifterSwift/SwifterSwift/tree/master/Sources/Extensions/UIKit/UIViewExtensions.swift"><code>UIView extensions</code></a></li>
<li><a href="https://github.com/SwifterSwift/SwifterSwift/tree/master/Sources/Extensions/UIKit/UIWindowExtensions.swift"><code>UIWindow extensions</code></a></li>
</ul>
</details>

Expand Down
48 changes: 48 additions & 0 deletions Sources/Extensions/UIKit/UIWindowExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// UIWindowExtensions.swift
// SwifterSwift
//
// Created by Omar Albeik on 6/2/18.
// Copyright © 2018 SwifterSwift
//

#if canImport(UIKit)
import UIKit

#if os(iOS)
public extension UIWindow {

/// SwifterSwift: Switch current root view controller with a new view controller.
///
/// - Parameters:
/// - viewController: new view controller.
/// - animated: set to true to animate view controller change (default is true).
/// - duration: animation duration in seconds (default is 0.5).
/// - options: animataion options (default is .transitionFlipFromRight).
/// - completion: optional completion handler called after view controller is changed.
public func switchRootViewController(
to viewController: UIViewController,
animated: Bool = true,
duration: TimeInterval = 0.5,
options: UIViewAnimationOptions = .transitionFlipFromRight,
_ completion: (() -> Void)? = nil) {

guard animated else {
rootViewController = viewController
completion?()
return
}

UIView.transition(with: self, duration: duration, options: options, animations: {
let oldState = UIView.areAnimationsEnabled
UIView.setAnimationsEnabled(false)
self.rootViewController = viewController
UIView.setAnimationsEnabled(oldState)
}, completion: { _ in
completion?()
})
}

}
#endif
#endif
8 changes: 8 additions & 0 deletions SwifterSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
071306701FB597920023A9D8 /* FoundationDeprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0713066E1FB597920023A9D8 /* FoundationDeprecated.swift */; };
071306711FB597920023A9D8 /* FoundationDeprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0713066E1FB597920023A9D8 /* FoundationDeprecated.swift */; };
071306721FB597920023A9D8 /* FoundationDeprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0713066E1FB597920023A9D8 /* FoundationDeprecated.swift */; };
0722CB3820C2E46C00C6C1B6 /* UIWindowExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0722CB3720C2E46B00C6C1B6 /* UIWindowExtensions.swift */; };
0722CB3A20C2E49A00C6C1B6 /* UIWindowExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0722CB3920C2E49900C6C1B6 /* UIWindowExtensionsTests.swift */; };
0726D7771F7C199E0028CAB5 /* ColorExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0726D7761F7C199E0028CAB5 /* ColorExtensions.swift */; };
0726D77A1F7C24830028CAB5 /* ColorExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0726D7761F7C199E0028CAB5 /* ColorExtensions.swift */; };
0726D77B1F7C24840028CAB5 /* ColorExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0726D7761F7C199E0028CAB5 /* ColorExtensions.swift */; };
Expand Down Expand Up @@ -433,6 +435,8 @@

/* Begin PBXFileReference section */
0713066E1FB597920023A9D8 /* FoundationDeprecated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoundationDeprecated.swift; sourceTree = "<group>"; };
0722CB3720C2E46B00C6C1B6 /* UIWindowExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIWindowExtensions.swift; sourceTree = "<group>"; };
0722CB3920C2E49900C6C1B6 /* UIWindowExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIWindowExtensionsTests.swift; sourceTree = "<group>"; };
0726D7761F7C199E0028CAB5 /* ColorExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorExtensions.swift; sourceTree = "<group>"; };
074EAF1A1F7BA68B00C74636 /* UIFontExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIFontExtensions.swift; sourceTree = "<group>"; };
074EAF1E1F7BA74600C74636 /* UIFontExtensionsTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIFontExtensionsTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -814,6 +818,7 @@
076AEC881FDB48580077D153 /* UIDatePickerExtensions.swift */,
42F53FEB2039C5AC0070DC11 /* UIStackViewExtensions.swift */,
86B3F7AB208D3D5C00BC297B /* UIScrollViewExtensions.swift */,
0722CB3720C2E46B00C6C1B6 /* UIWindowExtensions.swift */,
);
path = UIKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -897,6 +902,7 @@
42F53FEF2039C7140070DC11 /* UIStackViewExtensionsTest.swift */,
86B3F7AD208D3DF300BC297B /* UIScrollViewExtensionsTest.swift */,
90693554208B545100407C4D /* UIGestureRecognizerExtensionsTests.swift */,
0722CB3920C2E49900C6C1B6 /* UIWindowExtensionsTests.swift */,
);
path = UIKitTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1424,6 +1430,7 @@
077BA08F1F6BE83600D9C4AC /* UserDefaultsExtensions.swift in Sources */,
07B7F1A11F5EB42000E6F910 /* UISwitchExtensions.swift in Sources */,
078CE29F207643F8001720DF /* UIKitDeprecated.swift in Sources */,
0722CB3820C2E46C00C6C1B6 /* UIWindowExtensions.swift in Sources */,
07B7F1A41F5EB42000E6F910 /* UITextFieldExtensions.swift in Sources */,
07B7F20B1F5EB43C00E6F910 /* ArrayExtensions.swift in Sources */,
07B7F1A01F5EB42000E6F910 /* UIStoryboardExtensions.swift in Sources */,
Expand Down Expand Up @@ -1676,6 +1683,7 @@
07FE50451F891C95000766AA /* SignedNumericExtensionsTests.swift in Sources */,
07C50D391F5EB04700F46E5A /* UIStoryboardExtensionsTests.swift in Sources */,
784C75372051BE1D001C48DD /* MKPolylineTests.swift in Sources */,
0722CB3A20C2E49A00C6C1B6 /* UIWindowExtensionsTests.swift in Sources */,
9D9784E41FCAE6DD00D988E7 /* StringProtocolExtensionsTests.swift in Sources */,
07C50D361F5EB04700F46E5A /* UISearchBarExtensionsTests.swift in Sources */,
07C50D8C1F5EB06000F46E5A /* CGFloatExtensionsTests.swift in Sources */,
Expand Down
41 changes: 41 additions & 0 deletions Tests/UIKitTests/UIWindowExtensionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// UIWindowExtensionsTests.swift
// SwifterSwift
//
// Created by Omar Albeik on 6/2/18.
// Copyright © 2018 SwifterSwift
//

#if os(iOS)
import XCTest
@testable import SwifterSwift

final class UIWindowExtensionsTests: XCTestCase {

func testSwitchRootViewController() {
let viewController = UIViewController()
let tableViewController = UITableViewController()

let window = UIWindow()
window.rootViewController = viewController

XCTAssertNotNil(window.rootViewController)
XCTAssertEqual(window.rootViewController!, viewController)

window.switchRootViewController(to: tableViewController, animated: false)
XCTAssertNotNil(window.rootViewController)
XCTAssertEqual(window.rootViewController!, tableViewController)

let completionExpectation = expectation(description: "Completed")

window.switchRootViewController(to: viewController, animated: true, duration: 0.75) {
completionExpectation.fulfill()
XCTAssertNotNil(window.rootViewController)
XCTAssertEqual(window.rootViewController!, viewController)
}

waitForExpectations(timeout: 1, handler: nil)
}

}
#endif

0 comments on commit 20ca06c

Please sign in to comment.