feat: added alert factory and protocols

This commit is contained in:
Alexander Rutsman 2021-11-13 14:08:41 +03:00
parent 3c6a98a582
commit 6d4e20f15c
5 changed files with 263 additions and 0 deletions

View File

@ -0,0 +1,54 @@
//
// Copyright (c) 2021 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import UIKit
public enum AlertFactory {
/// The main method of creating alerts
public static func createAlert(with descriptor: AlertDescriptor) -> UIAlertController {
let controller = UIAlertController(title: descriptor.title,
message: descriptor.message,
preferredStyle: descriptor.style)
controller.view.tintColor = descriptor.tintColor
for action in descriptor.actions {
let alertAction = UIAlertAction(title: action.title, style: action.style) { _ in
action.closure?()
}
controller.addAction(alertAction)
}
return controller
}
/// The auxiliary method of creating alerts
public static func createAlert(title: String? = nil,
message: String? = nil,
actions: [AlertAction] = []) -> UIAlertController {
let descriptor = AlertDescriptor(title: title, message: message, actions: actions)
return createAlert(with: descriptor)
}
}

View File

@ -0,0 +1,46 @@
//
// Copyright (c) 2021 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import UIKit
import TISwiftUtils
/// Protocol describes alert action
public struct AlertAction {
/// Alert action title
public let title: String
/// Alert action style
public let style: UIAlertAction.Style
/// Alert action
public let closure: VoidClosure?
public init(title: String,
style: UIAlertAction.Style = .default,
closure: VoidClosure? = nil) {
self.title = title
self.style = style
self.closure = closure
}
}

View File

@ -0,0 +1,55 @@
//
// Copyright (c) 2021 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import UIKit
/// Protocol describes alert data
public struct AlertDescriptor {
/// Alert title
public let title: String?
/// Alert message
public let message: String?
/// Alert style
public let style: UIAlertController.Style
/// Alert tint color
public let tintColor: UIColor
/// Alert actions
public let actions: [AlertAction]
public init(title: String? = nil,
message: String? = nil,
style: UIAlertController.Style = .alert,
tintColor: UIColor = .blue,
actions: [AlertAction] = []) {
self.title = title
self.message = message
self.style = style
self.tintColor = tintColor
self.actions = actions
}
}

View File

@ -0,0 +1,72 @@
//
// Copyright (c) 2021 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import UIKit
import TISwiftUtils
protocol AlertInformative {
/// An alert informing about something with one closing button (by default ["OK"])
func presentInfoAlert(title: String?)
/// An alert informing about something with one closing button (by default ["OK"])
func presentInfoAlert(title: String?,
message: String?)
/// An alert informing about something with one closing button (by default ["OK"])
func presentInfoAlert(title: String?,
message: String?,
infoStyle: UIAlertAction.Style)
/// An alert informing about something with one closing button (by default ["OK"])
func presentInfoAlert(title: String?,
infoAction: VoidClosure?)
/// An alert informing about something with one closing button (by default ["OK"])
func presentInfoAlert(title: String?,
message: String?,
infoAction: VoidClosure?)
/// An alert informing about something with one closing button (by default ["OK"])
func presentInfoAlert(title: String?,
message: String?,
infoTitle: String?,
infoStyle: UIAlertAction.Style,
infoAction: VoidClosure?)
/// Alert offering to repeat the action (with the "Repeat" and "Cancel" buttons)
func presentRetryAlert(title: String?,
message: String?,
retryAction: @escaping VoidClosure)
/// Alert offering to repeat the action (with the "Repeat" and "Cancel" buttons)
func presentRetryAlert(title: String?,
message: String?,
retryAction: @escaping VoidClosure,
cancelAction: VoidClosure?)
/// Alert with custom actions and cancel button
func presentActionsAlert(title: String?,
message: String?,
actions: [AlertAction],
cancelAction: VoidClosure?)
}

View File

@ -0,0 +1,36 @@
//
// Copyright (c) 2021 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
import TISwiftUtils
public protocol AlertPresentable {
/// Alert title
var title: String? { get }
/// Alert message
var message: String? { get }
/// Attaches an action object to the alert
func addAction(title: String, action: VoidClosure)
}