From 6d4e20f15c6129c7deaa1df33059ef6a73bb3158 Mon Sep 17 00:00:00 2001 From: Alexander Rutsman Date: Sat, 13 Nov 2021 14:08:41 +0300 Subject: [PATCH] feat: added alert factory and protocols --- .../Views/Alert/Factories/AlertFactory.swift | 54 ++++++++++++++ .../Views/Alert/Models/AlertAction.swift | 46 ++++++++++++ .../Views/Alert/Models/AlertDescriptor.swift | 55 ++++++++++++++ .../Alert/Protocols/AlertInformative.swift | 72 +++++++++++++++++++ .../Alert/Protocols/AlertPresentable.swift | 36 ++++++++++ 5 files changed, 263 insertions(+) create mode 100644 TIUIElements/Sources/Views/Alert/Factories/AlertFactory.swift create mode 100644 TIUIElements/Sources/Views/Alert/Models/AlertAction.swift create mode 100644 TIUIElements/Sources/Views/Alert/Models/AlertDescriptor.swift create mode 100644 TIUIElements/Sources/Views/Alert/Protocols/AlertInformative.swift create mode 100644 TIUIElements/Sources/Views/Alert/Protocols/AlertPresentable.swift diff --git a/TIUIElements/Sources/Views/Alert/Factories/AlertFactory.swift b/TIUIElements/Sources/Views/Alert/Factories/AlertFactory.swift new file mode 100644 index 00000000..805f5d9e --- /dev/null +++ b/TIUIElements/Sources/Views/Alert/Factories/AlertFactory.swift @@ -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) + } +} diff --git a/TIUIElements/Sources/Views/Alert/Models/AlertAction.swift b/TIUIElements/Sources/Views/Alert/Models/AlertAction.swift new file mode 100644 index 00000000..d7cd3490 --- /dev/null +++ b/TIUIElements/Sources/Views/Alert/Models/AlertAction.swift @@ -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 + } +} diff --git a/TIUIElements/Sources/Views/Alert/Models/AlertDescriptor.swift b/TIUIElements/Sources/Views/Alert/Models/AlertDescriptor.swift new file mode 100644 index 00000000..c442fb10 --- /dev/null +++ b/TIUIElements/Sources/Views/Alert/Models/AlertDescriptor.swift @@ -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 + } +} diff --git a/TIUIElements/Sources/Views/Alert/Protocols/AlertInformative.swift b/TIUIElements/Sources/Views/Alert/Protocols/AlertInformative.swift new file mode 100644 index 00000000..4fb7bc27 --- /dev/null +++ b/TIUIElements/Sources/Views/Alert/Protocols/AlertInformative.swift @@ -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?) +} diff --git a/TIUIElements/Sources/Views/Alert/Protocols/AlertPresentable.swift b/TIUIElements/Sources/Views/Alert/Protocols/AlertPresentable.swift new file mode 100644 index 00000000..2384b7e2 --- /dev/null +++ b/TIUIElements/Sources/Views/Alert/Protocols/AlertPresentable.swift @@ -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) +}