Skip to content

Commit

Permalink
Added dynamic haptic detection for pressent with preset.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Dec 20, 2020
1 parent 6b8d761 commit 1570785
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
14 changes: 5 additions & 9 deletions Example iOS/Controllers/PresetsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
Expand All @@ -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")!)
),
]
}
Expand Down
1 change: 0 additions & 1 deletion Example iOS/Models/AlertPreset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ struct AlertPreset {
var title: String
var message: String?
var preset: SPAlertIconPreset
var haptic: SPAlertHaptic

var id: String {
return name
Expand Down
2 changes: 1 addition & 1 deletion SPAlert.podspec
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down
18 changes: 16 additions & 2 deletions Sources/SPAlert/SPAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,33 @@ 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.
- parameter preset: Icon ready-use style or custom image.
- 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.
Expand Down
12 changes: 12 additions & 0 deletions Sources/SPAlert/SPAlertIconPreset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1570785

Please sign in to comment.