SoundCell.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // SoundCell.swift
  3. // Bark
  4. //
  5. // Created by huangfeng on 2020/9/14.
  6. // Copyright © 2020 Fin. All rights reserved.
  7. //
  8. import AVKit
  9. import Material
  10. import UIKit
  11. class SoundCell: BaseTableViewCell<SoundCellViewModel> {
  12. let copyButton = IconButton(image: UIImage(named: "baseline_file_copy_white_24pt"), tintColor: BKColor.grey.base)
  13. let nameLabel: UILabel = {
  14. let label = UILabel()
  15. label.fontSize = 14
  16. label.textColor = BKColor.grey.darken4
  17. return label
  18. }()
  19. let durationLabel: UILabel = {
  20. let label = UILabel()
  21. label.fontSize = 12
  22. label.textColor = BKColor.grey.darken1
  23. return label
  24. }()
  25. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  26. super.init(style: style, reuseIdentifier: reuseIdentifier)
  27. self.selectionStyle = .none
  28. self.backgroundColor = BKColor.background.secondary
  29. self.contentView.addSubview(nameLabel)
  30. self.contentView.addSubview(durationLabel)
  31. self.contentView.addSubview(copyButton)
  32. nameLabel.snp.makeConstraints { make in
  33. make.left.top.equalToSuperview().offset(15)
  34. }
  35. durationLabel.snp.makeConstraints { make in
  36. make.left.equalTo(nameLabel)
  37. make.top.equalTo(nameLabel.snp.bottom).offset(5)
  38. make.bottom.equalToSuperview().offset(-15)
  39. }
  40. copyButton.snp.makeConstraints { make in
  41. make.right.equalToSuperview().offset(-15)
  42. make.centerY.equalToSuperview()
  43. make.width.height.equalTo(40)
  44. }
  45. }
  46. @available(*, unavailable)
  47. required init?(coder aDecoder: NSCoder) {
  48. fatalError("init(coder:) has not been implemented")
  49. }
  50. override func bindViewModel(model: SoundCellViewModel) {
  51. super.bindViewModel(model: model)
  52. model.name
  53. .bind(to: nameLabel.rx.text)
  54. .disposed(by: rx.reuseBag)
  55. model.duration
  56. .map { String(format: "%.2g second(s)", CMTimeGetSeconds($0)) }
  57. .bind(to: durationLabel.rx.text)
  58. .disposed(by: rx.reuseBag)
  59. copyButton.rx.tap
  60. .map { model.name.value }
  61. .bind(to: model.copyNameAction)
  62. .disposed(by: rx.reuseBag)
  63. }
  64. }