-
Notifications
You must be signed in to change notification settings - Fork 0
/
Countdowner.swift
66 lines (53 loc) · 1.82 KB
/
Countdowner.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// Countdowner.swift
// Countdowner
//
// Created by Rafael Almeida on 28/01/18.
// Copyright © 2018 Rafael Almeida. All rights reserved.
//
import Foundation
#if os(iOS) || os(watchOS) || os(tvOS)
import UIKit
#elseif os(OSX)
import Cocoa
#endif
class Countdowner {
var counter: Int = 0
var alert: Double = 0
var danger: Double = 0
var color: CGColor?
var window: WindowSettings?
init(counter: Int) {
self.setCountdownValue(counter: counter)
}
func update(counter: Int) -> (window: WindowSettings, color: CGColor, minutes: Int, seconds: Int) {
let minutes = counter / 60
let seconds = counter % 60
switch Double(counter) {
case self.danger ... self.alert:
color = .yellow
window = WindowSettings(width: 300, height: 200, x: 50, y: 50)
case 0 ... self.danger:
color = .red
window = WindowSettings(width: 400, height: 300, x: 75, y: 75)
default:
return self.defaultState(counter: counter)
}
return (window: window!, color: color!, minutes: minutes, seconds: seconds)
}
func secondsToTime(seconds: Int) -> (timeInMinutes: Int, timeInSeconds: Int) {
return (timeInMinutes: seconds / 60, timeInSeconds: seconds % 60)
}
func defaultState(counter: Int) -> (window: WindowSettings, color: CGColor, minutes: Int, seconds: Int) {
let minutes = counter / 60
let seconds = counter % 60
color = .green
window = WindowSettings(width: 200, height: 100, x: 25, y: 25)
return (window: window!, color: color!, minutes: minutes, seconds: seconds)
}
func setCountdownValue(counter: Int) {
self.counter = counter
self.alert = Double(counter) * 0.33
self.danger = Double(counter) * 0.17
}
}