CNLiveShopGoodsManageDetailsInputManager.swift 5.41 KB
//
//  CNLiveShopGoodsManageDetailsInputManager.swift
//  metaCode
//
//  Created by zx on 2023/11/16.
//

import Foundation

typealias completerDoneBlock = (_ k:String,_ v:String)->Void

    
class CNLiveShopGoodsManageDetailsInputManager:NSObject{
    var modalViewController = CNLiveModalPresentationViewController()
    var paramField = UITextField()
    var contentField = UITextField()
    var completerBlock:completerDoneBlock?
    
    func showCustomParamAlertWithCompleterBlock(completeBlock:@escaping completerDoneBlock){
        let contentView = UIView(frame: CGRect(x: 0, y: 0, width: KSreenWidth, height: 230))
        contentView.backgroundColor = .white
        contentView.layer.cornerRadius = 6
        
        let titleLabel = UILabel(frame: CGRect(x: 0, y: 20, width: KSreenWidth-20, height: 30))
        titleLabel.font = Font_16
        titleLabel.textAlignment = .center
        titleLabel.text = "添加通用参数"
        titleLabel.textColor = HexColor("#333333")
        titleLabel.backgroundColor = .white
        contentView.addSubview(titleLabel)

        let paramView = UIView(frame: CGRect(x: 10, y: titleLabel.bottom+10, width: KSreenWidth-40, height: 40))
        paramView.backgroundColor = HexColor("#F7F7F7")
        paramView.layer.masksToBounds = true
        paramView.layer.cornerRadius = 5
        contentView.addSubview(paramView)

        let paramField = UITextField(frame: CGRect(x: 5, y: 5, width: KSreenWidth-50, height: 35))
        paramField.placeholder = "请输入参数名称"
        paramField.returnKeyType = .next
        paramField.font = Font_14
        paramField.textColor = HexColor("#333333")
        paramField.backgroundColor = .clear
        paramView.addSubview(paramField)
        self.paramField = paramField

        let contentSubView = UIView(frame: CGRect(x: 10, y: paramView.bottom+10, width: contentView.width-40, height: 40))
        contentSubView.backgroundColor = HexColor("#F7F7F7")
        contentSubView.layer.masksToBounds = true
        contentSubView.layer.cornerRadius = 5
        contentView.addSubview(contentSubView)

        let contentField = UITextField(frame: CGRect(x: 5, y: 5, width: KSreenWidth-50, height: 30))
        contentField.placeholder = "请输入参数内容"
        contentField.returnKeyType = .done
        contentField.font = Font_14
        contentField.textColor = HexColor("#333333")
        contentField.backgroundColor = .clear
        contentSubView.addSubview(contentField)
        self.contentField = contentField
        
        let cancelButton = UIButton(type: .custom)
        cancelButton.frame = CGRect(x: 10, y: contentSubView.bottom+10, width: (contentView.width-50)*0.5, height: 40)
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.setTitle("取消", for: .highlighted)
        cancelButton.setTitleColor(HexColor("#999999"), for: .normal)
        cancelButton.setTitleColor(HexColor("#999999"), for: .highlighted)
        cancelButton.layer.cornerRadius = 5
        cancelButton.backgroundColor = .white
        cancelButton.layer.borderColor = HexColor("#999999")!.cgColor
        cancelButton.layer.borderWidth = 1
        cancelButton.layer.masksToBounds = true
        cancelButton.titleLabel?.font = Font_14
        contentView.addSubview(cancelButton)

        let confirmButton = UIButton(type: .custom)
        confirmButton.frame = CGRect(x: 20+cancelButton.width, y: contentSubView.bottom+10, width: (contentView.width-50)*0.5, height: 40)
        confirmButton.setTitle("确定", for: .normal)
        confirmButton.setTitle("确定", for: .highlighted)
        confirmButton.setTitleColor(HexColor("#FFFFFF"), for: .normal)
        confirmButton.setTitleColor(HexColor("#FFFFFF"), for: .highlighted)
        confirmButton.layer.cornerRadius = 5
        confirmButton.layer.masksToBounds = true
        confirmButton.backgroundColor = HexColor("#61C463")
        confirmButton.titleLabel?.font = Font_14
        contentView.addSubview(confirmButton)

        let modalViewController = CNLiveModalPresentationViewController()
        modalViewController.contentView = contentView
        
        currentViewController()?.addChild(modalViewController)
        modalViewController.didMove(toParent: currentViewController())
        currentViewController()?.view.addSubview(modalViewController.view)
        modalViewController.showingAnimationWithCompletion { finished in
            
        }
        self.modalViewController = modalViewController

        cancelButton.addTarget(self, action: #selector(cancelButtonAction), for: .touchUpInside)
        confirmButton.addTarget(self, action: #selector(confirmButtonAction), for: .touchUpInside)
        
        self.completerBlock = completeBlock
        self.modalViewController.confirmBlock = {
            self.confirmButtonAction()
        }
    }
    
    @objc func cancelButtonAction(){
        self.modalViewController.hidingAnimationWithCompletion { finished in
            
        }
    }
    @objc func confirmButtonAction(){
        
        if self.paramField.text?.count ?? 0 > 0 && self.contentField.text?.count ?? 0 > 0{
            self.modalViewController.hidingAnimationWithCompletion { finished in
                if finished{
                    
                    self.completerBlock!((self.paramField.text?.count ?? 0 > 10 ? self.paramField.text?.subString(start: 0, length: 10) : self.paramField.text) ?? "",self.contentField.text ?? "")
                }
            }
        }
    }
 
}