Skip to content

Commit

Permalink
Restructuring FeedbackEffect into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
mergesort committed Sep 28, 2017
1 parent bc84b64 commit 0a42711
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 161 deletions.
20 changes: 20 additions & 0 deletions Example/FeedbackEffect.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
B721D10C1F61DF880088DC13 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B721D10A1F61DF880088DC13 /* LaunchScreen.storyboard */; };
B721D1231F61E1E70088DC13 /* FeedbackEffect.swift in Sources */ = {isa = PBXBuildFile; fileRef = B721D1221F61DFD20088DC13 /* FeedbackEffect.swift */; };
B721D1271F61F0B10088DC13 /* Success.m4a in Resources */ = {isa = PBXBuildFile; fileRef = B721D1251F61F04D0088DC13 /* Success.m4a */; };
B795F1DF1F7D46E4001C1296 /* MessageTone.swift in Sources */ = {isa = PBXBuildFile; fileRef = B795F1DE1F7D46E4001C1296 /* MessageTone.swift */; };
B795F1E11F7D46EC001C1296 /* SoundEffect.swift in Sources */ = {isa = PBXBuildFile; fileRef = B795F1E01F7D46EC001C1296 /* SoundEffect.swift */; };
B795F1E31F7D4873001C1296 /* VibrationFeedback.swift in Sources */ = {isa = PBXBuildFile; fileRef = B795F1E21F7D4873001C1296 /* VibrationFeedback.swift */; };
B795F1E51F7D488B001C1296 /* URL+SoundEmitting.swift in Sources */ = {isa = PBXBuildFile; fileRef = B795F1E41F7D488B001C1296 /* URL+SoundEmitting.swift */; };
B795F1E71F7D49B6001C1296 /* HapticFeedback.swift in Sources */ = {isa = PBXBuildFile; fileRef = B795F1E61F7D49B6001C1296 /* HapticFeedback.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -38,6 +43,11 @@
B721D1181F61DF890088DC13 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B721D1221F61DFD20088DC13 /* FeedbackEffect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbackEffect.swift; sourceTree = "<group>"; };
B721D1251F61F04D0088DC13 /* Success.m4a */ = {isa = PBXFileReference; lastKnownFileType = file; path = Success.m4a; sourceTree = "<group>"; };
B795F1DE1F7D46E4001C1296 /* MessageTone.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageTone.swift; sourceTree = "<group>"; };
B795F1E01F7D46EC001C1296 /* SoundEffect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoundEffect.swift; sourceTree = "<group>"; };
B795F1E21F7D4873001C1296 /* VibrationFeedback.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VibrationFeedback.swift; sourceTree = "<group>"; };
B795F1E41F7D488B001C1296 /* URL+SoundEmitting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+SoundEmitting.swift"; sourceTree = "<group>"; };
B795F1E61F7D49B6001C1296 /* HapticFeedback.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HapticFeedback.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -103,6 +113,11 @@
isa = PBXGroup;
children = (
B721D1221F61DFD20088DC13 /* FeedbackEffect.swift */,
B795F1E61F7D49B6001C1296 /* HapticFeedback.swift */,
B795F1E21F7D4873001C1296 /* VibrationFeedback.swift */,
B795F1E41F7D488B001C1296 /* URL+SoundEmitting.swift */,
B795F1E01F7D46EC001C1296 /* SoundEffect.swift */,
B795F1DE1F7D46E4001C1296 /* MessageTone.swift */,
);
name = Source;
path = ../../Source;
Expand Down Expand Up @@ -222,7 +237,12 @@
files = (
B721D1231F61E1E70088DC13 /* FeedbackEffect.swift in Sources */,
B721D1041F61DF880088DC13 /* ViewController.swift in Sources */,
B795F1E11F7D46EC001C1296 /* SoundEffect.swift in Sources */,
B721D1021F61DF880088DC13 /* AppDelegate.swift in Sources */,
B795F1E51F7D488B001C1296 /* URL+SoundEmitting.swift in Sources */,
B795F1E71F7D49B6001C1296 /* HapticFeedback.swift in Sources */,
B795F1DF1F7D46E4001C1296 /* MessageTone.swift in Sources */,
B795F1E31F7D4873001C1296 /* VibrationFeedback.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
161 changes: 0 additions & 161 deletions Source/FeedbackEffect.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import AudioToolbox
import AVFoundation
import UIKit

Expand All @@ -14,166 +13,6 @@ public protocol HapticEmitting {
func generateFeedback()
}

/// An enum describing the different kinds of haptic feedback the taptic engine is capable of producing.
/// For use in `FeedbackEffect`, to give the user feedback when it's desired.
///
/// - impact: Used for calling `UIImpactFeedbackGenerator().impactOccurred()`.
/// - selection: Used for calling `UISelectionFeedbackGenerator().selectionChanged()`.
/// - notification: Used for calling `UINotificationFeedbackGenerator().notificationOccurred(notificationType)`.
public enum HapticFeedback: HapticEmitting {

fileprivate static let impactGenerator = UIImpactFeedbackGenerator()
fileprivate static let selectionGenerator = UISelectionFeedbackGenerator()
fileprivate static let notificationGenerator = UINotificationFeedbackGenerator()

case impact(UIImpactFeedbackStyle)
case selection
case notification(UINotificationFeedbackType)
}

extension HapticFeedback {

public func prepare() {
switch self {

case .impact:
HapticFeedback.impactGenerator.prepare()

case .selection:
HapticFeedback.selectionGenerator.prepare()

case .notification:
HapticFeedback.notificationGenerator.prepare()
}
}

public func generateFeedback() {
switch self {

case .impact(let impactStyle):
let impactGenerator = UIImpactFeedbackGenerator(style: impactStyle)
impactGenerator.impactOccurred()

case .selection:
UISelectionFeedbackGenerator().selectionChanged()

case .notification(let notificationType):
UINotificationFeedbackGenerator().notificationOccurred(notificationType)
}
}
}

/// A set of text message sound effects that iOS provides through the `AudioServicesPlaySystemSound` subsystem.
public enum MessageTone: Int {

case note = 1375
case aurora = 1366
case bamboo = 1361
case chord = 1312
case circles = 1368
case complete = 1362
case hello = 1363
case input = 1369
case keys = 1367
case popcorn = 1364
case pulse = 1120
case synth = 1365
case alert = 1005
case anticipate = 1020
case bell = 1013
case bloom = 1021
case calypso = 1022
case chime = 1008
case choochoo = 1023
case descent = 1024
case ding = 1000
case electronic = 1014
case fanfare = 1025
case glass = 1009
case horn = 1010
case ladder = 1026
case minuet = 1027
case newsflash = 1028
case noir = 1029
case sherwoodforest = 1030
case spell = 1031
case suspence = 1032
case swish = 1018
case swoosh = 1001
case telegraph = 1033
case tiptoes = 1034
case tritone = 1002
case tweet = 1016
case typewriters = 1035
case update = 1036
}

extension MessageTone: SoundEmitting {

public func makeSound() {
AudioServicesPlaySystemSound(UInt32(self.rawValue))
}
}

/// A set of sound effects that iOS provides through the `AudioServicesPlaySystemSound` subsystem.
public enum SoundEffect: Int {

case tap = 1104
}

extension SoundEffect: SoundEmitting {

public func makeSound() {
AudioServicesPlaySystemSound(UInt32(self.rawValue))
}
}

/// A set of vibration patterns that iOS uses through the `AudioServicesPlaySystemSound` subsystem
/// to generate vibrations on devices that do not have a taptic engine.
public enum VibrationFeedback {

case selection
case impact
case notification

}

private extension VibrationFeedback {

var audioServicesCode: UInt32 {
switch self {

case .selection:
return 1519

case .impact:
return 1520

case .notification:
return 1521

}
}

}

extension VibrationFeedback: HapticEmitting {

public func generateFeedback() {
AudioServicesPlaySystemSound(self.audioServicesCode)
}
}

extension URL: SoundEmitting {

public func makeSound() {
FeedbackEffect.player.removeAllItems()
let item = AVPlayerItem(url: self)
FeedbackEffect.player.insert(item, after: nil)
FeedbackEffect.player.play()
}
}

/// A type which encapsulates sound and haptic feedback. This is useful for providing feedback on user
/// interaction, or when an app would like to signal a noteworthy event.
public struct FeedbackEffect {
Expand Down
50 changes: 50 additions & 0 deletions Source/HapticFeedback.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import UIKit

/// An enum describing the different kinds of haptic feedback the taptic engine is capable of producing.
/// For use in `FeedbackEffect`, to give the user feedback when it's desired.
///
/// - impact: Used for calling `UIImpactFeedbackGenerator().impactOccurred()`.
/// - selection: Used for calling `UISelectionFeedbackGenerator().selectionChanged()`.
/// - notification: Used for calling `UINotificationFeedbackGenerator().notificationOccurred(notificationType)`.
public enum HapticFeedback: HapticEmitting {

fileprivate static let impactGenerator = UIImpactFeedbackGenerator()
fileprivate static let selectionGenerator = UISelectionFeedbackGenerator()
fileprivate static let notificationGenerator = UINotificationFeedbackGenerator()

case impact
case selection
case notification(UINotificationFeedbackType)
}

extension HapticFeedback {

public func prepare() {
switch self {

case .impact:
HapticFeedback.impactGenerator.prepare()

case .selection:
HapticFeedback.selectionGenerator.prepare()

case .notification:
HapticFeedback.notificationGenerator.prepare()
}
}

public func generateFeedback() {
switch self {

case .impact:
HapticFeedback.impactGenerator.impactOccurred()

case .selection:
HapticFeedback.selectionGenerator.selectionChanged()

case .notification(let notificationType):
HapticFeedback.notificationGenerator.notificationOccurred(notificationType)
}
}
}

54 changes: 54 additions & 0 deletions Source/MessageTone.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import AudioToolbox

/// A set of text message sound effects that iOS provides through the `AudioServicesPlaySystemSound` subsystem.
public enum MessageTone: Int {

case note = 1375
case aurora = 1366
case bamboo = 1361
case chord = 1312
case circles = 1368
case complete = 1362
case hello = 1363
case input = 1369
case keys = 1367
case popcorn = 1364
case pulse = 1120
case synth = 1365
case alert = 1005
case anticipate = 1020
case bell = 1013
case bloom = 1021
case calypso = 1022
case chime = 1008
case choochoo = 1023
case descent = 1024
case ding = 1000
case electronic = 1014
case fanfare = 1025
case glass = 1009
case horn = 1010
case ladder = 1026
case minuet = 1027
case newsflash = 1028
case noir = 1029
case sherwoodforest = 1030
case spell = 1031
case suspence = 1032
case swish = 1018
case swoosh = 1001
case telegraph = 1033
case tiptoes = 1034
case tritone = 1002
case tweet = 1016
case typewriters = 1035
case update = 1036
}

extension MessageTone: SoundEmitting {

public func makeSound() {
AudioServicesPlaySystemSound(UInt32(self.rawValue))
}
}

15 changes: 15 additions & 0 deletions Source/SoundEffect.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import AudioToolbox

/// A set of sound effects that iOS provides through the `AudioServicesPlaySystemSound` subsystem.
public enum SoundEffect: Int {

case tap = 1104
}

extension SoundEffect: SoundEmitting {

public func makeSound() {
AudioServicesPlaySystemSound(UInt32(self.rawValue))
}
}

12 changes: 12 additions & 0 deletions Source/URL+SoundEmitting.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import AVFoundation
import Foundation

extension URL: SoundEmitting {

public func makeSound() {
FeedbackEffect.player.removeAllItems()
let item = AVPlayerItem(url: self)
FeedbackEffect.player.insert(item, after: nil)
FeedbackEffect.player.play()
}
}
38 changes: 38 additions & 0 deletions Source/VibrationFeedback.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import AudioToolbox

/// A set of vibration patterns that iOS uses through the `AudioServicesPlaySystemSound` subsystem
/// to generate vibrations on devices that do not have a taptic engine.
public enum VibrationFeedback {

case selection
case impact
case notification

}

private extension VibrationFeedback {

var audioServicesCode: UInt32 {
switch self {

case .selection:
return 1519

case .impact:
return 1520

case .notification:
return 1521

}
}

}

extension VibrationFeedback: HapticEmitting {

public func generateFeedback() {
AudioServicesPlaySystemSound(self.audioServicesCode)
}
}

0 comments on commit 0a42711

Please sign in to comment.