-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheViewController.swift
More file actions
647 lines (559 loc) · 22 KB
/
Copy pathCacheViewController.swift
File metadata and controls
647 lines (559 loc) · 22 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//
// CacheViewController.swift
// HTTPMediaCacheiOSExample
//
// Created by Sun on 2026/5/9.
//
import HTTPMediaCache
import UIKit
final class CacheViewController: UITableViewController {
private static let cellReuseIdentifier = "Cell"
private let mediaItems: [MediaItem]
private let mediaURLs: Set<URL>
private var statuses: [Status]
private var cacheItemsByURL: [URL: CacheItem] = [:]
private var expandedURLs: Set<URL> = []
private var statusRefreshTask: Task<Void, Never>?
private var preloadTasks: [Int: Task<Void, Never>] = [:]
private lazy var startItem: UIBarButtonItem = {
let item = UIBarButtonItem(
image: UIImage(systemName: "arrow.down.circle"),
style: .plain,
target: self,
action: #selector(startPreload)
)
item.accessibilityLabel = "Preload All"
return item
}()
private lazy var deleteAllItem: UIBarButtonItem = {
let item = UIBarButtonItem(
image: UIImage(systemName: "trash"),
style: .plain,
target: self,
action: #selector(deleteAllCaches)
)
item.tintColor = .systemRed
item.accessibilityLabel = "Delete All Cache"
return item
}()
private var unmatchedCacheItems: [CacheItem] {
cacheItemsByURL.values
.filter { !mediaURLs.contains($0.url) && !isHLSResourceCacheURL($0.url) }
.sorted { $0.url.absoluteString < $1.url.absoluteString }
}
init(mediaItems: [MediaItem]) {
self.mediaItems = mediaItems
mediaURLs = Set(mediaItems.map(\.url))
statuses = Array(repeating: .waiting, count: mediaItems.count)
super.init(style: .insetGrouped)
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
statusRefreshTask?.cancel()
preloadTasks.values.forEach { $0.cancel() }
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Cache"
tableView.register(UITableViewCell.self, forCellReuseIdentifier: Self.cellReuseIdentifier)
navigationItem.rightBarButtonItems = [startItem, deleteAllItem]
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
startCacheStatusRefresh()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
stopCacheStatusRefresh()
}
}
private extension CacheViewController {
enum Section: Int, CaseIterable {
case samples
case unmatched
var title: String {
switch self {
case .samples:
return "Samples"
case .unmatched:
return "Unmatched Cache"
}
}
}
enum Status: Equatable {
case waiting
case loading(Double)
case finished
case failed(String)
var title: String {
switch self {
case .waiting:
return "Waiting"
case let .loading(progress):
return Self.percentText(for: progress)
case .finished:
return "100%"
case .failed:
return "Failed"
}
}
var textColor: UIColor {
switch self {
case .failed:
return .systemRed
case .waiting, .loading, .finished:
return .secondaryLabel
}
}
private static func percentText(for progress: Double) -> String {
let clampedProgress = min(max(progress, 0), 1)
return "\(Int(clampedProgress * 100))%"
}
}
enum Row {
case sample(Int)
case sampleZone(sampleIndex: Int, zoneIndex: Int)
case sampleHLSResourceSummary(Int)
case sampleHLSResource(sampleIndex: Int, resourceIndex: Int)
case sampleHLSResourceZone(sampleIndex: Int, resourceIndex: Int, zoneIndex: Int)
case unmatched(Int)
case unmatchedZone(cacheIndex: Int, zoneIndex: Int)
case emptyUnmatched
}
}
// MARK: - 表格
extension CacheViewController {
override func numberOfSections(in _: UITableView) -> Int {
Section.allCases.count
}
override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
rows(in: section).count
}
override func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
Section(rawValue: section)?.title
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Self.cellReuseIdentifier, for: indexPath)
configure(cell, for: row(at: indexPath))
return cell
}
override func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
handleSelection(of: row(at: indexPath))
}
override func tableView(
_: UITableView,
commit editingStyle: UITableViewCell.EditingStyle,
forRowAt indexPath: IndexPath
) {
guard editingStyle == .delete, let url = deletableURL(for: row(at: indexPath)) else {
return
}
deleteCache(for: url)
}
}
private extension CacheViewController {
func rows(in section: Int) -> [Row] {
guard let section = Section(rawValue: section) else {
return []
}
switch section {
case .samples:
return sampleRows()
case .unmatched:
return unmatchedRows()
}
}
func sampleRows() -> [Row] {
mediaItems.indices.flatMap { index -> [Row] in
let item = mediaItems[index]
guard expandedURLs.contains(item.url) else {
return [.sample(index)]
}
var rows: [Row] = [.sample(index)]
if let zones = cacheItemsByURL[item.url]?.zones, !zones.isEmpty {
rows.append(contentsOf: zones.indices.map { .sampleZone(sampleIndex: index, zoneIndex: $0) })
}
if item.format == .hls {
let resources = relatedHLSCacheItems(for: item)
rows.append(.sampleHLSResourceSummary(index))
for resourceIndex in resources.indices {
rows.append(.sampleHLSResource(sampleIndex: index, resourceIndex: resourceIndex))
guard expandedURLs.contains(resources[resourceIndex].url) else {
continue
}
rows.append(contentsOf: resources[resourceIndex].zones.indices.map {
.sampleHLSResourceZone(sampleIndex: index, resourceIndex: resourceIndex, zoneIndex: $0)
})
}
}
return rows
}
}
func unmatchedRows() -> [Row] {
let items = unmatchedCacheItems
guard !items.isEmpty else {
return [.emptyUnmatched]
}
return items.indices.flatMap { index -> [Row] in
guard expandedURLs.contains(items[index].url) else {
return [.unmatched(index)]
}
return [.unmatched(index)] + items[index].zones.indices.map {
.unmatchedZone(cacheIndex: index, zoneIndex: $0)
}
}
}
func row(at indexPath: IndexPath) -> Row {
rows(in: indexPath.section)[indexPath.row]
}
}
// MARK: - 单元格配置
private extension CacheViewController {
func configure(_ cell: UITableViewCell, for row: Row) {
var content = cell.defaultContentConfiguration()
content.secondaryTextProperties.numberOfLines = 3
cell.accessoryType = .none
cell.accessoryView = nil
switch row {
case let .sample(index):
configureSampleCell(&content, cell: cell, index: index)
case let .sampleZone(sampleIndex, zoneIndex):
configureZoneCell(&content, title: "Zone \(zoneIndex + 1)", zone: sampleZone(sampleIndex: sampleIndex, zoneIndex: zoneIndex))
case let .sampleHLSResourceSummary(index):
configureHLSResourceSummaryCell(&content, index: index)
case let .sampleHLSResource(sampleIndex, resourceIndex):
configureHLSResourceCell(&content, cell: cell, sampleIndex: sampleIndex, resourceIndex: resourceIndex)
case let .sampleHLSResourceZone(sampleIndex, resourceIndex, zoneIndex):
let resource = relatedHLSCacheItems(for: mediaItems[sampleIndex])[resourceIndex]
configureZoneCell(&content, title: "Resource Zone \(zoneIndex + 1)", zone: resource.zones[zoneIndex])
case let .unmatched(index):
configureUnmatchedCell(&content, cell: cell, index: index)
case let .unmatchedZone(cacheIndex, zoneIndex):
configureZoneCell(&content, title: "Zone \(zoneIndex + 1)", zone: unmatchedCacheItems[cacheIndex].zones[zoneIndex])
case .emptyUnmatched:
content.text = "No Cache"
content.secondaryText = "No cached item outside samples or HLS resources."
}
cell.contentConfiguration = content
}
func configureSampleCell(_ content: inout UIListContentConfiguration, cell: UITableViewCell, index: Int) {
let item = mediaItems[index]
let cacheItem = cacheItemsByURL[item.url]
content.text = item.title
content.secondaryText = sampleDetailText(for: item, cacheItem: cacheItem)
cell.accessoryView = makeSampleAccessoryView(for: item, index: index)
}
func configureHLSResourceSummaryCell(_ content: inout UIListContentConfiguration, index: Int) {
let resources = relatedHLSCacheItems(for: mediaItems[index])
let cachedLength = resources.reduce(0) { $0 + $1.cachedLength }
content.text = "HLS Resources"
content.secondaryText = resources.isEmpty
? "No cached segment, key, or init map resource."
: "\(resources.count) resources, \(byteCountText(cachedLength)) cached"
}
func configureHLSResourceCell(_ content: inout UIListContentConfiguration, cell: UITableViewCell, sampleIndex: Int, resourceIndex: Int) {
let item = relatedHLSCacheItems(for: mediaItems[sampleIndex])[resourceIndex]
content.text = resourceTitle(for: item.url)
content.secondaryText = "\(item.url.absoluteString)\n\(cacheDetailText(for: item))"
cell.accessoryType = item.zones.isEmpty ? .none : .disclosureIndicator
}
func configureUnmatchedCell(_ content: inout UIListContentConfiguration, cell: UITableViewCell, index: Int) {
let item = unmatchedCacheItems[index]
content.text = item.url.absoluteString
content.secondaryText = cacheDetailText(for: item)
cell.accessoryType = item.zones.isEmpty ? .none : .disclosureIndicator
}
func configureZoneCell(_ content: inout UIListContentConfiguration, title: String, zone: ByteRange?) {
content.text = title
content.secondaryText = zone.map(zoneDetailText)
}
func makeProgressLabel(for status: Status) -> UILabel {
let label = UILabel()
label.text = status.title
label.font = .monospacedDigitSystemFont(ofSize: 15, weight: .regular)
label.textColor = status.textColor
label.textAlignment = .right
label.setContentHuggingPriority(.required, for: .horizontal)
label.setContentCompressionResistancePriority(.required, for: .horizontal)
label.frame.size = CGSize(width: 72, height: 20)
label.sizeToFit()
label.frame.size.width = max(label.frame.width, 72)
return label
}
func sampleZone(sampleIndex: Int, zoneIndex: Int) -> ByteRange? {
let item = mediaItems[sampleIndex]
return cacheItemsByURL[item.url]?.zones[safe: zoneIndex]
}
func makeSampleAccessoryView(for item: MediaItem, index: Int) -> UIView {
let container = UIView(frame: CGRect(x: 0, y: 0, width: 132, height: 32))
let progressLabel = makeProgressLabel(for: statuses[index])
progressLabel.frame = CGRect(x: 0, y: 6, width: 72, height: 20)
container.addSubview(progressLabel)
let button = UIButton(type: .system)
button.configuration = preloadButtonConfiguration(for: item)
button.tag = index
button.frame = CGRect(x: 80, y: 2, width: 52, height: 28)
button.addTarget(self, action: #selector(preloadSample(_:)), for: .touchUpInside)
container.addSubview(button)
return container
}
func preloadButtonConfiguration(for item: MediaItem) -> UIButton.Configuration {
var configuration = UIButton.Configuration.bordered()
configuration.title = item.format == .hls ? "30s" : "2MB"
configuration.buttonSize = .mini
return configuration
}
}
// MARK: - 用户操作
private extension CacheViewController {
func handleSelection(of row: Row) {
switch row {
case let .sample(index):
toggleSampleExpansion(at: index)
case let .sampleHLSResource(sampleIndex, resourceIndex):
toggleExpansion(for: relatedHLSCacheItems(for: mediaItems[sampleIndex])[resourceIndex].url)
case let .unmatched(index):
toggleExpansion(for: unmatchedCacheItems[index].url)
case .sampleZone, .sampleHLSResourceSummary, .sampleHLSResourceZone, .unmatchedZone, .emptyUnmatched:
break
}
}
func toggleSampleExpansion(at index: Int) {
let item = mediaItems[index]
guard canExpandSample(item) else {
return
}
toggleExpansion(for: item.url)
}
func toggleExpansion(for url: URL) {
if expandedURLs.contains(url) {
expandedURLs.remove(url)
} else {
expandedURLs.insert(url)
}
tableView.reloadData()
}
@objc func preloadSample(_ sender: UIButton) {
startSamplePreload(at: sender.tag)
}
@objc func startPreload() {
startItem.isEnabled = false
Task {
for index in mediaItems.indices {
await preloadItem(at: index, options: .init())
await refreshCacheSnapshot()
}
await MainActor.run {
startItem.isEnabled = true
}
}
}
func startSamplePreload(at index: Int) {
preloadTasks[index]?.cancel()
preloadTasks[index] = Task { [weak self] in
guard let self else {
return
}
await self.preloadItem(at: index, options: self.preloadOptions(for: self.mediaItems[index]))
await self.refreshCacheSnapshot()
await MainActor.run {
self.preloadTasks[index] = nil
}
}
}
func preloadOptions(for item: MediaItem) -> PreloadOptions {
switch item.format {
case .hls:
return PreloadOptions(hlsLimit: .duration(30))
case .mp3, .aac, .wav, .flac, .ogg, .mp4, .mov:
return PreloadOptions(fileLimit: .byteCount(2 * 1024 * 1024))
}
}
@objc func deleteAllCaches() {
Task {
try? await HTTPMediaCache.deleteAllCaches()
await MainActor.run {
expandedURLs.removeAll()
}
await refreshCacheSnapshot()
}
}
func deleteCache(for url: URL) {
Task {
try? await HTTPMediaCache.deleteCache(for: url)
await refreshCacheSnapshot()
}
}
func deletableURL(for row: Row) -> URL? {
switch row {
case let .sample(index):
let url = mediaItems[index].url
return cacheItemsByURL[url] == nil ? nil : url
case let .sampleHLSResource(sampleIndex, resourceIndex):
return relatedHLSCacheItems(for: mediaItems[sampleIndex])[resourceIndex].url
case let .unmatched(index):
return unmatchedCacheItems[index].url
case .sampleZone, .sampleHLSResourceSummary, .sampleHLSResourceZone, .unmatchedZone, .emptyUnmatched:
return nil
}
}
}
// MARK: - 缓存状态
private extension CacheViewController {
func startCacheStatusRefresh() {
stopCacheStatusRefresh()
statusRefreshTask = Task { [weak self] in
while !Task.isCancelled {
await self?.refreshCacheSnapshot()
try? await Task.sleep(nanoseconds: 500_000_000)
}
}
}
func stopCacheStatusRefresh() {
statusRefreshTask?.cancel()
statusRefreshTask = nil
}
func refreshCacheSnapshot() async {
let items = (try? await HTTPMediaCache.allCacheItems()) ?? []
let itemsByURL = Dictionary(items.map { ($0.url, $0) }, uniquingKeysWith: { first, _ in first })
let activePreloadStatuses = await MainActor.run {
preloadTasks.keys.reduce(into: [Int: Status]()) { result, index in
result[index] = statuses[safe: index]
}
}
var nextStatuses: [Status] = []
nextStatuses.reserveCapacity(mediaItems.count)
for index in mediaItems.indices {
if let activeStatus = activePreloadStatuses[index] {
nextStatuses.append(activeStatus)
} else {
let item = mediaItems[index]
nextStatuses.append(await status(for: item.url, cacheItem: itemsByURL[item.url]))
}
}
await MainActor.run {
cacheItemsByURL = itemsByURL
statuses = nextStatuses
expandedURLs = expandedURLs.filter { url in
mediaURLs.contains(url) || itemsByURL[url]?.zones.isEmpty == false
}
tableView.reloadData()
}
}
func status(for url: URL, cacheItem: CacheItem?) async -> Status {
if let error = await HTTPMediaCache.error(for: url) {
return .failed(error.localizedDescription)
}
guard let cacheItem, cacheItem.totalLength > 0 else {
return .waiting
}
if cacheItem.progress >= 1 {
return .finished
}
if cacheItem.progress > 0 {
return .loading(cacheItem.progress)
}
return .waiting
}
func preloadItem(at index: Int, options: PreloadOptions) async {
let item = mediaItems[index]
updateStatus(.loading(0), at: index)
do {
let preloadTask = try await HTTPMediaCache.preload(CacheRequest(url: item.url), options: options)
for await progress in preloadTask.progress {
updateStatus(.loading(progress), at: index)
}
updateStatus(await finalStatus(for: item.url), at: index)
} catch {
updateStatus(.failed(String(describing: error)), at: index)
}
}
func finalStatus(for url: URL) async -> Status {
if let error = await HTTPMediaCache.error(for: url) {
return .failed(error.localizedDescription)
}
return .finished
}
@MainActor
func updateStatus(_ status: Status, at index: Int) {
statuses[index] = status
tableView.reloadData()
}
}
// MARK: - 文本格式
private extension CacheViewController {
func sampleDetailText(for item: MediaItem, cacheItem: CacheItem?) -> String {
let prefix = "\(item.category.title) · \(item.format.title)"
guard let cacheItem else {
if item.format == .hls {
return "\(prefix)\n\(item.url.absoluteString)\n\(hlsResourceSummaryText(for: item))"
}
return "\(prefix)\n\(item.url.absoluteString)"
}
if item.format == .hls {
return "\(prefix)\n\(item.url.absoluteString)\nPlaylist: \(cacheDetailText(for: cacheItem))\n\(hlsResourceSummaryText(for: item))"
}
return "\(prefix)\n\(item.url.absoluteString)\n\(cacheDetailText(for: cacheItem))"
}
func hlsResourceSummaryText(for item: MediaItem) -> String {
let relatedItems = relatedHLSCacheItems(for: item)
let relatedLength = relatedItems.reduce(0) { $0 + $1.cachedLength }
return "Resources: \(relatedItems.count), \(byteCountText(relatedLength))"
}
func cacheDetailText(for item: CacheItem) -> String {
"\(item.cachedLength)/\(item.totalLength) bytes, \(Int(item.progress * 100))%, \(item.zones.count) zones"
}
func zoneDetailText(for zone: ByteRange) -> String {
"Offset: \(zone.start), Length: \(zone.length ?? 0)"
}
func relatedHLSCacheItems(for item: MediaItem) -> [CacheItem] {
guard item.format == .hls else {
return []
}
return cacheItemsByURL.values
.filter { !mediaURLs.contains($0.url) && isLikelyRelatedHLSResource($0.url, to: item.url) }
.sorted { $0.url.absoluteString < $1.url.absoluteString }
}
func isHLSResourceCacheURL(_ url: URL) -> Bool {
mediaItems.contains { item in
item.format == .hls && isLikelyRelatedHLSResource(url, to: item.url)
}
}
func resourceTitle(for url: URL) -> String {
let filename = url.lastPathComponent
return filename.isEmpty ? "HLS Resource" : filename
}
func canExpandSample(_ item: MediaItem) -> Bool {
if item.format == .hls {
return true
}
return cacheItemsByURL[item.url]?.zones.isEmpty == false
}
func isLikelyRelatedHLSResource(_ resourceURL: URL, to playlistURL: URL) -> Bool {
guard resourceURL.host == playlistURL.host else {
return false
}
let parentPath = playlistURL.deletingLastPathComponent().path
return resourceURL.path.hasPrefix(parentPath)
}
func byteCountText(_ value: Int64) -> String {
ByteCountFormatter.string(fromByteCount: value, countStyle: .file)
}
}
private extension Array {
subscript(safe index: Index) -> Element? {
indices.contains(index) ? self[index] : nil
}
func asyncMap<T>(_ transform: (Element) async -> T) async -> [T] {
var values: [T] = []
values.reserveCapacity(count)
for element in self {
values.append(await transform(element))
}
return values
}
}