CNLiveMineAddInfoViewController.swift 8.02 KB
//
//  CNLiveMineAddInfoViewController.swift
//  metaCode
//
//  Created by open on 2023/6/7.
//

import Foundation

enum CNLiveMineInfoShowType {
    case add//添加身份
    case info//身份信息
    case message//消息中心
}

class CNLiveMineAddInfoViewController : CNLiveBaseViewController, UITableViewDelegate, UITableViewDataSource, UITableViewDragDelegate, UITableViewDropDelegate {
    
    let kCNLiveMineInfoLListTableViewIdentifier = "kCNLiveMineInfoLListTableViewIdentifier"
    let kCNLiveMineStandInfoTableViewIdentifier = "kCNLiveMineStandInfoTableViewIdentifier"
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.dragDelegate = self
        tableView.dropDelegate = self
        tableView.backgroundColor = HexColor("#F7F8FA")
        tableView.tableFooterView = UIView.init()
        tableView.rowHeight = 100
        tableView.contentInsetAdjustmentBehavior = .scrollableAxes
        tableView.separatorStyle = .none
        tableView.register(MineInfoLListTableViewCell.self, forCellReuseIdentifier: kCNLiveMineInfoLListTableViewIdentifier)
        tableView.register(MineStandInfoTableViewCell.self, forCellReuseIdentifier: kCNLiveMineStandInfoTableViewIdentifier)
        return tableView
    }()
    
    lazy var dataArray: NSMutableArray = {
        let dataArray = NSMutableArray()
        if _show_type == .add {
            dataArray.add(["image":"mine_header_icon_defult_ava","name":"志愿者身份"])
            dataArray.add(["image":"mine_header_icon_defult_ava","name":"退伍军人身份"])
        }
        if _show_type == .info {
            dataArray.add(["image":"mine_header_icon_defult_ava","name":"志愿者身份","number":"证件编号:410800111127954"])
            dataArray.add(["image":"mine_header_icon_defult_ava","name":"退伍军人身份","number":"证件编号:410800111127954"])
        }
        if _show_type == .message {
            dataArray.add(["image":"mine_header_icon_defult_ava","name":"志愿者云","number":"志愿者大礼包发放中,快来领取吧!","time":"03-12"])
            dataArray.add(["image":"mine_header_icon_defult_ava","name":"志愿者·志愿云","number":"志愿者大礼包快来领取吧~","time":"03-12"])
        }
        return dataArray
    }()
    
    lazy var setButton = {
        let setButton = UIButton.init(type: .custom)
        setButton.frame = CGRectMake(KSreenWidth - 35, (get_system_nav_height() - (get_system_statusBar_height() + 20))/2 + get_system_statusBar_height(), 20, 20)
        setButton.backgroundColor = .white
        setButton.setImage(UIImage(named: "mine_sub_header_note_set"), for: .normal)
        setButton.imageView?.contentMode = .scaleToFill
        setButton.addTarget(self, action: #selector(noteSetAction), for: .touchUpInside)
        return setButton
    }()
    
    lazy var copyButton = {
        let copyButton = UIButton.init(type: .custom)
        copyButton.frame = CGRectMake(KSreenWidth - 70, (get_system_nav_height() - (get_system_statusBar_height() + 20))/2 + get_system_statusBar_height(), 20, 20)
        copyButton.backgroundColor = .white
        copyButton.setImage(UIImage(named: "mine_sub_header_back_up_copy"), for: .normal)
        copyButton.imageView?.contentMode = .scaleToFill
        copyButton.addTarget(self, action: #selector(backCopyAction), for: .touchUpInside)
        return copyButton
    }()
    
    var _show_type:CNLiveMineInfoShowType!
    init(show_type:CNLiveMineInfoShowType) {
       super.init(nibName:nil, bundle:nil)
        _show_type = show_type
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        switch _show_type {
        case .add:
            titleName = "添加身份"
        case .info:
            titleName = "身份信息"
            tableView.dragInteractionEnabled = true
        case .message:
            titleName = "消息中心"
            topNavView.addSubview(setButton)
            topNavView.addSubview(copyButton)
        case .none:
            break
        }

        navigationBarHidden = false
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(get_system_nav_height())
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    @objc func noteSetAction() {
        
    }
    @objc func backCopyAction() {
        
    }
}

extension CNLiveMineAddInfoViewController {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if _show_type == .add {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveMineInfoLListTableViewIdentifier) as? MineInfoLListTableViewCell else {
                return MineInfoLListTableViewCell()
            }
            let dict = dataArray[indexPath.row] as! Dictionary<String, Any>
            cell.setupWithDataSource(icon: dict["image"] as! String, name: dict["name"] as! String)
            cell.userInteractionEnabledWhileDragging = true
            return cell
            
        } else if _show_type == .message {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveMineStandInfoTableViewIdentifier) as? MineStandInfoTableViewCell else {
                return MineStandInfoTableViewCell()
            }
            let dict = dataArray[indexPath.row] as! Dictionary<String, Any>
            cell.setupWithDataSource(icon: dict["image"] as! String, name: dict["name"] as! String,number: dict["number"] as! String, time: dict["time"] as! String)
            cell.userInteractionEnabledWhileDragging = true
            return cell
        } else {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveMineStandInfoTableViewIdentifier) as? MineStandInfoTableViewCell else {
                return MineStandInfoTableViewCell()
            }
            let dict = dataArray[indexPath.row] as! Dictionary<String, Any>
            cell.setupWithDataSource(icon: dict["image"] as! String, name: dict["name"] as! String,number: dict["number"] as! String)
            cell.userInteractionEnabledWhileDragging = false
            return cell
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        let item = UIDragItem(itemProvider: NSItemProvider(object: UIImage()))
        return [item]
    }
    // MARK: UITableViewDropDelegate
    func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {}
    func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
        return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
    }
    func tableView(_ tableView: UITableView, canHandle session: UIDropSession) -> Bool {
        // Only receive image data
        return session.canLoadObjects(ofClass: UIImage.self)
    }
    //移动cell事件
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath,
                   to destinationIndexPath: IndexPath) {
        objc_sync_enter(self)
//        let dict: Dictionary = dataArray[sourceIndexPath.row] as! Dictionary<String, Any>
//        dataArray.remove(sourceIndexPath.row)
//        if destinationIndexPath.row > dataArray.count {
//            dataArray.append(dict)
//        } else {
//            dataArray.insert(dict, at: destinationIndexPath.row)
//        }
        objc_sync_exit(self)
//        tableView.reloadData()
    }
}