Skip to content

Commit

Permalink
Merge pull request #216 from hyperoslo/update-swift-5
Browse files Browse the repository at this point in the history
Update swift 5
  • Loading branch information
3lvis authored Jun 5, 2020
2 parents 48bf881 + 5d97d02 commit fd0dee7
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 85 deletions.
1 change: 0 additions & 1 deletion .swift-version

This file was deleted.

111 changes: 55 additions & 56 deletions Source/ShoutFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class ShoutView: UIView {
view.clipsToBounds = true

return view
}()
}()

open fileprivate(set) lazy var indicatorView: UIView = {
let view = UIView()
Expand All @@ -29,7 +29,7 @@ open class ShoutView: UIView {
view.isUserInteractionEnabled = true

return view
}()
}()

open fileprivate(set) lazy var imageView: UIImageView = {
let imageView = UIImageView()
Expand All @@ -38,105 +38,104 @@ open class ShoutView: UIView {
imageView.contentMode = .scaleAspectFill

return imageView
}()
}()

open fileprivate(set) lazy var titleLabel: UILabel = {
let label = UILabel()
label.font = FontList.Shout.title
label.textColor = ColorList.Shout.title
label.numberOfLines = 2

return label
}()
}()

open fileprivate(set) lazy var subtitleLabel: UILabel = {
let label = UILabel()
label.font = FontList.Shout.subtitle
label.textColor = ColorList.Shout.subtitle
label.numberOfLines = 2

return label
}()
}()

open fileprivate(set) lazy var tapGestureRecognizer: UITapGestureRecognizer = { [unowned self] in
let gesture = UITapGestureRecognizer()
gesture.addTarget(self, action: #selector(ShoutView.handleTapGestureRecognizer))

return gesture
}()

open fileprivate(set) lazy var panGestureRecognizer: UIPanGestureRecognizer = { [unowned self] in
let gesture = UIPanGestureRecognizer()
gesture.addTarget(self, action: #selector(ShoutView.handlePanGestureRecognizer))

return gesture
}()

open fileprivate(set) var announcement: Announcement?
open fileprivate(set) var displayTimer = Timer()
open fileprivate(set) var panGestureActive = false
open fileprivate(set) var shouldSilent = false
open fileprivate(set) var completion: (() -> ())?

private var subtitleLabelOriginalHeight: CGFloat = 0
private var internalHeight: CGFloat = 0

// MARK: - Initializers

public override init(frame: CGRect) {
super.init(frame: frame)

addSubview(backgroundView)
[imageView, titleLabel, subtitleLabel, indicatorView].forEach {
$0.autoresizingMask = []
backgroundView.addSubview($0)
}

clipsToBounds = false
isUserInteractionEnabled = true
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0, height: 0.5)
layer.shadowOpacity = 0.1
layer.shadowRadius = 0.5

backgroundView.addGestureRecognizer(tapGestureRecognizer)
addGestureRecognizer(panGestureRecognizer)

NotificationCenter.default.addObserver(self, selector: #selector(ShoutView.orientationDidChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ShoutView.orientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
}

public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
}

// MARK: - Configuration

open func craft(_ announcement: Announcement, to: UIViewController, completion: (() -> ())?) {
panGestureActive = false
shouldSilent = false
configureView(announcement)
shout(to: to)

self.completion = completion
}

open func configureView(_ announcement: Announcement) {
self.announcement = announcement
imageView.image = announcement.image
titleLabel.text = announcement.title
subtitleLabel.text = announcement.subtitle

displayTimer.invalidate()
displayTimer = Timer.scheduledTimer(timeInterval: announcement.duration,
target: self, selector: #selector(ShoutView.displayTimerDidFire), userInfo: nil, repeats: false)

target: self, selector: #selector(ShoutView.displayTimerDidFire), userInfo: nil, repeats: false)
setupFrames()
}

open func shout(to controller: UIViewController) {
controller.view.addSubview(self)

Expand All @@ -145,78 +144,78 @@ open class ShoutView: UIView {
self.frame.size.height = self.internalHeight + Dimensions.touchOffset
})
}

// MARK: - Setup

public func setupFrames() {
internalHeight = (UIApplication.shared.isStatusBarHidden ? 55 : 65)

let totalWidth = UIScreen.main.bounds.width
let offset: CGFloat = UIApplication.shared.isStatusBarHidden ? 2.5 : 5
let textOffsetX: CGFloat = imageView.image != nil ? Dimensions.textOffset : 18
let imageSize: CGFloat = imageView.image != nil ? Dimensions.imageSize : 0

[titleLabel, subtitleLabel].forEach {
$0.frame.size.width = totalWidth - imageSize - (Dimensions.imageOffset * 2)
$0.sizeToFit()
$0.frame.size.width = totalWidth - imageSize - (Dimensions.imageOffset * 2)
$0.sizeToFit()
}

internalHeight += subtitleLabel.frame.height

imageView.frame = CGRect(x: Dimensions.imageOffset, y: (internalHeight - imageSize) / 2 + offset,
width: imageSize, height: imageSize)

width: imageSize, height: imageSize)
let textOffsetY = imageView.image != nil ? imageView.frame.origin.x + 3 : textOffsetX + 5

titleLabel.frame.origin = CGPoint(x: textOffsetX, y: textOffsetY)
subtitleLabel.frame.origin = CGPoint(x: textOffsetX, y: titleLabel.frame.maxY + 2.5)

if subtitleLabel.text?.isEmpty ?? true {
titleLabel.center.y = imageView.center.y - 2.5
}

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

// MARK: - Frame

open override var frame: CGRect {
didSet {
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,
width: Dimensions.indicatorWidth,
height: Dimensions.indicatorHeight)
}
}

