// // RecordDetailVC.swift // Twear // // Created by yangbin on 2021/12/30. // import UIKit import SwiftDate class RecordDetailVC: UIViewController { @IBOutlet weak var titleTopLayout: NSLayoutConstraint! @IBOutlet weak var tableViewHeight: NSLayoutConstraint! @IBOutlet weak var tableView: UITableView! @IBOutlet weak var timeTitleLabel: UILabel! @IBOutlet weak var distanceTitleLabel: UILabel! @IBOutlet weak var calorieTitleLabel: UILabel! @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var totalDistanceLabel: UILabel! @IBOutlet weak var totalCalorieLabel: UILabel! @IBOutlet weak var totalTimeLabel: UILabel! @IBOutlet weak var totalNumberLabel: UILabel! @IBOutlet weak var maxDistanceLabel: UILabel! @IBOutlet weak var maxCalorieLabel: UILabel! @IBOutlet weak var maxTimeLabel: UILabel! @IBOutlet weak var scrollTopLayout: NSLayoutConstraint! var motionType: TrainType = .running private var page: Int = 1 private let nowDate = DateInRegion() private var motionList: [MotionRecord] = [] override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(true, animated: true) } override func viewDidLoad() { super.viewDidLoad() titleTopLayout.constant = StatusBarHeight + 10 scrollTopLayout.constant = StatusBarHeight calorieTitleLabel.text = LocString("单次最高热量(千卡)") switch motionType { case .running: titleLabel.text = LocString("跑步记录") timeTitleLabel.text = LocString("单次最长用时") distanceTitleLabel.text = LocString("单次最远距离(公里)") case .walking: titleLabel.text = LocString("步行记录") timeTitleLabel.text = LocString("单次最长用时") distanceTitleLabel.text = LocString("单次最远距离(公里)") case .bicycle: titleLabel.text = LocString("骑行记录") timeTitleLabel.text = LocString("单次最长用时") distanceTitleLabel.text = LocString("单次最远距离(公里)") case .mountaineering: titleLabel.text = LocString("爬山记录") timeTitleLabel.text = LocString("单次最远距离(公里)") distanceTitleLabel.text = LocString("单次最高爬升(米)") default: break } tableView.register(UINib.init(nibName: "MotionRecordCell", bundle: Bundle.main), forCellReuseIdentifier: "MotionRecordCell") tableView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 10, right: 0) tableView.tableFooterView = UIView(frame: CGRect.zero) getMotionRecordList() // Do any additional setup after loading the view. } @IBAction func back(_ sender: Any) { navigationController?.popViewController(animated: true) } private func getMotionRecordList(page: Int = 1) { if page == 1 { motionList = [] } var totalMotions: [MotionModel] = [] for i in 12*(page-1)..<12*(page-1)+12 { let motionArray = MotionModel.getMonthData(motionType.rawValue, date: (nowDate-i.months).date) // if 12-i == 10 || 12-i == 9 || 12-i == 8 || 12-i == 7 { // let t1 = MotionModel(type: .running, date: DateInRegion().date-100, length: 100, calorie: 200, distance: 300, steps: 500, altitude: 2) // let t2 = MotionModel(type: .steppers, date: DateInRegion().date-300.minutes, length: 66, calorie: 1000, distance: 300, steps: 500, altitude: 2) // let t3 = MotionModel(type: .steppers, date: DateInRegion().date+7.hours, length: 1000, calorie: 1000, distance: 300, steps: 500, altitude: 2) // motionArray = [t2, t1, t3] // } totalMotions.append(contentsOf: motionArray) let motion = MotionRecord(isShow: false, motionArray: motionArray, date: (nowDate-i.months).toString(.custom("yyyy-MM"))) motionList.append(motion) } self.tableView.reloadData() if totalMotions.count == 0 { return } if motionType == .mountaineering { if let maxAltitude = totalMotions.max(\.altitude)?.altitude { maxDistanceLabel.text = "\(String(format:"%.2f", Float(maxAltitude)))" } if let maxDistance = totalMotions.max(\.distance)?.distance { maxTimeLabel.text = "\(String(format:"%.2f", Float(maxDistance)/1000))" } } else { if let maxDistance = totalMotions.max(\.distance)?.distance { maxDistanceLabel.text = "\(String(format:"%.2f", Float(maxDistance)/1000))" } if let maxTime = totalMotions.max(\.length)?.length { maxTimeLabel.toTimeType3(length: maxTime) } } if let maxCalorie = totalMotions.max(\.calorie)?.calorie { maxCalorieLabel.text = "\(String(format:"%.2f", Float(maxCalorie)))" } // totalStepsLabel.text = "\(stepArray.sum(\.number))步" totalDistanceLabel.text = "\(String(format:"%.2f", Float(totalMotions.sum(\.distance))/1000))" totalCalorieLabel.text = "\(String(format:"%.2f", Float(totalMotions.sum(\.calorie))/1000))" totalTimeLabel.toTimeType3(length: totalMotions.sum(\.length)) totalNumberLabel.text = "\(totalMotions.count)" } private func reloadSectionCell(_ section: Int) { motionList[section].isShow = !motionList[section].isShow tableView.reloadSections(IndexSet.init(integer: section), with: .automatic) if motionList[section].isShow { tableViewHeight.constant += CGFloat(motionList[section].motionArray.count*50) } else { tableViewHeight.constant -= CGFloat(motionList[section].motionArray.count*50) } } } extension RecordDetailVC: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 10 } func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { let footerView = UIView() footerView.backgroundColor = UIColor.rgbColorFromHex(0xF2F2F2) // print("???") return footerView } // func height func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 50 } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerView = RecordHeaderView(section: section, motion: motionList[section]) // headerView.backgroundColor = .red headerView.headerClosure = {[weak self] section in self?.reloadSectionCell(section) } return headerView } func numberOfSections(in tableView: UITableView) -> Int { return motionList.count } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 50 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return motionList[section].isShow ? motionList[section].motionArray.count : 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MotionRecordCell", for: indexPath) as! MotionRecordCell if motionList[indexPath.section].isShow { cell.motion = motionList[indexPath.section].motionArray[indexPath.row] } return cell } }