HomePageSortVC.swift 5.16 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": "运动记录"]
    
    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)
        
        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 {
        var cell  = tableView.dequeueReusableCell(withIdentifier: "SortCellId")
        if cell == nil {
            cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "SortCellId")
        }
        cell?.textLabel?.font = RegularFont(15)
        if indexPath.section == 1 {
            cell?.textLabel?.text = LocString(dic[ary2[indexPath.row]]!)
        } else {
            cell?.textLabel?.text = LocString(dic[ary1[indexPath.row]]!)
        }
        return cell!
    }
    
    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 50
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 35
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.5
    }
//    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
//        if section == 1 {
//            let view = UIView()
//            let label = UILabel()
//            label.font =
//            return view
//        } else {
//            return nil
//        }
//    }
    
    
}