PreviewCardCellViewModel.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // PreviewCardCellViewModel.swift
  3. // Bark
  4. //
  5. // Created by huangfeng on 2020/11/23.
  6. // Copyright © 2020 Fin. All rights reserved.
  7. //
  8. import Foundation
  9. import Material
  10. import RxCocoa
  11. class PreviewCardCellViewModel: ViewModel {
  12. let title = BehaviorRelay(value: "")
  13. let body = BehaviorRelay(value: "")
  14. let content = BehaviorRelay(value: NSAttributedString())
  15. let notice = BehaviorRelay(value: NSAttributedString())
  16. let contentImage: BehaviorRelay<UIImage?>
  17. let noticeTap = PublishRelay<ViewModel>()
  18. let copy = PublishRelay<String>()
  19. let preview = PublishRelay<URL>()
  20. let previewModel: PreviewModel
  21. init(previewModel: PreviewModel, clientState: Driver<Client.ClienState>) {
  22. self.previewModel = previewModel
  23. contentImage = BehaviorRelay<UIImage?>(value: previewModel.image)
  24. super.init()
  25. if let modelTitle = previewModel.title {
  26. title.accept(modelTitle)
  27. }
  28. if let modelBody = previewModel.body {
  29. body.accept(modelBody)
  30. }
  31. // client State 更改时,重新生成 content
  32. // 因为这时可能 ServerManager.shared.currentAddress 或 Client.shared.key 发生了改变。
  33. // 这不是一个好的写法,viewModel 应尽可能只依赖固定的 input ,而不应依赖不可预测的外部变量( currentAddress 与 key )。
  34. // 但这个项目是由 MVC 临时重构为 MVVM ,之前是这样写的,所以懒得改动了。
  35. clientState.compactMap { [weak self] _ -> NSAttributedString? in
  36. self?.contentAttrStr()
  37. }
  38. .drive(content)
  39. .disposed(by: rx.disposeBag)
  40. let noticeStr = "\(previewModel.notice ?? "")"
  41. let noticeAttrStr = NSMutableAttributedString(string: noticeStr, attributes: [
  42. NSAttributedString.Key.foregroundColor: BKColor.grey.base,
  43. NSAttributedString.Key.font: RobotoFont.regular(with: 12)
  44. ])
  45. if let moreInfo = previewModel.moreInfo {
  46. noticeAttrStr.append(NSMutableAttributedString(string: " \(moreInfo)", attributes: [
  47. NSAttributedString.Key.foregroundColor: BKColor.blue.base,
  48. NSAttributedString.Key.font: RobotoFont.regular(with: 12)
  49. ]))
  50. }
  51. notice.accept(noticeAttrStr)
  52. }
  53. func contentAttrStr() -> NSAttributedString {
  54. var fontSize: CGFloat = 14
  55. if UIScreen.main.bounds.size.width <= 320 {
  56. fontSize = 11
  57. }
  58. let serverUrl = URL(string: ServerManager.shared.currentServer.address)!
  59. let attrStr = NSMutableAttributedString(string: "")
  60. attrStr.append(NSAttributedString(string: serverUrl.absoluteString, attributes: [
  61. NSAttributedString.Key.foregroundColor: BKColor.grey.darken4,
  62. NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
  63. ]))
  64. let key = ServerManager.shared.currentServer.key
  65. attrStr.append(NSAttributedString(string: "/\(key.count > 0 ? key : "Your Key")", attributes: [
  66. NSAttributedString.Key.foregroundColor: BKColor.grey.darken3,
  67. NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
  68. ]))
  69. if let modelTitle = previewModel.title {
  70. attrStr.append(NSAttributedString(string: "/\(modelTitle)", attributes: [
  71. NSAttributedString.Key.foregroundColor: BKColor.grey.darken1,
  72. NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
  73. ]))
  74. }
  75. if let modelBody = previewModel.body {
  76. attrStr.append(NSAttributedString(string: "/\(modelBody)", attributes: [
  77. NSAttributedString.Key.foregroundColor: BKColor.grey.base,
  78. NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
  79. ]))
  80. }
  81. if let queryParameter = previewModel.queryParameter {
  82. attrStr.append(NSAttributedString(string: "?\(queryParameter)", attributes: [
  83. NSAttributedString.Key.foregroundColor: BKColor.grey.lighten1,
  84. NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
  85. ]))
  86. }
  87. return attrStr
  88. }
  89. }