<commit_before>//
//  SelectPlayersCollectionViewController.swift
//  myscoreboard_lite
//
//  Created by PiHan on 2017/9/18.
//  Copyright © 2017年 PiHan. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"


class SelectPlayersCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    
    var team : Team?
    var selectedPlayers = [Player]()
    var isPlayingMode = false

    @IBOutlet weak var gameStartBarButton: UIBarButtonItem!
    override func viewDidLoad() {
        super.viewDidLoad()
        DataSource.sharedInstance.currentPlayingTeam = team
        // Register cell classes
        let nib = UINib(nibName: "PlayerCollectionViewCell", bundle: nil)
        collectionView?.register(nib, forCellWithReuseIdentifier: "PlayerCollectionViewCell")
 
        // Allouw Multiple Selection
        collectionView?.allowsMultipleSelection = true
        
        // set barbutton title
        if isPlayingMode {
            gameStartBarButton.title = "Continue"
        }else{
            gameStartBarButton.title = "Start Game"
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        collectionView?.reloadData()
    }

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (team?.players.count)!
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PlayerCollectionViewCell", for: indexPath) as! PlayerCollectionViewCell
        cell.playerNameLabel.text = team?.players[indexPath.row].name
        if isPlayingMode{
            selectedPlayers = DataSource.sharedInstance.selectedPlayers
            if selectedPlayers.contains((team?.players[indexPath.row])!){
                cell.checkMarkImageView.isHidden = false
            }else{
                 cell.checkMarkImageView.isHidden = true
            }
            if team?.players[indexPath.row].uWeight != 0 {
                cell.countLabel.text = String(describing: (team?.players[indexPath.row].uWeight)!)
            }
        }
       
        return cell
    }

    // MARK: UICollectionViewDelegate
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
         print("select")
         let player = team?.players[indexPath.row]
         selectedPlayers.append(player!)
    }
    
    override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
         print("deselect")
        let player = team?.players[indexPath.row]
        selectedPlayers = selectedPlayers.filter(){$0 != player!}
    }

    <commit_msg>update flow, did not allow to add new player when is in playing mode<commit_after>//
//  SelectPlayersCollectionViewController.swift
//  myscoreboard_lite
//
//  Created by PiHan on 2017/9/18.
//  Copyright © 2017年 PiHan. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"


class SelectPlayersCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    
    var team : Team?
    var selectedPlayers = [Player]()
    var isPlayingMode = false

    @IBOutlet weak var gameStartBarButton: UIBarButtonItem!
    override func viewDidLoad() {
        super.viewDidLoad()
        DataSource.sharedInstance.currentPlayingTeam = team
        // Register cell classes
        let nib = UINib(nibName: "PlayerCollectionViewCell", bundle: nil)
        collectionView?.register(nib, forCellWithReuseIdentifier: "PlayerCollectionViewCell")
 
        // Allouw Multiple Selection
        collectionView?.allowsMultipleSelection = true
        
        // set barbutton title
        if isPlayingMode {
            gameStartBarButton.title = "Continue"
        }else{
            gameStartBarButton.title = "Start Game"
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        collectionView?.reloadData()
    }

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if isPlayingMode{
            return DataSource.sharedInstance.selectedPlayers.count
        }else{
             return (team?.players.count)!
        }
       
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PlayerCollectionViewCell", for: indexPath) as! PlayerCollectionViewCell
        
        if isPlayingMode {
             selectedPlayers = DataSource.sharedInstance.selectedPlayers
            cell.isSelected = false
            cell.playerNameLabel.text = selectedPlayers[indexPath.row].name
            cell.countLabel.text = String(describing: (selectedPlayers[indexPath.row].uWeight))
        }else{
             cell.playerNameLabel.text = team?.players[indexPath.row].name
        }
       
        return cell
    }

    // MARK: UICollectionViewDelegate
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
         let player = team?.players[indexPath.row]
         selectedPlayers.append(player!)
    }
    
    override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
         print("deselect")
        let player = team?.players[indexPath.row]
        selectedPlayers = selectedPlayers.filter(){$0 != player!}
    }

    
{'fraction_non_alphanumeric': 0.08153437396213882, 'fraction_numerical': 0.004483560278977084, 'mean_word_length': 2.785669390320553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2017003', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1568, 'n_words': 350}