-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ProjectPamphletSubpageCellViewModel.swift
162 lines (133 loc) · 5.27 KB
/
ProjectPamphletSubpageCellViewModel.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
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
import KsApi
import Prelude
import ReactiveSwift
import UIKit
public protocol ProjectPamphletSubpageCellViewModelInputs {
/// Call with the ProjectPamphletSubpage
func configureWith(subpage: ProjectPamphletSubpage)
}
public protocol ProjectPamphletSubpageCellViewModelOutputs {
/// Emits the background color for the count label
var countLabelBackgroundColor: Signal<UIColor, Never> { get }
/// Emits the border color for the count label
var countLabelBorderColor: Signal<UIColor, Never> { get }
/// Emits the count label's text
var countLabelText: Signal<String, Never> { get }
/// Emits the count label's text color
var countLabelTextColor: Signal<UIColor, Never> { get }
/// Emits the cell's primary label text
var labelText: Signal<String, Never> { get }
/// Emits the cell's primary label text color
var labelTextColor: Signal<UIColor, Never> { get }
/// Emits when the top gradient view should be hidden
var topSeparatorViewHidden: Signal<Bool, Never> { get }
/// Emits when the separator view should be hidden
var separatorViewHidden: Signal<Bool, Never> { get }
}
public protocol ProjectPamphletSubpageCellViewModelType {
var inputs: ProjectPamphletSubpageCellViewModelInputs { get }
var outputs: ProjectPamphletSubpageCellViewModelOutputs { get }
}
public final class ProjectPamphletSubpageCellViewModel: ProjectPamphletSubpageCellViewModelType,
ProjectPamphletSubpageCellViewModelInputs, ProjectPamphletSubpageCellViewModelOutputs {
public init() {
let commentsSubpage = self.subpageProperty.signal.skipNil().filter { $0.isComments }
let updatesSubpage = self.subpageProperty.signal.skipNil().filter { $0.isUpdates }
self.labelText = Signal.merge(
commentsSubpage.mapConst(Strings.project_menu_buttons_comments()),
updatesSubpage.mapConst(Strings.project_menu_buttons_updates())
)
self.labelTextColor = Signal.merge(
commentsSubpage.mapConst(.ksr_support_700),
updatesSubpage.mapConst(.ksr_support_700)
)
self.topSeparatorViewHidden = self.subpageProperty.signal.skipNil()
.map { $0.position != .first }
self.separatorViewHidden = self.subpageProperty.signal.skipNil()
.map { $0.position == .last }
self.countLabelText = Signal.merge(commentsSubpage, updatesSubpage)
.map { Format.wholeNumber($0.count ?? 0) }
self.countLabelTextColor = Signal.merge(commentsSubpage, updatesSubpage).mapConst(.ksr_support_700)
self.countLabelBorderColor = Signal.merge(commentsSubpage, updatesSubpage)
.mapConst(.clear)
self.countLabelBackgroundColor = Signal.merge(commentsSubpage, updatesSubpage).mapConst(.ksr_support_100)
}
private let subpageProperty = MutableProperty<ProjectPamphletSubpage?>(nil)
public func configureWith(subpage: ProjectPamphletSubpage) {
self.subpageProperty.value = subpage
}
public let countLabelText: Signal<String, Never>
public let countLabelTextColor: Signal<UIColor, Never>
public let countLabelBorderColor: Signal<UIColor, Never>
public let countLabelBackgroundColor: Signal<UIColor, Never>
public let labelText: Signal<String, Never>
public let labelTextColor: Signal<UIColor, Never>
public let topSeparatorViewHidden: Signal<Bool, Never>
public let separatorViewHidden: Signal<Bool, Never>
public var inputs: ProjectPamphletSubpageCellViewModelInputs { return self }
public var outputs: ProjectPamphletSubpageCellViewModelOutputs { return self }
}
public enum ProjectPamphletSubpageCellPosition {
case first
case middle
case last
}
extension ProjectPamphletSubpageCellPosition: Equatable {}
public func == (lhs: ProjectPamphletSubpageCellPosition, rhs: ProjectPamphletSubpageCellPosition) -> Bool {
switch (lhs, rhs) {
case (.first, .first), (.middle, .middle), (.last, .last):
return true
default:
return false
}
}
public enum ProjectPamphletSubpage {
case comments(Int?, ProjectPamphletSubpageCellPosition)
case updates(Int?, ProjectPamphletSubpageCellPosition)
case reportProject(ProjectPamphletSubpageCellPosition)
public var count: Int? {
switch self {
case let .comments(count, _): return count
case let .updates(count, _): return count
case .reportProject: return nil
}
}
public var position: ProjectPamphletSubpageCellPosition {
switch self {
case let .comments(_, position): return position
case let .updates(_, position): return position
case let .reportProject(position): return position
}
}
public var isComments: Bool {
switch self {
case .comments: return true
default: return false
}
}
public var isUpdates: Bool {
switch self {
case .updates: return true
default: return false
}
}
public var isReportProject: Bool {
switch self {
case .reportProject: return true
default: return false
}
}
}
extension ProjectPamphletSubpage: Equatable {}
public func == (lhs: ProjectPamphletSubpage, rhs: ProjectPamphletSubpage) -> Bool {
switch (lhs, rhs) {
case let (.comments(lhsCount, lhsPos), .comments(rhsCount, rhsPos)):
return lhsCount == rhsCount && lhsPos == rhsPos
case let (.updates(lhsCount, lhsPos), .updates(rhsCount, rhsPos)):
return lhsCount == rhsCount && lhsPos == rhsPos
case let (.reportProject(lhsPos), .reportProject(rhsPos)):
return lhsPos == rhsPos
default:
return false
}
}