forked from insidegui/WWDC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncEngine.swift
More file actions
77 lines (57 loc) · 2.49 KB
/
SyncEngine.swift
File metadata and controls
77 lines (57 loc) · 2.49 KB
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
//
// SyncEngine.swift
// WWDC
//
// Created by Guilherme Rambo on 22/04/17.
// Copyright © 2017 Guilherme Rambo. All rights reserved.
//
import Foundation
import RxSwift
extension Notification.Name {
public static let SyncEngineDidSyncSessionsAndSchedule = Notification.Name("SyncEngineDidSyncSessionsAndSchedule")
}
public final class SyncEngine {
public let storage: Storage
public let client: AppleAPIClient
private var didRunIndexingService = false
private lazy var transcriptIndexingConnection: NSXPCConnection = {
let c = NSXPCConnection(serviceName: "io.wwdc.app.TranscriptIndexingService")
c.remoteObjectInterface = NSXPCInterface(with: TranscriptIndexingServiceProtocol.self)
return c
}()
private var transcriptIndexingService: TranscriptIndexingServiceProtocol? {
return transcriptIndexingConnection.remoteObjectProxy as? TranscriptIndexingServiceProtocol
}
public init(storage: Storage, client: AppleAPIClient) {
self.storage = storage
self.client = client
NotificationCenter.default.addObserver(forName: .SyncEngineDidSyncSessionsAndSchedule, object: nil, queue: OperationQueue.main) { [unowned self] _ in
self.startTranscriptIndexingIfNeeded()
}
}
public func syncContent() {
client.fetchContent { [unowned self] scheduleResult in
DispatchQueue.main.async {
self.storage.store(contentResult: scheduleResult) {
NotificationCenter.default.post(name: .SyncEngineDidSyncSessionsAndSchedule, object: self)
}
}
}
}
public func syncLiveVideos() {
client.fetchLiveVideoAssets { [weak self] result in
DispatchQueue.main.async {
self?.storage.store(liveVideosResult: result)
}
}
}
private func startTranscriptIndexingIfNeeded() {
guard !ProcessInfo.processInfo.arguments.contains("--disable-transcripts") else { return }
guard TranscriptIndexer.needsUpdate(in: storage) else { return }
guard let url = storage.realmConfig.fileURL else { return }
guard !didRunIndexingService else { return }
didRunIndexingService = true
transcriptIndexingConnection.resume()
transcriptIndexingService?.indexTranscriptsIfNeeded(storageURL: url, schemaVersion: storage.realmConfig.schemaVersion)
}
}