HomePageSortVC.swift 6.37 KB
//
//  HomePageSortVC.swift
//  Twear
//
//  Created by yangbin on 2021/12/31.
//

import UIKit


class HomePageSortVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    var sortClosure: ((_ homePage: [String]) -> ())?
    
    let device = CurDevice
    var ary1: [String] = [String]()
    var ary2 = [String]()
    
    let dic: [String: String] = ["BloodPressure": "血压", "BloodOxygen": "血氧", "HeartRate": "心率", "Sleep": "睡眠", "Train": "训练",  "WomenHealth": "女性健康", "MotionRecord": "运动记录", "Pressure": "压力", "Mett": "梅脱"]
    let iconDic: [String: String] = ["BloodPressure": "home_bp", "BloodOxygen": "home_bo", "HeartRate": "home_hr", "Sleep": "home_sleep", "Train": "home_train",  "WomenHealth": "home_women_health", "MotionRecord": "home_motion"]
    
    lazy var footerView: UIView = {
       let view = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 200))
        let label = UILabel(frame: CGRect(x: 40, y: 0, width: SCREEN_WIDTH-80, height: 60))
        label.font = RegularFont(11)
        label.textAlignment = .center
        label.text = LocString("点击左侧减去或者增加按钮自定义卡片,上下拖动右侧图标对卡片进行排序。")
        label.numberOfLines = 0

        view.addSubview(label)
        return view
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        sortClosure?(ary1)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = LocString("编辑健康功能")
        tableView.setEditing(true, animated: false)
        tableView.contentInset = UIEdgeInsets.init(top: 5, left: 0, bottom: 0, right: 0)
        tableView.separatorStyle = .none
        tableView.register(UINib(nibName: "SortPageSectionHeader", bundle: .main), forHeaderFooterViewReuseIdentifier: "SortPageSectionHeader")
        tableView.register(UINib(nibName: "SortPageCell", bundle: .main), forCellReuseIdentifier: "SortPageCell")
        
        ary2 = device.allFunctions
        testDate()
//        tableView.tableFooterView = footerView

        for a in ary1 {
            if let i = ary2.firstIndex(of: a) {
                ary2.remove(at: i)
            }
        }
        
        // Do any additional setup after loading the view.
    }
    
    func testDate() {
        if IsTest {
            if let i = ary2.firstIndex(of: "BloodPressure") {
                ary2.remove(at: i)
            }
            if let i = ary2.firstIndex(of: "BloodOxygen") {
                ary2.remove(at: i)
            }

        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        section == 0 ? ary1.count :  ary2.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell  = tableView.dequeueReusableCell(withIdentifier: "SortPageCell", for: indexPath) as! SortPageCell
        if indexPath.section == 1 {
            cell.iconVIew.image = UIImage(named: iconDic[ary2[indexPath.row]]!)
            cell.nameLabel.text = LocString(dic[ary2[indexPath.row]]!)
        } else {
            cell.iconVIew.image = UIImage(named: iconDic[ary1[indexPath.row]]!)
            cell.nameLabel.text = LocString(dic[ary1[indexPath.row]]!)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SortPageSectionHeader") as! SortPageSectionHeader
        if section == 0 {
            header.topLabelTopConstraint.constant = 8
            header.topLabel.text = LocString("显示在首页")
            header.bottomLabel.text = LocString("长按并拖拽可调整排序")
        }else {
            header.topLabelTopConstraint.constant = 15
            header.topLabel.text = LocString("不显示在首页")
            header.bottomLabel.text = ""
        }
        return header
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return indexPath.section == 0
    }

    
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

        let obj = ary1.remove(at: sourceIndexPath.row)
        if destinationIndexPath.section == 1 {
            ary2.insert(obj, at: destinationIndexPath.row)
        } else {
            ary1.insert(obj, at: destinationIndexPath.row)
        }
        device.homePage = ary1
        AdminHelper.shared.updateDevice(device)
        tableView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return indexPath.section == 0 ? .delete : .insert
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            let obj = ary1.remove(at: indexPath.row)
            ary2.append(obj)
            ary2.sort()
        } else {
            let obj = ary2.remove(at: indexPath.row)
            ary1.append(obj)
        }
        device.homePage = ary1
        AdminHelper.shared.updateDevice(device)
        tableView.reloadData()
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 72
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 57
    }
//    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
//        if section == 1 {
//            let view = UIView()
//            let label = UILabel()
//            label.font =
//            return view
//        } else {
//            return nil
//        }
//    }
    
    
}