mirror of
https://github.com/Finb/Bark.git
synced 2025-12-08 21:36:01 +00:00
使用泛型重构 BaseTableViewCell。
This commit is contained in:
parent
03499f9532
commit
7133530b0e
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
class ArchiveSettingCell: BaseTableViewCell {
|
class ArchiveSettingCell: BaseTableViewCell<ArchiveSettingCellViewModel> {
|
||||||
let switchButton: UISwitch = {
|
let switchButton: UISwitch = {
|
||||||
let btn = UISwitch()
|
let btn = UISwitch()
|
||||||
return btn
|
return btn
|
||||||
@ -32,12 +32,9 @@ class ArchiveSettingCell: BaseTableViewCell {
|
|||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override func bindViewModel(model: ViewModel) {
|
override func bindViewModel(model: ArchiveSettingCellViewModel) {
|
||||||
super.bindViewModel(model: model)
|
super.bindViewModel(model: model)
|
||||||
guard let viewModel = model as? ArchiveSettingCellViewModel else {
|
(self.switchButton.rx.isOn <-> model.on)
|
||||||
return
|
|
||||||
}
|
|
||||||
(self.switchButton.rx.isOn <-> viewModel.on)
|
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,9 +8,9 @@
|
|||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
class BaseTableViewCell: UITableViewCell {
|
class BaseTableViewCell<T>: UITableViewCell where T: ViewModel {
|
||||||
var viewModel: ViewModel?
|
var viewModel: T?
|
||||||
func bindViewModel(model: ViewModel) {
|
func bindViewModel(model: T) {
|
||||||
self.viewModel = model
|
self.viewModel = model
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
import Material
|
import Material
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
class GroupTableViewCell: BaseTableViewCell {
|
class GroupTableViewCell: BaseTableViewCell<GroupCellViewModel> {
|
||||||
let nameLabel: UILabel = {
|
let nameLabel: UILabel = {
|
||||||
let label = UILabel()
|
let label = UILabel()
|
||||||
label.fontSize = 14
|
label.fontSize = 14
|
||||||
@ -47,7 +47,7 @@ class GroupTableViewCell: BaseTableViewCell {
|
|||||||
let tap = UITapGestureRecognizer()
|
let tap = UITapGestureRecognizer()
|
||||||
self.contentView.addGestureRecognizer(tap)
|
self.contentView.addGestureRecognizer(tap)
|
||||||
tap.rx.event.subscribe(onNext: { [weak self] _ in
|
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)
|
}).disposed(by: rx.disposeBag)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,24 +56,21 @@ class GroupTableViewCell: BaseTableViewCell {
|
|||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override func bindViewModel(model: ViewModel) {
|
override func bindViewModel(model: GroupCellViewModel) {
|
||||||
super.bindViewModel(model: model)
|
super.bindViewModel(model: model)
|
||||||
guard let viewModel = model as? GroupCellViewModel else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
viewModel.name
|
model.name
|
||||||
.map { name in
|
.map { name in
|
||||||
name ?? NSLocalizedString("default")
|
name ?? NSLocalizedString("default")
|
||||||
}
|
}
|
||||||
.bind(to: nameLabel.rx.text)
|
.bind(to: nameLabel.rx.text)
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
|
|
||||||
viewModel.checked
|
model.checked
|
||||||
.bind(to: self.checkButton.rx.isSelected)
|
.bind(to: self.checkButton.rx.isSelected)
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
|
|
||||||
viewModel.checked.subscribe(
|
model.checked.subscribe(
|
||||||
onNext: { [weak self] checked in
|
onNext: { [weak self] checked in
|
||||||
self?.checkButton.tintColor = checked ? BKColor.lightBlue.darken3 : BKColor.grey.base
|
self?.checkButton.tintColor = checked ? BKColor.lightBlue.darken3 : BKColor.grey.base
|
||||||
}).disposed(by: rx.reuseBag)
|
}).disposed(by: rx.reuseBag)
|
||||||
|
|||||||
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import Material
|
import Material
|
||||||
import UIKit
|
import UIKit
|
||||||
class MessageTableViewCell: BaseTableViewCell {
|
class MessageTableViewCell: BaseTableViewCell<MessageTableViewCellViewModel> {
|
||||||
let backgroundPanel: UIView = {
|
let backgroundPanel: UIView = {
|
||||||
let view = UIView()
|
let view = UIView()
|
||||||
view.layer.cornerRadius = 3
|
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)
|
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)
|
model.title.bind(to: self.titleLabel.rx.text).disposed(by: rx.reuseBag)
|
||||||
viewModel.url.map { $0.count <= 0 }.bind(to: self.urlLabel.rx.isHidden).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
|
self.urlLabel.gestureRecognizers?.first?.rx.event
|
||||||
.map { [weak self] _ in self?.urlLabel.text ?? "" }
|
.map { [weak self] _ in self?.urlLabel.text ?? "" }
|
||||||
.bind(to: viewModel.urlTap).disposed(by: rx.reuseBag)
|
.bind(to: model.urlTap).disposed(by: rx.reuseBag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
class MutableTextCell: BaseTableViewCell {
|
class MutableTextCell: BaseTableViewCell<MutableTextCellViewModel> {
|
||||||
|
|
||||||
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
||||||
super.init(style: UITableViewCell.CellStyle.value1, reuseIdentifier: reuseIdentifier)
|
super.init(style: UITableViewCell.CellStyle.value1, reuseIdentifier: reuseIdentifier)
|
||||||
@ -23,13 +23,11 @@ class MutableTextCell: BaseTableViewCell {
|
|||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override func bindViewModel(model: ViewModel) {
|
override func bindViewModel(model: MutableTextCellViewModel) {
|
||||||
super.bindViewModel(model: model)
|
super.bindViewModel(model: model)
|
||||||
guard let viewModel = model as? MutableTextCellViewModel else {
|
|
||||||
return
|
self.textLabel?.text = model.title
|
||||||
}
|
model.text
|
||||||
self.textLabel?.text = viewModel.title
|
|
||||||
viewModel.text
|
|
||||||
.drive(self.detailTextLabel!.rx.text)
|
.drive(self.detailTextLabel!.rx.text)
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
import Material
|
import Material
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
class PreviewCardCell: BaseTableViewCell {
|
class PreviewCardCell: BaseTableViewCell<PreviewCardCellViewModel> {
|
||||||
let previewButton = IconButton(image: Icon.cm.skipForward, tintColor: BKColor.grey.base)
|
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)
|
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())
|
noticeLabel.addGestureRecognizer(UITapGestureRecognizer())
|
||||||
}
|
}
|
||||||
|
|
||||||
override func bindViewModel(model: ViewModel) {
|
override func bindViewModel(model: PreviewCardCellViewModel) {
|
||||||
guard let viewModel = model as? PreviewCardCellViewModel else {
|
super.bindViewModel(model: model)
|
||||||
return
|
|
||||||
}
|
model.title
|
||||||
viewModel.title
|
|
||||||
.bind(to: self.titleLabel.rx.text).disposed(by: rx.reuseBag)
|
.bind(to: self.titleLabel.rx.text).disposed(by: rx.reuseBag)
|
||||||
viewModel.body
|
model.body
|
||||||
.bind(to: self.bodyLabel.rx.text).disposed(by: rx.reuseBag)
|
.bind(to: self.bodyLabel.rx.text).disposed(by: rx.reuseBag)
|
||||||
viewModel.content
|
model.content
|
||||||
.bind(to: self.contentLabel.rx.attributedText).disposed(by: rx.reuseBag)
|
.bind(to: self.contentLabel.rx.attributedText).disposed(by: rx.reuseBag)
|
||||||
viewModel.notice
|
model.notice
|
||||||
.bind(to: self.noticeLabel.rx.attributedText).disposed(by: rx.reuseBag)
|
.bind(to: self.noticeLabel.rx.attributedText).disposed(by: rx.reuseBag)
|
||||||
viewModel.contentImage
|
model.contentImage
|
||||||
.compactMap { $0 }
|
.compactMap { $0 }
|
||||||
.bind(to: self.contentImageView.rx.image)
|
.bind(to: self.contentImageView.rx.image)
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
viewModel.contentImage
|
model.contentImage
|
||||||
.map { $0 == nil }
|
.map { $0 == nil }
|
||||||
.bind(to: self.contentImageView.rx.isHidden)
|
.bind(to: self.contentImageView.rx.isHidden)
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
@ -170,14 +169,14 @@ class PreviewCardCell: BaseTableViewCell {
|
|||||||
// 仅在有 moreViewModel 时 点击
|
// 仅在有 moreViewModel 时 点击
|
||||||
weakModel?.previewModel.moreViewModel
|
weakModel?.previewModel.moreViewModel
|
||||||
}
|
}
|
||||||
.bind(to: viewModel.noticeTap)
|
.bind(to: model.noticeTap)
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
|
|
||||||
// 点击复制
|
// 点击复制
|
||||||
copyButton.rx.tap.map { [weak self] () -> String in
|
copyButton.rx.tap.map { [weak self] () -> String in
|
||||||
self?.contentLabel.text ?? ""
|
self?.contentLabel.text ?? ""
|
||||||
}
|
}
|
||||||
.bind(to: viewModel.copy)
|
.bind(to: model.copy)
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
|
|
||||||
// 点击预览
|
// 点击预览
|
||||||
@ -189,7 +188,7 @@ class PreviewCardCell: BaseTableViewCell {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
.bind(to: viewModel.preview)
|
.bind(to: model.preview)
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import AVKit
|
|||||||
import Material
|
import Material
|
||||||
import UIKit
|
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 copyButton = IconButton(image: UIImage(named: "baseline_file_copy_white_24pt"), tintColor: BKColor.grey.base)
|
||||||
let nameLabel: UILabel = {
|
let nameLabel: UILabel = {
|
||||||
let label = UILabel()
|
let label = UILabel()
|
||||||
@ -54,23 +54,20 @@ class SoundCell: BaseTableViewCell {
|
|||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override func bindViewModel(model: ViewModel) {
|
override func bindViewModel(model: SoundCellViewModel) {
|
||||||
super.bindViewModel(model: model)
|
super.bindViewModel(model: model)
|
||||||
guard let viewModel = model as? SoundCellViewModel else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
viewModel.name
|
model.name
|
||||||
.bind(to: nameLabel.rx.text)
|
.bind(to: nameLabel.rx.text)
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
viewModel.duration
|
model.duration
|
||||||
.map { String(format: "%.2g second(s)", CMTimeGetSeconds($0)) }
|
.map { String(format: "%.2g second(s)", CMTimeGetSeconds($0)) }
|
||||||
.bind(to: durationLabel.rx.text)
|
.bind(to: durationLabel.rx.text)
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
|
|
||||||
copyButton.rx.tap
|
copyButton.rx.tap
|
||||||
.map { viewModel.name.value }
|
.map { model.name.value }
|
||||||
.bind(to: viewModel.copyNameAction)
|
.bind(to: model.copyNameAction)
|
||||||
.disposed(by: rx.reuseBag)
|
.disposed(by: rx.reuseBag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user