Skip to content

Commit 9b14222

Browse files
author
Jakob Mygind
authored
Merge pull request #22 from nodes-ios/add_tap
Added tapgesture to dismiss and slight refactor
2 parents a86663e + e847bd5 commit 9b14222

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

Rye/Rye/RyePresentation.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import UIKit
1313
public extension RyeViewController {
1414
func show() {
1515

16-
switch self.alertType! {
16+
switch self.alertType {
1717
case .toast:
1818
// create a new UIWindow
1919
let window = UIWindow(frame: UIScreen.main.bounds)
@@ -55,9 +55,11 @@ public extension RyeViewController {
5555
return
5656
}
5757
// animate the RyeView off screen
58-
animateRyeOut(completion: {
58+
animateRyeOut(completion: { [weak self] in
5959

60-
switch self.alertType! {
60+
guard let self = self else { return }
61+
62+
switch self.alertType {
6163
case .toast:
6264
// remove the UIWindow
6365
self.window?.isHidden = true
@@ -101,7 +103,7 @@ public extension RyeViewController {
101103
let safeArea = getSafeAreaSpacing()
102104

103105
// update RyeView bottom constraint to position it on screen
104-
switch position! {
106+
switch position {
105107
case .bottom(let inset):
106108
return -safeArea - inset
107109
case .top(let inset):
@@ -135,7 +137,7 @@ public extension RyeViewController {
135137
// update RyeView bottom constraint to position it off screen
136138

137139
func getRyeViewPositionConstant() -> CGFloat {
138-
switch position! {
140+
switch position {
139141
case .bottom:
140142
return ryeView.frame.height
141143
case .top:

Rye/Rye/RyeViewController.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class RyeViewController: UIViewController {
2929

3030
// all presentation logic is done using parentView
3131
var parentView: UIView {
32-
switch alertType! {
32+
switch alertType {
3333
case .snackBar:
3434
if var topController = UIApplication.shared.keyWindow?.rootViewController {
3535
while let presentedViewController = topController.presentedViewController {
@@ -50,10 +50,10 @@ public class RyeViewController: UIViewController {
5050
return keyWindow
5151
}
5252
}
53-
var alertType: Rye.AlertType!
54-
var viewType: Rye.ViewType!
53+
var alertType: Rye.AlertType
54+
var viewType: Rye.ViewType
5555
var timeAlive: TimeInterval?
56-
var position: Rye.Position!
56+
var position: Rye.Position
5757
var animationDuration: TimeInterval!
5858
var animationType: Rye.AnimationType!
5959

@@ -72,16 +72,16 @@ public class RyeViewController: UIViewController {
7272
- Parameter timeAlive: Represents the duration for the RyeView to be displayed to the user. If nil is provided, then you will be responsable of removing the RyeView
7373

7474
*/
75-
public init(alertType: Rye.AlertType? = .toast,
76-
viewType: Rye.ViewType? = .standard(configuration: nil),
77-
at position: Rye.Position? = .bottom(inset: 16),
75+
public init(alertType: Rye.AlertType = .toast,
76+
viewType: Rye.ViewType = .standard(configuration: nil),
77+
at position: Rye.Position = .bottom(inset: 16),
7878
timeAlive: TimeInterval? = nil) {
7979
self.alertType = alertType
8080
self.viewType = viewType
8181
self.timeAlive = timeAlive
8282
self.position = position
8383

84-
switch viewType! {
84+
switch viewType {
8585
case .standard(let configuration):
8686
animationDuration = configuration?[Rye.Configuration.Key.animationDuration] as? TimeInterval ?? 0.3
8787
animationType = configuration?[Rye.Configuration.Key.animationType] as? Rye.AnimationType ?? .slideInOut
@@ -93,7 +93,7 @@ public class RyeViewController: UIViewController {
9393
super.init(nibName: nil, bundle: nil)
9494

9595
// check if an alert is currently showing and update the isShowing value
96-
switch alertType! {
96+
switch alertType {
9797
case .toast:
9898
isShowing = UIApplication.shared.windows.contains(where: {$0.windowLevel == .alert})
9999
case .snackBar:
@@ -113,19 +113,26 @@ public class RyeViewController: UIViewController {
113113
view.backgroundColor = .clear
114114
}
115115

116+
@objc func ryeTapped(_ sender: Any) {
117+
dismiss()
118+
}
119+
116120
// MARK: - Display helpers
117121

118122
func showRye(for type: Rye.ViewType) {
119123
func addRyeView(_ ryeView: UIView) {
120124
self.ryeView = ryeView
125+
126+
ryeView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ryeTapped(_:))))
127+
121128
// add RyeView to hierarchy
122129
parentView.addSubview(ryeView)
123130
ryeView.translatesAutoresizingMaskIntoConstraints = false
124131
ryeView.centerXAnchor.constraint(equalTo: parentView.centerXAnchor).isActive = true
125132
ryeView.widthAnchor.constraint(lessThanOrEqualTo: parentView.widthAnchor, constant: -16).isActive = true
126133

127134
// setup constraint
128-
switch position! {
135+
switch position {
129136
case .bottom:
130137
ryeViewPositionConstraint = ryeView.bottomAnchor.constraint(equalTo: parentView.bottomAnchor)
131138
case .top:
@@ -139,7 +146,7 @@ public class RyeViewController: UIViewController {
139146
ryeView.layoutIfNeeded()
140147

141148
// update RyeView bottom constraint constat to position it outside of the application's UIWindow
142-
switch position! {
149+
switch position {
143150
case .bottom:
144151
ryeViewPositionConstraint.constant = ryeView.frame.height
145152
case .top:

0 commit comments

Comments
 (0)