// // DailSyncSubVC.swift // HDFwear // // Created by yangbin on 2022/3/12. // import UIKit import Kingfisher import Alamofire import MBProgressHUD class DailSyncSubVC: UIViewController { @IBOutlet weak var collectionView: UICollectionView! @IBOutlet weak var collectionViewFlowLayout: UICollectionViewFlowLayout! var syncDialClosure: ((_ indexPath: IndexPath) -> ())? var dialNum: Int = 9 var dialWidth: Int = 240 var dialHeight: Int = 280 var identifierDic: [String: String] = [:] var user = UserInfo var styleIndex: Int = 0 var dialIndex: Int = -1 override func navigationShouldPop() -> Bool { if !IsDailSyncing { return true } else { MBProgressHUD.showh(LocString("正在推送表盘中,请稍后...")) return false } } 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() 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) } func showAlert() -> Bool { if !DeviceIsConnected { IsDailSyncing = false self.showAlert(title: LocString("设备已断开"), message: "", cancelText: nil) {[weak self] action in self?.navigationController?.popToRootViewController(animated: true) } cancel: { _ in } return true } else { return false } } func downloadDailFile(_ indexPath: IndexPath) { if showAlert() { return } if IsDailSyncing { return } let cell = self.collectionView.cellForItem(at: indexPath) as! DailCell if cell.progressView.label.text == LocString("已安装") { return } updateUserInteraction(true) cell.updateProgressView(0, isDownload: true) let dest: DownloadRequest.DownloadFileDestination = { _, _ in let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] let fileURL = documentsURL.appendingPathComponent("dial.bin") return (fileURL, [.removePreviousFile, .createIntermediateDirectories]) } Alamofire.download("http://www.hodafone.com.cn/hodafone/dial/rtl/\(dialWidth)x\(dialHeight)/\(styleIndex)/\(indexPath.row)/dial.bin", to: dest).downloadProgress { progress in cell.updateProgressView(Int(progress.fractionCompleted*5), isDownload: true) }.responseData {[weak self] response in if let _ = response.value { self?.syncDail(indexPath, path: response.destinationURL!.path) } else { cell.initProgressView() self?.updateUserInteraction(false) self?.showAlert(title: LocString("提示"), message: LocString("安装失败,请重试"), cancelText: nil) } } } private func syncDail(_ indexPath: IndexPath, path: String) { if showAlert() { return } if !FileManager.default.fileExists(atPath: path) { updateUserInteraction(false) showAlert(title: LocString("提示"), message: LocString("安装失败,请重试"), cancelText: nil) return } let cell = self.collectionView.cellForItem(at: indexPath) as! DailCell BluetoothManager.shared.syncFileWith(path: path) { progress in cell.updateProgressView(Int(Double(progress)*0.95)+5) } completion: {[weak self] error in self?.updateUserInteraction(false) if error == nil { self?.syncComplete(indexPath: indexPath) cell.syncComplete() } else { cell.initProgressView() self?.showAlert(title: LocString("提示"), message: LocString("安装失败,请重试"), cancelText: nil) } } } func syncComplete(indexPath: IndexPath) { dialIndex = indexPath.row collectionView.reloadData() // user.isDailSync = true // user.dialIndexPath = "\(indexPath.section)-\(indexPath.row)" syncDialClosure?(IndexPath(row: indexPath.row, section: styleIndex)) // AdminHelper.shared.updateUser(user) } func updateUserInteraction(_ isSyncing: Bool) { UIApplication.shared.isIdleTimerDisabled = isSyncing IsDailSyncing = isSyncing let navController = navigationController as? ZCNavigationController navController?.enableScreenEdgePanGestureRecognizer(!isSyncing) } func clearCache() { let cache = ImageCache.default cache.clearMemoryCache() cache.clearDiskCache { DispatchQueue.main.async { print("s.清除缓存成功") } } } deinit { print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!") } } extension DailSyncSubVC: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dialNum } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let identifier: String = "DailCell\(indexPath.row)\(indexPath.section)" if identifierDic[identifier] == nil { collectionView.register(UINib(nibName: "DailCell", bundle: .main), forCellWithReuseIdentifier: identifier) identifierDic[identifier] = identifier } let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! DailCell switch CurDevice.platform { case ._828: cell.dailImageView.kf.setImage(with: URL(string: "http://www.hodafone.com.cn/hodafone/dial/rtl/\(dialWidth)x\(dialHeight)/\(styleIndex)/\(indexPath.row)/preview.png"), placeholder: UIImage(named: "dail_default_3"), options: [.transition(.fade(0.3))]) // cell.dailImageView.image = UIImage(contentsOfFile: "\(Bundle.main.resourcePath!)/dial/dialrtkpreviewHQ7/\(indexPath.row)/preview") default: break } cell.cellClickClosure = {[weak self] in self?.downloadDailFile(indexPath) } if indexPath.row == dialIndex { cell.syncComplete() } else { cell.initProgressView() } return cell } }