Skip to content

Commit

Permalink
Fixed internet reconnection bug
Browse files Browse the repository at this point in the history
  • Loading branch information
planecore committed Jan 12, 2019
1 parent 1e15240 commit 73f247d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Auto Dark.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = "";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = "";
Expand All @@ -288,6 +289,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.14;
PRODUCT_BUNDLE_IDENTIFIER = "com.matan.Auto-Dark";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
Expand All @@ -298,6 +300,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = "";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = "";
Expand All @@ -306,6 +309,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.14;
PRODUCT_BUNDLE_IDENTIFIER = "com.matan.Auto-Dark";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
Expand Down
5 changes: 3 additions & 2 deletions Auto Dark/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Auto_Dark" customModuleProvider="target"/>
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<customObject id="FTK-KS-jiM" customClass="ViewController" customModule="Auto_Dark" customModuleProvider="target">
<connections>
<outlet property="button" destination="nQy-si-fb5" id="5mr-Ir-0O6"/>
<outlet property="informationLabel" destination="x2j-lb-dGE" id="h2a-gN-Zth"/>
<outlet property="locationLabel" destination="RV8-CO-E97" id="gSy-qa-8XP"/>
<outlet property="statusMenu" destination="7xj-JG-sLp" id="n0X-pL-B5w"/>
Expand All @@ -25,7 +26,7 @@
<menuItem title="Current Location" enabled="NO" id="RV8-CO-E97">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem title="Next Switch" enabled="NO" id="x2j-lb-dGE">
<menuItem title="Calculating..." enabled="NO" id="x2j-lb-dGE">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem title="Change Location" id="nQy-si-fb5">
Expand Down
6 changes: 4 additions & 2 deletions Auto Dark/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.1</string>
<string>1.2</string>
<key>CFBundleVersion</key>
<string>3</string>
<string>4</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand All @@ -30,5 +30,7 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>LSApplicationCategoryType</key>
<string></string>
</dict>
</plist>
25 changes: 18 additions & 7 deletions Auto Dark/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ func runAppleScript(_ source: String) -> String {
return outstr
}


class ViewController: NSObject {

@IBOutlet weak var statusMenu: NSMenu!
var currentLocation = UserDefaults.standard.string(forKey: "location")
@IBOutlet weak var locationLabel: NSMenuItem!
@IBOutlet weak var informationLabel: NSMenuItem!
@IBOutlet weak var button: NSMenuItem!
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
var timer: Timer?
var date: Date?
Expand All @@ -78,12 +80,11 @@ class ViewController: NSObject {
icon?.isTemplate = true
statusItem.button?.image = icon
statusItem.menu = statusMenu
informationLabel.title = "Calculating..."
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 {
if let date = self.date, Date() > date || self.button.title == "Try Again" {
self.getSunTimes()
}
}
Expand All @@ -92,6 +93,7 @@ class ViewController: NSObject {
}

@objc func getSunTimes() {
informationLabel.title = "Calculating..."
guard let address = currentLocation else {
let getLocation = textBoxAlert(title: "Change Location", question: "What's the address?", defaultValue: "")
if let loc = getLocation {
Expand All @@ -102,10 +104,12 @@ class ViewController: NSObject {
getSunTimes()
return
}
self.locationLabel.title = address
let geo = CLGeocoder()
geo.geocodeAddressString(address) { (place, error) in
if let place = place {
if let coord = place.first?.location?.coordinate {
self.button.title = "Change Location"
print("Calculating for \(address)")
let solar = Solar(coordinate: coord)!
var nextRun: Date?
Expand All @@ -128,6 +132,9 @@ class ViewController: NSObject {
self.informationLabel.title = "Can't calculate auto dark mode at your location."
}
}
} else if error.debugDescription.contains("Domain=kCLErrorDomain Code=2") {
self.informationLabel.title = "Please check your internet connection."
self.button.title = "Try Again"
} else {
let getLocation = self.textBoxAlert(title: "Invalid Location", question: "What's the address?", defaultValue: "")
if let loc = getLocation {
Expand All @@ -141,12 +148,16 @@ class ViewController: NSObject {
}

@IBAction func changeLocation(sender: NSMenuItem) {
let getLocation = textBoxAlert(title: "Change Location", question: "What's the address?", defaultValue: "")
if let loc = getLocation {
UserDefaults.standard.set(loc, forKey: "location")
locationLabel.title = loc
currentLocation = loc
if sender.title == "Try Again" {
getSunTimes()
} else {
let getLocation = textBoxAlert(title: "Change Location", question: "What's the address?", defaultValue: "")
if let loc = getLocation {
UserDefaults.standard.set(loc, forKey: "location")
locationLabel.title = loc
currentLocation = loc
getSunTimes()
}
}
}

Expand Down

0 comments on commit 73f247d

Please sign in to comment.