Skip to content

Commit

Permalink
Implemented volume controls for sfx instead of simple on/off toggles
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoronin committed Sep 9, 2023
1 parent 2345704 commit 0c99581
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 56 deletions.
32 changes: 22 additions & 10 deletions TomatoBar/Player.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import AppKit
import AVFoundation
import SwiftUI

class TBPlayer {
class TBPlayer: ObservableObject {
private var windupSound: AVAudioPlayer
private var dingSound: AVAudioPlayer
private var tickingSound: AVAudioPlayer

@AppStorage("windupVolume") var windupVolume: Double = 1.0 {
didSet {
setVolume(windupSound, windupVolume)
}
}
@AppStorage("dingVolume") var dingVolume: Double = 1.0 {
didSet {
setVolume(dingSound, dingVolume)
}
}
@AppStorage("tickingVolume") var tickingVolume: Double = 1.0 {
didSet {
setVolume(tickingSound, tickingVolume)
}
}

private func setVolume(_ sound: AVAudioPlayer, _ volume: Double) {
sound.setVolume(Float(volume), fadeDuration: 0)
}

init() {
let windupSoundAsset = NSDataAsset(name: "windup")
let dingSoundAsset = NSDataAsset(name: "ding")
Expand Down Expand Up @@ -41,12 +61,4 @@ class TBPlayer {
func stopTicking() {
tickingSound.stop()
}

func toggleTicking() {
if tickingSound.isPlaying {
stopTicking()
} else {
startTicking()
}
}
}
23 changes: 4 additions & 19 deletions TomatoBar/Timer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import SwiftState
import SwiftUI

class TBTimer: ObservableObject {
@AppStorage("isWindupEnabled") var isWindupEnabled = true
@AppStorage("isDingEnabled") var isDingEnabled = true
@AppStorage("isTickingEnabled") var isTickingEnabled = true
@AppStorage("stopAfterBreak") var stopAfterBreak = false
@AppStorage("showTimerInMenuBar") var showTimerInMenuBar = true
@AppStorage("workIntervalLength") var workIntervalLength = 25
Expand All @@ -16,7 +13,7 @@ class TBTimer: ObservableObject {
@AppStorage("overrunTimeLimit") var overrunTimeLimit = -60.0

private var stateMachine = TBStateMachine(state: .idle)
private let player = TBPlayer()
public let player = TBPlayer()
private var consecutiveWorkIntervals: Int = 0
private var notificationCenter = TBNotificationCenter()
private var finishTime: Date!
Expand Down Expand Up @@ -122,12 +119,6 @@ class TBTimer: ObservableObject {
stateMachine <-! .skipRest
}

func toggleTicking() {
if stateMachine.state == .work {
player.toggleTicking()
}
}

func updateTimeLeft() {
timeLeftString = timerFormatter.string(from: Date(), to: finishTime)!
if timer != nil, showTimerInMenuBar {
Expand Down Expand Up @@ -186,20 +177,14 @@ class TBTimer: ObservableObject {

private func onWorkStart(context _: TBStateMachine.Context) {
TBStatusItem.shared.setIcon(name: .work)
if isWindupEnabled {
player.playWindup()
}
if isTickingEnabled {
player.startTicking()
}
player.playWindup()
player.startTicking()
startTimer(seconds: workIntervalLength * 60)
}

private func onWorkFinish(context _: TBStateMachine.Context) {
consecutiveWorkIntervals += 1
if isDingEnabled {
player.playDing()
}
player.playDing()
}

private func onWorkEnd(context _: TBStateMachine.Context) {
Expand Down
58 changes: 31 additions & 27 deletions TomatoBar/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,39 @@ private struct SettingsView: View {
}
}

private struct VolumeSlider: View {
@Binding var volume: Double

var body: some View {
Slider(value: $volume, in: 0...2) {
Text(String(format: "%.1f", volume))
}.gesture(TapGesture(count: 2).onEnded({
volume = 1.0
}))
}
}

private struct SoundsView: View {
@EnvironmentObject var timer: TBTimer
@EnvironmentObject var player: TBPlayer

private var columns = [
GridItem(.flexible()),
GridItem(.fixed(110))
]

var body: some View {
VStack {
Toggle(isOn: $timer.isWindupEnabled) {
Text(NSLocalizedString("SoundsView.isWindupEnabled.label",
comment: "Windup label"))
.frame(maxWidth: .infinity, alignment: .leading)
}
.toggleStyle(.switch)
Toggle(isOn: $timer.isDingEnabled) {
Text(NSLocalizedString("SoundsView.isDingEnabled.label",
comment: "Ding label"))
.frame(maxWidth: .infinity, alignment: .leading)
}
.toggleStyle(.switch)
Toggle(isOn: $timer.isTickingEnabled) {
Text(NSLocalizedString("SoundsView.isTickingEnabled.label",
comment: "Ticking label"))
.frame(maxWidth: .infinity, alignment: .leading)
}
.toggleStyle(.switch)
.onChange(of: timer.isTickingEnabled) { _ in
timer.toggleTicking()
}
Spacer().frame(minHeight: 0)
}
.padding(4)
LazyVGrid(columns: columns, alignment: .leading, spacing: 4) {
Text(NSLocalizedString("SoundsView.isWindupEnabled.label",
comment: "Windup label"))
VolumeSlider(volume: $player.windupVolume)
Text(NSLocalizedString("SoundsView.isDingEnabled.label",
comment: "Ding label"))
VolumeSlider(volume: $player.dingVolume)
Text(NSLocalizedString("SoundsView.isTickingEnabled.label",
comment: "Ticking label"))
VolumeSlider(volume: $player.tickingVolume)
}.padding(4)
Spacer().frame(minHeight: 0)
}
}

Expand Down Expand Up @@ -176,7 +180,7 @@ struct TBPopoverView: View {
case .settings:
SettingsView().environmentObject(timer)
case .sounds:
SoundsView().environmentObject(timer)
SoundsView().environmentObject(timer.player)
}
}

Expand Down

0 comments on commit 0c99581

Please sign in to comment.