<commit_before>//
//  ViewModel.swift
//  todoMVVM
//
//  Created by <NAME> on 19/01/16.
//  Copyright © 2016 mcomisso. All rights reserved.
//

import Foundation
import UIKit

class ToDoViewModel: NSObject {
    var toDoDataSource: Array<ToDo>
    var toDoCompleted: Array<ToDo>

    let demo1 = ToDo(withContent: "Primo todo")
    let demo2 = ToDo(withContent: "Secondo")
    
    override init() {
        self.toDoDataSource = []
        self.toDoCompleted = []
    }

    // Demo data
    func completeWithDemoData() {
        self.toDoDataSource.append(self.demo1)
        self.toDoDataSource.append(self.demo2)
    }

    // Create new todos
    func createNewTodo(content: String) {
        let newTodo = ToDo(withContent: content)
        self.toDoDataSource.append(newTodo)
    }

    // Handle current completion status of todos
    func addCompletedTodo(todo: ToDo) {
        todo.completed = true
        self.toDoCompleted.append(todo)
    }

    func removeCompletedTodo(todo: ToDo) {
        todo.completed = false
        self.toDoCompleted.removeAtIndex(self.toDoCompleted.indexOf(todo)!)
    }

    func clearCompletedTodo() {
        for todo in self.toDoCompleted {
            todo.completed = false
        }

        self.toDoCompleted.removeAll()
    }

    func isCompleted(todo: ToDo) -> Bool {
        return self.toDoCompleted.contains(todo)
    }

    func handleToDoAtIndexPath(indexPath: NSIndexPath) {
        let todo = self.toDoDataSource[indexPath.row]
        todo.completed ? self.removeCompletedTodo(todo) : self.addCompletedTodo(todo)
    }
}<commit_msg>Private functions to wrap direct handling of arrays<commit_after>//
//  ViewModel.swift
//  todoMVVM
//
//  Created by <NAME> on 19/01/16.
//  Copyright © 2016 mcomisso. All rights reserved.
//

import Foundation
import UIKit

class ToDoViewModel: NSObject {
    var todoDataSource: Array<ToDo>
    var todoCompleted: Array<ToDo>
    
    let demo1 = ToDo(withContent: "First Todo")
    let demo2 = ToDo(withContent: "Second Todo")
    
    override init() {
        self.todoDataSource = []
        self.todoCompleted = []
    }
    
    /**
     Set a bunch of demo data
     */
    func completeWithDemoData() {
        self.appendToDataSource(self.demo1)
        self.appendToDataSource(self.demo2)
    }
    
    /**
     Create new todos
     
     - parameter content: The content string of newly created Todo
     */
    func createNewTodo(content: String) {
        let newTodo = ToDo(withContent: content)
        self.appendToDataSource(newTodo)
    }
    
    /**
     Handle current completion status of todos
     
     - parameter todo: The item to handle
     */
    func addCompletedTodo(todo: ToDo) {
        todo.completed = true
        self.appedToCompleted(todo)
    }
    
    /**
     Removes the Todo item from the completed array
     
     - parameter todo: The item to remove
     */
    func removeCompletedTodo(todo: ToDo) {
        self.removeFromCompleted(todo)
    }
    
    /**
     Removes all completed Todos
     */
    func clearCompletedTodo() {
        for todo in self.todoCompleted {
            guard let index = self.todoDataSource.indexOf(todo) else {
                continue
            }
            self.todoDataSource.removeAtIndex(index)
        }
        
        self.todoCompleted.removeAll()
    }
    
    func isCompleted(todo: ToDo) -> Bool {
        return self.todoCompleted.contains(todo)
    }
    
    func handleTodoAtIndexPath(indexPath: NSIndexPath) {
        let todo = self.todoDataSource[indexPath.row]
        todo.completed ? self.removeCompletedTodo(todo) : self.addCompletedTodo(todo)
    }
    
    // Deletion
    func deleteTodoItem(todo: ToDo) {
        self.removeFromDataSource(todo)
        self.removeFromCompleted(todo)
    }
}

extension ToDoViewModel {
    
    private func appedToCompleted(todo: ToDo) {
        self.todoCompleted.append(todo)
    }
    
    private func appendToDataSource(todo: ToDo) {
        self.todoDataSource.append(todo)
    }
    
    private func removeFromDataSource(todo: ToDo) {
        guard let index = self.todoDataSource.indexOf(todo) else {
            return
        }
        self.todoDataSource.removeAtIndex(index)
    }
    
    private func removeFromCompleted(todo: ToDo) {
        defer { todo.completed = false }
        guard let index = self.todoCompleted.indexOf(todo) else {
            return
        }
        self.todoCompleted.removeAtIndex(index)
    }
}
{'fraction_non_alphanumeric': 0.09246880570409982, 'fraction_numerical': 0.006238859180035651, 'mean_word_length': 2.2318214542836574, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '5076642', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1362, 'n_words': 291}