Skip to content

Commit

Permalink
Merge pull request #817 from glouel/master
Browse files Browse the repository at this point in the history
Starts moving stuff to AppSupport, this is probably incomplete but se…
  • Loading branch information
glouel authored Jun 24, 2019
2 parents dd0131a + a0c3bad commit b9c96d2
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 53 deletions.
6 changes: 3 additions & 3 deletions Aerial/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var topLevelObjects: NSArray? = NSArray()
print("bundle \(bundle)")

let res = Bundle.main.loadNibNamed(nibName, owner: preferencesWindowController, topLevelObjects: &topLevelObjects)
//let res = Bundle.main.loadNibNamed(nibName, owner: preferencesWindowController, topLevelObjects: &topLevelObjects)

/*let res = bundle.loadNibNamed(nibName,
let res = bundle.loadNibNamed(nibName,
owner: preferencesWindowController,
topLevelObjects: &topLevelObjects)
*/

print("res")
return topLevelObjects! as [AnyObject]
}
Expand Down
2 changes: 1 addition & 1 deletion Aerial/Source/Controllers/PWC Tabs/PWC+Videos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ extension PreferencesWindowController {

// We need catalina for HDR !
if #available(OSX 10.15, *) {
if preferences.useHDR {
if !preferences.useHDR {
useHDRCheckbox.state = .off
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions Aerial/Source/Controllers/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ final class PreferencesWindowController: NSWindowController, NSOutlineViewDataSo
setupVideosTab()
setupDisplaysTab()
setupTextTab()
setupTimeTab()
//setupTimeTab()
setupBrightnessTab()
setupCacheTab()
setupUpdatesTab()
Expand All @@ -339,13 +339,13 @@ final class PreferencesWindowController: NSWindowController, NSOutlineViewDataSo
}

// We also load our CustomVideos nib here
let bundle = Bundle(for: PreferencesWindowController.self)
/*let bundle = Bundle(for: PreferencesWindowController.self)
var topLevelObjects: NSArray? = NSArray()
if !bundle.loadNibNamed(NSNib.Name("CustomVideos"),
owner: customVideosController,
topLevelObjects: &topLevelObjects) {
errorLog("Could not load nib for CustomVideos, please report")
}
}*/
}

override func windowDidLoad() {
Expand Down
63 changes: 56 additions & 7 deletions Aerial/Source/Models/Cache/VideoCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,61 @@ final class VideoCache {
let URL: URL

static var computedCacheDirectory: String?
static var computedAppSupportDirectory: String?

static var appSupportDirectory: String? {
// We only process this once if successful
if computedAppSupportDirectory != nil {
return computedAppSupportDirectory
}

var foundDirectory: String?

let appSupportPaths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory,
.userDomainMask,
true)

if appSupportPaths.isEmpty {
errorLog("Couldn't find appSupport paths!")
return nil
}
let appSupportDirectory = appSupportPaths[0] as NSString
if aerialFolderExists(at: appSupportDirectory) {
debugLog("app support exists")
foundDirectory = appSupportDirectory.appendingPathComponent("Aerial")
} else {
debugLog("creating app support directory")
// We create in user appSupport which may be containairized
// so ~/Library/Application Support/ on pre 10.15
// or ~/Library/Containers/com.apple.ScreenSaver.Engine.legacyScreenSaver
// /Data/Library/Application Support/
foundDirectory = appSupportDirectory.appendingPathComponent("Aerial")

let fileManager = FileManager.default
if fileManager.fileExists(atPath: foundDirectory!) == false {
do {
try fileManager.createDirectory(atPath: foundDirectory!,
withIntermediateDirectories: false, attributes: nil)
} catch let error {
errorLog("Couldn't create appSupport directory in User directory: \(error)")
errorLog("FATAL : There's nothing more we can do at this point")
return nil
}
}
}

// Cache the computed value
computedAppSupportDirectory = foundDirectory
return computedAppSupportDirectory
}

static var cacheDirectory: String? {

// We only process this once if successful
if computedCacheDirectory != nil {
return computedCacheDirectory
}
debugLog("appSupport : \(appSupportDirectory)")

var cacheDirectory: String?
let preferences = Preferences.sharedInstance
Expand Down Expand Up @@ -56,13 +105,13 @@ final class VideoCache {
.localDomainMask,
true)
let localCacheDirectory = localCachePaths[0] as NSString
if aerialCacheExists(at: localCacheDirectory) {
if aerialFolderExists(at: localCacheDirectory) {
debugLog("local cache exists")
cacheDirectory = localCacheDirectory.appendingPathComponent("Aerial")
}
}

if cacheDirectory == nil && aerialCacheExists(at: userCacheDirectory) {
if cacheDirectory == nil && aerialFolderExists(at: userCacheDirectory) {
debugLog("user cache exists")
cacheDirectory = userCacheDirectory.appendingPathComponent("Aerial")
} else {
Expand All @@ -88,15 +137,15 @@ final class VideoCache {
computedCacheDirectory = cacheDirectory

// Save it
preferences.customCacheDirectory = computedCacheDirectory
preferences.synchronize()
//preferences.customCacheDirectory = computedCacheDirectory
//preferences.synchronize()
debugLog("cache to be used : \(String(describing: cacheDirectory))")
return cacheDirectory
}

static func aerialCacheExists(at: NSString) -> Bool {
let aerialCache = at.appendingPathComponent("Aerial")
if FileManager.default.fileExists(atPath: aerialCache as String) {
static func aerialFolderExists(at: NSString) -> Bool {
let aerialFolder = at.appendingPathComponent("Aerial")
if FileManager.default.fileExists(atPath: aerialFolder as String) {
return true
} else {
return false
Expand Down
4 changes: 2 additions & 2 deletions Aerial/Source/Models/Downloads/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ extension DownloadOperation: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
do {
let manager = FileManager.default
var destinationURL = URL(fileURLWithPath: VideoCache.cacheDirectory!)
var destinationURL = URL(fileURLWithPath: VideoCache.appSupportDirectory!)

// tvOS11 and tvOS10 JSONs are named entries.json, so we rename them here
if downloadTask.originalRequest!.url!.absoluteString.contains("2x/entries.json") {
Expand Down Expand Up @@ -172,7 +172,7 @@ extension DownloadOperation: URLSessionTaskDelegate {

// Extract json
let process: Process = Process()
let cacheDirectory = VideoCache.cacheDirectory!
let cacheDirectory = VideoCache.appSupportDirectory!

var cacheResourcesString = cacheDirectory
cacheResourcesString.append(contentsOf: "/resources.tar")
Expand Down
25 changes: 7 additions & 18 deletions Aerial/Source/Models/ManifestLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ class ManifestLoader {
// MARK: - Custom videos
func loadCustomVideos() {
do {
if let cacheDirectory = VideoCache.cacheDirectory {
if let cacheDirectory = VideoCache.appSupportDirectory {
// tvOS12
var cacheFileUrl = URL(fileURLWithPath: cacheDirectory as String)
cacheFileUrl.appendPathComponent("customvideos.json")
Expand All @@ -404,7 +404,7 @@ class ManifestLoader {
}

func saveCustomVideos() {
if let cvf = customVideoFolders, let cacheDirectory = VideoCache.cacheDirectory {
if let cvf = customVideoFolders, let cacheDirectory = VideoCache.appSupportDirectory {
var cacheFileUrl = URL(fileURLWithPath: cacheDirectory as String)
cacheFileUrl.appendPathComponent("customvideos.json")

Expand Down Expand Up @@ -473,7 +473,7 @@ class ManifestLoader {
dayCheck = 30
}

let cacheDirectory = VideoCache.cacheDirectory!
let cacheDirectory = VideoCache.appSupportDirectory!
var cacheResourcesString = cacheDirectory
cacheResourcesString.append(contentsOf: "/backups")
let cacheUrl = URL(fileURLWithPath: cacheResourcesString)
Expand All @@ -500,7 +500,7 @@ class ManifestLoader {

func moveOldManifests() {
debugLog("move")
let cacheDirectory = VideoCache.cacheDirectory!
let cacheDirectory = VideoCache.appSupportDirectory!
var cacheResourcesString = cacheDirectory

let dateFormatter = DateFormatter()
Expand Down Expand Up @@ -550,7 +550,7 @@ class ManifestLoader {

// Check if a Manifest is saved in our cache directory
func isManifestCached(manifest: Manifests) -> Bool {
if let cacheDirectory = VideoCache.cacheDirectory {
if let cacheDirectory = VideoCache.appSupportDirectory {
let fileManager = FileManager.default

var cacheResourcesString = cacheDirectory
Expand All @@ -568,7 +568,7 @@ class ManifestLoader {

// Load the JSON Data cached on disk
func loadCachedManifests() {
if let cacheDirectory = VideoCache.cacheDirectory {
if let cacheDirectory = VideoCache.appSupportDirectory {
// tvOS12
var cacheFileUrl = URL(fileURLWithPath: cacheDirectory as String)
cacheFileUrl.appendPathComponent("entries.json")
Expand Down Expand Up @@ -652,17 +652,6 @@ class ManifestLoader {

self.loadedManifest = processedVideos

/*
// POI Extracter code
infoLog("\(processedVideos.count) videos processed !")
let poiStringProvider = PoiStringProvider.sharedInstance
for video in processedVideos {
infoLog(video.name + " " + video.secondaryName)
for poi in video.poi {
infoLog(poi.key + ": " + poiStringProvider.getString(key: poi.value))
}
}*/

debugLog("Total videos processed : \(processedVideos.count) callbacks : \(callbacks.count)")
// callbacks
for callback in self.callbacks {
Expand Down Expand Up @@ -930,7 +919,7 @@ class ManifestLoader {
// swiftlint:disable:next cyclomatic_complexity
func moveOldVideos() {
debugLog("move old videos")
let cacheDirectory = VideoCache.cacheDirectory!
let cacheDirectory = VideoCache.appSupportDirectory!
var cacheResourcesString = cacheDirectory

let dateFormatter = DateFormatter()
Expand Down
10 changes: 9 additions & 1 deletion Aerial/Source/Models/Time/TimeManagement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,16 @@ final class TimeManagement: NSObject {

// swiftlint:disable:next cyclomatic_complexity large_tuple
func getNightShiftInformation() -> (Bool, sunrise: Date?, sunset: Date?, error: String?) {
// Catalina workaround, /usr/bin/corebrightnessdiag no longer exists
return (false, nil, nil, "Nightshift disabled because of Catalina")

/*

if isNightShiftDataCached {
return (nightShiftAvailable, nightShiftSunrise, nightShiftSunset, nil)
}



let (nsInfo, ts) = shell(launchPath: "/usr/bin/corebrightnessdiag", arguments: ["nightshift-internal"])

if ts != 0 {
Expand Down Expand Up @@ -311,6 +317,8 @@ final class TimeManagement: NSObject {
// /usr/bin/corebrightnessdiag nightshift-internal | grep nextSunset | cut -d \" -f2
warnLog("Location services may be disabled, Night Shift can't detect Sunrise and Sunset times without them")
return (false, nil, nil, "Location services may be disabled")

*/
}

private func shell(launchPath: String, arguments: [String] = []) -> (String?, Int32) {
Expand Down
Loading

0 comments on commit b9c96d2

Please sign in to comment.