Skip to content

Commit 1570785

Browse files
committed
Added dynamic haptic detection for pressent with preset.
1 parent 6b8d761 commit 1570785

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

Example iOS/Controllers/PresetsController.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class PresetsController: SPDiffableTableController {
6060
.init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
6161
.init(systemItem: .play, primaryAction: .init(handler: { [weak self] (action) in
6262
guard let preset = self?.currentPreset else { return }
63-
SPAlert.present(title: preset.title, message: preset.message, preset: preset.preset, haptic: preset.haptic, completion: nil)
63+
SPAlert.present(title: preset.title, message: preset.message, preset: preset.preset, completion: nil)
6464
}), menu: nil),
6565
.init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
6666
]
@@ -74,29 +74,25 @@ class PresetsController: SPDiffableTableController {
7474
name: "Done",
7575
title: "Added to Library",
7676
message: nil,
77-
preset: .done,
78-
haptic: .success
77+
preset: .done
7978
),
8079
AlertPreset(
8180
name: "Error",
8281
title: "Oops",
8382
message: "Please try again later",
84-
preset: .error,
85-
haptic: .error
83+
preset: .error
8684
),
8785
AlertPreset(
8886
name: "Heart",
8987
title: "Love",
9088
message: "We'll recommend more like this for you",
91-
preset: .heart,
92-
haptic: .success
89+
preset: .heart
9390
),
9491
AlertPreset(
9592
name: "Custom Image",
9693
title: "Custom Image",
9794
message: "Passed UIImage object for preset with style custom.",
98-
preset: .custom(UIImage.init(systemName: "pencil.and.outline")!),
99-
haptic: .success
95+
preset: .custom(UIImage.init(systemName: "pencil.and.outline")!)
10096
),
10197
]
10298
}

Example iOS/Models/AlertPreset.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ struct AlertPreset {
3030
var title: String
3131
var message: String?
3232
var preset: SPAlertIconPreset
33-
var haptic: SPAlertHaptic
3433

3534
var id: String {
3635
return name

SPAlert.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = 'SPAlert'
4-
s.version = '3.0'
4+
s.version = '3.0.1'
55
s.summary = 'Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets.'
66
s.homepage = 'https://github.com/varabeis/SPAlert'
77
s.license = { :type => "MIT", :file => "LICENSE" }

Sources/SPAlert/SPAlert.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,33 @@ import UIKit
2828
public enum SPAlert {
2929

3030
/**
31-
Present alert with preset and haptic.
31+
Present alert with preset and custom haptic.
3232

3333
- parameter title: Title text in alert.
3434
- parameter message: Subtitle text in alert. Optional.
3535
- parameter preset: Icon ready-use style or custom image.
3636
- parameter haptic: Haptic response with present. Default is `.success`.
3737
- parameter completion: Will call with dismiss alert.
3838
*/
39-
public static func present(title: String, message: String? = nil, preset: SPAlertIconPreset, haptic: SPAlertHaptic = .success, completion: (() -> Void)? = nil) {
39+
public static func present(title: String, message: String? = nil, preset: SPAlertIconPreset, haptic: SPAlertHaptic, completion: (() -> Void)? = nil) {
4040
let alertView = SPAlertView(title: title, message: message, preset: preset)
4141
alertView.present(haptic: haptic, completion: completion)
4242
}
4343

44+
/**
45+
Present alert with preset and automatically detect type haptic.
46+
47+
- parameter title: Title text in alert.
48+
- parameter message: Subtitle text in alert. Optional.
49+
- parameter preset: Icon ready-use style or custom image.
50+
- parameter completion: Will call with dismiss alert.
51+
*/
52+
public static func present(title: String, message: String? = nil, preset: SPAlertIconPreset, completion: (() -> Void)? = nil) {
53+
let alertView = SPAlertView(title: title, message: message, preset: preset)
54+
let haptic = preset.getHaptic()
55+
alertView.present(haptic: haptic, completion: completion)
56+
}
57+
4458
/**
4559
Show only message, without title and icon.
4660

Sources/SPAlert/SPAlertIconPreset.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ public extension SPAlertIconPreset {
5151
}
5252
}
5353

54+
// Get haptic by preset
55+
56+
public extension SPAlertIconPreset {
57+
58+
func getHaptic() -> SPAlertHaptic {
59+
switch self {
60+
case .error: return .error
61+
default: return .success
62+
}
63+
}
64+
}
65+
5466
// Get layout by preset
5567

5668
public extension SPAlertLayout {

0 commit comments

Comments
 (0)