Skip to content

Commit

Permalink
Implemented menu option to select between 3 sounds or no sound (#107)
Browse files Browse the repository at this point in the history
Fixes #49
  • Loading branch information
simongroenewolt authored Jan 8, 2021
1 parent ecd7933 commit e8fbd02
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Timer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
40A85EF925A64C3100DA10CC /* alert-sound-3.caf in Resources */ = {isa = PBXBuildFile; fileRef = 40A85EF725A64C3100DA10CC /* alert-sound-3.caf */; };
40A85EFA25A64C3100DA10CC /* alert-sound-2.caf in Resources */ = {isa = PBXBuildFile; fileRef = 40A85EF825A64C3100DA10CC /* alert-sound-2.caf */; };
41A37D0724426E360078EA40 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 41A37D0624426E360078EA40 /* Credits.rtf */; };
41B080C62114CE2700EEE7E8 /* Keycodes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41B080C52114CE2700EEE7E8 /* Keycodes.swift */; };
4C30BBFC1CA7C56500C45EBF /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C30BBFB1CA7C56500C45EBF /* AppDelegate.swift */; };
Expand All @@ -23,6 +25,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
40A85EF725A64C3100DA10CC /* alert-sound-3.caf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "alert-sound-3.caf"; sourceTree = "<group>"; };
40A85EF825A64C3100DA10CC /* alert-sound-2.caf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "alert-sound-2.caf"; sourceTree = "<group>"; };
41A37D0624426E360078EA40 /* Credits.rtf */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; path = Credits.rtf; sourceTree = "<group>"; };
41B080C52114CE2700EEE7E8 /* Keycodes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Keycodes.swift; sourceTree = "<group>"; };
4C30BBF81CA7C56500C45EBF /* Timer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Timer.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -83,6 +87,8 @@
41A37D0624426E360078EA40 /* Credits.rtf */,
4C30BC121CA7D73C00C45EBF /* MainMenu.xib */,
4C6F0F291CAA79EB00E9A6F7 /* alert-sound.caf */,
40A85EF825A64C3100DA10CC /* alert-sound-2.caf */,
40A85EF725A64C3100DA10CC /* alert-sound-3.caf */,
41B080C52114CE2700EEE7E8 /* Keycodes.swift */,
);
path = Timer;
Expand Down Expand Up @@ -151,6 +157,8 @@
4C30BBFE1CA7C56500C45EBF /* Assets.xcassets in Resources */,
4C6F0F2A1CAA79EB00E9A6F7 /* alert-sound.caf in Resources */,
4C30BC141CA7D73C00C45EBF /* MainMenu.xib in Resources */,
40A85EF925A64C3100DA10CC /* alert-sound-3.caf in Resources */,
40A85EFA25A64C3100DA10CC /* alert-sound-2.caf in Resources */,
41A37D0724426E360078EA40 /* Credits.rtf in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
35 changes: 35 additions & 0 deletions Timer/MVMainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MVMainView: NSView {
weak var controller: MVTimerController?
private var contextMenu: NSMenu?
public var menuItem: NSMenuItem?
private var soundMenuItems: [NSMenuItem] = []

// swiftlint:disable unused_setter_value
override var menu: NSMenu? {
Expand All @@ -32,7 +33,28 @@ class MVMainView: NSView {
action: #selector(self.toggleShowInDock),
keyEquivalent: ""
)
let submenu = NSMenu()
let menuItemSoundChoice = NSMenuItem(
title: "Sound",
action: nil,
keyEquivalent: ""
)
let soundOptions = [
(title: "Sound 1", value: 0),
(title: "Sound 2", value: 1),
(title: "Sound 3", value: 2),
(title: "No Sound", value: -1)
]
for option in soundOptions {
let soundItem = NSMenuItem(title: option.title, action: #selector(self.pickSound), keyEquivalent: "")
soundItem.representedObject = option.value
self.soundMenuItems.append(soundItem)
submenu.addItem(soundItem)
}
self.soundMenuItems.first?.state = .on
self.contextMenu?.addItem(menuItem!)
self.contextMenu?.addItem(menuItemSoundChoice)
self.contextMenu?.setSubmenu(submenu, for: menuItemSoundChoice)

let notificationCenter = NotificationCenter.default

Expand Down Expand Up @@ -67,6 +89,19 @@ class MVMainView: NSView {
}
}

@objc func pickSound(_ sender: NSMenuItem) {
for item in self.soundMenuItems {
if item == sender {
item.state = .on
} else {
item.state = .off
}
}
if let soundIdx = sender.representedObject as? Int {
self.controller!.pickSound(soundIdx)
}
}

deinit {
NotificationCenter.default.removeObserver(self)
}
Expand Down
38 changes: 34 additions & 4 deletions Timer/MVTimerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class MVTimerController: NSWindowController {
private var clockView: MVClockView!

private var audioPlayer: AVAudioPlayer? // player must be kept in memory
private var soundURL = Bundle.main.url(forResource: "alert-sound", withExtension: "caf")

convenience init() {
let mainView = MVMainView(frame: NSRect.zero)
Expand Down Expand Up @@ -52,10 +53,11 @@ class MVTimerController: NSWindowController {
}

func playAlarmSound() {
let soundURL = Bundle.main.url(forResource: "alert-sound", withExtension: "caf")
audioPlayer = try? AVAudioPlayer(contentsOf: soundURL!)
//audioPlayer?.volume = self.volume
audioPlayer?.play()
if soundURL != nil {
audioPlayer = try? AVAudioPlayer(contentsOf: soundURL!)
//audioPlayer?.volume = self.volume
audioPlayer?.play()
}
}

@objc func handleClockTimer(_ clockView: MVClockView) {
Expand All @@ -75,4 +77,32 @@ class MVTimerController: NSWindowController {

override func keyDown(with event: NSEvent) {
}

func pickSound(_ index: Int) {
let sound: String?
switch index {
case -1:
sound = nil

case 0:
sound = "alert-sound"

case 1:
sound = "alert-sound-2"

case 2:
sound = "alert-sound-3"

default:
sound = "alert-sound"
}
if sound != nil {
self.soundURL = Bundle.main.url(forResource: sound, withExtension: "caf")

// 'preview'
playAlarmSound()
} else {
self.soundURL = nil
}
}
}
Binary file added Timer/alert-sound-2.caf
Binary file not shown.
Binary file added Timer/alert-sound-3.caf
Binary file not shown.

0 comments on commit e8fbd02

Please sign in to comment.