DialSyncLocalVC.swift 4.91 KB
//
//  DialSyncLocalVC.swift
//  HDFwear
//
//  Created by yangbin on 2022/3/16.
//

import UIKit

class DialSyncLocalVC: UIViewController {
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewFlowLayout: UICollectionViewFlowLayout!
    var dialNum: Int = 9
    var identifierDic: [String: String] = [:]
//    var isSyncing: Bool = false
    
//    var tempIndex: Int = -1
//    var syncIndex = UserInfo.dailIndex
//    var isSynced: Bool = UserInfo.isDailSync
    var user = UserInfo
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        let navController = navigationController as? ZCNavigationController
        navController?.enableScreenEdgePanGestureRecognizer(true)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = LocString("表盘推送")
        
        collectionViewFlowLayout.minimumLineSpacing = 10
        collectionViewFlowLayout.minimumInteritemSpacing = 20
        collectionViewFlowLayout.sectionInset = UIEdgeInsets(top: 5, left: 10, bottom: 10+NAVBAR_HEIGHT+10, right: 10)
        collectionViewFlowLayout.itemSize = CGSize(width: (SCREEN_WIDTH-60)/3.0, height: 130)
    }
    
    
    private func syncDail(_ indexPath: IndexPath) {
        if IsDailSyncing { return }
        let cell = self.collectionView.cellForItem(at: indexPath) as! DailCell
        if cell.progressView.label.text == LocString("已安装") { return }
        updateUserInteraction(true)
        cell.updateProgressView(0)
        switch CurDevice.platform {
        case ._828:
            BluetoothManager.shared.syncFileWith(path: "\(Bundle.main.resourcePath!)/dial/DialDataHQ7/\(indexPath.row)/dial.bin") { progress in
                cell.updateProgressView(progress)
            } completion: {[weak self] error in
                self?.updateUserInteraction(false)
                if error == nil {
                    self?.syncComplete(index: indexPath.row)
                    cell.syncComplete()
                } else {
                    cell.initProgressView()
                    self?.showAlert(title: LocString("提示"), message: LocString("安装失败,请重试"), cancelText: nil)
                }
            }
        default:
            break
        }
    }
    
    func syncComplete(index: Int) {
//        if user.dailIndex >= 0 {
//            let cell = collectionView.cellForItem(at: IndexPath(row: user.dailIndex, section: 0)) as! DailCell
//            cell.initProgressView()
//        }
        collectionView.reloadData()
        user.isDailSync = true
        user.dailIndex = index
        AdminHelper.shared.updateUser(user)
    }
    
    func updateUserInteraction(_ isSyncing: Bool) {
//        collectionView.isUserInteractionEnabled = !isSyncing
        UIApplication.shared.isIdleTimerDisabled = isSyncing
        IsDailSyncing = isSyncing
        let navController = navigationController as? ZCNavigationController
        navController?.enableScreenEdgePanGestureRecognizer(!isSyncing)
    }
    
    deinit {
        print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!")
    }
    
}


extension DialSyncLocalVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dialNum
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DailCell", for: indexPath) as! DailCell
        let identifier: String = "DailCell\(indexPath.row)\(indexPath.section)"
        if identifierDic[identifier] == nil {
            collectionView.register(UINib(nibName: "DailCell", bundle: .main), forCellWithReuseIdentifier: identifier)
            identifierDic[identifier] = identifier
        }
        
//        collectionView.register(DailCell.self, forCellWithReuseIdentifier: "DailCell")
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! DailCell
        // 此处可起到防止重用的作用
//        for view in cell.contentView.subviews {
////            if view != nil {
//                view.removeFromSuperview()
////            }
//
//        }
           
        
        switch CurDevice.platform {
        case ._828:
            cell.dailImageView.image = UIImage(contentsOfFile:  "\(Bundle.main.resourcePath!)/dial/dialrtkpreviewHQ7/\(indexPath.row)/preview")
        default:
            break
        }
        
        cell.cellClickClosure = {[weak self] in
            self?.syncDail(indexPath)
        }
        if indexPath.row == user.dailIndex {
            cell.syncComplete()
        } else {
            cell.initProgressView()
        }
        
        return cell
    }
    
}