<commit_before>//
//  VolumeControl.swift
//  FaderBar
//
//  Created by iain on 04/12/16.
//  Copyright © 2016 iain. All rights reserved.
//

import Foundation

/// Default fade length in minutes
struct Defaults {
    static let fadeLength = 30.0
}

class VolumeControl {
    /// Time (in seconds) between volume changes
    let interval: UInt

    /// Timer used for scheduling
    var timer: Timer

    /// Initial volume when schedule started
    var initialVolume: Double

    /// Amount to change volume;
    var delta: Double

    /// Length of fade (in minutes)
    var fadeLength: Double

    /**

        Initialises a volume control with default parameters

        - Returns: A new volume control

    */
    init() {
        self.timer = Timer()
        self.initialVolume = 0.0
        self.interval = 30
        self.delta = 0.0
        self.fadeLength = Defaults.fadeLength

        UserDefaults.standard.setValue(self.fadeLength, forKey: "fadeTime")
    }

    /**

        Set the volume based on the delta from the current volume.

    */
    @objc func changeVolume() {
        let currentVolume = Double(NSSound.systemVolume())
        let newVolume = Float(currentVolume - delta)

        if newVolume > 0 {
            NSSound.setSystemVolume(newVolume)
        } else {
            NSSound.setSystemVolume(0.0)

            self.showNotification(message: "Sound muted")

            self.timer.invalidate()
        }
    }

    /**

        Show a notification

        - Parameter message: The notification contents

    */
    func showNotification(message: String) -> Void {
        let notification = NSUserNotification()
        notification.title = "Message from faderbar"
        notification.informativeText = message
        notification.soundName = nil

        NSUserNotificationCenter.default.deliver(notification)
    }

    /**

        Start the volume reduction schedule

    */
    func startShrinkage() -> Void {
        self.timer.invalidate()

        self.initialVolume = Double(NSSound.systemVolume())

        // calculate change per interval from number of times timer is called
        self.delta = self.initialVolume / ((self.fadeLength * 60) / Double(self.interval))

        self.timer = Timer.scheduledTimer(
            timeInterval: 30,
            target: self,
            selector: #selector(self.changeVolume),
            userInfo: nil,<commit_msg>Fixes #7 set interval based on fade length<commit_after>//
//  VolumeControl.swift
//  FaderBar
//
//  Created by iain on 04/12/16.
//  Copyright © 2016 iain. All rights reserved.
//

import Foundation

/// Default fade length in minutes
struct Defaults {
    static let fadeLength = 30.0
}

class VolumeControl {

    /// Timer used for scheduling
    var timer: Timer

    /// Initial volume when schedule started
    var initialVolume: Double

    /// Amount to change volume;
    var delta: Double

    /// Length of fade (in minutes)
    var fadeLength: Double

    /**

        Initialises a volume control with default parameters

        - Returns: A new volume control

    */
    init() {
        self.timer = Timer()
        self.initialVolume = 0.0
        self.delta = 0.0
        self.fadeLength = Defaults.fadeLength

        UserDefaults.standard.setValue(self.fadeLength, forKey: "fadeTime")
    }

    /**

        Set the volume based on the delta from the current volume.

    */
    @objc func changeVolume() {
        let currentVolume = Double(NSSound.systemVolume())
        let newVolume = Float(currentVolume - delta)

        if newVolume > 0 {
            NSSound.setSystemVolume(newVolume)
        } else {
            NSSound.setSystemVolume(0.0)

            self.showNotification(message: "Sound muted")

            self.timer.invalidate()
        }
    }

    /**

        Show a notification

        - Parameter message: The notification contents

    */
    func showNotification(message: String) -> Void {
        let notification = NSUserNotification()
        notification.title = "Message from faderbar"
        notification.informativeText = message
        notification.soundName = nil

        NSUserNotificationCenter.default.deliver(notification)
    }

    /**

        Start the volume reduction schedule

    */
    func startShrinkage() -> Void {
        self.timer.invalidate()

        self.initialVolume = Double(NSSound.systemVolume())

        // calculate interval...
        // 5m 5s 300s 5s
        // 10m 20s 600s 10s
        // 15m 30s
        // 30m 60s
        // 1h 120s
        let interval = self.fadeLength / 60.0

        // calculate change per interval from number of times timer is called
        self.delta = self.initialVolume / ((self.fadeLength * 60) / interval)

        self.timer = Timer.scheduledTimer(
            timeInterval: interval,
            target: self,
            selector: #selector(self.changeVolume),
            userInfo: nil,
{'fraction_non_alphanumeric': 0.08987624264556705, 'fraction_numerical': 0.01602759180361128, 'mean_word_length': 1.9933211900425014, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4123144', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1276, 'n_words': 389}