Newer
Older
Meterage_iOS / Meterage / ViewControllers / Sample / SamplePageViewController.swift
//
//  SamplePageViewController.swift
//  Meterage
//
//  Created by 203 on 2023/2/1.
//

import UIKit

class SamplePageViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {

    private var collectionView: UICollectionView!
    private var segmentedControl: UISegmentedControl!

    private let underTask = UnderTaskViewController()
    private let futureTask = FutureTaskViewController()
    private let totalTask = TotalTaskViewController()

    private var segmentView: UIScrollView!

    private let device = DeviceSizeUtil.shared
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .mainBackground
        navigationItem.title = "样品记录"
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.mainTextColor]
        // Do any additional setup after loading the view.

        let layout = UICollectionViewFlowLayout()
        // 宽高
        let collectionViewHeight = CGFloat(15) + SCREEN_WIDTH / 5
        layout.itemSize = CGSize(width: SCREEN_WIDTH / 5, height: collectionViewHeight)
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
        collectionView.register(UINib(nibName: "SampleCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "sampleCollectionViewCell")
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .white
        collectionView.layer.cornerRadius = 10
        collectionView.layer.masksToBounds = true
        collectionView.isScrollEnabled = false
        view.addSubview(collectionView)
        // 动态布局
        collectionView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(SCREEN_WIDTH - 20)
            make.height.equalTo(collectionViewHeight)
            make.top.equalTo(100)
        }

        segmentedControl = UISegmentedControl(items: ["检定中", "预期任务", "全部"])
        segmentedControl.apportionsSegmentWidthsByContent = true
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.addTarget(self, action: #selector(segmentedControlChanged(_:)), for: .valueChanged)
        view.addSubview(segmentedControl)
        segmentedControl.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(SCREEN_WIDTH - 20)
            make.top.equalTo(collectionView.snp.bottom).offset(10)
        }
        // 定义一个视图用于存放具体显示的内容
        segmentView = UIScrollView()
        segmentView.layer.cornerRadius = 7
        segmentView.layer.masksToBounds = true
        segmentView.delegate = self
        view.addSubview(segmentView)
        
        let safeAreaHeight = device.getSafeAreaFrame(self).height
        let segmentViewHeight = safeAreaHeight - collectionViewHeight - segmentedControl.frame.height - 4 * 10
        segmentView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(SCREEN_WIDTH - 20)
            make.height.equalTo(segmentViewHeight)
            make.top.equalTo(segmentedControl.snp.bottom).offset(10)
        }

        underTask.view.frame = CGRect(x: 0, y: 0, width: (SCREEN_WIDTH - 20), height: segmentViewHeight)
        futureTask.view.frame = CGRect(x: 0, y: 0, width: (SCREEN_WIDTH - 20), height: segmentViewHeight)
        totalTask.view.frame = CGRect(x: 0, y: 0, width: (SCREEN_WIDTH - 20), height: segmentViewHeight)

        //初始化一个页面
        segmentView.addSubview(underTask.view)
    }

    /// UISegmentedControl
    @objc func segmentedControlChanged(_ sender: AnyObject?) {
        let segment: UISegmentedControl = sender as! UISegmentedControl
        for view in segmentView.subviews {
            view.removeFromSuperview()
        }
        switch segment.selectedSegmentIndex {
        case 0:
            segmentView.addSubview(underTask.view)
        case 1:
            segmentView.addSubview(futureTask.view)
        case 2:
            segmentView.addSubview(totalTask.view)
        default:
            print("default ")
        }
    }

    /// UICollectionView
    // 显示多少个Item
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        SAMPLE_TITLE_ARRAY.count
    }

    // 每个cell显示的内容
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: SampleCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "sampleCollectionViewCell", for: indexPath) as! SampleCollectionViewCell
        cell.imageView.image = UIImage(named: SAMPLE_IMAGE_ARRAY[indexPath.row])
        cell.countView.text = "0"
        cell.titleView.text = SAMPLE_TITLE_ARRAY[indexPath.row]
        return cell
    }
}