Bark/View/PreviewCardCellViewModel.swift

102 lines
4.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// PreviewCardCellViewModel.swift
// Bark
//
// Created by huangfeng on 2020/11/23.
// Copyright © 2020 Fin. All rights reserved.
//
import Foundation
import Material
import RxCocoa
class PreviewCardCellViewModel: ViewModel {
let title = BehaviorRelay(value: "")
let body = BehaviorRelay(value: "")
let content = BehaviorRelay(value: NSAttributedString())
let notice = BehaviorRelay(value: NSAttributedString())
let contentImage: BehaviorRelay<UIImage?>
let noticeTap = PublishRelay<ViewModel>()
let copy = PublishRelay<String>()
let preview = PublishRelay<URL>()
let previewModel: PreviewModel
init(previewModel: PreviewModel, clientState: Driver<Client.ClienState>) {
self.previewModel = previewModel
contentImage = BehaviorRelay<UIImage?>(value: previewModel.image)
super.init()
if let modelTitle = previewModel.title {
title.accept(modelTitle)
}
if let modelBody = previewModel.body {
body.accept(modelBody)
}
// client State content
// ServerManager.shared.currentAddress Client.shared.key
// viewModel input currentAddress key
// MVC MVVM
clientState.compactMap { [weak self] _ -> NSAttributedString? in
self?.contentAttrStr()
}
.drive(content)
.disposed(by: rx.disposeBag)
let noticeStr = "\(previewModel.notice ?? "")"
let noticeAttrStr = NSMutableAttributedString(string: noticeStr, attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.base,
NSAttributedString.Key.font: RobotoFont.regular(with: 12)
])
if let moreInfo = previewModel.moreInfo {
noticeAttrStr.append(NSMutableAttributedString(string: " \(moreInfo)", attributes: [
NSAttributedString.Key.foregroundColor: BKColor.blue.base,
NSAttributedString.Key.font: RobotoFont.regular(with: 12)
]))
}
notice.accept(noticeAttrStr)
}
func contentAttrStr() -> NSAttributedString {
var fontSize: CGFloat = 14
if UIScreen.main.bounds.size.width <= 320 {
fontSize = 11
}
let serverUrl = URL(string: ServerManager.shared.currentServer.address)!
let attrStr = NSMutableAttributedString(string: "")
attrStr.append(NSAttributedString(string: serverUrl.absoluteString, attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.darken4,
NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
]))
let key = ServerManager.shared.currentServer.key
attrStr.append(NSAttributedString(string: "/\(key.count > 0 ? key : "Your Key")", attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.darken3,
NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
]))
if let modelTitle = previewModel.title {
attrStr.append(NSAttributedString(string: "/\(modelTitle)", attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.darken1,
NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
]))
}
if let modelBody = previewModel.body {
attrStr.append(NSAttributedString(string: "/\(modelBody)", attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.base,
NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
]))
}
if let queryParameter = previewModel.queryParameter {
attrStr.append(NSAttributedString(string: "?\(queryParameter)", attributes: [
NSAttributedString.Key.foregroundColor: BKColor.grey.lighten1,
NSAttributedString.Key.font: RobotoFont.regular(with: fontSize)
]))
}
return attrStr
}
}