Skip to content

Commit 7133530

Browse files
committed
使用泛型重构 BaseTableViewCell。
1 parent 03499f9 commit 7133530

7 files changed

+49
-63
lines changed

View/ArchiveSettingCell.swift

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import UIKit
1010

11-
class ArchiveSettingCell: BaseTableViewCell {
11+
class ArchiveSettingCell: BaseTableViewCell<ArchiveSettingCellViewModel> {
1212
let switchButton: UISwitch = {
1313
let btn = UISwitch()
1414
return btn
@@ -32,12 +32,9 @@ class ArchiveSettingCell: BaseTableViewCell {
3232
fatalError("init(coder:) has not been implemented")
3333
}
3434

35-
override func bindViewModel(model: ViewModel) {
35+
override func bindViewModel(model: ArchiveSettingCellViewModel) {
3636
super.bindViewModel(model: model)
37-
guard let viewModel = model as? ArchiveSettingCellViewModel else {
38-
return
39-
}
40-
(self.switchButton.rx.isOn <-> viewModel.on)
37+
(self.switchButton.rx.isOn <-> model.on)
4138
.disposed(by: rx.reuseBag)
4239
}
4340
}

View/BaseTableViewCell.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import UIKit
1010

11-
class BaseTableViewCell: UITableViewCell {
12-
var viewModel: ViewModel?
13-
func bindViewModel(model: ViewModel) {
11+
class BaseTableViewCell<T>: UITableViewCell where T: ViewModel {
12+
var viewModel: T?
13+
func bindViewModel(model: T) {
1414
self.viewModel = model
1515
}
1616
}

View/GroupTableViewCell.swift

+7-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Material
1010
import UIKit
1111

12-
class GroupTableViewCell: BaseTableViewCell {
12+
class GroupTableViewCell: BaseTableViewCell<GroupCellViewModel> {
1313
let nameLabel: UILabel = {
1414
let label = UILabel()
1515
label.fontSize = 14
@@ -47,7 +47,7 @@ class GroupTableViewCell: BaseTableViewCell {
4747
let tap = UITapGestureRecognizer()
4848
self.contentView.addGestureRecognizer(tap)
4949
tap.rx.event.subscribe(onNext: { [weak self] _ in
50-
(self?.viewModel as? GroupCellViewModel)?.checked.accept(!self!.checkButton.isSelected)
50+
self?.viewModel?.checked.accept(!self!.checkButton.isSelected)
5151
}).disposed(by: rx.disposeBag)
5252
}
5353

@@ -56,24 +56,21 @@ class GroupTableViewCell: BaseTableViewCell {
5656
fatalError("init(coder:) has not been implemented")
5757
}
5858

59-
override func bindViewModel(model: ViewModel) {
59+
override func bindViewModel(model: GroupCellViewModel) {
6060
super.bindViewModel(model: model)
61-
guard let viewModel = model as? GroupCellViewModel else {
62-
return
63-
}
64-
65-
viewModel.name
61+
62+
model.name
6663
.map { name in
6764
name ?? NSLocalizedString("default")
6865
}
6966
.bind(to: nameLabel.rx.text)
7067
.disposed(by: rx.reuseBag)
7168

72-
viewModel.checked
69+
model.checked
7370
.bind(to: self.checkButton.rx.isSelected)
7471
.disposed(by: rx.reuseBag)
7572

76-
viewModel.checked.subscribe(
73+
model.checked.subscribe(
7774
onNext: { [weak self] checked in
7875
self?.checkButton.tintColor = checked ? BKColor.lightBlue.darken3 : BKColor.grey.base
7976
}).disposed(by: rx.reuseBag)

View/MessageTableViewCell.swift

+10-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Material
1010
import UIKit
11-
class MessageTableViewCell: BaseTableViewCell {
11+
class MessageTableViewCell: BaseTableViewCell<MessageTableViewCellViewModel> {
1212
let backgroundPanel: UIView = {
1313
let view = UIView()
1414
view.layer.cornerRadius = 3
@@ -121,21 +121,19 @@ class MessageTableViewCell: BaseTableViewCell {
121121
}
122122
}
123123

124-
override func bindViewModel(model: ViewModel) {
124+
override func bindViewModel(model: MessageTableViewCellViewModel) {
125125
super.bindViewModel(model: model)
126-
guard let viewModel = model as? MessageTableViewCellViewModel else {
127-
return
128-
}
129-
viewModel.title.bind(to: self.titleLabel.rx.text).disposed(by: rx.reuseBag)
130-
viewModel.body.bind(to: self.bodyLabel.rx.text).disposed(by: rx.reuseBag)
131-
viewModel.url.bind(to: self.urlLabel.rx.text).disposed(by: rx.reuseBag)
132-
viewModel.date.bind(to: self.dateLabel.rx.text).disposed(by: rx.reuseBag)
126+
127+
model.title.bind(to: self.titleLabel.rx.text).disposed(by: rx.reuseBag)
128+
model.body.bind(to: self.bodyLabel.rx.text).disposed(by: rx.reuseBag)
129+
model.url.bind(to: self.urlLabel.rx.text).disposed(by: rx.reuseBag)
130+
model.date.bind(to: self.dateLabel.rx.text).disposed(by: rx.reuseBag)
133131

134-
viewModel.title.map { $0.count <= 0 }.bind(to: self.titleLabel.rx.isHidden).disposed(by: rx.reuseBag)
135-
viewModel.url.map { $0.count <= 0 }.bind(to: self.urlLabel.rx.isHidden).disposed(by: rx.reuseBag)
132+
model.title.map { $0.count <= 0 }.bind(to: self.titleLabel.rx.isHidden).disposed(by: rx.reuseBag)
133+
model.url.map { $0.count <= 0 }.bind(to: self.urlLabel.rx.isHidden).disposed(by: rx.reuseBag)
136134

137135
self.urlLabel.gestureRecognizers?.first?.rx.event
138136
.map { [weak self] _ in self?.urlLabel.text ?? "" }
139-
.bind(to: viewModel.urlTap).disposed(by: rx.reuseBag)
137+
.bind(to: model.urlTap).disposed(by: rx.reuseBag)
140138
}
141139
}

View/MutableTextCell.swift

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import UIKit
1010

11-
class MutableTextCell: BaseTableViewCell {
11+
class MutableTextCell: BaseTableViewCell<MutableTextCellViewModel> {
1212

1313
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
1414
super.init(style: UITableViewCell.CellStyle.value1, reuseIdentifier: reuseIdentifier)
@@ -22,14 +22,12 @@ class MutableTextCell: BaseTableViewCell {
2222
required init?(coder aDecoder: NSCoder) {
2323
fatalError("init(coder:) has not been implemented")
2424
}
25-
26-
override func bindViewModel(model: ViewModel) {
25+
26+
override func bindViewModel(model: MutableTextCellViewModel) {
2727
super.bindViewModel(model: model)
28-
guard let viewModel = model as? MutableTextCellViewModel else {
29-
return
30-
}
31-
self.textLabel?.text = viewModel.title
32-
viewModel.text
28+
29+
self.textLabel?.text = model.title
30+
model.text
3331
.drive(self.detailTextLabel!.rx.text)
3432
.disposed(by: rx.reuseBag)
3533
}

View/PreviewCardCell.swift

+13-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Material
1010
import UIKit
1111

12-
class PreviewCardCell: BaseTableViewCell {
12+
class PreviewCardCell: BaseTableViewCell<PreviewCardCellViewModel> {
1313
let previewButton = IconButton(image: Icon.cm.skipForward, tintColor: BKColor.grey.base)
1414
let copyButton = IconButton(image: UIImage(named: "baseline_file_copy_white_24pt"), tintColor: BKColor.grey.base)
1515

@@ -142,23 +142,22 @@ class PreviewCardCell: BaseTableViewCell {
142142
noticeLabel.addGestureRecognizer(UITapGestureRecognizer())
143143
}
144144

145-
override func bindViewModel(model: ViewModel) {
146-
guard let viewModel = model as? PreviewCardCellViewModel else {
147-
return
148-
}
149-
viewModel.title
145+
override func bindViewModel(model: PreviewCardCellViewModel) {
146+
super.bindViewModel(model: model)
147+
148+
model.title
150149
.bind(to: self.titleLabel.rx.text).disposed(by: rx.reuseBag)
151-
viewModel.body
150+
model.body
152151
.bind(to: self.bodyLabel.rx.text).disposed(by: rx.reuseBag)
153-
viewModel.content
152+
model.content
154153
.bind(to: self.contentLabel.rx.attributedText).disposed(by: rx.reuseBag)
155-
viewModel.notice
154+
model.notice
156155
.bind(to: self.noticeLabel.rx.attributedText).disposed(by: rx.reuseBag)
157-
viewModel.contentImage
156+
model.contentImage
158157
.compactMap { $0 }
159158
.bind(to: self.contentImageView.rx.image)
160159
.disposed(by: rx.reuseBag)
161-
viewModel.contentImage
160+
model.contentImage
162161
.map { $0 == nil }
163162
.bind(to: self.contentImageView.rx.isHidden)
164163
.disposed(by: rx.reuseBag)
@@ -170,14 +169,14 @@ class PreviewCardCell: BaseTableViewCell {
170169
// 仅在有 moreViewModel 时 点击
171170
weakModel?.previewModel.moreViewModel
172171
}
173-
.bind(to: viewModel.noticeTap)
172+
.bind(to: model.noticeTap)
174173
.disposed(by: rx.reuseBag)
175174

176175
// 点击复制
177176
copyButton.rx.tap.map { [weak self] () -> String in
178177
self?.contentLabel.text ?? ""
179178
}
180-
.bind(to: viewModel.copy)
179+
.bind(to: model.copy)
181180
.disposed(by: rx.reuseBag)
182181

183182
// 点击预览
@@ -189,7 +188,7 @@ class PreviewCardCell: BaseTableViewCell {
189188
}
190189
return nil
191190
}
192-
.bind(to: viewModel.preview)
191+
.bind(to: model.preview)
193192
.disposed(by: rx.reuseBag)
194193
}
195194
}

View/SoundCell.swift

+7-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import AVKit
1010
import Material
1111
import UIKit
1212

13-
class SoundCell: BaseTableViewCell {
13+
class SoundCell: BaseTableViewCell<SoundCellViewModel> {
1414
let copyButton = IconButton(image: UIImage(named: "baseline_file_copy_white_24pt"), tintColor: BKColor.grey.base)
1515
let nameLabel: UILabel = {
1616
let label = UILabel()
@@ -54,23 +54,20 @@ class SoundCell: BaseTableViewCell {
5454
fatalError("init(coder:) has not been implemented")
5555
}
5656

57-
override func bindViewModel(model: ViewModel) {
57+
override func bindViewModel(model: SoundCellViewModel) {
5858
super.bindViewModel(model: model)
59-
guard let viewModel = model as? SoundCellViewModel else {
60-
return
61-
}
62-
63-
viewModel.name
59+
60+
model.name
6461
.bind(to: nameLabel.rx.text)
6562
.disposed(by: rx.reuseBag)
66-
viewModel.duration
63+
model.duration
6764
.map { String(format: "%.2g second(s)", CMTimeGetSeconds($0)) }
6865
.bind(to: durationLabel.rx.text)
6966
.disposed(by: rx.reuseBag)
7067

7168
copyButton.rx.tap
72-
.map { viewModel.name.value }
73-
.bind(to: viewModel.copyNameAction)
69+
.map { model.name.value }
70+
.bind(to: model.copyNameAction)
7471
.disposed(by: rx.reuseBag)
7572
}
7673
}

0 commit comments

Comments
 (0)