Skip to content

Commit

Permalink
Merge pull request #596 from glouel/feature/location
Browse files Browse the repository at this point in the history
Feature/location
  • Loading branch information
glouel committed Oct 29, 2018
2 parents 3aec095 + f7c03e4 commit a13453e
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 140 deletions.
13 changes: 9 additions & 4 deletions Aerial.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
03510C6F21834F38008F74F2 /* IOBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 03510C6E21834F38008F74F2 /* IOBridge.m */; };
03510C7021834FC3008F74F2 /* Aerial-Bridging-Header.h in Headers */ = {isa = PBXBuildFile; fileRef = 03510C6D21834F38008F74F2 /* Aerial-Bridging-Header.h */; };
03510C7121834FC7008F74F2 /* IOBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 03510C6E21834F38008F74F2 /* IOBridge.m */; };
03510C732185EF76008F74F2 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03510C722185EF76008F74F2 /* CoreLocation.framework */; };
03510C772185EF8F008F74F2 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03510C722185EF76008F74F2 /* CoreLocation.framework */; };
03893CB3217749F0008E7125 /* ErrorLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03893CB2217749F0008E7125 /* ErrorLog.swift */; };
03893CB4217753AC008E7125 /* ErrorLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03893CB2217749F0008E7125 /* ErrorLog.swift */; };
0393857A2175D4B80040B850 /* AVPlayerViewExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 039385792175D4B80040B850 /* AVPlayerViewExtension.swift */; };
Expand Down Expand Up @@ -96,6 +98,7 @@
03510C6A21834EB2008F74F2 /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = System/Library/Frameworks/IOKit.framework; sourceTree = SDKROOT; };
03510C6D21834F38008F74F2 /* Aerial-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Aerial-Bridging-Header.h"; sourceTree = "<group>"; };
03510C6E21834F38008F74F2 /* IOBridge.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IOBridge.m; sourceTree = "<group>"; };
03510C722185EF76008F74F2 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
03893CB2217749F0008E7125 /* ErrorLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorLog.swift; sourceTree = "<group>"; };
039385792175D4B80040B850 /* AVPlayerViewExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AVPlayerViewExtension.swift; sourceTree = "<group>"; };
03958348217F4416008E8F9C /* Solar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Solar.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -138,6 +141,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
03510C772185EF8F008F74F2 /* CoreLocation.framework in Frameworks */,
03510C6B21834EB2008F74F2 /* IOKit.framework in Frameworks */,
FA143CE61BDA3EEF0041A82B /* AVKit.framework in Frameworks */,
);
Expand All @@ -154,6 +158,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
03510C732185EF76008F74F2 /* CoreLocation.framework in Frameworks */,
03510C6C21834EFF008F74F2 /* IOKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -192,6 +197,7 @@
FA2D7AA01BDD849E009EA54C /* Frameworks */ = {
isa = PBXGroup;
children = (
03510C722185EF76008F74F2 /* CoreLocation.framework */,
03510C6A21834EB2008F74F2 /* IOKit.framework */,
FA143CE51BDA3EEF0041A82B /* AVKit.framework */,
);
Expand Down Expand Up @@ -240,8 +246,7 @@
0395835121807D1F008E8F9C /* thumbnail@2x.png */,
FAC36F3A1BE1756D007F2A20 /* PreferencesWindow.xib */,
);
name = Resources;
path = Aerial/Resources;
path = Resources;
sourceTree = "<group>";
};
FAC36F3C1BE1756D007F2A20 /* Source */ = {
Expand Down Expand Up @@ -783,7 +788,7 @@
CLANG_ENABLE_MODULES = YES;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
INFOPLIST_FILE = Aerial/Resources/Info.plist;
INFOPLIST_FILE = "$(SRCROOT)/Resources/Info.plist";
INSTALL_PATH = "$(HOME)/Library/Screen Savers";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.johncoates.Aerial;
Expand All @@ -802,7 +807,7 @@
CLANG_ENABLE_MODULES = YES;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
INFOPLIST_FILE = Aerial/Resources/Info.plist;
INFOPLIST_FILE = "$(SRCROOT)/Resources/Info.plist";
INSTALL_PATH = "$(HOME)/Library/Screen Savers";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.johncoates.Aerial;
Expand Down
6 changes: 5 additions & 1 deletion Aerial/App/Resources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.4.4test2</string>
<string>1.4.4beta4</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand All @@ -31,6 +31,10 @@
</dict>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2015 John Coates. All rights reserved.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Aerial uses location services to calculate Sunset and Sunrise times from your position</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Aerial uses location services to calculate Sunset and Sunrise times from your position</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
Expand Down
76 changes: 75 additions & 1 deletion Aerial/Source/Controllers/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import AVKit
import AVFoundation
import ScreenSaver
import VideoToolbox

import CoreLocation
class TimeOfDay {
let title: String
var videos: [AerialVideo] = [AerialVideo]()
Expand Down Expand Up @@ -95,6 +95,7 @@ NSOutlineViewDelegate {

@IBOutlet var latitudeTextField: NSTextField!
@IBOutlet var longitudeTextField: NSTextField!
@IBOutlet var findCoordinatesButton: NSButton!

@IBOutlet var calculateCoordinatesLabel: NSTextField!

Expand Down Expand Up @@ -171,6 +172,8 @@ NSOutlineViewDelegate {

var savedBrightness: Float?

var locationManager: CLLocationManager?

// MARK: - Init
required init?(coder decoder: NSCoder) {
self.fontManager = NSFontManager.shared
Expand Down Expand Up @@ -208,6 +211,7 @@ NSOutlineViewDelegate {
longitudeFormatter.maximumSignificantDigits = 10



// This used to grab the preview player and put it in our own video preview thing.
// While kinda cool, it showed a random video that wasn't selected, and with new lifecycle, it was paused
/*if let previewPlayer = AerialView.previewPlayer {
Expand All @@ -233,6 +237,7 @@ NSOutlineViewDelegate {
iconTime1.image = NSImage(named: NSImage.touchBarHistoryTemplateName)
iconTime2.image = NSImage(named: NSImage.touchBarComposeTemplateName)
iconTime3.image = NSImage(named: NSImage.touchBarOpenInBrowserTemplateName)
findCoordinatesButton.image = NSImage(named: NSImage.touchBarOpenInBrowserTemplateName)
}

// Help popover, GVA detection requires 10.13
Expand Down Expand Up @@ -952,6 +957,7 @@ NSOutlineViewDelegate {
}

@IBAction func longitudeChange(_ sender: NSTextField) {
debugLog("longitudechange")
preferences.longitude = sender.stringValue
updateLatitudeLongitude()
}
Expand All @@ -977,6 +983,56 @@ NSOutlineViewDelegate {
let url = URL(string: "https://en.wikipedia.org/wiki/Twilight")!
workspace.open(url)
}

@IBAction func findCoordinatesButtonClick(_ sender: NSButton) {
debugLog("UI findCoordinatesButton")
/*let tm = TimeManagement.sharedInstance
tm.startLocationDetection()*/

locationManager = CLLocationManager()
locationManager!.delegate = self
locationManager!.desiredAccuracy = kCLLocationAccuracyBest
locationManager!.distanceFilter = 100
locationManager!.purpose = "Aerial uses your location to calculate sunrise and sunset times"

if CLLocationManager.locationServicesEnabled() {
debugLog("Location services enabled")
//print(locationManager.location)

_ = CLLocationManager.authorizationStatus()
/*if status == .restricted || status == .denied {
print("Location Denied")
}
else if status == .notDetermined {
print("Show ask for location")
}
else if status == .authorized {
print("This should work?")
}*/
locationManager!.startUpdatingLocation()
} else {
errorLog("Location services are disabled, please check your macOS settings!")
return
}
/*
if #available(OSX 10.14, *) {
locationManager.requestLocation()
} else {
// Fallback on earlier versions
locationManager.startUpdatingLocation()
}*/
}

func pushCoordinates(_ coordinates: CLLocationCoordinate2D)
{
latitudeTextField.stringValue = String(coordinates.latitude)
longitudeTextField.stringValue = String(coordinates.longitude)

preferences.latitude = String(coordinates.latitude)
preferences.longitude = String(coordinates.longitude)
updateLatitudeLongitude()
}
// MARK: - Brightness panel

@IBAction func dimBrightnessClick(_ button: NSButton) {
Expand Down Expand Up @@ -1645,6 +1701,24 @@ NSOutlineViewDelegate {
}*/
}

// MARK: - Core Location Delegates
extension PreferencesWindowController : CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
debugLog("LM Coordinates")
let currentLocation = locations[locations.count - 1]
pushCoordinates(currentLocation.coordinate)
locationManager!.stopUpdatingLocation() // We only want them once
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
debugLog("LMauth status change : \(status.rawValue)")
}

/*func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
errorLog("Location Manager error : \(error)")
}*/
}


// MARK: - Font Panel Delegates

Expand Down
73 changes: 68 additions & 5 deletions Aerial/Source/Models/Time/TimeManagement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import Cocoa
import CoreLocation

class TimeManagement {
class TimeManagement : NSObject {
static let sharedInstance = TimeManagement()

// Night shift
Expand All @@ -21,7 +21,8 @@ class TimeManagement {
var solar:Solar?

// MARK: - Lifecycle
init() {
override init() {
super.init()
debugLog("Time Management initialized")
_ = calculateFromCoordinates()
}
Expand Down Expand Up @@ -172,10 +173,14 @@ class TimeManagement {
default:
sunriseString = dateFormatter.string(from: (solar?.astronomicalSunrise)!)
sunsetString = dateFormatter.string(from: (solar?.astronomicalSunset)!)
}

if preferences.solarMode == Preferences.SolarMode.official.rawValue ||
preferences.solarMode == Preferences.SolarMode.strict.rawValue {
return(true, "Today’s sunrise: " + sunriseString + " Today’s sunset: " + sunsetString)
} else {
return(true, "Today’s dawn: " + sunriseString + " Today’s dusk: " + sunsetString)
}

return(true, "Today's Sunrise: " + sunriseString + " Today's Sunset: " + sunsetString)
}
}

Expand Down Expand Up @@ -218,7 +223,7 @@ class TimeManagement {
let sunriseString = dateFormatter.string(from: sunriseDate!)
let sunsetString = dateFormatter.string(from: sunsetDate!)

return (true,"Today's Sunrise: " + sunriseString + " Today's Sunset: " + sunsetString)
return (true,"Today’s sunrise: " + sunriseString + " Today’s sunset: " + sunsetString)

} else {
isNightShiftDataCached = true
Expand Down Expand Up @@ -381,4 +386,62 @@ class TimeManagement {
}
}

// MARK: - Location detection
func startLocationDetection()
{
let locationManager = CLLocationManager()
locationManager.delegate = self

if CLLocationManager.locationServicesEnabled() {
debugLog("Location services enabled")
locationManager.startUpdatingLocation()
} else {
errorLog("Location services are disabled, please check your macOS settings!")
}

/*let status = CLLocationManager.authorizationStatus()
if status == .restricted || status == .denied {
print("Location Denied")
return
}
else if status == .notDetermined {
print("Show ask for location")
return
}
else if status == .authorized {
print("This should work?")
return
}*/

if #available(OSX 10.14, *) {
print("reqloc")
locationManager.requestLocation()
} else {
// Fallback on earlier versions
}
}

}

// MARK: - Core Location Delegates
extension TimeManagement : CLLocationManagerDelegate {
/* func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation, from oldLocation: CLLocation) {
print("\(newLocation) \(oldLocation)")
}*/

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let currentLocation = locations[locations.count - 1]
print("\(currentLocation)")
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
errorLog("Location Manager error : \(error)")
}
}
23 changes: 19 additions & 4 deletions Aerial/Source/Views/AerialView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,17 @@ class AerialView: ScreenSaverView {
fontSize = 12
}

// We get the horizontal margin
var mx = CGFloat(preferences.marginX!)

if !preferences.overrideMargins {
mx = 50
}
if isPreview {
mx = 10
}
let boundingRect = CGSize(width: layer!.visibleRect.size.width-2*mx, height: layer!.visibleRect.size.height)

// Get font with a fallback in case
var font = NSFont(name: "Helvetica Neue Medium", size: 28)
if let tryFont = NSFont(name: preferences.fontName!,size: fontSize) {
Expand All @@ -735,13 +746,13 @@ class AerialView: ScreenSaverView {
// Calculate bounding box
let s = NSAttributedString(string: string, attributes: attributes)

var rect = s.boundingRect(with: layer!.visibleRect.size, options: [.truncatesLastVisibleLine, .usesLineFragmentOrigin])
var rect = s.boundingRect(with: boundingRect, options: [.truncatesLastVisibleLine, .usesLineFragmentOrigin])
// Last line won't appear if we don't adjust
rect = CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.width, height: rect.height+10)

// Rebind frame
self.textLayer.frame = rect

// At the position the user wants
if preferences.descriptionCorner == Preferences.DescriptionCorner.random.rawValue {
// Randomish, we still want something different
Expand Down Expand Up @@ -1026,15 +1037,19 @@ class AerialView: ScreenSaverView {
if (position == Preferences.DescriptionCorner.topLeft.rawValue) {
self.textLayer.anchorPoint = CGPoint(x: 0, y: 1)
self.textLayer.position = CGPoint(x: mx, y: layer!.bounds.height-my)
self.textLayer.alignmentMode = .left
} else if (position == Preferences.DescriptionCorner.bottomLeft.rawValue) {
self.textLayer.anchorPoint = CGPoint(x: 0, y: 0)
self.textLayer.position = CGPoint(x: mx, y: my)
self.textLayer.alignmentMode = .left
} else if (position == Preferences.DescriptionCorner.topRight.rawValue) {
self.textLayer.anchorPoint = CGPoint(x: 1, y: 1)
self.textLayer.position = CGPoint(x: layer!.bounds.width-mx, y: layer!.bounds.height-my)
self.textLayer.alignmentMode = .right
} else if (position == Preferences.DescriptionCorner.bottomRight.rawValue) {
self.textLayer.anchorPoint = CGPoint(x: 1, y: 0)
self.textLayer.position = CGPoint(x: layer!.bounds.width-mx, y: my)
self.textLayer.alignmentMode = .right
}
}

Expand All @@ -1044,10 +1059,10 @@ class AerialView: ScreenSaverView {
fadeAnimation.values = [0, 0, 1, 1, 0] as [NSNumber]
fadeAnimation.keyTimes = [0, Double( 1/duration ), Double( (1+AerialView.textFadeDuration)/duration ), Double( 1-AerialView.textFadeDuration/duration ), 1] as [NSNumber]
fadeAnimation.duration = duration

return fadeAnimation
}


// Create a move animation
func createMoveAnimation(layer : CALayer, to: CGPoint, duration: Double) -> CABasicAnimation {
let moveAnimation = CABasicAnimation(keyPath: "position")
moveAnimation.fromValue = layer.position
Expand Down
File renamed without changes.
Loading

0 comments on commit a13453e

Please sign in to comment.