MotionRecordVC.swift 5.97 KB
//
//  MotionRecordVC.swift
//  Twear
//
//  Created by yangbin on 2021/11/25.
//

import UIKit
import SwiftDate
import MJRefresh

struct MotionRecord {
    var isShow: Bool
    var motionArray: [MotionModel]
    var date: String
}

class MotionRecordVC: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    
    private var page: Int = 1
    private let nowDate = DateInRegion()
    private var motionList: [MotionRecord] = [] {
        didSet {
//            points = []
//            if trainArray.count == 0 {
//                resetLabel()
//            }
//            let max = trainArray.max{$0.calorie < $1.calorie}?.calorie
//            barChartView.leftAxis.axisMaximum = Double(max ?? 100) * 1.1
//            self.tableView.reloadData()
        }
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        title = LocString("运动记录")
        

//        tableView.reloadData()
        tableView.register(UINib.init(nibName: "MotionRecordCell", bundle: Bundle.main), forCellReuseIdentifier: "MotionRecordCell")
        tableView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 10, right: 0)
        
        let shareButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28))
        shareButton.setImage(UIImage(named: "share_btn"), for: .normal)
        shareButton.addTarget(self, action: #selector(share), for: .touchUpInside)
        navigationItem.rightBarButtonItem =  UIBarButtonItem(customView: shareButton)
//        self.tableView.estimatedRowHeight = 50;
//        self.tableView.estimatedSectionHeaderHeight = 50;
//        self.tableView.estimatedSectionFooterHeight = 10;
//        tableView.rowHeight = UITableView.automaticDimension;
//        tableView.sectionHeaderHeight = UITableView.automaticDimension;
//        tableView.sectionFooterHeight = UITableView.automaticDimension
        /*
        let header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in
            self?.page = 1
            self?.getMotionRecordList(page: 1)
        })
        header.lastUpdatedTimeLabel?.isHidden = true
        tableView.mj_header = header
        tableView.mj_header?.isHidden = true
        
        
        let footer = MJRefreshAutoNormalFooter.init {[weak self] in
            if !(self?.tableView.mj_header?.isRefreshing)! {
                self?.getMotionRecordList(page: (self?.page)!)
            } else {
                self?.tableView.mj_footer?.endRefreshing()
            }
        }
        footer.setTitle("", for: .noMoreData)
        tableView.mj_footer = footer
        tableView.mj_footer?.isHidden = true
         
         */
        
        getMotionRecordList()
    }
    
    @objc func share() {
        let shareView = ShareView(view.captureImage)
        shareView.show()
    }
    
    private func getMotionRecordList(page: Int = 1) {
        if page == 1 {
            motionList = []
        }
        for i in 12*(page-1)..<12*(page-1)+12 {
            let motionArray = MotionModel.getMotionByMonth((nowDate-i.months).date)
//            if 12-i == 10 {
//                let t1 = TrainModel(type: .running, date: DateInRegion().date-100, length: 100, calorie: 200, mileage: 300, steps: 500)
//                let t2 = TrainModel(type: .steppers, date: DateInRegion().date-300.minutes, length: 66, calorie: 1000, mileage: 300, steps: 500)
//                let t3 = TrainModel(type: .steppers, date: DateInRegion().date+7.hours, length: 1000, calorie: 1000, mileage: 300, steps: 500)
//                trainArray = [t2, t1, t3]
//            }
            let motion = MotionRecord(isShow: false, motionArray: motionArray, date: (nowDate-i.months).toString(.custom("yyyy-MM")))
            motionList.append(motion)
        }
    }
    
    deinit {
        print("MotionRecordVC dealloc")
    }

}

extension MotionRecordVC: 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 = BackgroundColor
        return footerView
    }
    
    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.section = section
//        headerView.isShow = motionArray[section].isShow
        
        headerView.headerClosure = {[weak self] section in
            self?.motionList[section].isShow = !(self?.motionList[section].isShow)!
//            UIView.performWithoutAnimation {
                self?.tableView.reloadSections(IndexSet.init(integer: section), with: .none)
//            }
            
//            headerView.rotateArrow((self?.motionArray[section].isShow)!)
        }
        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
    }
}