使用泛型重构 BaseTableViewCell。

This commit is contained in:
Fin 2022-03-25 14:50:33 +08:00
parent 03499f9532
commit 7133530b0e
7 changed files with 49 additions and 63 deletions

View File

@ -8,7 +8,7 @@
import UIKit
class ArchiveSettingCell: BaseTableViewCell {
class ArchiveSettingCell: BaseTableViewCell<ArchiveSettingCellViewModel> {
let switchButton: UISwitch = {
let btn = UISwitch()
return btn
@ -32,12 +32,9 @@ class ArchiveSettingCell: BaseTableViewCell {
fatalError("init(coder:) has not been implemented")
}
override func bindViewModel(model: ViewModel) {
override func bindViewModel(model: ArchiveSettingCellViewModel) {
super.bindViewModel(model: model)
guard let viewModel = model as? ArchiveSettingCellViewModel else {
return
}
(self.switchButton.rx.isOn <-> viewModel.on)
(self.switchButton.rx.isOn <-> model.on)
.disposed(by: rx.reuseBag)
}
}

View File

@ -8,9 +8,9 @@
import UIKit
class BaseTableViewCell: UITableViewCell {
var viewModel: ViewModel?
func bindViewModel(model: ViewModel) {
class BaseTableViewCell<T>: UITableViewCell where T: ViewModel {
var viewModel: T?
func bindViewModel(model: T) {
self.viewModel = model
}
}

View File

@ -9,7 +9,7 @@
import Material
import UIKit
class GroupTableViewCell: BaseTableViewCell {
class GroupTableViewCell: BaseTableViewCell<GroupCellViewModel> {
let nameLabel: UILabel = {
let label = UILabel()
label.fontSize = 14
@ -47,7 +47,7 @@ class GroupTableViewCell: BaseTableViewCell {
let tap = UITapGestureRecognizer()
self.contentView.addGestureRecognizer(tap)
tap.rx.event.subscribe(onNext: { [weak self] _ in
(self?.viewModel as? GroupCellViewModel)?.checked.accept(!self!.checkButton.isSelected)
self?.viewModel?.checked.accept(!self!.checkButton.isSelected)
}).disposed(by: rx.disposeBag)
}
@ -56,24 +56,21 @@ class GroupTableViewCell: BaseTableViewCell {
fatalError("init(coder:) has not been implemented")
}
override func bindViewModel(model: ViewModel) {
override func bindViewModel(model: GroupCellViewModel) {
super.bindViewModel(model: model)
guard let viewModel = model as? GroupCellViewModel else {
return
}
viewModel.name
model.name
.map { name in
name ?? NSLocalizedString("default")
}
.bind(to: nameLabel.rx.text)
.disposed(by: rx.reuseBag)
viewModel.checked
model.checked
.bind(to: self.checkButton.rx.isSelected)
.disposed(by: rx.reuseBag)
viewModel.checked.subscribe(
model.checked.subscribe(
onNext: { [weak self] checked in
self?.checkButton.tintColor = checked ? BKColor.lightBlue.darken3 : BKColor.grey.base
}).disposed(by: rx.reuseBag)

View File

@ -8,7 +8,7 @@
import Material
import UIKit
class MessageTableViewCell: BaseTableViewCell {
class MessageTableViewCell: BaseTableViewCell<MessageTableViewCellViewModel> {
let backgroundPanel: UIView = {
let view = UIView()
view.layer.cornerRadius = 3
@ -121,21 +121,19 @@ class MessageTableViewCell: BaseTableViewCell {
}
}
override func bindViewModel(model: ViewModel) {
override func bindViewModel(model: MessageTableViewCellViewModel) {
super.bindViewModel(model: model)
guard let viewModel = model as? MessageTableViewCellViewModel else {
return
}
viewModel.title.bind(to: self.titleLabel.rx.text).disposed(by: rx.reuseBag)
viewModel.body.bind(to: self.bodyLabel.rx.text).disposed(by: rx.reuseBag)
viewModel.url.bind(to: self.urlLabel.rx.text).disposed(by: rx.reuseBag)
viewModel.date.bind(to: self.dateLabel.rx.text).disposed(by: rx.reuseBag)
viewModel.title.map { $0.count <= 0 }.bind(to: self.titleLabel.rx.isHidden).disposed(by: rx.reuseBag)
viewModel.url.map { $0.count <= 0 }.bind(to: self.urlLabel.rx.isHidden).disposed(by: rx.reuseBag)
model.title.bind(to: self.titleLabel.rx.text).disposed(by: rx.reuseBag)
model.body.bind(to: self.bodyLabel.rx.text).disposed(by: rx.reuseBag)
model.url.bind(to: self.urlLabel.rx.text).disposed(by: rx.reuseBag)
model.date.bind(to: self.dateLabel.rx.text).disposed(by: rx.reuseBag)
model.title.map { $0.count <= 0 }.bind(to: self.titleLabel.rx.isHidden).disposed(by: rx.reuseBag)
model.url.map { $0.count <= 0 }.bind(to: self.urlLabel.rx.isHidden).disposed(by: rx.reuseBag)
self.urlLabel.gestureRecognizers?.first?.rx.event
.map { [weak self] _ in self?.urlLabel.text ?? "" }
.bind(to: viewModel.urlTap).disposed(by: rx.reuseBag)
.bind(to: model.urlTap).disposed(by: rx.reuseBag)
}
}

View File

@ -8,7 +8,7 @@
import UIKit
class MutableTextCell: BaseTableViewCell {
class MutableTextCell: BaseTableViewCell<MutableTextCellViewModel> {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: UITableViewCell.CellStyle.value1, reuseIdentifier: reuseIdentifier)
@ -23,13 +23,11 @@ class MutableTextCell: BaseTableViewCell {
fatalError("init(coder:) has not been implemented")
}
override func bindViewModel(model: ViewModel) {
override func bindViewModel(model: MutableTextCellViewModel) {
super.bindViewModel(model: model)
guard let viewModel = model as? MutableTextCellViewModel else {
return
}
self.textLabel?.text = viewModel.title
viewModel.text
self.textLabel?.text = model.title
model.text
.drive(self.detailTextLabel!.rx.text)
.disposed(by: rx.reuseBag)
}

View File

@ -9,7 +9,7 @@
import Material
import UIKit
class PreviewCardCell: BaseTableViewCell {
class PreviewCardCell: BaseTableViewCell<PreviewCardCellViewModel> {
let previewButton = IconButton(image: Icon.cm.skipForward, tintColor: BKColor.grey.base)
let copyButton = IconButton(image: UIImage(named: "baseline_file_copy_white_24pt"), tintColor: BKColor.grey.base)
@ -142,23 +142,22 @@ class PreviewCardCell: BaseTableViewCell {
noticeLabel.addGestureRecognizer(UITapGestureRecognizer())
}
override func bindViewModel(model: ViewModel) {
guard let viewModel = model as? PreviewCardCellViewModel else {
return
}
viewModel.title
override func bindViewModel(model: PreviewCardCellViewModel) {
super.bindViewModel(model: model)
model.title
.bind(to: self.titleLabel.rx.text).disposed(by: rx.reuseBag)
viewModel.body
model.body
.bind(to: self.bodyLabel.rx.text).disposed(by: rx.reuseBag)
viewModel.content
model.content
.bind(to: self.contentLabel.rx.attributedText).disposed(by: rx.reuseBag)
viewModel.notice
model.notice
.bind(to: self.noticeLabel.rx.attributedText).disposed(by: rx.reuseBag)
viewModel.contentImage
model.contentImage
.compactMap { $0 }
.bind(to: self.contentImageView.rx.image)
.disposed(by: rx.reuseBag)
viewModel.contentImage
model.contentImage
.map { $0 == nil }
.bind(to: self.contentImageView.rx.isHidden)
.disposed(by: rx.reuseBag)
@ -170,14 +169,14 @@ class PreviewCardCell: BaseTableViewCell {
// moreViewModel
weakModel?.previewModel.moreViewModel
}
.bind(to: viewModel.noticeTap)
.bind(to: model.noticeTap)
.disposed(by: rx.reuseBag)
//
copyButton.rx.tap.map { [weak self] () -> String in
self?.contentLabel.text ?? ""
}
.bind(to: viewModel.copy)
.bind(to: model.copy)
.disposed(by: rx.reuseBag)
//
@ -189,7 +188,7 @@ class PreviewCardCell: BaseTableViewCell {
}
return nil
}
.bind(to: viewModel.preview)
.bind(to: model.preview)
.disposed(by: rx.reuseBag)
}
}

View File

@ -10,7 +10,7 @@ import AVKit
import Material
import UIKit
class SoundCell: BaseTableViewCell {
class SoundCell: BaseTableViewCell<SoundCellViewModel> {
let copyButton = IconButton(image: UIImage(named: "baseline_file_copy_white_24pt"), tintColor: BKColor.grey.base)
let nameLabel: UILabel = {
let label = UILabel()
@ -54,23 +54,20 @@ class SoundCell: BaseTableViewCell {
fatalError("init(coder:) has not been implemented")
}
override func bindViewModel(model: ViewModel) {
override func bindViewModel(model: SoundCellViewModel) {
super.bindViewModel(model: model)
guard let viewModel = model as? SoundCellViewModel else {
return
}
viewModel.name
model.name
.bind(to: nameLabel.rx.text)
.disposed(by: rx.reuseBag)
viewModel.duration
model.duration
.map { String(format: "%.2g second(s)", CMTimeGetSeconds($0)) }
.bind(to: durationLabel.rx.text)
.disposed(by: rx.reuseBag)
copyButton.rx.tap
.map { viewModel.name.value }
.bind(to: viewModel.copyNameAction)
.map { model.name.value }
.bind(to: model.copyNameAction)
.disposed(by: rx.reuseBag)
}
}