Skip to content

Commit

Permalink
Bug fixes and better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
planecore committed Jan 31, 2019
1 parent c07971e commit f23c7af
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 33 deletions.
Binary file removed Assets/MenuScreenshot.png
Binary file not shown.
Binary file added Assets/Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions Auto Dark.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
908230AF21F1FBB600FA2D36 /* LocationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationManager.swift; sourceTree = "<group>"; };
908230B121F20F6600FA2D36 /* PreferencesController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesController.swift; sourceTree = "<group>"; };
90D19C2321F35FF9001C410D /* ScheduleManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScheduleManager.swift; sourceTree = "<group>"; };
90FB494F22022444000D6510 /* LICENSE.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = LICENSE.md; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -48,6 +49,7 @@
isa = PBXGroup;
children = (
905E4A8C21A31BCD00D1DBC7 /* README.md */,
90FB494F22022444000D6510 /* LICENSE.md */,
905E4A7721A19E2500D1DBC7 /* Auto Dark */,
905E4A7621A19E2500D1DBC7 /* Products */,
);
Expand Down
5 changes: 2 additions & 3 deletions Auto Dark/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ var mode = ScheduleMode.location
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
if !UserDefaults.standard.contains(key: "new") {
UserDefaults.standard.set(true, forKey: "new")
if !UserDefaults.standard.contains(key: "mode") {
UserDefaults.standard.set("Location", forKey: "mode")
UserDefaults.standard.set("7:00", forKey: "sunrise")
UserDefaults.standard.set("19:00", forKey: "sunset")
Expand Down
2 changes: 1 addition & 1 deletion Auto Dark/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.3</string>
<key>CFBundleVersion</key>
<string>5</string>
<string>6</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand Down
56 changes: 34 additions & 22 deletions Auto Dark/LocationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
// Created by Matan Mashraki on 18/01/2019.
// Copyright © 2019 Matan Mashraki. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Cocoa
import CoreLocation
Expand Down Expand Up @@ -51,15 +69,7 @@ class LocationManager: NSObject, CLLocationManagerDelegate, DarkManager {
if pref == .location, CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
} else {
let res = showAlert(title: "Location Services Aren't Available", subtitle: "Please turn on location services or set location manually.", button1: "I Enabled Location Services", button2: "Set Location Manually")
if res == .alertFirstButtonReturn {
determineCurrentLocation()
} else if res == .alertSecondButtonReturn {
pref = .manual
setManualLocation()
} else {
NSApplication.shared.terminate(self)
}
self.delegate?.setInformationLabel(string: "Authorize Auto Dark to detect your location in System Preferences.")
}
}
}
Expand All @@ -74,7 +84,7 @@ class LocationManager: NSObject, CLLocationManagerDelegate, DarkManager {
self.calculateNextDate()
self.delegate?.setLocationLabel(string: location)
} else {
self.delegate?.setInformationLabel(string: "Can't calculate Dark Mode for your location.")
self.presentError(error: error as? CLError)
}
}
}
Expand All @@ -89,26 +99,28 @@ class LocationManager: NSObject, CLLocationManagerDelegate, DarkManager {
self.delegate?.setLocationLabel(string: self.stringLocation!)
self.calculateNextDate()
} else {
self.delegate?.setInformationLabel(string: "Can't calculate Dark Mode for your location.")
self.delegate?.setLocationLabel(string: "Auto Dark")
}
}
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error Location: \(error)")
self.delegate?.setInformationLabel(string: "Can't detect your location.")
if currentLocation == nil {
presentError(error: error as? CLError)
}
}

func showAlert(title: String, subtitle: String, button1: String, button2: String?) -> NSApplication.ModalResponse {
let msg = NSAlert()
msg.addButton(withTitle: button1)
if let b2 = button2 {
msg.addButton(withTitle: b2)
func presentError(error: CLError?) {
if let err = error {
var message = ""
switch err.code.rawValue {
case 0, 2: message = "There's no internet connection."
case 1: message = "Authorize Auto Dark to detect your location in System Preferences."
default: message = "Auto Dark can't detect your location"
}
self.delegate?.setLocationLabel(string: "Auto Dark")
self.delegate?.setInformationLabel(string: message)
}
msg.addButton(withTitle: "Cancel")
msg.messageText = title
msg.informativeText = subtitle
return msg.runModal()
}

}
4 changes: 2 additions & 2 deletions Auto Dark/Preferences.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="tSk-pi-Mqe">
<rect key="frame" x="149" y="185" width="70" height="19"/>
<rect key="frame" x="149" y="182" width="70" height="19"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="roundRect" title="Location" bezelStyle="roundedRect" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyUpOrDown" inset="2" id="bdP-bu-Dzr">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand Down Expand Up @@ -135,7 +135,7 @@
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="4g0-aj-6c0">
<rect key="frame" x="39" y="186" width="107" height="18"/>
<rect key="frame" x="39" y="183" width="107" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Type Location" bezelStyle="regularSquare" imagePosition="left" inset="2" id="QaH-zO-2Up">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand Down
27 changes: 23 additions & 4 deletions Auto Dark/PreferencesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
// Created by Matan Mashraki on 18/01/2019.
// Copyright © 2019 Matan Mashraki. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Cocoa
import CoreLocation
Expand Down Expand Up @@ -179,10 +197,11 @@ class TypeLocationViewController: NSViewController {
if error == nil {
self.delegate?.updateLocation(location: text)
self.dismiss(self)
} else if error.debugDescription.contains("Domain=kCLErrorDomain Code=2") {
self.message.stringValue = "No Internet"
} else {
self.message.stringValue = "Invalid Location"
} else if let err = error as? CLError {
switch err.code.rawValue {
case 0, 2: self.message.stringValue = "No Internet"
default: self.message.stringValue = "Invalid Location"
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions Auto Dark/ScheduleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
// Created by Matan Mashraki on 19/01/2019.
// Copyright © 2019 Matan Mashraki. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

Expand Down
2 changes: 1 addition & 1 deletion Auto Dark/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Auto Dark
//
// Created by Matan Mashraki on 18/11/2018.
// Copyright © 2018 Matan Mashraki. All rights reserved.
// Copyright © 2019 Matan Mashraki. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to deal
Expand Down
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Matan Mashraki

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

This simple menu bar app calculates the sunrise and sunset times at your loaction and switches Dark Mode accordingly.

![Screenshot](https://raw.githubusercontent.com/planecore/Auto-Dark/master/Assets/Screenshot.png)

Special thanks for this awesome library that calculates sunrise and sunset times: https://github.com/ceeK/Solar/blob/master/Solar/Solar.swift

0 comments on commit f23c7af

Please sign in to comment.