<commit_before>//
//  SelectRequestsViewController.swift
//  Shifty
//
//  Created by <NAME> on 16/06/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit
import SwiftDate
import Parse

extension NSDate: HasDate
{
    func getWeekOfYear() -> String
    {
        return "Week: " + String(self.weekOfYear)
    }
    
    var date: NSDate { get { return self } set { self.date = newValue } }
}

class SelectRequestsViewController: UITableViewController
{
    let amountOfDaysToGenerate = 31
    var sectionsInTable = [String]()
    var possibleDates = [NSDate]()
    var sectionedDates = [[NSDate]]()
    var selectedDates = [NSDate]()
    
    let rooster = Rooster()
        
    override func viewDidLoad()
    {
        possibleDates = getDates()
        sectionsInTable = rooster.getSections(possibleDates)
        sectionedDates = rooster.splitIntoSections(possibleDates, sections: sectionsInTable)
        
        tableView.reloadData()
        super.viewDidLoad()
    }
    
    @IBAction func finishedSelecting(sender: UIBarButtonItem)
    {
        for date in selectedDates
        {
            let request = PFObject(className: "RequestedShifts")
            request["date"] = date
            request["requestedBy"] = PFUser.currentUser()
            
            request.saveInBackgroundWithBlock() { (succes: Bool, error: NSError?) -> Void in
                
                if error != nil
                {
                    println(error?.description)
                }
                else if succes
                {
                    self.navigationController?.popViewControllerAnimated(false)
                }
            }
        }
    }
    
    func getDates() -> [NSDate]
    {
        let today = NSDate()
        var comingDays = [NSDate]()
        
        for days in 0..<amountOfDaysToGenerate
        {
            comingDays.append(today + days.day)
        }

        return comingDays
    }
    
    // tableView delegate fuctions
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return sectionedDates[section].count
    }
    
    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
    {
        return sectionsInTable[section]
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return sectionedDates.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("Date", forIndexPath: indexPath) as! UITableViewCell
        
        let date = sectionedDates[indexPath.section][indexPath.row]
        
        cell.textLabel?.text = date.toString(format: DateFormat.Custom("EEEE dd MMM"))
        cell.textLabel?.textAlignment = NSTextAlignment.Center
        
        return cell
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        let selectedDate = sectionedDates[indexPath.section][indexPath.row]
        selectedDates.append(selectedDate)
    }
    
    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
    {
        let deselectedDate = sectionedDates[indexPath.section][indexPath.row]
        
        if let index = find(selectedDates, deselectedDate)
        {
            selectedDates.removeAtIndex(index)
        }
    }
    
}<commit_msg>Fixed sections<commit_after>//
//  SelectRequestsViewController.swift
//  Shifty
//
//  Created by <NAME> on 16/06/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit
import SwiftDate
import Parse

extension NSDate: HasDate
{
    func getWeekOfYear() -> String
    {
        return "Week: " + String((self - 1.day).weekOfYear)
    }
    
    var date: NSDate { get { return self } set { self.date = newValue } }
}

class SelectRequestsViewController: UITableViewController
{
    let amountOfDaysToGenerate = 31
    var sectionsInTable = [String]()
    var possibleDates = [NSDate]()
    var sectionedDates = [[NSDate]]()
    var selectedDates = [NSDate]()
    var previousRequests = [NSDate]()
    
    let rooster = Rooster()
    let helper = Helper()
        
    override func viewDidLoad()
    {
        showOptionsForCurrentUser()
        super.viewDidLoad()
    }
    
    @IBAction func finishedSelecting(sender: UIBarButtonItem)
    {
        println(selectedDates)
        for (i,date) in enumerate(selectedDates)
        {
            let request = PFObject(className: "RequestedShifts")
            request["date"] = date
            request["requestedBy"] = PFUser.currentUser()
            
            request.saveInBackgroundWithBlock() { (succes: Bool, error: NSError?) -> Void in
                
                if error != nil
                {
                    println(error?.description)
                }
                else if succes && i == self.selectedDates.count - 1
                {
                    self.navigationController?.popViewControllerAnimated(false)
                }
            }
        }
    }
    
    func getDates()
    {
        let today = NSDate()
        let alreadySubmitted = previousRequests.map() { String($0.day) + String($0.month) }
        
        for days in 0..<amountOfDaysToGenerate
        {
            let date = today + days.day
            let check = String(date.day) + String(date.month)

            if !contains(alreadySubmitted, check)
            {
                possibleDates.append(date)
            }
        }
    }
    
    func showOptionsForCurrentUser()
    {
        let query = PFQuery(className: "RequestedShifts")
        query.whereKey("requestedBy", equalTo: PFUser.currentUser()!)
        query.findObjectsInBackgroundWithBlock() { (objects: [AnyObject]?, error: NSError?) -> Void in
            
            if let requests = self.helper.returnObjectAfterErrorCheck(objects, error: error) as? [PFObject]
            {
                for request in requests
                {
                    let date = request["date"] as! NSDate
                    self.previousRequests.append(date)
                }
            }
            
            self.getDates()
            self.sectionsInTable = self.rooster.getSections(self.possibleDates)
            self.sectionedDates = self.rooster.splitIntoSections(self.possibleDates, sections: self.sectionsInTable)
            self.tableView.reloadData()
        }
    }
    // tableView delegate fuctions
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return sectionedDates[section].count
    }
    
    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
    {
        return sectionsInTable[section]
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return sectionedDates.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("Date", forIndexPath: indexPath) as! UITableViewCell
        
        let date = sectionedDates[indexPath.section][indexPath.row]
        
        cell.textLabel?.text = date.toString(format: DateFormat.Custom("EEEE dd MMM"))
        cell.textLabel?.textAlignment = NSTextAlignment.Center
        
        return cell
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        let selectedDate = sectionedDates[indexPath.section][indexPath.row]
        selectedDates.append(selectedDate)
        println("Selected \(selectedDate)")
    }
    
    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
    {
        let deselectedDate = sectionedDates[indexPath.section][indexPath.row]
        
        if let index = find(selectedDates, deselectedDate)
        {
            selectedDates.removeAtIndex(index)
            println(deselectedDate)
        }
    }
    
}
{'fraction_non_alphanumeric': 0.08959044368600683, 'fraction_numerical': 0.0036567528035104826, 'mean_word_length': 2.1328751431844215, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '2133436', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2254, 'n_words': 468}