SettingVC.swift 5.96 KB
//
//  SettingVC.swift
//  hhVDoctorSDK
//
//  Created by 程言方 on 2020/11/13.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit
import hhVDoctorSDK

class SettingVC : UIViewController {
    
    @IBOutlet weak var mTableView : UITableView!
    
    private var sections = [(String,[(String,Bool,Bool)])]()
        
    override public func viewDidLoad() {
        
        super.viewDidLoad()
        
        setUpUI()
        
        setUpData()
    }

    override func viewWillAppear(_ animated: Bool) {
                
    }
    
    
    func setUpUI(){
        title = "设置"
        
        mTableView.separatorStyle = .none
        mTableView.rowHeight = UITableView.automaticDimension
        mTableView.delegate = self
        mTableView.dataSource = self
        mTableView.separatorStyle = .none
    }
    
    
    func setUpData() {
        
        
        let baseSetting = ("基础设置",[
            ("测试环境",HHSDKOptions.default.isDevelopment,true),
            ("扩展参数",false,false),
        ])
        sections.append(baseSetting)
        
        
        
        let videoSetting = ("音视频设置", [
            ("呼叫是否过滤生日性别",HHSDKOptions.default.mVideoOptions.filterCallerInfo,true),
            ("是否开启美颜",HHSDKOptions.default.mVideoOptions.allowBeauty,true),
            ("是否可以选择多人视频",HHSDKOptions.default.mVideoOptions.allowMulti,true),
            ("是否可以增加成员",HHSDKOptions.default.mVideoOptions.allowAddMember,true),
            ("视频完成后是否有评价",HHSDKOptions.default.mVideoOptions.allowEvaluate,true),
        ])
        sections.append(videoSetting)
        
        
        mTableView.reloadData()
    }
}



extension SettingVC : UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].1.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SettingCell") as? SettingCell
        let cellInfo = sections[indexPath.section].1[indexPath.row]
        cell?.configCell(title: cellInfo.0, isOpen: cellInfo.1,isSwitch: cellInfo.2)
        
        let view = UIView()
        view.backgroundColor = .white
        cell?.selectedBackgroundView = view
        
        
        cell?.switchCallback = { [weak self] isOn in
            self?.onConfigChange(indexPath: indexPath, isOpen: isOn)
        }
        
        
        return cell ?? UITableViewCell()
    }
    
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        sections[section].0
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch (indexPath.section,indexPath.row) {
        
        case (0,1):
            
            showEditAlter(title: "扩展参数",info: HHSDKOptions.default.mExtension) { info in
                HHSDKOptions.default.mExtension = info
            }
            
            break
        
        default:
            break
            
        }
    }

}


extension SettingVC {
    
    
    func onConfigChange(indexPath : IndexPath , isOpen : Bool) {
        
        switch (indexPath.section,indexPath.row) {
        
        case (0,0):
            HHMSDK.default.switchEnv(isOpen)
            NotificationCenter.default.post(name: Notification.Name(rawValue: "onEnvChange"), object: nil)
            break
     
        case (1,0):
            HHSDKOptions.default.mVideoOptions.filterCallerInfo = isOpen
    
        case (1,1):
            HHSDKOptions.default.mVideoOptions.allowBeauty = isOpen
            
        case (1,2):
            HHSDKOptions.default.mVideoOptions.allowMulti = isOpen
            
        case (1,3):
            HHSDKOptions.default.mVideoOptions.allowAddMember = isOpen
            
        case (1,4):
            HHSDKOptions.default.mVideoOptions.allowEvaluate = isOpen
            
        default:
            break
        }
        
        
    }
    
    
    
    func showEditAlter(title : String , info : String , callback : @escaping ((String)->Void)) {
        
        let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
        
        alert.addTextField { (textField) in
            textField.text = info
        }
        
        let cancelAction = UIAlertAction(title: "取消", style: .cancel) { _ in
            
            alert.dismiss(animated: true, completion: nil)
        }
        
        cancelAction.setValue(UIColor.gray, forKey: "titleTextColor")
        
        alert.addAction(cancelAction)

        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: {(_) in
            
            let info = alert.textFields?.first?.text
            
            guard info?.count ?? 0 > 0 else { return }
            
            callback(info ?? "")
            
        }))

        present(alert, animated: true, completion: nil)
        
    }
    
}





class SettingCell: UITableViewCell {
    
    @IBOutlet weak var mTitleLabel: UILabel!
    
    @IBOutlet weak var mSwitch : UISwitch!
    
    @IBOutlet weak var mArrow : UIImageView!
    
    var switchCallback : ((Bool) -> Void)?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    
    func configCell(title: String, isOpen : Bool, isSwitch: Bool) {
        mTitleLabel.text = title
        mSwitch.isOn = isOpen
        
        if isSwitch {
            mSwitch.isHidden = false
            mArrow.isHidden = true
        }else{
            mSwitch.isHidden = true
            mArrow.isHidden = false
        }
    }
    
    @IBAction func onSwitch(_ sender : UISwitch) {
        switchCallback?(sender.isOn)
    }
}