Skip to content

Commit

Permalink
Fixed timer
Browse files Browse the repository at this point in the history
  • Loading branch information
planecore authored and Matan Mashraki committed Nov 21, 2018
1 parent 8fc6728 commit 1e15240
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Auto Dark/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}
Expand Down
8 changes: 4 additions & 4 deletions Auto Dark/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.1</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>3</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
<true/>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2018 Matan Mashraki. All rights reserved.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>LSUIElement</key>
<true/>
</dict>
</plist>
14 changes: 11 additions & 3 deletions Auto Dark/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class ViewController: NSObject {
@IBOutlet weak var locationLabel: NSMenuItem!
@IBOutlet weak var informationLabel: NSMenuItem!
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
var timer: Timer?
var date: Date?

override func awakeFromNib() {
let icon = NSImage(named: "StatusIcon")
Expand All @@ -80,6 +82,12 @@ class ViewController: NSObject {
if let loc = currentLocation {
locationLabel.title = loc
}
timer = Timer(fire: Date().addingTimeInterval(10), interval: 60, repeats: true) { (t) in
if let date = self.date, Date() > date {
self.getSunTimes()
}
}
RunLoop.main.add(timer!, forMode: .common)
getSunTimes()
}

Expand Down Expand Up @@ -111,12 +119,12 @@ class ViewController: NSObject {
nextRun = Solar(for: Calendar.current.date(byAdding: .day, value: 1, to: Date())!, coordinate: coord)?.sunrise
}
if let next = nextRun {
let timer = Timer(fireAt: next, interval: 0, target: self, selector: #selector(self.getSunTimes), userInfo: nil, repeats: false)
RunLoop.main.add(timer, forMode: RunLoop.Mode.common)
self.date = next
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm"
dateFormatter.dateFormat = "H:mm"
self.informationLabel.title = (solar.isDaytime ? "Sunset: " : "Sunrise: ") + dateFormatter.string(from: next)
} else {
self.date = nil
self.informationLabel.title = "Can't calculate auto dark mode at your location."
}
}
Expand Down

0 comments on commit 1e15240

Please sign in to comment.