forked from insidegui/WWDC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranscriptTableViewController.swift
More file actions
188 lines (134 loc) · 6.11 KB
/
TranscriptTableViewController.swift
File metadata and controls
188 lines (134 loc) · 6.11 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// TranscriptTableViewController.swift
// WWDC
//
// Created by Guilherme Rambo on 28/05/17.
// Copyright © 2017 Guilherme Rambo. All rights reserved.
//
import Cocoa
import ConfCore
import RealmSwift
extension Notification.Name {
static let TranscriptControllerDidSelectAnnotation = Notification.Name("TranscriptControllerDidSelectAnnotation")
}
class TranscriptTableViewController: NSViewController {
var viewModel: SessionViewModel? {
didSet {
guard viewModel?.identifier != oldValue?.identifier else { return }
updateUI()
}
}
init() {
super.init(nibName: nil, bundle: nil)!
identifier = "transcriptList"
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var tableView: WWDCTableView = {
let v = WWDCTableView()
v.wantsLayer = true
v.focusRingType = .none
v.allowsMultipleSelection = true
v.backgroundColor = .clear
v.headerView = nil
v.rowHeight = 36
v.autoresizingMask = [.viewWidthSizable, .viewHeightSizable]
v.floatsGroupRows = true
let column = NSTableColumn(identifier: "transcript")
v.addTableColumn(column)
return v
}()
lazy var scrollView: NSScrollView = {
let v = NSScrollView()
v.focusRingType = .none
v.backgroundColor = .clear
v.drawsBackground = false
v.borderType = .noBorder
v.documentView = self.tableView
v.hasVerticalScroller = true
v.hasHorizontalScroller = false
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
override func loadView() {
view = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: MainWindowController.defaultRect.height))
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.darkWindowBackground.cgColor
scrollView.frame = view.bounds
tableView.frame = view.bounds
view.heightAnchor.constraint(equalToConstant: 180).isActive = true
view.setContentCompressionResistancePriority(NSLayoutPriorityDefaultLow, for: .vertical)
view.addSubview(scrollView)
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.dataSource = self
tableView.delegate = self
}
fileprivate var transcript: Transcript?
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(highlightTranscriptLine(_:)), name: .HighlightTranscriptAtCurrentTimecode, object: nil)
}
private func updateUI() {
guard let transcript = viewModel?.session.transcript() else { return }
self.transcript = transcript
tableView.reloadData()
}
fileprivate var selectionLocked = false
fileprivate func withTableViewSelectionLocked(then executeBlock: () -> Void) {
selectionLocked = true
executeBlock()
perform(#selector(unlockTableViewSelection), with: nil, afterDelay: 0)
}
@objc fileprivate func unlockTableViewSelection() {
selectionLocked = false
}
@objc private func highlightTranscriptLine(_ note: Notification) {
withTableViewSelectionLocked {
guard let transcript = transcript else { return }
guard let timecode = note.object as? String else { return }
guard let annotation = transcript.annotations.filter({ Transcript.roundedStringFromTimecode($0.timecode) == timecode }).first else { return }
guard let row = transcript.annotations.index(of: annotation) else { return }
tableView.selectRowIndexes(IndexSet([row]), byExtendingSelection: false)
tableView.scrollRowToVisible(row)
}
}
}
extension TranscriptTableViewController: NSTableViewDataSource, NSTableViewDelegate {
private struct Constants {
static let cellIdentifier = "annotation"
static let rowIdentifier = "row"
}
func numberOfRows(in tableView: NSTableView) -> Int {
return transcript?.annotations.count ?? 0
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
guard let annotations = transcript?.annotations else { return nil }
var cell = tableView.make(withIdentifier: Constants.cellIdentifier, owner: tableView) as? TranscriptTableCellView
if cell == nil {
cell = TranscriptTableCellView(frame: .zero)
cell?.identifier = Constants.cellIdentifier
}
cell?.annotation = annotations[row]
return cell
}
func tableViewSelectionDidChange(_ notification: Notification) {
guard !selectionLocked else { return }
guard let transcript = transcript else { return }
guard tableView.selectedRow >= 0 && tableView.selectedRow < transcript.annotations.count else { return }
let row = tableView.selectedRow
let notificationObject = (transcript, transcript.annotations[row])
NotificationCenter.default.post(name: NSNotification.Name.TranscriptControllerDidSelectAnnotation, object: notificationObject)
}
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
var rowView = tableView.make(withIdentifier: Constants.rowIdentifier, owner: tableView) as? WWDCTableRowView
if rowView == nil {
rowView = WWDCTableRowView(frame: .zero)
rowView?.identifier = Constants.rowIdentifier
}
return rowView
}
}