Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
VeinGuo committed Sep 19, 2017
1 parent 5d1f0bc commit 0bc832b
Show file tree
Hide file tree
Showing 17 changed files with 472 additions and 419 deletions.
14 changes: 7 additions & 7 deletions VGPlayer/Classes/MediaCache/VGPlayerCacheManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ open class VGPlayerCacheManager: NSObject {

public override init() {
super.init()
self.ioQueue.sync { self.fileManager = FileManager() }
ioQueue.sync { fileManager = FileManager() }
}

static public func cacheDirectory() -> String {
Expand Down Expand Up @@ -65,22 +65,22 @@ open class VGPlayerCacheManager: NSObject {
}

open func cleanAllCache() {
self.ioQueue.sync {
ioQueue.sync {
do {
let cacheDirectory = VGPlayerCacheManager.cacheDirectory()
try self.fileManager.removeItem(atPath: cacheDirectory)
try fileManager.removeItem(atPath: cacheDirectory)
} catch { }
}
}

open func cleanOldFiles(completion handler: (()->())? = nil) {
self.ioQueue.sync {
ioQueue.sync {
let cacheDirectory = VGPlayerCacheManager.cacheDirectory()
var (URLsToDelete, diskCacheSize, cachedFiles) = self.cachedFiles(atPath: cacheDirectory, onlyForCacheSize: false)

for fileURL in URLsToDelete {
do {
try self.fileManager.removeItem(at: fileURL)
try fileManager.removeItem(at: fileURL)
} catch _ { }
}

Expand All @@ -104,7 +104,7 @@ open class VGPlayerCacheManager: NSObject {
diskCacheSize -= cacheSize

do {
try self.fileManager.removeItem(at: fileURL)
try fileManager.removeItem(at: fileURL)
} catch { }

URLsToDelete.append(fileURL)
Expand Down Expand Up @@ -139,7 +139,7 @@ open class VGPlayerCacheManager: NSObject {
do {
let url = URL(fileURLWithPath: fullPath)

if let directoryEnumerator = self.fileManager.enumerator(at:url, includingPropertiesForKeys: Array(resourceKeys), options: [.skipsHiddenFiles], errorHandler: nil) {
if let directoryEnumerator = fileManager.enumerator(at:url, includingPropertiesForKeys: Array(resourceKeys), options: [.skipsHiddenFiles], errorHandler: nil) {
for (_ , value) in directoryEnumerator.enumerated() {
do {
if let fileURL = value as? URL{
Expand Down
16 changes: 8 additions & 8 deletions VGPlayer/Classes/MediaCache/VGPlayerCacheMedia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ open class VGPlayerCacheMedia: NSObject, NSCoding {
}

public func encode(with aCoder: NSCoder) {
aCoder.encode(self.contentType, forKey: "contentType")
aCoder.encode(self.isByteRangeAccessSupported, forKey: "isByteRangeAccessSupported")
aCoder.encode(self.contentLength, forKey: "contentLength")
aCoder.encode(self.downloadedLength, forKey: "downloadedLength")
aCoder.encode(contentType, forKey: "contentType")
aCoder.encode(isByteRangeAccessSupported, forKey: "isByteRangeAccessSupported")
aCoder.encode(contentLength, forKey: "contentLength")
aCoder.encode(downloadedLength, forKey: "downloadedLength")
}

public required init(coder aDecoder: NSCoder) {
super.init()
self.contentType = aDecoder.decodeObject(forKey: "contentType") as? String
self.isByteRangeAccessSupported = aDecoder.decodeBool(forKey: "isByteRangeAccessSupported")
self.contentLength = aDecoder.decodeInt64(forKey: "contentLength")
contentType = aDecoder.decodeObject(forKey: "contentType") as? String
isByteRangeAccessSupported = aDecoder.decodeBool(forKey: "isByteRangeAccessSupported")
contentLength = aDecoder.decodeInt64(forKey: "contentLength")
if let downloadedLength = aDecoder.decodeObject(forKey: "downloadedLength") as? UInt64 {
self.downloadedLength = downloadedLength
} else {
self.downloadedLength = 0
downloadedLength = 0
}

}
Expand Down
42 changes: 21 additions & 21 deletions VGPlayer/Classes/MediaCache/VGPlayerCacheMediaConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ open class VGPlayerCacheMediaConfiguration:NSObject, NSCoding, NSCopying {

public fileprivate(set) var progress: Double = 0.0 {
didSet {
if let contentLength = self.cacheMedia?.contentLength,
let downloadedBytes = self.downloadedBytes {
if let contentLength = cacheMedia?.contentLength,
let downloadedBytes = downloadedBytes {
progress = Double(downloadedBytes / contentLength)
}
}
Expand All @@ -33,21 +33,21 @@ open class VGPlayerCacheMediaConfiguration:NSObject, NSCoding, NSCopying {
didSet {
var bytes = 0

self.cacheSegmentQueue.sync {
for range in self.cacheSegments {
cacheSegmentQueue.sync {
for range in cacheSegments {
bytes += range.rangeValue.length
}
}
self.downloadedBytes = Int64(bytes)
downloadedBytes = Int64(bytes)
}
}

public fileprivate(set) var downloadSpeed: Double? { // kb/s
didSet {
var bytes: UInt64 = 0
var time = 0.0
if self.downloadInfo.count > 0 {
self.cacheDownloadInfoQueue.sync {
if downloadInfo.count > 0 {
cacheDownloadInfoQueue.sync {
for a in downloadInfo {
if let arr = a as? Array<Any>{
bytes += arr.first as! UInt64
Expand All @@ -58,7 +58,7 @@ open class VGPlayerCacheMediaConfiguration:NSObject, NSCoding, NSCopying {
}
}
}
self.downloadSpeed = Double(bytes) / 1024.0 / time
downloadSpeed = Double(bytes) / 1024.0 / time
}
}

Expand All @@ -79,21 +79,21 @@ open class VGPlayerCacheMediaConfiguration:NSObject, NSCoding, NSCopying {
}

public func encode(with aCoder: NSCoder) {
aCoder.encode(self.fileName, forKey: "fileName")
aCoder.encode(self.cacheSegments, forKey: "cacheSegments")
aCoder.encode(self.downloadInfo, forKey: "downloadInfo")
aCoder.encode(self.cacheMedia, forKey: "cacheMedia")
aCoder.encode(self.url, forKey: "url")
aCoder.encode(fileName, forKey: "fileName")
aCoder.encode(cacheSegments, forKey: "cacheSegments")
aCoder.encode(downloadInfo, forKey: "downloadInfo")
aCoder.encode(cacheMedia, forKey: "cacheMedia")
aCoder.encode(url, forKey: "url")
}
public func copy(with zone: NSZone? = nil) -> Any {
let confi = VGPlayerCacheMediaConfiguration()
confi.filePath = self.filePath
confi.fileName = self.fileName
confi.cacheSegments = self.cacheSegments
confi.cacheMedia = self.cacheMedia
confi.url = self.url
confi.fileName = self.fileName
confi.downloadInfo = self.downloadInfo
confi.filePath = filePath
confi.fileName = fileName
confi.cacheSegments = cacheSegments
confi.cacheMedia = cacheMedia
confi.url = url
confi.fileName = fileName
confi.downloadInfo = downloadInfo
return confi
}

Expand Down Expand Up @@ -125,7 +125,7 @@ open class VGPlayerCacheMediaConfiguration:NSObject, NSCoding, NSCopying {
extension VGPlayerCacheMediaConfiguration {
open func save() {
self.cacheSegmentQueue.sync {
let _ = NSKeyedArchiver.archiveRootObject(self, toFile: self.filePath!)
let _ = NSKeyedArchiver.archiveRootObject(self, toFile: filePath!)
}
}

Expand Down
58 changes: 29 additions & 29 deletions VGPlayer/Classes/MediaCache/VGPlayerCacheMediaWorker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ open class VGPlayerCacheMediaWorker: NSObject {

public init(url: URL) {
let path = VGPlayerCacheManager.cacheFilePath(for: url)
self.filePath = path
filePath = path
let fileManager = FileManager.default
let cacheFolder = (path as NSString).deletingLastPathComponent
var err: Error?
Expand All @@ -52,7 +52,7 @@ open class VGPlayerCacheMediaWorker: NSObject {
let fileURL = URL(fileURLWithPath: path)

do {
try self.readFileHandle = FileHandle(forReadingFrom: fileURL)
try readFileHandle = FileHandle(forReadingFrom: fileURL)
} catch {
err = error
}
Expand All @@ -63,26 +63,26 @@ open class VGPlayerCacheMediaWorker: NSObject {
}

do {
try self.writeFileHandle = FileHandle(forWritingTo: fileURL)
self.cacheConfiguration = VGPlayerCacheMediaConfiguration.configuration(filePath: path)
self.cacheConfiguration?.url = url
try writeFileHandle = FileHandle(forWritingTo: fileURL)
cacheConfiguration = VGPlayerCacheMediaConfiguration.configuration(filePath: path)
cacheConfiguration?.url = url
} catch {
err = error
}
}
}

self.setupError = err;
setupError = err;
super.init()
}

open func cache(_ data: Data, forRange range: NSRange, closure: (Bool) -> Void) {
self.writeFileQueue.sync {
writeFileQueue.sync {

if let _ = self.writeFileHandle?.seek(toFileOffset: UInt64(range.location)),
let _ = self.writeFileHandle?.write(data) {
self.writeBytes += Double(data.count)
self.cacheConfiguration?.addCache(range)
if let _ = writeFileHandle?.seek(toFileOffset: UInt64(range.location)),
let _ = writeFileHandle?.write(data) {
writeBytes += Double(data.count)
cacheConfiguration?.addCache(range)
closure(true)
} else {
closure(false)
Expand All @@ -91,8 +91,8 @@ open class VGPlayerCacheMediaWorker: NSObject {
}

open func cache(forRange range: NSRange) -> Data? {
self.readFileHandle?.seek(toFileOffset: UInt64(range.location))
let data = self.readFileHandle?.readData(ofLength: range.length)
readFileHandle?.seek(toFileOffset: UInt64(range.location))
let data = readFileHandle?.readData(ofLength: range.length)
return data
}

Expand All @@ -104,7 +104,7 @@ open class VGPlayerCacheMediaWorker: NSObject {

let endOffset = range.location + range.length

if let cachedSegments = self.cacheConfiguration?.cacheSegments {
if let cachedSegments = cacheConfiguration?.cacheSegments {

for (_, value) in cachedSegments.enumerated() {
let segmentRange = value.rangeValue
Expand Down Expand Up @@ -167,43 +167,43 @@ open class VGPlayerCacheMediaWorker: NSObject {
}

open func set(cacheMedia: VGPlayerCacheMedia) -> Bool {
self.cacheConfiguration?.cacheMedia = cacheMedia
if let _ = self.writeFileHandle?.truncateFile(atOffset: UInt64(cacheMedia.contentLength)),
let _ = self.writeFileHandle?.synchronizeFile(){
cacheConfiguration?.cacheMedia = cacheMedia
if let _ = writeFileHandle?.truncateFile(atOffset: UInt64(cacheMedia.contentLength)),
let _ = writeFileHandle?.synchronizeFile(){
return true
} else {
return false
}
}

open func save() {
self.writeFileQueue.sync {
self.writeFileHandle?.synchronizeFile()
self.cacheConfiguration?.save()
writeFileQueue.sync {
writeFileHandle?.synchronizeFile()
cacheConfiguration?.save()
}
}

open func startWritting() {
if !self.isWritting {
if !isWritting {
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: .UIApplicationDidEnterBackground, object: nil)
}
self.isWritting = true
self.starWriteDate = NSDate()
self.writeBytes = 0.0
isWritting = true
starWriteDate = NSDate()
writeBytes = 0.0
}

open func finishWritting() {
if self.isWritting {
self.isWritting = false
if isWritting {
isWritting = false
NotificationCenter.default.removeObserver(self)
if let starWriteDate = self.starWriteDate {
if let starWriteDate = starWriteDate {
let time = Date().timeIntervalSince(starWriteDate as Date)
self.cacheConfiguration?.add(UInt64(self.writeBytes), time: time)
cacheConfiguration?.add(UInt64(writeBytes), time: time)
}
}
}

@objc internal func applicationDidEnterBackground(_ notification: Notification) {
self.save()
save()
}
}
2 changes: 1 addition & 1 deletion VGPlayer/Classes/MediaCache/VGPlayerCacheSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ open class VGPlayerCacheSession: NSObject {
public override init() {
let queue = OperationQueue()
queue.name = "com.vgplayer.downloadSession"
self.downloadQueue = queue
downloadQueue = queue
}
}
Loading

0 comments on commit 0bc832b

Please sign in to comment.