HealthReportVC.swift 7.76 KB
//
//  HealthReportVC.swift
//  Twear
//
//  Created by yangbin on 2022/1/1.
//

import UIKit
import SwiftDate

class HealthReportVC: UIViewController {
    @IBOutlet weak var collectionView: UICollectionView!
    
    @IBOutlet private weak var monthButton: UIButton!
    @IBOutlet private weak var dayButton: UIButton!
    @IBOutlet private weak var yearButton: UIButton!
    @IBOutlet private weak var weekButton: UIButton!
    
    private var dateType: DateType = .day
    
    var collectArray = CurDevice.allFunctions
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = LocString("健康报告")
        
        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)
        
        if let i = collectArray.firstIndex(of: "MotionRecord") {
            collectArray.remove(at: i)
        }
        if let i = collectArray.firstIndex(of: "WomenHealth") {
            collectArray.remove(at: i)
        }
        if let i = collectArray.firstIndex(of: "Train") {
            collectArray.remove(at: i)
        }
        if let i = collectArray.firstIndex(of: "Pressure") {
            collectArray.remove(at: i)
        }
        if let i = collectArray.firstIndex(of: "Mett") {
            collectArray.remove(at: i)
        }
        testDate()
        collectArray.insert("Step", at: 0)
        
        configureCollectionView()
        
    }
    
    
    func testDate() {
        //        let testDate = DateInRegion(year: 2022, month: 1, day: 13, hour: 0, minute: 0, second: 0)
        if IsTest {
            if let i = collectArray.firstIndex(of: "BloodPressure") {
                collectArray.remove(at: i)
            }
            if let i = collectArray.firstIndex(of: "BloodOxygen") {
                collectArray.remove(at: i)
            }
            
        }
    }
    @objc func share() {
        
        collectionView.scrollToItem(at: IndexPath(row: collectArray.count-1, section: 0), at: .bottom, animated: false)
        if let image = screenShot() {
            let shareView = ShareView(self.screenShot())
            shareView.show()
        }
        
    }
    
    func screenShot() -> UIImage? {
        
        UIGraphicsBeginImageContextWithOptions(self.collectionView.contentSize, true, 0.0)
        //先保存原来frame 和 偏移量
        let savedContentOffset = self.collectionView.contentOffset;
        let savedFrame = self.collectionView.frame;
        let contentSize = self.collectionView.contentSize;
        let oldBounds = self.collectionView.layer.bounds;
        if #available(iOS 13, *) {
            //iOS 13 系统截屏需要改变tableview 的bounds
            self.collectionView.layer.bounds = CGRect(x: oldBounds.origin.x, y: oldBounds.origin.y, width: contentSize.width, height: contentSize.height)
            
        }
        //        let transY = NAVBAR_HEIGHT
        //        CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, transY);
        //        [colorLayer renderInContext:UIGraphicsGetCurrentContext()];
        //偏移量归零
        self.collectionView.contentOffset = CGPoint.zero;
        //frame变为contentSize
        self.collectionView.frame = CGRect(x: 0, y: 0, width: self.collectionView.contentSize.width, height: self.collectionView.contentSize.height)
        
        
        //截图
        self.collectionView.layer.render(in: UIGraphicsGetCurrentContext()!)
        
        if #available(iOS 13,*) {
            self.collectionView.layer.bounds = oldBounds
        }
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        //还原frame 和 偏移量
        self.collectionView.contentOffset = savedContentOffset
        self.collectionView.frame = savedFrame
        return image
        //        }
        
    }
    
    
    func getShareImage() -> UIImage? {
        collectionView.removeFromSuperview()
        let image = collectionView.captureLongImage
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(50)
            make.left.bottom.right.equalToSuperview()
        }
        return image
    }
    
    
    @IBAction func selectedDate(_ sender: UIButton) {
        monthButton.setTitleColor(.black, for: .normal)
        dayButton.setTitleColor(.black, for: .normal)
        yearButton.setTitleColor(.black, for: .normal)
        weekButton.setTitleColor(.black, for: .normal)
        sender.setTitleColor(TintColor, for: .normal)
        dateType = DateType(rawValue: sender.tag) ?? .day
        collectionView.reloadData()
    }
    
    
    
    
    
}

extension HealthReportVC: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    private func configureCollectionView(){
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .vertical
        flowLayout.minimumLineSpacing = 12
        flowLayout.minimumInteritemSpacing = 12
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 12.5, bottom: 15, right: 12.5)
        flowLayout.itemSize = CGSize(width: SCREEN_WIDTH-25, height: 335)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.bounces = false
        collectionView.collectionViewLayout = flowLayout
        collectionView.register(UINib(nibName: "StepReportCell", bundle: .main), forCellWithReuseIdentifier: "StepReportCell")
        collectionView.register(UINib(nibName: "BOReportCell", bundle: .main), forCellWithReuseIdentifier: "BOReportCell")
        collectionView.register(UINib(nibName: "HRReportCell", bundle: .main), forCellWithReuseIdentifier: "HRReportCell")
        collectionView.register(UINib(nibName: "SleepReportCell", bundle: .main), forCellWithReuseIdentifier: "SleepReportCell")
        collectionView.register(UINib(nibName: "BPReportCell", bundle: .main), forCellWithReuseIdentifier: "BPReportCell")
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return collectArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch collectArray[indexPath.row] {
        case "BloodPressure":
            let cell: BPReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "BPReportCell", for: indexPath) as! BPReportCell
            cell.dateType = dateType
            return cell
        case "BloodOxygen":
            let cell: BOReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "BOReportCell", for: indexPath) as! BOReportCell
            cell.dateType = dateType
            return cell
        case "HeartRate":
            let cell: HRReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HRReportCell", for: indexPath) as! HRReportCell
            cell.dateType = dateType
            return cell
        case "Sleep":
            
            let cell: SleepReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SleepReportCell", for: indexPath) as! SleepReportCell
            cell.dateType = dateType
            return cell
        case "Step":
            let cell: StepReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "StepReportCell", for: indexPath) as! StepReportCell
            cell.dateType = dateType
            return cell
        default:
            let cell: StepReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "StepReportCell", for: indexPath) as! StepReportCell
            return cell
        }
        
    }
}