Skip to content

Commit

Permalink
Refactor computing safeAreaInsets in Shout- and WhistleFactory
Browse files Browse the repository at this point in the history
This commit refactors the iPhone X fix
(#179) to reduce the amount of
`#available(iOS 11.0, *)` annotations.
  • Loading branch information
zenangst committed Oct 17, 2017
1 parent 437a3bb commit aeff2e2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
8 changes: 4 additions & 4 deletions Demo/WhisperDemo/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
PODS:
- Whisper (4.0.0)
- Whisper (5.0.0)

DEPENDENCIES:
- Whisper (from `../../`)

EXTERNAL SOURCES:
Whisper:
:path: "../../"
:path: ../../

SPEC CHECKSUMS:
Whisper: 08be92623311f8e53201e62e17f6d7b9599a4714
Whisper: 39b833d0f279912a7f38a19c4ba603435514a550

PODFILE CHECKSUM: e20a3f258a5cd7e57b37f62aadb18fd14d135133

COCOAPODS: 1.2.0
COCOAPODS: 1.3.1
8 changes: 7 additions & 1 deletion Demo/WhisperDemo/WhisperDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,12 @@
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-WhisperDemo/Pods-WhisperDemo-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/Whisper/Whisper.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Whisper.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand All @@ -245,13 +248,16 @@
files = (
);
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-WhisperDemo-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
BCC353EC93296D0E781E45A8 /* [CP] Copy Pods Resources */ = {
Expand Down
21 changes: 6 additions & 15 deletions Source/ShoutFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,27 +174,18 @@ open class ShoutView: UIView {
if subtitleLabel.text?.isEmpty ?? true {
titleLabel.center.y = imageView.center.y - 2.5
}
if #available(iOS 11.0, *) {
frame = CGRect(x: 0, y: self.safeAreaInsets.top, width: totalWidth, height: internalHeight + Dimensions.touchOffset)
} else {
frame = CGRect(x: 0, y: 0, width: totalWidth, height: internalHeight + Dimensions.touchOffset)
}

frame = CGRect(x: 0, y: safeYCoordinate,
width: totalWidth, height: internalHeight + Dimensions.touchOffset)
}

// MARK: - Frame

open override var frame: CGRect {
didSet {
if #available(iOS 11.0, *) {
backgroundView.frame = CGRect(x: 0, y: self.safeAreaInsets.top,
width: frame.size.width,
height: frame.size.height - Dimensions.touchOffset)
} else {
// Fallback on earlier versions
backgroundView.frame = CGRect(x: 0, y: 0,
width: frame.size.width,
height: frame.size.height - Dimensions.touchOffset)
}
backgroundView.frame = CGRect(x: 0, y: safeYCoordinate,
width: frame.size.width,
height: frame.size.height - Dimensions.touchOffset)

indicatorView.frame = CGRect(x: (backgroundView.frame.size.width - Dimensions.indicatorWidth) / 2,
y: backgroundView.frame.height - Dimensions.indicatorHeight - 5,
Expand Down
14 changes: 14 additions & 0 deletions Source/UIView+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import UIKit

extension UIView {
var safeYCoordinate: CGFloat {
let y: CGFloat
if #available(iOS 11.0, *) {
y = safeAreaInsets.top
} else {
y = 0
}

return y
}
}
11 changes: 3 additions & 8 deletions Source/WhistleFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,9 @@ open class WhistleFactory: UIViewController {
titleLabel.sizeToFit()
}

if #available(iOS 11.0, *) {
whistleWindow.frame = CGRect(x: 0, y: self.view.safeAreaInsets.top, width: labelWidth,
height: titleLabelHeight)
} else {
// Fallback on earlier versions
whistleWindow.frame = CGRect(x: 0, y: 0, width: labelWidth,
height: titleLabelHeight)
}
whistleWindow.frame = CGRect(x: 0, y: view.safeYCoordinate,
width: labelWidth,
height: titleLabelHeight)
view.frame = whistleWindow.bounds
titleLabel.frame = view.bounds
}
Expand Down
4 changes: 4 additions & 0 deletions Whisper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
290531131C20517E00FB382C /* WhisperFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2905310B1C20517E00FB382C /* WhisperFactory.swift */; };
290531141C20517E00FB382C /* WhisperView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2905310C1C20517E00FB382C /* WhisperView.swift */; };
290531151C20517E00FB382C /* WhistleFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2905310D1C20517E00FB382C /* WhistleFactory.swift */; };
BDA2138F1F95D75F003FBC71 /* UIView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDA2138E1F95D75F003FBC71 /* UIView+Extensions.swift */; };
D2D71BB81D54B62B006AB907 /* Showing.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2D71BB71D54B62B006AB907 /* Showing.swift */; };
D5170D641C4955F3006F2F37 /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5170D631C4955F3006F2F37 /* Config.swift */; };
/* End PBXBuildFile section */
Expand All @@ -28,6 +29,7 @@
2905310C1C20517E00FB382C /* WhisperView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WhisperView.swift; sourceTree = "<group>"; };
2905310D1C20517E00FB382C /* WhistleFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WhistleFactory.swift; sourceTree = "<group>"; };
290531171C2051F400FB382C /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
BDA2138E1F95D75F003FBC71 /* UIView+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Extensions.swift"; sourceTree = "<group>"; };
D2D71BB71D54B62B006AB907 /* Showing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Showing.swift; sourceTree = "<group>"; };
D5170D631C4955F3006F2F37 /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -71,6 +73,7 @@
2905310C1C20517E00FB382C /* WhisperView.swift */,
2905310D1C20517E00FB382C /* WhistleFactory.swift */,
D5170D631C4955F3006F2F37 /* Config.swift */,
BDA2138E1F95D75F003FBC71 /* UIView+Extensions.swift */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -173,6 +176,7 @@
files = (
290531121C20517E00FB382C /* ShoutFactory.swift in Sources */,
290531141C20517E00FB382C /* WhisperView.swift in Sources */,
BDA2138F1F95D75F003FBC71 /* UIView+Extensions.swift in Sources */,
290531151C20517E00FB382C /* WhistleFactory.swift in Sources */,
290531111C20517E00FB382C /* Message.swift in Sources */,
290531131C20517E00FB382C /* WhisperFactory.swift in Sources */,
Expand Down

0 comments on commit aeff2e2

Please sign in to comment.