// MARK: - Actions

open func silent() {
UIView.animate(withDuration: 0.35, animations: {
self.frame.size.height = 0
}, completion: { finished in
self.completion?()
self.displayTimer.invalidate()
self.removeFromSuperview()
}, completion: { finished in
self.completion?()
self.displayTimer.invalidate()
self.removeFromSuperview()
})
}

// MARK: - Timer methods

@objc open func displayTimerDidFire() {
@objc open func displayTimerDidFire() {
shouldSilent = true

if panGestureActive { return }
silent()
}

// MARK: - Gesture methods

@objc fileprivate func handleTapGestureRecognizer() {
guard let announcement = announcement else { return }
announcement.action?()
Expand Down Expand Up @@ -251,18 +250,18 @@ open class ShoutView: UIView {
UIView.animate(withDuration: 0.2, animations: {
self.frame.size.height = height + Dimensions.touchOffset
}, completion: { _ in
if translation.y < -5 {
self.completion?()
self.removeFromSuperview()
if translation.y < -5 {
self.completion?()
self.removeFromSuperview()
}
})
}
}


// MARK: - Handling screen orientation

@objc func orientationDidChange() {
@objc func orientationDidChange() {
setupFrames()
}
}
2 changes: 1 addition & 1 deletion Source/Showing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public func show(whisper message: Message, to: UINavigationController, action: W
}

public func show(shout announcement: Announcement, to: UIViewController, completion: (() -> Void)? = nil) {
shoutView.craft(announcement, to: to, completion: completion)
shoutView.craft(announcement, to: to, completion: completion)
}

public func show(whistle murmur: Murmur, action: WhistleAction = .show(1.5)) {
Expand Down
14 changes: 7 additions & 7 deletions Source/WhisperFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class WhisperFactory: NSObject {

override init() {
super.init()
NotificationCenter.default.addObserver(self, selector: #selector(WhisperFactory.orientationDidChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(WhisperFactory.orientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
}

deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
}

func craft(_ message: Message, navigationController: UINavigationController, action: WhisperAction) {
Expand Down Expand Up @@ -237,14 +237,14 @@ class WhisperFactory: NSObject {

if let tableView = viewController.view as? UITableView
, viewController is UITableViewController {
tableView.contentInset = UIEdgeInsetsMake(tableView.contentInset.top + edgeInsetHeight, tableView.contentInset.left, tableView.contentInset.bottom, tableView.contentInset.right)
tableView.contentInset = UIEdgeInsets.init(top: tableView.contentInset.top + edgeInsetHeight, left: tableView.contentInset.left, bottom: tableView.contentInset.bottom, right: tableView.contentInset.right)
} else if let collectionView = viewController.view as? UICollectionView
, viewController is UICollectionViewController {
collectionView.contentInset = UIEdgeInsetsMake(collectionView.contentInset.top + edgeInsetHeight, collectionView.contentInset.left, collectionView.contentInset.bottom, collectionView.contentInset.right)
collectionView.contentInset = UIEdgeInsets.init(top: collectionView.contentInset.top + edgeInsetHeight, left: collectionView.contentInset.left, bottom: collectionView.contentInset.bottom, right: collectionView.contentInset.right)
} else {
for view in viewController.view.subviews {
if let scrollView = view as? UIScrollView {
scrollView.contentInset = UIEdgeInsetsMake(scrollView.contentInset.top + edgeInsetHeight, scrollView.contentInset.left, scrollView.contentInset.bottom, scrollView.contentInset.right)
scrollView.contentInset = UIEdgeInsets.init(top: scrollView.contentInset.top + edgeInsetHeight, left: scrollView.contentInset.left, bottom: scrollView.contentInset.bottom, right: scrollView.contentInset.right)
}
}
}
Expand Down Expand Up @@ -280,7 +280,7 @@ extension WhisperFactory: UINavigationControllerDelegate {
var maximumY = navigationController.navigationBar.frame.maxY - UIApplication.shared.statusBarFrame.height

for subview in navigationController.navigationBar.subviews {
if subview is WhisperView { navigationController.navigationBar.bringSubview(toFront: subview) }
if subview is WhisperView { navigationController.navigationBar.bringSubviewToFront(subview) }

if subview.frame.maxY > maximumY && !(subview is WhisperView) {
maximumY = subview.frame.maxY
Expand All @@ -295,7 +295,7 @@ extension WhisperFactory: UINavigationControllerDelegate {
for subview in navigationController.navigationBar.subviews where subview is WhisperView {
moveControllerViews(true)

if let index = navigationController.viewControllers.index(of: viewController) , index > 0 {
if let index = navigationController.viewControllers.firstIndex(of: viewController) , index > 0 {
edgeInsetHeight = -WhisperView.Dimensions.height
performControllerMove(navigationController.viewControllers[Int(index) - 1])
break
Expand Down
10 changes: 5 additions & 5 deletions Source/WhistleFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ open class WhistleFactory: UIViewController {

view.addGestureRecognizer(tapGestureRecognizer)

NotificationCenter.default.addObserver(self, selector: #selector(WhistleFactory.orientationDidChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(WhistleFactory.orientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
}

public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
}

// MARK: - Configuration
Expand Down Expand Up @@ -96,7 +96,7 @@ open class WhistleFactory: UIViewController {
}

func moveWindowToFront() {
whistleWindow.windowLevel = view.isiPhoneX ? UIWindowLevelNormal : UIWindowLevelStatusBar
whistleWindow.windowLevel = view.isiPhoneX ? UIWindow.Level.normal : UIWindow.Level.statusBar
setNeedsStatusBarAppearanceUpdate()
}

Expand All @@ -117,7 +117,7 @@ open class WhistleFactory: UIViewController {
NSString(string: text).boundingRect(
with: CGSize(width: labelWidth, height: CGFloat.infinity),
options: NSStringDrawingOptions.usesLineFragmentOrigin,
attributes: [NSAttributedStringKey.font: titleLabel.font],
attributes: [NSAttributedString.Key.font: titleLabel.font!],
context: nil
)
titleLabelHeight = CGFloat(neededDimensions.size.height)
Expand Down Expand Up @@ -172,7 +172,7 @@ open class WhistleFactory: UIViewController {
}, completion: { _ in
if let window = self.previousKeyWindow {
window.isHidden = false
self.whistleWindow.windowLevel = UIWindowLevelNormal - 1
self.whistleWindow.windowLevel = UIWindow.Level.normal - 1
self.previousKeyWindow = nil
window.rootViewController?.setNeedsStatusBarAppearanceUpdate()
}
Expand Down
4 changes: 2 additions & 2 deletions Whisper.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "Whisper"
s.summary = "Whisper is a component that will make the task of display messages and in-app notifications simple."
s.version = "6.0.2"
s.version = "6.1.0"
s.homepage = "https://github.com/hyperoslo/Whisper"
s.license = 'MIT'
s.author = { "Hyper Interaktiv AS" => "[email protected]" }
Expand All @@ -11,5 +11,5 @@ Pod::Spec.new do |s|
s.requires_arc = true
s.source_files = 'Source/**/*'
s.frameworks = 'UIKit', 'Foundation'
s.pod_target_xcconfig = { 'SWIFT_VERSION' => '4.0' }
s.swift_version = '5.0'
end
Loading

0 comments on commit fd0dee7

Please sign in to comment.