forked from Ascoware/get-iplayer-automator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewProgrammeHistory.swift
80 lines (64 loc) · 2.9 KB
/
NewProgrammeHistory.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// NewProgrammeHistory.swift
// Get iPlayer Automator
//
// Created by Scott Kovatch on 3/16/18.
//
import Foundation
import CocoaLumberjackSwift
@objc public class NewProgrammeHistory: NSObject {
@objc public var programmeHistoryArray = [ProgrammeHistoryObject]()
var itemsAdded = false
var timeIntervalSince1970UTC: TimeInterval = 0
var dateFound = ""
static let _sharedInstance = NewProgrammeHistory()
@objc public static func sharedInstance() -> NewProgrammeHistory {
return _sharedInstance
}
var historyFile: URL {
if let applicationSupportPath = FileManager.default.applicationSupportDirectory() {
return URL(fileURLWithPath: applicationSupportPath.appending("/").appending("history.gia"))
}
return URL(fileURLWithPath: NSHomeDirectory().appending("/.get_iplayer/history.gia"))
}
private override init() {
timeIntervalSince1970UTC = Date().timeIntervalSince1970
timeIntervalSince1970UTC += Double(NSTimeZone.system.secondsFromGMT(for: Date()))
timeIntervalSince1970UTC /= 24 * 60 * 60
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE MMM dd"
dateFound = dateFormatter.string(from: Date())
super.init()
do {
let data = try Data(contentsOf: historyFile)
if let unarchivedHistory = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? [ProgrammeHistoryObject] {
programmeHistoryArray = unarchivedHistory
}
} catch {
DDLogError("Couldn't read program history file.")
}
/* Cull history if > 3,000 entries */
while programmeHistoryArray.count > 3000 {
programmeHistoryArray.remove(at: 0)
}
}
@objc func add(name: String?, tvChannel: String?, networkName: String?) {
itemsAdded = true
let newEntry = ProgrammeHistoryObject(sortKey: timeIntervalSince1970UTC, programmeName: name ?? "", dateFound: dateFound, tvChannel: tvChannel ?? "", networkName: networkName ?? "")
programmeHistoryArray.append(newEntry)
}
@objc func flushHistoryToDisk() {
itemsAdded = false
/* Sort history array and flush to disk */
let sort1 = NSSortDescriptor(key: "sortKey", ascending: true)
let sort2 = NSSortDescriptor(key: "programmeName", ascending: true)
let sort3 = NSSortDescriptor(key: "tvChannel", ascending: true)
programmeHistoryArray = (programmeHistoryArray as NSArray).sortedArray(using: [sort1, sort2, sort3]) as? [ProgrammeHistoryObject] ?? programmeHistoryArray
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: programmeHistoryArray, requiringSecureCoding: false)
try data.write(to: historyFile)
} catch {
DDLogError("Couldn't write program history file.")
}
}
}