//
import UIKit
+import Kingfisher
class HomeDeviceVC:BaseVC{
private var collectionView:UICollectionView!
v.delegate = self
v.backgroundColor = .clear
v.bounces = true
+ v.isHidden = true
v.contentInset = UIEdgeInsets(top: 0, left: 18, bottom: 0, right: 18)
- v.register(HomeDeviceCell.self, forCellWithReuseIdentifier: HomeDeviceCell.reuseID)
+ v.register(DcHomeDeviceCell.self, forCellWithReuseIdentifier: DcHomeDeviceCell.reuseID)
+ v.register(DcHomeAddCell.self, forCellWithReuseIdentifier: DcHomeAddCell.reuseID)
v.register(HomeHeadInfoView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier:HomeHeadInfoView.reuseID )
v.register(HomeAddDeviceView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier:HomeAddDeviceView.reuseID )
}
}
func reloadData() {
DeviceVM.shared.refresh {[weak self] result in
+ self?.collectionView.isHidden = false
self?.collectionView.reloadData()
if CCBleManager.shared.isConnetd{
return
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return DeviceVM.shared.dataList.count
+ if DeviceVM.shared.dataList.count > 0 {
+ return DeviceVM.shared.dataList.count + 1
+ }
+ else{
+ return 0
+ }
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = HomeDeviceCell.cell(withCollectionView: collectionView, indexPath: indexPath)
- cell.model = DeviceVM.shared.dataList[indexPath.row]
- cell.longPress = {[weak self] model in
- self?.unbindAction(model)
+ if DeviceVM.shared.dataList.count > 0 && indexPath.row == DeviceVM.shared.dataList.count {
+ let cell = DcHomeAddCell.cell(withCollectionView: collectionView, indexPath: indexPath)
+ return cell
+ }
+ else{
+ let cell = DcHomeDeviceCell.cell(withCollectionView: collectionView, indexPath: indexPath)
+ cell.model = DeviceVM.shared.dataList[indexPath.row]
+ cell.longPress = {[weak self] model in
+ self?.unbindAction(model)
+ }
+ return cell
}
- return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- let model = DeviceVM.shared.dataList[indexPath.row]
- App.shared.goToPage(AiFunctionVC(device: model))
+ if indexPath.row == DeviceVM.shared.dataList.count {
+ App.shared.goToPage(BleScanVC())
+ }
+ else{
+ let model = DeviceVM.shared.dataList[indexPath.row]
+ App.shared.goToPage(AiFunctionVC(device: model))
+ }
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView{
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
- return CGSize(width: kScreenWid, height: 160)
+ return CGSize(width: kScreenWid, height: 128)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: kScreenWid, height: 82)
}
}
+
+
+
+class DcHomeDeviceCell: BaseCollectionViewCell {
+ let bgView = UIView()
+ let icon = UIImageView()
+ let titleLabel = UILabel()
+ let valueLabel = UILabel()
+ var longPress:((_ model:HomeDeviceModel)->Void)?
+
+ var model:HomeDeviceModel!{
+ didSet{
+ titleLabel.text = model.productName
+
+ icon.kf.setImage(with: model.image.yc.asUrl)
+
+ if CCBleManager.shared.isConected(mac: model.mac) {
+ valueLabel.text = "已连接".l10n
+ bgView.alpha = 1
+ }
+ else{
+ valueLabel.text = "未连接".l10n
+ bgView.alpha = 0.5
+ }
+ }
+ }
+ var enable:Bool = false{
+ didSet{
+ if enable {
+ alpha = 1
+ }
+ else{
+ alpha = 0.5
+ }
+ }
+ }
+
+ override func setupViews() {
+ addSubview(bgView)
+ bgView.addSubviews([titleLabel,icon,valueLabel])
+ bgView.do { v in
+ v.backgroundColor = .white
+ v.yc.setCornerRadius(20)
+ }
+ icon.do { v in
+ // v.backgroundColor = .lightGray
+ v.contentMode = .scaleAspectFit
+ }
+ titleLabel.do { (v) in
+ v.textColor = .rgb(0x121212)
+ v.font = .bold(14)
+ v.adjustsFontSizeToFitWidth = true
+ }
+ valueLabel.do { (v) in
+ v.textColor = .color(.placeholder)
+ v.font = .regular(12)
+ }
+ let longTap = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
+ bgView.addGestureRecognizer(longTap)
+ }
+
+ override func setupLayout() {
+ bgView.snp.makeConstraints { make in
+ make.left.right.top.bottom.equalToSuperview()
+ }
+
+ titleLabel.snp.makeConstraints { (m) in
+ m.top.equalToSuperview().inset(80)
+ m.left.right.equalToSuperview().inset(15)
+ }
+ valueLabel.snp.makeConstraints { (m) in
+ m.top.equalTo(titleLabel.snp.bottom).offset(2)
+ m.left.equalTo(titleLabel)
+ }
+ icon.snp.makeConstraints { (m) in
+ m.width.equalTo(80)
+ m.height.equalTo(60)
+ m.left.equalToSuperview().inset(0)
+ m.top.equalToSuperview().inset(17)
+ }
+ }
+
+ @objc func handleLongPress(_ gesture: UILongPressGestureRecognizer) {
+ if gesture.state == .began {
+ CustomAlert.showWith(title: "提示".l10n, messgage: "确定要解绑该设备吗?".l10n, cancelCallback: {}) {[weak self] in
+ guard let self = self else{return}
+ longPress?(model)
+ }
+ } else if gesture.state == .ended {
+ print("长按结束")
+ }
+ }
+
+}
+fileprivate class DcHomeAddCell: BaseCollectionViewCell {
+ let bgView = UIView()
+ let icon = UIImageView()
+ let titleLabel = UILabel()
+
+
+ override func setupViews() {
+ addSubview(bgView)
+ bgView.addSubviews([titleLabel,icon])
+ bgView.do { v in
+ v.backgroundColor = .white
+ v.yc.setCornerRadius(20)
+ }
+ icon.do { v in
+ // v.backgroundColor = .lightGray
+ v.contentMode = .scaleAspectFit
+ v.image = UIImage(named: "add_device")
+ }
+ titleLabel.do { (v) in
+ v.textColor = .color(.secondary)
+ v.font = .semiBold(14)
+ v.adjustsFontSizeToFitWidth = true
+ v.text = "添加新设备".l10n
+ }
+
+ }
+
+ override func setupLayout() {
+ bgView.snp.makeConstraints { make in
+ make.left.right.top.bottom.equalToSuperview()
+ }
+ icon.snp.makeConstraints { (m) in
+ m.size.equalTo(24)
+ m.left.equalToSuperview().inset(30)
+ m.centerY.equalToSuperview()
+ }
+ titleLabel.snp.makeConstraints { (m) in
+ m.centerY.equalToSuperview()
+ m.right.equalToSuperview().inset(15)
+ m.left.equalTo(64)
+ }
+
+ }
+
+
+}