// // ContactsViewController.swift // Twear // // Created by yangbin on 2022/1/15. // import UIKit import MBProgressHUD class ContactsViewController: UIViewController { @IBOutlet weak var tableView: UITableView! @IBOutlet weak var noContactsView: UIView! @IBOutlet weak var syncButton: CustomProgress! var contacts: [ContactModel] = UserInfo.contacts // var isEdit: Bool = false lazy var footerView: UIView = { let view = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 50)) let button = UIButton(frame: CGRect(x: 0, y: 10, width: 54, height: 30)) button.addTarget(self, action: #selector(add), for: .touchUpInside) button.setImage(UIImage(named: "contact_add"), for: .normal) view.addSubview(button) return view }() lazy var headerView: UIView = { let view = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 45)) let label = UILabel(frame: CGRect(x: 12.5, y: 2.5, width: SCREEN_WIDTH-30, height: 40)) label.font = RegularFont(11) label.textColor = UIColor.rgbColorFromHex(0x808080) label.numberOfLines = 0 label.text = LocString("可以选定通讯录中的8个联系人,将其快捷方式显示在手表上。") view.addSubview(label) return view }() lazy var rightButton: UIButton = { let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28)) addButton.setImage(UIImage(named: "device_add"), for: .normal) addButton.setTitle(nil, for: .normal) addButton.titleLabel?.contentMode = .right addButton.titleLabel?.font = RegularFont(14) addButton.setTitleColor(.black, for: .normal) addButton.addTarget(self, action: #selector(clickRightButton), for: .touchUpInside) return addButton }() override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // contacts = UserInfo.contacts // noContactsView.isHidden = contacts.count > 0 // tableView.isHidden = contacts.count == 0 // tableView.reloadData() } override func viewDidLoad() { super.viewDidLoad() title = LocString("快捷通讯") refreshUI() navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton) tableView.setEditing(true, animated: true) tableView.register(UINib.init(nibName: "ContactCell", bundle: Bundle.main), forCellReuseIdentifier: "ContactCell") tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0) // tableView.tableFooterView = UIView(frame: CGRect.zero) tableView.tableFooterView = footerView tableView.tableHeaderView = headerView // Do any additional setup after loading the view. syncButton.label.text = LocString("传输") syncButton.label.textColor = UIColor.white syncButton.label.font = RegularFont(15) // syncButton.layer.borderWidth = 1 // syncButton.layer.borderColor = UIColor.rgbColorFromHex(0x00993E).cgColor syncButton.backView.backgroundColor = UIColor.rgbColorFromHex(0x00993E) syncButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(syncContacts(_:)))) if contacts.count == 0 { syncButton.isHidden = true } } func updateProgressView(_ value: Int) { if contacts.count > 0 { syncButton.isHidden = false } if value == -1 { syncButton.value = 0 syncButton.label.text = LocString("传输") syncButton.label.textColor = .white syncButton.layer.borderWidth = 0 syncButton.backView.backgroundColor = UIColor.rgbColorFromHex(0x00993E) } else if value < 100 { syncButton.value = value syncButton.backView.backgroundColor = .white syncButton.trackView.backgroundColor = UIColor.rgbColorFromHex(0x64C876) syncButton.layer.borderWidth = 1 syncButton.layer.borderColor = UIColor.rgbColorFromHex(0x00993E).cgColor syncButton.label.text = LocString("联系人数据传输中...") syncButton.label.textColor = .black } else if value == 100 { syncButton.value = 100 syncButton.trackView.backgroundColor = LineColor syncButton.layer.borderColor = LineColor.cgColor syncButton.layer.borderWidth = 0 syncButton.label.text = LocString("传输完成") syncButton.label.textColor = .white let user = UserInfo user.contacts = contacts AdminHelper.shared.updateUser(user) } } @IBAction func syncContacts(_ gestureRecognizer: UITapGestureRecognizer) { //0-2 3-5 6-7 var array = [ContactModel](repeating: ContactModel(), count: 8) for (i, model) in contacts.enumerated() { array[i] = model } updateProgressView(0) sync1(array) } func sync1(_ contactArray: [ContactModel]) { BluetoothManager.shared.syncContacts(Array(contactArray[0...2]), num: 1) {[weak self] error in if error == nil { print("同步联系人1") self?.sync2(contactArray) self?.updateProgressView(33) } else { self?.updateProgressView(-1) } } } func sync2(_ contactArray: [ContactModel]) { BluetoothManager.shared.syncContacts(Array(contactArray[3...5]), num: 2) {[weak self] error in if error == nil { print("同步联系人2") self?.updateProgressView(66) self?.sync3(contactArray) } else { self?.updateProgressView(-1) } } } func sync3(_ contactArray: [ContactModel]) { BluetoothManager.shared.syncContacts(Array(contactArray[6...7]), num: 3) {[weak self] error in if error == nil { print("同步联系人3") self?.updateProgressView(100) } else { self?.updateProgressView(-1) } } } @objc private func add() { let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Setting", identifier: "PhoneContactsVC") as! PhoneContactsVC vc.preContacts = contacts vc.contactClosure = { [weak self] array in self?.selectContacts(array) self?.updateProgressView(-1) } navigationController?.pushViewController(vc, animated: true) } @objc private func clickRightButton() { // if contacts.count == 0 { add() // } else { // isEdit = !isEdit // tableView.setEditing(isEdit, animated: true) // } } func refreshUI() { noContactsView.isHidden = contacts.count > 0 tableView.isHidden = contacts.count == 0 footerView.isHidden = contacts.count == 8 if contacts.count == 0 { rightButton.setImage(UIImage(named: "device_add"), for: .normal) rightButton.setTitle(nil, for: .normal) } else { rightButton.setTitle(LocString("全部修改"), for: .normal) rightButton.setImage(nil, for: .normal) var sos: Bool = false for contact in contacts { if contact.isSOS { sos = true } } if !sos { contacts[0].isSOS = true } } tableView.reloadData() } func selectContacts(_ array: [ContactModel]) { contacts = array refreshUI() } func selectSosContact(_ item: Int) { if contacts[item].isSOS { contacts[item].isSOS = false } else { for contact in contacts { contact.isSOS = false } contacts[item].isSOS = true } updateProgressView(-1) tableView.reloadData() } deinit { print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!") } } extension ContactsViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { return .delete//UITableViewCell.EditingStyle(rawValue: UITableViewCell.EditingStyle.delete.rawValue | UITableViewCell.EditingStyle.insert.rawValue)! } // // func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool { // return false // } // // func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { // let delete = UITableViewRowAction(style: .normal, title: LocString("删除")) {[weak self] action, index in // self?.contacts.remove(at: indexPath.row) // tableView.deleteRows(at: [indexPath], with: .automatic) // } // delete.backgroundColor = UIColor.red // return [delete] // } // func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { contacts.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .automatic) updateProgressView(-1) refreshUI() } } func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { if sourceIndexPath != destinationIndexPath { let contact = contacts[sourceIndexPath.row] contacts.remove(at: sourceIndexPath.row) contacts.insert(contact, at: destinationIndexPath.row) updateProgressView(-1) } tableView.reloadData() } func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { return true } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 50 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return contacts.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as! ContactCell cell.isPhone = false cell.contact = contacts[indexPath.row] cell.sosClosure = {[weak self] in self?.selectSosContact(indexPath.row) } // cell.alarmClock = alarmClockArray[indexPath.row] // cell.switchClosure = {[weak self] isOn in // self?.switchValueChanged(isOn, index: indexPath.row) // } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // let cell = tableView.cellForRow(at: indexPath) as! SettingCell3 // cell.isSelected = false // let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Setting", identifier: "EditAlarmClockVC") as! EditAlarmClockVC // vc.alarmClock = CurDevice.alarmClocks[indexPath.row] // vc.index = indexPath.row // vc.isEdit = true // navigationController?.pushViewController(vc, animated: true) } }