Files
GitHawk/Classes/View Controllers/UIViewController+CancelAction.swift
Bas Broek ddddb6363f Add merge status tests (#1604)
* Add merge status tests

* Introduce containsAll and containsNone

* Rename containsAll to containsOnly
2018-03-04 21:36:25 -05:00

39 lines
1.0 KiB
Swift

//
// UIViewController+CancelAction.swift
// Freetime
//
// Created by Ryan Nystrom on 10/8/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import UIKit
extension UIViewController {
func cancelAction_onCancel(
texts: [String?],
title: String = NSLocalizedString("Unsaved Changes", comment: ""),
message: String
) {
let dismissBlock = {
self.dismiss(animated: trueUnlessReduceMotionEnabled)
}
// dismiss if all text entries are empty
let canDismissNow = texts.containsOnly { $0 == nil || $0!.isEmpty }
if canDismissNow {
dismissBlock()
} else {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addActions([
AlertAction.goBack(),
AlertAction.discard { _ in
dismissBlock()
}
])
present(alert, animated: trueUnlessReduceMotionEnabled)
}
}
}