diff --git a/View/ArchiveSettingCell.swift b/View/ArchiveSettingCell.swift index f764147..35d28db 100644 --- a/View/ArchiveSettingCell.swift +++ b/View/ArchiveSettingCell.swift @@ -8,7 +8,7 @@ import UIKit -class ArchiveSettingCell: BaseTableViewCell { +class ArchiveSettingCell: BaseTableViewCell { 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) } } diff --git a/View/BaseTableViewCell.swift b/View/BaseTableViewCell.swift index 5dacced..228d371 100644 --- a/View/BaseTableViewCell.swift +++ b/View/BaseTableViewCell.swift @@ -8,9 +8,9 @@ import UIKit -class BaseTableViewCell: UITableViewCell { - var viewModel: ViewModel? - func bindViewModel(model: ViewModel) { +class BaseTableViewCell: UITableViewCell where T: ViewModel { + var viewModel: T? + func bindViewModel(model: T) { self.viewModel = model } } diff --git a/View/GroupTableViewCell.swift b/View/GroupTableViewCell.swift index 7689cdf..89c38a0 100644 --- a/View/GroupTableViewCell.swift +++ b/View/GroupTableViewCell.swift @@ -9,7 +9,7 @@ import Material import UIKit -class GroupTableViewCell: BaseTableViewCell { +class GroupTableViewCell: BaseTableViewCell { 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) diff --git a/View/MessageTableViewCell.swift b/View/MessageTableViewCell.swift index dcb7dd8..08e056b 100644 --- a/View/MessageTableViewCell.swift +++ b/View/MessageTableViewCell.swift @@ -8,7 +8,7 @@ import Material import UIKit -class MessageTableViewCell: BaseTableViewCell { +class MessageTableViewCell: BaseTableViewCell { 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) + + 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) - 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.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) } } diff --git a/View/MutableTextCell.swift b/View/MutableTextCell.swift index 937acbe..d594d63 100644 --- a/View/MutableTextCell.swift +++ b/View/MutableTextCell.swift @@ -8,7 +8,7 @@ import UIKit -class MutableTextCell: BaseTableViewCell { +class MutableTextCell: BaseTableViewCell { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: UITableViewCell.CellStyle.value1, reuseIdentifier: reuseIdentifier) @@ -22,14 +22,12 @@ class MutableTextCell: BaseTableViewCell { required init?(coder aDecoder: NSCoder) { 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) } diff --git a/View/PreviewCardCell.swift b/View/PreviewCardCell.swift index a4b543b..6e6de0d 100644 --- a/View/PreviewCardCell.swift +++ b/View/PreviewCardCell.swift @@ -9,7 +9,7 @@ import Material import UIKit -class PreviewCardCell: BaseTableViewCell { +class PreviewCardCell: BaseTableViewCell { 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) } } diff --git a/View/SoundCell.swift b/View/SoundCell.swift index f57423a..354d6ef 100644 --- a/View/SoundCell.swift +++ b/View/SoundCell.swift @@ -10,7 +10,7 @@ import AVKit import Material import UIKit -class SoundCell: BaseTableViewCell { +class SoundCell: BaseTableViewCell { 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) } }