forked from Automattic/pocket-casts-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EpisodeRowViewModel.swift
79 lines (67 loc) · 2.74 KB
/
EpisodeRowViewModel.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
import Combine
import Foundation
import PocketCastsDataModel
import PocketCastsUtils
class EpisodeRowViewModel: EpisodeViewModel, Identifiable {
var id: String { episode.uuid }
@Published var displayInfo: String = ""
@Published var accessibilityInfo: String = ""
@Published var downloadStatusIconName: String?
@Published var isDownloading = false
init(episode: BaseEpisode) {
super.init(episode: episode, skipHydration: true)
}
override func hydrate() {
if alreadyHydrated {
return
}
super.hydrate()
updateProperties(episode: episode, downloadProgress: downloadProgress)
Publishers.CombineLatest($episode, $downloadProgress)
.receive(on: RunLoop.main)
.dropFirst()
.sink(receiveValue: { [unowned self] episode, downloadProgress in
updateProperties(episode: episode, downloadProgress: downloadProgress)
})
.store(in: &cancellables)
}
private func updateProperties(episode: BaseEpisode, downloadProgress: DownloadProgress?) {
var informationLabel = [String]()
var accessibilityLabel = [episode.title ?? ""]
if let publishedDate = episode.publishedDate {
let info = DateFormatHelper.sharedHelper.shortLocalizedFormat(publishedDate)
informationLabel.append(info)
accessibilityLabel.append(info)
}
isDownloading = episode.downloading() || downloadProgress != nil
let info: String
let statusText: String?
switch DownloadStatus(rawValue: episode.episodeStatus) {
case .downloaded:
self.downloadStatusIconName = "episodedownloaded"
info = episode.displayableTimeLeft()
statusText = L10n.statusDownloaded
case .downloading:
self.downloadStatusIconName = nil
info = episode.displayableInfo(includeSize: false)
statusText = L10n.statusDownloading
case .downloadFailed:
self.downloadStatusIconName = "downloadfailed"
informationLabel = []
accessibilityLabel = [L10n.downloadFailed]
info = episode.displayableInfo(includeSize: false)
statusText = nil
default:
self.downloadStatusIconName = nil
info = episode.displayableInfo(includeSize: false)
statusText = L10n.statusNotDownloaded
}
informationLabel.append(info)
accessibilityLabel.append(info)
if let statusText = statusText {
accessibilityLabel.append(statusText)
}
self.displayInfo = informationLabel.joined(separator: " • ")
self.accessibilityInfo = accessibilityLabel.joined(separator: " , ")
}
}