// // SettingViewController.swift // LaserMethane // // Created by 203 on 2021/7/28. // import Alamofire import AudioToolbox import KeychainAccess import SnapKit import SwiftyJSON import UIKit class SettingViewController: UIViewController { @IBOutlet var tableView: UITableView! private var titleArray: [String] = [String]() private var keychain = Keychain() private var userModel: UserInfoModel! override func viewDidLoad() { super.viewDidLoad() title = "系统设置" navigationController?.navigationBar.isTranslucent = false navigationController?.navigationBar.barTintColor = .systemBlue let dict: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white] navigationController?.navigationBar.titleTextAttributes = dict navigationController?.navigationBar.tintColor = .white // Do any additional setup after loading the view. setDefaultData() setupTableView() } func setDefaultData() { titleArray = ["用户名", "手机号", "报警声音/震动", "报警事件自动记录", "查看日志", "版本号"] // 读取用户信息,因为存的是JsonString,所以需要将JsonString转为Data,然后再转为JSON对象,最后转为Model let userJson = keychain[Constant.UserJson.rawValue]! let data = userJson.data(using: String.Encoding.utf8, allowLossyConversion: false) ?? Data() userModel = UserInfoModel(respJson: JSON(data)) } func setupTableView() { tableView.register(UINib(nibName: "DefaultTableViewCell", bundle: nil), forCellReuseIdentifier: "defaultTableViewCell") tableView.register(UINib(nibName: "SwitchTableViewCell", bundle: nil), forCellReuseIdentifier: "switchTableViewCell") tableView.register(UINib(nibName: "CommonTableViewCell", bundle: nil), forCellReuseIdentifier: "commonTableViewCell") tableView.dataSource = self tableView.delegate = self // 添加底部按钮 addFooterButton() } func addFooterButton() { // 先添加View,然后再在View里面添加UIButton let footerView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 57 * 6)) let button = UIButton(type: .roundedRect) button.setTitle("退出登录", for: .normal) button.setTitleColor(.white, for: .normal) button.titleLabel?.font = UIFont.systemFont(ofSize: CGFloat(20)) button.layer.cornerRadius = 18.0 button.backgroundColor = .systemBlue button.addTarget(self, action: #selector(loginOutAction), for: .touchUpInside) footerView.addSubview(button) button.snp.makeConstraints { (make) -> Void in make.size.equalTo(CGSize(width: 320, height: BUTTON_HEIGHT)) make.centerX.equalToSuperview() make.top.equalTo(footerView.frame.height * 0.6) } tableView.tableFooterView = footerView } @objc func loginOutAction(_ sender: UIButton) { let alertController = UIAlertController(title: "提示", message: "确定要退出吗?", preferredStyle: .alert) let cancelAction = UIAlertAction(title: "取消", style: .default, handler: nil) cancelAction.setValue(UIColor.red, forKey: "titleTextColor") let confirmAction = UIAlertAction(title: "确定", style: .default, handler: { [self] _ in // 执行退出登录操作 let loginOutURL = keychain[Constant.ServerConfig.rawValue]! + Constant.loginOut.rawValue Alamofire.request(loginOutURL, method: .get, headers: ["token": keychain[Constant.Token.rawValue]!]) .responseJSON { response in switch response.result { case let .success(value): let loginOutModel = ActionResultModel(respJson: JSON(value)) if loginOutModel.code == 200 { // 删除token keychain[Constant.Token.rawValue] = "" // 回到登录页 let destinationController = LoginViewController(nibName: "LoginViewController", bundle: nil) destinationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen destinationController.modalTransitionStyle = UIModalTransitionStyle.coverVertical present(destinationController, animated: true, completion: nil) } else { AlertHub.shared.showWaringAlert(controller: self, message: "操作失败,请重试") } case .failure: AlertHub.shared.showWaringAlert(controller: self, message: "操作失败,请重试") } } }) alertController.addAction(cancelAction) alertController.addAction(confirmAction) present(alertController, animated: true, completion: nil) } } extension SettingViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 57 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if indexPath.row == 4 { navigationController?.pushViewController(LogViewController(), animated: true) } } } extension SettingViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { titleArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 5 { let cell: DefaultTableViewCell = tableView.dequeueReusableCell(withIdentifier: "defaultTableViewCell", for: indexPath) as! DefaultTableViewCell cell.selectionStyle = .none // 选中时无阴影效果 if indexPath.row == 5 { let infoDictionary: [String: Any] = Bundle.main.infoDictionary! let mainVersion: Any = infoDictionary["CFBundleShortVersionString"] as Any // 主程序版本号 cell.setText(leftText: titleArray[indexPath.row], rightText: (mainVersion as? String)!) } else if indexPath.row == 1 { let phoneNumber = userModel.data?.phone! if phoneNumber == "" { cell.setText(leftText: titleArray[indexPath.row], rightText: "手机号未知") } else { cell.setText(leftText: titleArray[indexPath.row], rightText: phoneNumber!) } } else if indexPath.row == 0 { cell.setText(leftText: titleArray[indexPath.row], rightText: (userModel.data?.name)!) } return cell } else if indexPath.row == 2 || indexPath.row == 3 { let cell: SwitchTableViewCell = tableView.dequeueReusableCell(withIdentifier: "switchTableViewCell", for: indexPath) as! SwitchTableViewCell cell.leftLabel.text = titleArray[indexPath.row] // 判断开关状态 if indexPath.row == 2 { let isOpen = keychain[Constant.OpenWarning.rawValue] ?? "OFF" if isOpen == "ON" { cell.rightSwitch.setOn(true, animated: true) } else { cell.rightSwitch.setOn(false, animated: true) } cell.rightSwitch.addTarget(self, action: #selector(switchWaringAction), for: .valueChanged) } else { let isAuto = keychain[Constant.AutoRecord.rawValue] ?? "OFF" if isAuto == "ON" { cell.rightSwitch.setOn(true, animated: true) } else { cell.rightSwitch.setOn(false, animated: true) } cell.rightSwitch.addTarget(self, action: #selector(autoWriteWarningAction), for: .valueChanged) } cell.selectionStyle = .none return cell } else { let cell: CommonTableViewCell = tableView.dequeueReusableCell(withIdentifier: "commonTableViewCell", for: indexPath) as! CommonTableViewCell cell.leftLabel.text = titleArray[indexPath.row] cell.selectionStyle = .none return cell } } @objc func switchWaringAction(_ sender: UISwitch) { if sender.isOn { keychain[Constant.OpenWarning.rawValue] = "ON" // 振动 AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) } else { keychain[Constant.OpenWarning.rawValue] = "OFF" } } @objc func autoWriteWarningAction(_ sender: UISwitch) { if sender.isOn { keychain[Constant.AutoRecord.rawValue] = "ON" } else { keychain[Constant.AutoRecord.rawValue] = "OFF" } } }