Skip to content

Commit

Permalink
SwiftLint compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoates committed Sep 23, 2016
1 parent b09f5fc commit ad50032
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 109 deletions.
13 changes: 13 additions & 0 deletions .swiftlint.yml
@@ -0,0 +1,13 @@
disabled_rules:
- trailing_whitespace
- force_cast
- function_body_length
- type_body_length
- file_length

variable_name:
min_length:
warning: 2
error: 2
line_length:
warning: 120
17 changes: 17 additions & 0 deletions Aerial.xcodeproj/project.pbxproj
Expand Up @@ -237,6 +237,7 @@
isa = PBXNativeTarget;
buildConfigurationList = FA143CE01BDA3E880041A82B /* Build configuration list for PBXNativeTarget "Aerial Test" */;
buildPhases = (
FA74B8481D94DCE0004FE056 /* ShellScript */,
FA143CD21BDA3E880041A82B /* Sources */,
FA143CD31BDA3E880041A82B /* Frameworks */,
FA143CD41BDA3E880041A82B /* Resources */,
Expand Down Expand Up @@ -334,6 +335,22 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
FA74B8481D94DCE0004FE056 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if which swiftlint >/dev/null; then\nswiftlint\nelse\necho \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
FA143CD21BDA3E880041A82B /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down
23 changes: 9 additions & 14 deletions Aerial/Source/Controllers/PreferencesWindowController.swift
Expand Up @@ -34,16 +34,16 @@ class City {
if timeOfDay.lowercased() == "night" {
video.arrayPosition = night.videos.count
night.videos.append(video)
}
else {
} else {
video.arrayPosition = day.videos.count
day.videos.append(video)
}
}
}

