UserViewController.swift 7.08 KB
//
//  UserViewController.swift
//  YouerLiveVideo
//
//  Created by 左丞 on 2017/5/16.
//  Copyright © 2017年 左丞. All rights reserved.
//

import UIKit

class UserViewController: UIViewController {

    
    @IBOutlet weak var historyList: UITableView!
    @IBOutlet weak var userName: UILabel!
    @IBOutlet weak var userPhoto: UIImageView!
    var historyType:[String] = ["资源历史","微课历史"]
    var myResourceType:[String] = ["我的资源","我的微课"]
    var sectionList:[String] = ["历史记录","我的"]
    var isCloseHistoryList:Bool = false
    var isCloseMyResource:Bool = false
    var user = AppDelegate.instance().accountManager
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configTheme()
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "icon_setting"), style: .plain, target: self, action: #selector(UserViewController.editBtnClick))
        self.automaticallyAdjustsScrollViewInsets = false
        setUserInfor()
        NotificationCenter.default.addObserver(self, selector: #selector(UserViewController.setUserInfor), name: NSNotification.Name(rawValue: "refreshFirstVC"), object: nil)
        // Do any additional setup after loading the view.
    }
    
    func setUserInfor(){
        userPhoto.layer.cornerRadius = userPhoto.frame.size.height/2
        userPhoto.layer.masksToBounds = true
        userName.text = user.name()
        userPhoto.sd_setImage(with: URL(string: user.photo()), placeholderImage: #imageLiteral(resourceName: "defphoto.png"))
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func userEditClcik(_ sender: Any) {
        if user.isOnline(){
            let vc = Story.instantiateViewControllerWithIdentifier("UserEditViewControllerVC", storyName: "User") as! UserEditViewController
            vc.title = "个人信息"
            self.navigationController?.pushViewController(vc, animated: true)
        }else{
            let vc=Story.instantiateViewControllerWithIdentifier("NewLoginViewControllerVC", storyName: "Login") as! NewLoginViewController
            self.present(vc, animated: true, completion: nil)
        }

    }
    
    func editBtnClick(){
        let vc = Story.instantiateViewControllerWithIdentifier("AppSettingViewControllerVC", storyName: "User") as! AppSettingViewController
        vc.title = "设置"
        self.navigationController?.pushViewController(vc, animated: true)
    }

    func headerTap(tap:UITapGestureRecognizer){
        if tap.view?.tag == 200{
            isCloseHistoryList = !isCloseHistoryList
        }else{
            isCloseMyResource = !isCloseMyResource
        }
        historyList.reloadData()
    }
    
//    override func viewWillAppear(_ animated: Bool) {
//        super.viewWillAppear(animated)
//        self.tabBarController?.tabBar.isHidden = false
//    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension UserViewController:UITableViewDelegate,UITableViewDataSource{
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = Story.instantiateViewControllerWithIdentifier("HistoryViewControllerVC", storyName: "User") as! HistoryViewController
        vc.type = indexPath.row
        vc.isHistory = indexPath.section
        vc.title = historyType[indexPath.row]
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UserCenterHistoryCell
        if indexPath.section == 0{
            cell.titleName.text = historyType[indexPath.row]
        }else if indexPath.section == 1{
            cell.titleName.text = myResourceType[indexPath.row]
        }
        return cell
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        if user.roletype() == 3 || user.roletype() == 4 || user.roletype() == 5{
            return 1
        }
        return sectionList.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            if isCloseHistoryList{
                return 0
            }
            return historyType.count
        }else if section == 1{
            if isCloseMyResource{
                return 0
            }
            return myResourceType.count
        }else{
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: getScreenWidth(), height: 44))
        headerView.backgroundColor = UIColor.white
        let label = UILabel(frame: CGRect(x: 8, y: headerView.frame.height/2-10, width: 100, height: 21))
        label.text = sectionList[section]
        label.sizeToFit()
        headerView.addSubview(label)
        
        let image = UIImageView(frame: CGRect(x: 0, y: 0, width: 9, height: 15))
        image.center = CGPoint(x: getScreenWidth() - 20, y: headerView.frame.height/2)
        image.image = #imageLiteral(resourceName: "arrow")
        image.isUserInteractionEnabled = true
        var angle:Double = 0
        if !isCloseHistoryList && section == 0{
            angle = M_PI_2
        }
        if !isCloseMyResource && section == 1{
            angle = M_PI_2
        }
        image.transform = CGAffineTransform(rotationAngle: CGFloat(angle))
        headerView.addSubview(image)
        
        let line = UILabel(frame: CGRect(x: 0, y: headerView.frame.height-1, width: getScreenWidth(), height: 1))
        line.backgroundColor = historyList.backgroundColor
        headerView.addSubview(line)
        headerView.tag = 200+section
        headerView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(UserViewController.headerTap(tap:))))
        return headerView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 15
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: getScreenWidth(), height: 15))
        view.backgroundColor = UIColor.clear
        return view
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.isHidden = false
    }

    
}

class UserCenterHistoryCell:UITableViewCell{
    
    @IBOutlet weak var titleName: UILabel!
    
    
    
}