-
Notifications
You must be signed in to change notification settings - Fork 25
/
RecentlyClosedTabs.swift
76 lines (64 loc) · 2.38 KB
/
RecentlyClosedTabs.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import Shared
open class ClosedTabsStore {
let prefs: Prefs
lazy open var tabs: [ClosedTab] = {
guard let tabsArray: Data = self.prefs.objectForKey("recentlyClosedTabs") as Any? as? Data,
let unarchivedArray = NSKeyedUnarchiver.unarchiveObject(with: tabsArray) as? [ClosedTab] else {
return []
}
return unarchivedArray
}()
public init(prefs: Prefs) {
self.prefs = prefs
}
open func addTab(_ url: URL, title: String?, faviconURL: String?) {
let recentlyClosedTab = ClosedTab(url: url, title: title ?? "", faviconURL: faviconURL ?? "")
tabs.insert(recentlyClosedTab, at: 0)
if tabs.count > 5 {
tabs.removeLast()
}
let archivedTabsArray = NSKeyedArchiver.archivedData(withRootObject: tabs)
prefs.setObject(archivedTabsArray, forKey: "recentlyClosedTabs")
}
open func clearTabs() {
prefs.removeObjectForKey("recentlyClosedTabs")
tabs = []
}
}
open class ClosedTab: NSObject, NSCoding {
public let url: URL
public let title: String?
public let faviconURL: String?
var jsonDictionary: [String: Any] {
let title = (self.title ?? "")
let faviconURL = (self.faviconURL ?? "")
let json: [String: Any] = ["title": title, "url": url, "faviconURL": faviconURL]
return json
}
init(url: URL, title: String?, faviconURL: String?) {
assert(Thread.isMainThread)
self.title = title
self.url = url
self.faviconURL = faviconURL
super.init()
}
required convenience public init?(coder: NSCoder) {
guard let url = coder.decodeObject(forKey: "url") as? URL,
let faviconURL = coder.decodeObject(forKey: "faviconURL") as? String,
let title = coder.decodeObject(forKey: "title") as? String else { return nil }
self.init(
url: url,
title: title,
faviconURL: faviconURL
)
}
open func encode(with coder: NSCoder) {
coder.encode(url, forKey: "url")
coder.encode(faviconURL, forKey: "faviconURL")
coder.encode(title, forKey: "title")
}
}