@objc(PreferencesWindowController)
class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource, NSOutlineViewDelegate, VideoDownloadDelegate {
class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource,
NSOutlineViewDelegate, VideoDownloadDelegate {

@IBOutlet var outlineView: NSOutlineView!
@IBOutlet var playerView: AVPlayerView!
Expand Down Expand Up @@ -113,7 +113,7 @@ class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource,
let color = NSColor(calibratedRed: 0.18, green: 0.39, blue: 0.76, alpha: 1)
let link = projectPageLink.attributedTitle
let coloredLink = NSMutableAttributedString(attributedString: link)
let fullRange = NSMakeRange(0, coloredLink.length)
let fullRange = NSRange(location: 0, length: coloredLink.length)
coloredLink.addAttribute(NSForegroundColorAttributeName,
value: color,
range: fullRange)
Expand Down Expand Up @@ -217,7 +217,7 @@ class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource,
// MARK: - Manifest

func loadJSON() {
if (PreferencesWindowController.loadedJSON) {
if PreferencesWindowController.loadedJSON {
return
}
PreferencesWindowController.loadedJSON = true
Expand All @@ -244,7 +244,6 @@ class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource,
videos.append(video)
}


self.videos = videos

// sort cities by name
Expand Down Expand Up @@ -314,10 +313,9 @@ class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource,
case is City:
let city = item as! City

if (index == 0 && city.day.videos.count > 0) {
if index == 0 && city.day.videos.count > 0 {
return city.day
}
else {
} else {
return city.night
}

Expand All @@ -330,7 +328,8 @@ class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource,
}
}

func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? {
func outlineView(_ outlineView: NSOutlineView,
objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? {
switch item {
case is City:
let city = item as! City
Expand Down Expand Up @@ -418,7 +417,6 @@ class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource,
view.checkButton.state = NSOffState
}


view.onCheck = { checked in
self.preferences.setVideo(videoID: video.id,
inRotation: checked)
Expand All @@ -445,7 +443,6 @@ class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource,
player.replaceCurrentItem(with: item)
player.play()


return true
case is TimeOfDay:
return false
Expand Down Expand Up @@ -498,7 +495,6 @@ class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource,
return video.isAvailableOffline == false
}


if uncached.count == 0 {
cacheStatusLabel.stringValue = "All videos have been cached"
return
Expand All @@ -510,7 +506,6 @@ class PreferencesWindowController: NSWindowController, NSOutlineViewDataSource,
totalProgress.doubleValue = Double(manifestVideos.count) - Double(uncached.count)
NSLog("total process max value: \(totalProgress.maxValue), current value: \(totalProgress.doubleValue)")


let video = uncached[0]

// find video that hasn't been cached
Expand Down
2 changes: 0 additions & 2 deletions Aerial/Source/Models/AerialVideo.swift
Expand Up @@ -8,7 +8,6 @@

import Foundation


class AerialVideo {
let id: String
let name: String
Expand All @@ -25,7 +24,6 @@ class AerialVideo {
}
}


init(id: String, name: String, type: String,
timeOfDay: String, url: String) {
self.id = id
Expand Down
10 changes: 6 additions & 4 deletions Aerial/Source/Models/Cache/AssetLoaderDelegate.swift
Expand Up @@ -22,8 +22,7 @@ func CachedOrCachingAsset(_ URL: Foundation.URL) -> AVURLAsset {
return asset
}


class AssetLoaderDelegate : NSObject, AVAssetResourceLoaderDelegate, VideoLoaderDelegate {
class AssetLoaderDelegate: NSObject, AVAssetResourceLoaderDelegate, VideoLoaderDelegate {

let URL: Foundation.URL
var videoLoaders: [VideoLoader] = []
Expand Down Expand Up @@ -56,7 +55,8 @@ class AssetLoaderDelegate : NSObject, AVAssetResourceLoaderDelegate, VideoLoader
}

// MARK: - Asset Resource Loader Delegate
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, didCancel loadingRequest: AVAssetResourceLoadingRequest) {
func resourceLoader(_ resourceLoader: AVAssetResourceLoader,
didCancel loadingRequest: AVAssetResourceLoadingRequest) {
// debugLog("cancelled load request: \(loadingRequest)")

var remove: VideoLoader?
Expand All @@ -80,7 +80,9 @@ class AssetLoaderDelegate : NSObject, AVAssetResourceLoaderDelegate, VideoLoader
}
}

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
func resourceLoader(_ resourceLoader: AVAssetResourceLoader,
shouldWaitForLoadingOfRequestedResource
loadingRequest: AVAssetResourceLoadingRequest) -> Bool {

// check if cache can fulfill this without a request
if videoCache.canFulfillLoadingRequest(loadingRequest) {
Expand Down
17 changes: 6 additions & 11 deletions Aerial/Source/Models/Cache/VideoCache.swift
Expand Up @@ -10,7 +10,6 @@ import Foundation
import AVFoundation
import ScreenSaver


class VideoCache {
var videoData: Data
var mutableVideoData: NSMutableData?
Expand Down Expand Up @@ -48,9 +47,9 @@ class VideoCache {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: appCacheDirectory as String) == false {
do {
try fileManager.createDirectory(atPath: appCacheDirectory as String, withIntermediateDirectories: false, attributes: nil)
}
catch let error {
try fileManager.createDirectory(atPath: appCacheDirectory as String,
withIntermediateDirectories: false, attributes: nil)
} catch let error {
NSLog("Aerial Error: Couldn't create cache directory: \(error)")
return nil
}
Expand All @@ -64,7 +63,6 @@ class VideoCache {
NSLog("Aerial Error: Couldn't get video cache path!")
return false
}


let fileManager = FileManager.default

Expand Down Expand Up @@ -167,8 +165,7 @@ class VideoCache {

do {
try mutableVideoData.write(toFile: videoCachePath, options: .atomicWrite)
}
catch let error {
} catch let error {
NSLog("Aerial Error: Couldn't write cache file: \(error)")
}
}
Expand Down Expand Up @@ -236,7 +233,7 @@ class VideoCache {
// Whether the video cache can fulfill this request
func canFulfillLoadingRequest(_ loadingRequest: AVAssetResourceLoadingRequest) -> Bool {

if (loading == false) {
if !loading {
return true
}

Expand All @@ -261,7 +258,6 @@ class VideoCache {
return false
}


// MARK: - Consolidating

func consolidateLoadedRanges() {
Expand Down Expand Up @@ -289,8 +285,7 @@ class VideoCache {
consolidatedRanges.remove(at: lastIndex!)
consolidatedRanges.append(previousRange!)
continue
}
else {
} else {
// skip adding this to the array, previous range is already bigger
// debugLog("skipping add of \(range), previous: \(previousRange)")
continue
Expand Down
38 changes: 20 additions & 18 deletions Aerial/Source/Models/Cache/VideoDownload.swift
Expand Up @@ -8,11 +8,12 @@

import Foundation


protocol VideoDownloadDelegate : NSObjectProtocol {
func videoDownload(_ videoDownload: VideoDownload , finished success: Bool, errorMessage: String?)
protocol VideoDownloadDelegate: NSObjectProtocol {
func videoDownload(_ videoDownload: VideoDownload,
finished success: Bool, errorMessage: String?)
// bytes received for bytes/second count
func videoDownload(_ videoDownload: VideoDownload, receivedBytes: Int, progress: Float)
func videoDownload(_ videoDownload: VideoDownload,
receivedBytes: Int, progress: Float)
}

class VideoDownloadStream {
Expand All @@ -29,7 +30,7 @@ class VideoDownloadStream {
}
}

class VideoDownload : NSObject, NSURLConnectionDataDelegate {
class VideoDownload: NSObject, NSURLConnectionDataDelegate {
var streams: [VideoDownloadStream] = []
weak var delegate: VideoDownloadDelegate!

Expand Down Expand Up @@ -67,8 +68,8 @@ class VideoDownload : NSObject, NSURLConnectionDataDelegate {
debugLog("Starting download for range \(requestRangeField)")
}


guard let connection = NSURLConnection(request: request as URLRequest, delegate: self, startImmediately: false) else {
guard let connection = NSURLConnection(request: request as URLRequest,
delegate: self, startImmediately: false) else {
NSLog("Aerial: Error creating connection with request: \(request)")
return
}
Expand Down Expand Up @@ -115,14 +116,13 @@ class VideoDownload : NSObject, NSURLConnectionDataDelegate {
// let queue = DispatchQueue.main
for i in 0 ..< streamCount {
let isLastStream: Bool = i == (streamCount - 1)
var range: NSRange = NSMakeRange(offset, streamPiece)
var range = NSRange(location: offset, length: streamPiece)

if isLastStream {
let bytesLeft = contentLength - offset
range = NSMakeRange(offset, bytesLeft)
range = NSRange(location: offset, length: bytesLeft)
debugLog("last stream range: \(range)")
}


let delay = DispatchTime.now() + Double(Int64(delayTime * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
queue.asyncAfter(deadline: delay) {
Expand All @@ -144,7 +144,8 @@ class VideoDownload : NSObject, NSURLConnectionDataDelegate {
return
}

let replaceRange = NSMakeRange(stream.downloadOffset, receivedData.count)
let replaceRange = NSRange(location: stream.downloadOffset,
length: receivedData.count)
videoData.replaceBytes(in: replaceRange, withBytes: (receivedData as NSData).bytes)
stream.downloadOffset += receivedData.count
}
Expand All @@ -165,8 +166,7 @@ class VideoDownload : NSObject, NSURLConnectionDataDelegate {
var errorMessage: String?
do {
try videoData.write(toFile: videoCachePath, options: .atomicWrite)
}
catch let error {
} catch let error {
NSLog("Aerial Error: Couldn't write cache file: \(error)")
errorMessage = "Couldn't write to cache file!"
success = false
Expand Down Expand Up @@ -201,8 +201,7 @@ class VideoDownload : NSObject, NSURLConnectionDataDelegate {
})

return
}
else {
} else {
// get real offset of receiving data

queue.async(execute: { () -> Void in
Expand Down Expand Up @@ -275,10 +274,11 @@ class VideoDownload : NSObject, NSURLConnectionDataDelegate {
// MARK: - Range
func startOffsetFromResponse(_ response: URLResponse) -> Int? {
// get range response
var regex : NSRegularExpression!
var regex: NSRegularExpression!
do {
// Check to see if the server returned a valid byte-range
regex = try NSRegularExpression(pattern: "bytes (\\d+)-\\d+/\\d+", options: NSRegularExpression.Options.caseInsensitive)
regex = try NSRegularExpression(pattern: "bytes (\\d+)-\\d+/\\d+",
options: NSRegularExpression.Options.caseInsensitive)
} catch let error as NSError {
NSLog("Aerial: Error formatting regex: \(error)")
return nil
Expand All @@ -291,7 +291,9 @@ class VideoDownload : NSObject, NSURLConnectionDataDelegate {
return nil
}

guard let match : NSTextCheckingResult = regex.firstMatch(in: contentRange as String, options: NSRegularExpression.MatchingOptions.anchored, range: NSMakeRange(0, contentRange.length)) else {
guard let match = regex.firstMatch(in: contentRange as String,
options: NSRegularExpression.MatchingOptions.anchored,
range: NSRange(location:0, length: contentRange.length)) else {
debugLog("Weird, couldn't make a regex match for byte offset: \(contentRange)")
return nil
}
Expand Down

0 comments on commit ad50032

Please sign in to comment.