forked from mozilla-mobile/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisit.swift
125 lines (107 loc) · 3.6 KB
/
Visit.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 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
// These are taken from the Places docs
// http://mxr.mozilla.org/mozilla-central/source/toolkit/components/places/nsINavHistoryService.idl#1187
@objc public enum VisitType: Int, CaseIterable {
case unknown
/**
* This transition type means the user followed a link and got a new toplevel
* window.
*/
case link
/**
* This transition type means that the user typed the page's URL in the
* URL bar or selected it from URL bar autocomplete results, clicked on
* it from a history query (from the History sidebar, History menu,
* or history query in the personal toolbar or Places organizer).
*/
case typed
case bookmark
case embed
case permanentRedirect
case temporaryRedirect
case download
case framedLink
case recentlyClosed
}
// WKWebView has these:
/*
WKNavigationTypeLinkActivated,
WKNavigationTypeFormSubmitted,
WKNavigationTypeBackForward,
WKNavigationTypeReload,
WKNavigationTypeFormResubmitted,
WKNavigationTypeOther = -1,
*/
/**
* SiteVisit is a sop to the existing API, which expects to be able to go
* backwards from a visit to a site, and preserve the ID of the database row.
* Visit is the model of what lives on the wire: just a date and a type.
* Ultimately we'll end up with something similar to ClientAndTabs: the tabs
* don't need to know about the client, and visits don't need to know about
* the site, because they're bound together.
*
* (Furthermore, we probably shouldn't ever need something like SiteVisit
* to reach the UI: we care about "last visited", "visit count", or just
* "places ordered by frecency" — we don't care about lists of visits.)
*/
open class Visit: Hashable {
public let date: MicrosecondTimestamp
public let type: VisitType
public func hash(into hasher: inout Hasher) {
hasher.combine(date)
hasher.combine(type)
}
public init(date: MicrosecondTimestamp, type: VisitType = .unknown) {
self.date = date
self.type = type
}
open class func fromJSON(_ json: [String: Any]) -> Visit? {
if let type = json["type"] as? Int,
let typeEnum = VisitType(rawValue: type),
let date = json["date"] as? Int64, date >= 0 {
return Visit(date: MicrosecondTimestamp(date), type: typeEnum)
}
return nil
}
open func toJSON() -> [String: Any] {
let d = NSNumber(value: self.date)
let o: [String: Any] = ["type": self.type.rawValue, "date": d]
return o
}
}
public func == (lhs: Visit, rhs: Visit) -> Bool {
return lhs.date == rhs.date &&
lhs.type == rhs.type
}
open class SiteVisit: Visit {
var id: Int?
public let site: Site
public override func hash(into hasher: inout Hasher) {
hasher.combine(date)
hasher.combine(type)
hasher.combine(id)
hasher.combine(site.id)
}
public init(site: Site, date: MicrosecondTimestamp, type: VisitType = .unknown) {
self.site = site
super.init(date: date, type: type)
}
}
public func == (lhs: SiteVisit, rhs: SiteVisit) -> Bool {
if let lhsID = lhs.id, let rhsID = rhs.id {
if lhsID != rhsID {
return false
}
} else {
if lhs.id != nil || rhs.id != nil {
return false
}
}
// TODO: compare Site.
return lhs.date == rhs.date &&
lhs.type == rhs.type
}