-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathSQLiteHistoryFactories.swift
74 lines (61 loc) · 2.68 KB
/
SQLiteHistoryFactories.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
/* 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
/*
* Factory methods for converting rows from SQLite into model objects
*/
extension SQLiteHistory {
class func basicHistoryColumnFactory(_ row: SDRow) -> Site {
let id = row["historyID"] as? Int
let url = row["url"] as! String
let title = row["title"] as! String
let guid = row["guid"] as! String
// Extract a boolean from the row if it's present.
let iB = row["is_bookmarked"] as? Int
let isBookmarked: Bool? = (iB == nil) ? nil : (iB! != 0)
let site = Site(url: url, title: title, bookmarked: isBookmarked)
site.guid = guid
site.id = id
// Find the most recent visit, regardless of which column it might be in.
let local = row.getTimestamp("localVisitDate") ?? 0
let remote = row.getTimestamp("remoteVisitDate") ?? 0
let either = row.getTimestamp("visitDate") ?? 0
let latest = max(local, remote, either)
if latest > 0 {
site.latestVisit = Visit(date: latest, type: VisitType.unknown)
}
return site
}
class func iconColumnFactory(_ row: SDRow) -> Favicon? {
if let iconURL = row["iconURL"] as? String,
let iconDate = row["iconDate"] as? Double,
let _ = row["iconID"] as? Int {
let date = Date(timeIntervalSince1970: iconDate)
return Favicon(url: iconURL, date: date)
}
return nil
}
class func pageMetadataColumnFactory(_ row: SDRow) -> PageMetadata? {
guard let siteURL = row["url"] as? String else {
return nil
}
return PageMetadata(id: row["metadata_id"] as? Int, siteURL: siteURL, mediaURL: row["media_url"] as? String, title: row["metadata_title"] as? String, description: row["description"] as? String, type: row["type"] as? String, providerName: row["provider_name"] as? String, mediaDataURI: nil)
}
class func iconHistoryColumnFactory(_ row: SDRow) -> Site {
let site = basicHistoryColumnFactory(row)
site.icon = iconColumnFactory(row)
return site
}
class func iconHistoryMetadataColumnFactory(_ row: SDRow) -> Site {
let site = iconHistoryColumnFactory(row)
site.metadata = pageMetadataColumnFactory(row)
return site
}
class func basicHistoryMetadataColumnFactory(_ row: SDRow) -> Site {
let site = basicHistoryColumnFactory(row)
site.metadata = pageMetadataColumnFactory(row)
return site
}
}