<commit_before>//
//  StudentListViewController.swift
//  TeacherTools
//
//  Created by <NAME> on 10/31/16.
//  Copyright © 2016 AppsByPJ. All rights reserved.
//

import UIKit
import BetterSegmentedControl

class StudentListViewController: UIViewController, AutoStoryboardInitializable {
    
    @IBOutlet var emptyStateView: UIView!
    @IBOutlet var emptyStateImages: [UIImageView]!
    @IBOutlet var emptyStateLabels: [UILabel]!
    
    @IBOutlet weak var backgroundImageView: UIImageView!
    @IBOutlet weak var navBarButton: NavBarButton!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var newEntryView: UIView!
    @IBOutlet weak var newStudentTextField: UITextField!
    @IBOutlet weak var segmentedControl: BetterSegmentedControl!

    var core = App.core
    var group: Group?
    var students = [Student]()
    var absentStudents = [Student]()
    
    fileprivate var plusBarButton = UIBarButtonItem()
    fileprivate var pasteBarButton = UIBarButtonItem()
    fileprivate var saveBarButton = UIBarButtonItem()
    fileprivate var cancelBarButton = UIBarButtonItem()
    fileprivate var doneBarButton = UIBarButtonItem()
    fileprivate var editBarButton = UIBarButtonItem()
    
    var currentSortType: SortType = App.core.state.currentUser!.lastFirst ? .last : .first {
        didSet {
            students = currentSortType.sort(students)
            tableView.reloadData()
            isAdding = false
        }
    }
    var isAdding = false {
        didSet {
            toggleEntryView(hidden: !isAdding)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        newEntryView.isHidden = true
        setUp()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        core.add(subscriber: self)
        
        if !navBarButton.isPointingDown {
            animateArrow(up: false)
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        core.remove(subscriber: self)
        NotificationCenter.default.removeObserver(self)
    }
    
    @IBAction func newStudentTextFieldChanged(_ sender: UITextField) {
        updateRightBarButton()
    }
    
    @IBAction func segmentedControlValueChanged(_ sender: BetterSegmentedControl) {
        guard let selectedSortType = SortType(rawValue: Int(sender.index)) else { return }
        currentSortType = selectedSortType
    }
    
    @IBAction func pasteViewTapped(_ sender: UITapGestureRecognizer) {
        pasteButtonPressed()
    }
    
    @IBAction func addViewTapped(_ sender: UITapGestureRecognizer) {
        startEditing()
    }
    
    func editButtonPressed(_ sender: UIBarButtonItem) {
        tableView.isEditing = !tableView.isEditing
        tableView.reloadData()
        navigationItem.leftBarButtonItem = tableView.isEditing ? doneBarButton : editBarButton
    }
    
}


// MARK: - Subscriber

extension StudentListViewController: Subscriber {
    
    func update(with state: AppState) {
        guard let group = state.selectedGroup else { return }
        self.group = group
        navBarButton.mainTitle = group.name
        let count = allStudents.filter { group.studentIds.contains($0.id) }.count
        navBarButton.subTitle = "\(count) students"
        absentStudents = state.absentStudents<commit_msg>Fixes count bug<commit_after>//
//  StudentListViewController.swift
//  TeacherTools
//
//  Created by <NAME> on 10/31/16.
//  Copyright © 2016 AppsByPJ. All rights reserved.
//

import UIKit
import BetterSegmentedControl

class StudentListViewController: UIViewController, AutoStoryboardInitializable {
    
    @IBOutlet var emptyStateView: UIView!
    @IBOutlet var emptyStateImages: [UIImageView]!
    @IBOutlet var emptyStateLabels: [UILabel]!
    
    @IBOutlet weak var backgroundImageView: UIImageView!
    @IBOutlet weak var navBarButton: NavBarButton!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var newEntryView: UIView!
    @IBOutlet weak var newStudentTextField: UITextField!
    @IBOutlet weak var segmentedControl: BetterSegmentedControl!

    var core = App.core
    var group: Group?
    var students = [Student]()
    var absentStudents = [Student]()
    
    fileprivate var plusBarButton = UIBarButtonItem()
    fileprivate var pasteBarButton = UIBarButtonItem()
    fileprivate var saveBarButton = UIBarButtonItem()
    fileprivate var cancelBarButton = UIBarButtonItem()
    fileprivate var doneBarButton = UIBarButtonItem()
    fileprivate var editBarButton = UIBarButtonItem()
    
    var currentSortType: SortType = App.core.state.currentUser!.lastFirst ? .last : .first {
        didSet {
            students = currentSortType.sort(students)
            tableView.reloadData()
            isAdding = false
        }
    }
    var isAdding = false {
        didSet {
            toggleEntryView(hidden: !isAdding)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        newEntryView.isHidden = true
        setUp()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        core.add(subscriber: self)
        
        if !navBarButton.isPointingDown {
            animateArrow(up: false)
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        core.remove(subscriber: self)
        NotificationCenter.default.removeObserver(self)
    }
    
    @IBAction func newStudentTextFieldChanged(_ sender: UITextField) {
        updateRightBarButton()
    }
    
    @IBAction func segmentedControlValueChanged(_ sender: BetterSegmentedControl) {
        guard let selectedSortType = SortType(rawValue: Int(sender.index)) else { return }
        currentSortType = selectedSortType
    }
    
    @IBAction func pasteViewTapped(_ sender: UITapGestureRecognizer) {
        pasteButtonPressed()
    }
    
    @IBAction func addViewTapped(_ sender: UITapGestureRecognizer) {
        startEditing()
    }
    
    func editButtonPressed(_ sender: UIBarButtonItem) {
        tableView.isEditing = !tableView.isEditing
        tableView.reloadData()
        navigationItem.leftBarButtonItem = tableView.isEditing ? doneBarButton : editBarButton
    }
    
}


// MARK: - Subscriber

extension StudentListViewController: Subscriber {
    
    func update(with state: AppState) {
        guard let group = state.selectedGroup else { return }
        self.group = group
        navBarButton.mainTitle = group.name
        navBarButton.subTitle = "\(group.studentIds.count) students"
        absentStudents = state.absentStudents
{'fraction_non_alphanumeric': 0.07848706832112423, 'fraction_numerical': 0.0031394827328449695, 'mean_word_length': 2.8470385278895916, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '664346', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1896, 'n_words': 434}