Coder Social home page Coder Social logo

timer's People

Watchers

Bunny13 avatar

timer's Issues

code.swift

import Cocoa

class ViewController: NSViewController {
@IBOutlet weak var timerLabel: NSTextField!
@IBOutlet weak var startButton: NSButton!

let workTime: TimeInterval = 25 * 60 // 25 minutes
let shortRestTime: TimeInterval = 5 * 60 // 5 minutes
let longRestTime: TimeInterval = 15 * 60 // 15 minutes
let workCountPerRound: Int = 4

var timer: Timer?
var timeLeft: TimeInterval = 0
var isWorking: Bool = false
var workCount: Int = 0

override func viewDidLoad() {
    super.viewDidLoad()
    
    timerLabel.stringValue = stringFromTimeInterval(workTime)
}

@IBAction func startButtonClicked(_ sender: NSButton) {
    if timer == nil {
        startTimer()
        startButton.title = "Stop"
    } else {
        stopTimer()
        startButton.title = "Start"
    }
}

func startTimer() {
    if isWorking {
        timeLeft = workTime
    } else {
        timeLeft = shortRestTime
    }
    timerLabel.stringValue = stringFromTimeInterval(timeLeft)
    
    timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
        guard let self = self else { return }
        
        self.timeLeft -= 1
        self.timerLabel.stringValue = self.stringFromTimeInterval(self.timeLeft)
        
        if self.timeLeft <= 0 {
            self.stopTimer()
            
            if self.isWorking {
                self.workCount += 1
                if self.workCount >= self.workCountPerRound {
                    self.timeLeft = self.longRestTime
                    self.workCount = 0
                } else {
                    self.timeLeft = self.shortRestTime
                }
            } else {
                self.timeLeft = self.workTime
            }
            
            self.isWorking = !self.isWorking
            self.timerLabel.stringValue = self.stringFromTimeInterval(self.timeLeft)
            self.startTimer()
        }
    }
}

func stopTimer() {
    timer?.invalidate()
    timer = nil
}

func stringFromTimeInterval(_ interval: TimeInterval) -> String {
    let formatter = DateComponentsFormatter()
    formatter.allowedUnits = [.minute, .second]
    formatter.unitsStyle = .positional
    formatter.zeroFormattingBehavior = .pad
    return formatter.string(from: interval)!
}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.