From 15707853dfd1d3e834e3e3a320c588631f3e995a Mon Sep 17 00:00:00 2001 From: Ivan Vorobei Date: Sun, 20 Dec 2020 10:34:22 +0300 Subject: [PATCH] Added dynamic haptic detection for pressent with preset. --- .../Controllers/PresetsController.swift | 14 +++++--------- Example iOS/Models/AlertPreset.swift | 1 - SPAlert.podspec | 2 +- Sources/SPAlert/SPAlert.swift | 18 ++++++++++++++++-- Sources/SPAlert/SPAlertIconPreset.swift | 12 ++++++++++++ 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/Example iOS/Controllers/PresetsController.swift b/Example iOS/Controllers/PresetsController.swift index e246686..ecfa023 100644 --- a/Example iOS/Controllers/PresetsController.swift +++ b/Example iOS/Controllers/PresetsController.swift @@ -60,7 +60,7 @@ class PresetsController: SPDiffableTableController { .init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), .init(systemItem: .play, primaryAction: .init(handler: { [weak self] (action) in guard let preset = self?.currentPreset else { return } - SPAlert.present(title: preset.title, message: preset.message, preset: preset.preset, haptic: preset.haptic, completion: nil) + SPAlert.present(title: preset.title, message: preset.message, preset: preset.preset, completion: nil) }), menu: nil), .init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), ] @@ -74,29 +74,25 @@ class PresetsController: SPDiffableTableController { name: "Done", title: "Added to Library", message: nil, - preset: .done, - haptic: .success + preset: .done ), AlertPreset( name: "Error", title: "Oops", message: "Please try again later", - preset: .error, - haptic: .error + preset: .error ), AlertPreset( name: "Heart", title: "Love", message: "We'll recommend more like this for you", - preset: .heart, - haptic: .success + preset: .heart ), AlertPreset( name: "Custom Image", title: "Custom Image", message: "Passed UIImage object for preset with style custom.", - preset: .custom(UIImage.init(systemName: "pencil.and.outline")!), - haptic: .success + preset: .custom(UIImage.init(systemName: "pencil.and.outline")!) ), ] } diff --git a/Example iOS/Models/AlertPreset.swift b/Example iOS/Models/AlertPreset.swift index 0c57f7f..00d17b3 100644 --- a/Example iOS/Models/AlertPreset.swift +++ b/Example iOS/Models/AlertPreset.swift @@ -30,7 +30,6 @@ struct AlertPreset { var title: String var message: String? var preset: SPAlertIconPreset - var haptic: SPAlertHaptic var id: String { return name diff --git a/SPAlert.podspec b/SPAlert.podspec index 06342a4..87f7471 100644 --- a/SPAlert.podspec +++ b/SPAlert.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = 'SPAlert' - s.version = '3.0' + s.version = '3.0.1' s.summary = 'Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets.' s.homepage = 'https://github.com/varabeis/SPAlert' s.license = { :type => "MIT", :file => "LICENSE" } diff --git a/Sources/SPAlert/SPAlert.swift b/Sources/SPAlert/SPAlert.swift index af355bd..b22b71a 100644 --- a/Sources/SPAlert/SPAlert.swift +++ b/Sources/SPAlert/SPAlert.swift @@ -28,7 +28,7 @@ import UIKit public enum SPAlert { /** - Present alert with preset and haptic. + Present alert with preset and custom haptic. - parameter title: Title text in alert. - parameter message: Subtitle text in alert. Optional. @@ -36,11 +36,25 @@ public enum SPAlert { - parameter haptic: Haptic response with present. Default is `.success`. - parameter completion: Will call with dismiss alert. */ - public static func present(title: String, message: String? = nil, preset: SPAlertIconPreset, haptic: SPAlertHaptic = .success, completion: (() -> Void)? = nil) { + public static func present(title: String, message: String? = nil, preset: SPAlertIconPreset, haptic: SPAlertHaptic, completion: (() -> Void)? = nil) { let alertView = SPAlertView(title: title, message: message, preset: preset) alertView.present(haptic: haptic, completion: completion) } + /** + Present alert with preset and automatically detect type haptic. + + - parameter title: Title text in alert. + - parameter message: Subtitle text in alert. Optional. + - parameter preset: Icon ready-use style or custom image. + - parameter completion: Will call with dismiss alert. + */ + public static func present(title: String, message: String? = nil, preset: SPAlertIconPreset, completion: (() -> Void)? = nil) { + let alertView = SPAlertView(title: title, message: message, preset: preset) + let haptic = preset.getHaptic() + alertView.present(haptic: haptic, completion: completion) + } + /** Show only message, without title and icon. diff --git a/Sources/SPAlert/SPAlertIconPreset.swift b/Sources/SPAlert/SPAlertIconPreset.swift index 370816d..2bace1d 100644 --- a/Sources/SPAlert/SPAlertIconPreset.swift +++ b/Sources/SPAlert/SPAlertIconPreset.swift @@ -51,6 +51,18 @@ public extension SPAlertIconPreset { } } +// Get haptic by preset + +public extension SPAlertIconPreset { + + func getHaptic() -> SPAlertHaptic { + switch self { + case .error: return .error + default: return .success + } + } +} + // Get layout by preset public extension SPAlertLayout {