Skip to content

Commit

Permalink
Merge pull request #22 from nodes-ios/add_tap
Browse files Browse the repository at this point in the history
Added tapgesture to dismiss and slight refactor
  • Loading branch information
Jakob Mygind authored Oct 17, 2019
2 parents a86663e + e847bd5 commit 9b14222
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
12 changes: 7 additions & 5 deletions Rye/Rye/RyePresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import UIKit
public extension RyeViewController {
func show() {

switch self.alertType! {
switch self.alertType {
case .toast:
// create a new UIWindow
let window = UIWindow(frame: UIScreen.main.bounds)
Expand Down Expand Up @@ -55,9 +55,11 @@ public extension RyeViewController {
return
}
// animate the RyeView off screen
animateRyeOut(completion: {
animateRyeOut(completion: { [weak self] in

switch self.alertType! {
guard let self = self else { return }

switch self.alertType {
case .toast:
// remove the UIWindow
self.window?.isHidden = true
Expand Down Expand Up @@ -101,7 +103,7 @@ public extension RyeViewController {
let safeArea = getSafeAreaSpacing()

// update RyeView bottom constraint to position it on screen
switch position! {
switch position {
case .bottom(let inset):
return -safeArea - inset
case .top(let inset):
Expand Down Expand Up @@ -135,7 +137,7 @@ public extension RyeViewController {
// update RyeView bottom constraint to position it off screen

func getRyeViewPositionConstant() -> CGFloat {
switch position! {
switch position {
case .bottom:
return ryeView.frame.height
case .top:
Expand Down
29 changes: 18 additions & 11 deletions Rye/Rye/RyeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class RyeViewController: UIViewController {

// all presentation logic is done using parentView
var parentView: UIView {
switch alertType! {
switch alertType {
case .snackBar:
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
Expand All @@ -50,10 +50,10 @@ public class RyeViewController: UIViewController {
return keyWindow
}
}
var alertType: Rye.AlertType!
var viewType: Rye.ViewType!
var alertType: Rye.AlertType
var viewType: Rye.ViewType
var timeAlive: TimeInterval?
var position: Rye.Position!
var position: Rye.Position
var animationDuration: TimeInterval!
var animationType: Rye.AnimationType!

Expand All @@ -72,16 +72,16 @@ public class RyeViewController: UIViewController {
- 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
*/
public init(alertType: Rye.AlertType? = .toast,
viewType: Rye.ViewType? = .standard(configuration: nil),
at position: Rye.Position? = .bottom(inset: 16),
public init(alertType: Rye.AlertType = .toast,
viewType: Rye.ViewType = .standard(configuration: nil),
at position: Rye.Position = .bottom(inset: 16),
timeAlive: TimeInterval? = nil) {
self.alertType = alertType
self.viewType = viewType
self.timeAlive = timeAlive
self.position = position

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

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

@objc func ryeTapped(_ sender: Any) {
dismiss()
}

// MARK: - Display helpers

func showRye(for type: Rye.ViewType) {
func addRyeView(_ ryeView: UIView) {
self.ryeView = ryeView

ryeView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ryeTapped(_:))))

// add RyeView to hierarchy
parentView.addSubview(ryeView)
ryeView.translatesAutoresizingMaskIntoConstraints = false
ryeView.centerXAnchor.constraint(equalTo: parentView.centerXAnchor).isActive = true
ryeView.widthAnchor.constraint(lessThanOrEqualTo: parentView.widthAnchor, constant: -16).isActive = true

// setup constraint
switch position! {
switch position {
case .bottom:
ryeViewPositionConstraint = ryeView.bottomAnchor.constraint(equalTo: parentView.bottomAnchor)
case .top:
Expand All @@ -139,7 +146,7 @@ public class RyeViewController: UIViewController {
ryeView.layoutIfNeeded()

// update RyeView bottom constraint constat to position it outside of the application's UIWindow
switch position! {
switch position {
case .bottom:
ryeViewPositionConstraint.constant = ryeView.frame.height
case .top:
Expand Down

0 comments on commit 9b14222

Please sign in to comment.