import UIKit
class OnlineChannelCardCell: UICollectionViewCell, GeneralListViewCellOverallDataSettable, SelectableView, BaseDeviceUIItem {
lazy var isDeviceListOld: Bool = {
return TPAppContextFactory.shared().isVmsDeviceListOldVersion
}()
typealias T = LDeviceChannel
var collecAction: ((Bool, LDevice, LChannel) -> Void)?
lazy var background = SelectionBackground()
private lazy var channelView = CoverImageBigContainerView.createFromNib()!
private lazy var infoContainer = ListCellDeviceInfoLabelContainer()
private var supportEditDeviceConfigRemote: Bool{
get{
return TPSSAppContext.shared.currentRolePermission.deviceEditDeviceConfigRemote
}
}
private var supportEditDeviceConfigLocal: Bool{
get {
return TPSSAppContext.shared.currentRolePermission.deviceEditDeviceConfigLocal
}
}
lazy var settingButton: UIButton = HorizontalLayoutResistantButton(type: .system)
private var infoFieldContainerHeightConstraint: NSLayoutConstraint!
private lazy var infoFieldBackground = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.backgroundColor = .tpbCard
background.backgroundColor = .tpbCard
contentView.addEqualSizeSubview(background)
infoFieldBackground.translatesAutoresizingMaskIntoConstraints = false
background.addSubview(infoFieldBackground)
background.addEqualConstraints(view: infoFieldBackground, attributes: [.leading, .trailing, .top])
infoFieldContainerHeightConstraint = NSLayoutConstraint(item: infoFieldBackground, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: 60)
infoFieldContainerHeightConstraint.isActive = true
let container = UIView(frame: .zero)
container.translatesAutoresizingMaskIntoConstraints = false
background.addSubview(container)
NSLayoutConstraint(item: container, attribute: .leading, relatedBy: .equal, toItem: background, attribute: .leading, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: container, attribute: .trailing, relatedBy: .equal, toItem: background, attribute: .trailing, multiplier: 1, constant: -0).isActive = true
NSLayoutConstraint(item: container, attribute: .bottom, relatedBy: .equal, toItem: background, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: container, attribute: .top, relatedBy: .equal, toItem: infoFieldBackground, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
container.addEqualSizeSubview(channelView)
infoFieldBackground.addSubview(infoContainer)
infoContainer.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: infoContainer, attribute: .leading, relatedBy: .equal, toItem: infoFieldBackground, attribute: .leading, multiplier: 1, constant: 16).isActive = true
NSLayoutConstraint(item: infoContainer, attribute: .centerY, relatedBy: .equal, toItem: infoFieldBackground, attribute: .centerY, multiplier: 1, constant: 0).isActive = true
settingButton.setBackgroundImage(TPImageLiteral("common_light_more_nor").fixSize(withWidth: 24), for: .normal)
settingButton.contentMode = .scaleAspectFit
let buttonContainer = OAStackView(arrangedSubviews: [settingButton])
buttonContainer.spacing = 8
buttonContainer.translatesAutoresizingMaskIntoConstraints = false
infoFieldBackground.addSubview(buttonContainer)
NSLayoutConstraint(item: buttonContainer, attribute: .trailing, relatedBy: .equal, toItem: infoFieldBackground, attribute: .trailing, multiplier: 1, constant: -12).isActive = true
NSLayoutConstraint(item: buttonContainer, attribute: .centerY, relatedBy: .equal, toItem: infoFieldBackground, attribute: .centerY, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: buttonContainer, attribute: .leading, relatedBy: .equal, toItem: infoContainer, attribute: .trailing, multiplier: 1, constant: 8).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func apply(_ item: LDeviceChannel, at indexPath: IndexPath) {
self.indexPath = indexPath
if let channel = item.channel {
// NVR Channel
channelView.nvrChannelNameContainer.isHidden = false
channelView.nvrChannelNameLabel.text = channel.name
channelView.collectAction = { [weak self] (isCollected) in
self?.doCollectAction(isCollected, item.device, channel)
}
channelView.configure(channel: channel, nvr: item.device, size: .extraLarge, isCard: true, folded: false)
infoFieldContainerHeightConstraint.constant = 0
infoFieldBackground.isHidden = true
}
let appContext = TPAppContextFactory.shared()
if appContext.isVmsLogin {
if isDeviceListOld {
if appContext.isLogin && item.device.listType == .remote{
settingButton.isHidden = true
}
}
else {
//新体系下,判断是否有本地修改设备设置的权限
let accessLocal: Bool = supportEditDeviceConfigLocal && TPSSAppContext.shared.localDeviceMac.contains(item.channel?.mac ?? "")
settingButton.isHidden = ((item.channel?.vmsRoleType != .admin && TPSSAppContext.shared.isVmsRolePermissionOldVersion) || (!(supportEditDeviceConfigRemote || accessLocal) && !TPSSAppContext.shared.isVmsRolePermissionOldVersion) ) && appContext.isVmsLogin && item.device.listType != .local
}
}
}
private func doCollectAction(_ collect: Bool, _ device: LDevice, _ channel: LChannel) {
collecAction?(collect, device, channel)
}
override func layoutSubviews() {
super.layoutSubviews()
var bp1: UIBezierPath? = nil
let cornerRaduis: CGFloat = kBorderRadius
//四个圆角
bp1 = UIBezierPath.init(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: cornerRaduis, height: cornerRaduis))
if indexPath.row == 0 {
if bp1 != nil{
let maskLayer = CAShapeLayer.init()
maskLayer.path = bp1?.cgPath
background.layer.mask = maskLayer
}
//底下两圆角
var bp2: UIBezierPath? = nil
bp2 = UIBezierPath.init(roundedRect: bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: cornerRaduis, height: cornerRaduis))
if bp2 != nil{
let maskLayer = CAShapeLayer.init()
maskLayer.path = bp2?.cgPath
self.layer.mask = maskLayer
}
}
else {
if bp1 != nil{
background.layer.mask = nil
let maskLayer = CAShapeLayer.init()
maskLayer.path = bp1?.cgPath
self.layer.mask = maskLayer
}
}
}
}