<commit_before>//
//  ExampleViewController.swift
//  DataSourceExample
//
//  Created by <NAME> on 15/06/15.
//  Copyright (c) 2015 Fueled. All rights reserved.
//

import UIKit
import DataSource

private func space() -> UIBarButtonItem {
    return UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
}

class ExampleViewController: UIViewController, UITableViewDelegate, UICollectionViewDelegate {
    
    @IBOutlet var tableView: UITableView?
    @IBOutlet var collectionView: UICollectionView?
    
    let tableDataSource = TableViewDataSourceWithHeaderFooterTitles()
    let collectionDataSource = CollectionViewDataSource()
    
    var viewModel: ExampleViewModel? {
        didSet {
            if let viewModel = self.viewModel {
                self.navigationItem.title = viewModel.title
                self.tableDataSource.dataSource.innerDataSource.value = viewModel.dataSource
                self.collectionDataSource.dataSource.innerDataSource.value = viewModel.dataSource
                self.toolbarItems = viewModel.actions.flatMap {
                    [space(), $0.barButtonItem()]
                } + [space()]
            }
        }
    }
    
    @IBAction func toggleView() {
        if let tableView = self.tableView,
            collectionView = self.collectionView
        {
            UIView.animateWithDuration(0.35) {
                tableView.alpha = 1 - tableView.alpha
                collectionView.alpha = 1 - collectionView.alpha
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let tableView = self.tableView {
            tableView.estimatedRowHeight = 44
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.sectionHeaderHeight = UITableViewAutomaticDimension
            tableView.dataSource = self.tableDataSource
            self.tableDataSource.tableView = tableView
        }
        if let collectionView = self.collectionView {
            collectionView.dataSource = self.collectionDataSource
            self.collectionDataSource.collectionView = collectionView
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if let tableView = self.tableView,
            collectionView = self.collectionView
        {
            collectionView.scrollIndicatorInsets = tableView.scrollIndicatorInsets
            collectionView.contentInset = tableView.contentInset
        }
    }
    
    func selectItemAtIndexPath(indexPath: NSIndexPath) {
        if let item = self.viewModel?.dataSource.itemAtIndexPath(indexPath) as? ExampleItem {
            item.on.value = !item.on.value
        }
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.selectItemAtIndexPath(indexPath)
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        self.selectItemAtIndexPath(indexPath)
    }

}<commit_msg>Do not show non-existing section headers<commit_after>//
//  ExampleViewController.swift
//  DataSourceExample
//
//  Created by <NAME> on 15/06/15.
//  Copyright (c) 2015 Fueled. All rights reserved.
//

import UIKit
import DataSource

private func space() -> UIBarButtonItem {
    return UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
}

class ExampleViewController: UIViewController, UITableViewDelegate, UICollectionViewDelegateFlowLayout {
    
    @IBOutlet var tableView: UITableView?
    @IBOutlet var collectionView: UICollectionView?
    
    let tableDataSource = TableViewDataSourceWithHeaderFooterTitles()
    let collectionDataSource = CollectionViewDataSource()
    
    var viewModel: ExampleViewModel? {
        didSet {
            if let viewModel = self.viewModel {
                self.navigationItem.title = viewModel.title
                self.tableDataSource.dataSource.innerDataSource.value = viewModel.dataSource
                self.collectionDataSource.dataSource.innerDataSource.value = viewModel.dataSource
                self.toolbarItems = viewModel.actions.flatMap {
                    [space(), $0.barButtonItem()]
                } + [space()]
            }
        }
    }
    
    @IBAction func toggleView() {
        if let tableView = self.tableView,
            collectionView = self.collectionView
        {
            UIView.animateWithDuration(0.35) {
                tableView.alpha = 1 - tableView.alpha
                collectionView.alpha = 1 - collectionView.alpha
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let tableView = self.tableView {
            tableView.estimatedRowHeight = 44
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.sectionHeaderHeight = UITableViewAutomaticDimension
            tableView.dataSource = self.tableDataSource
            self.tableDataSource.tableView = tableView
        }
        if let collectionView = self.collectionView {
            collectionView.dataSource = self.collectionDataSource
            self.collectionDataSource.collectionView = collectionView
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if let tableView = self.tableView,
            collectionView = self.collectionView
        {
            collectionView.scrollIndicatorInsets = tableView.scrollIndicatorInsets
            collectionView.contentInset = tableView.contentInset
        }
    }
    
    func selectItemAtIndexPath(indexPath: NSIndexPath) {
        if let item = self.viewModel?.dataSource.itemAtIndexPath(indexPath) as? ExampleItem {
            item.on.value = !item.on.value
        }
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.selectItemAtIndexPath(indexPath)
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        self.selectItemAtIndexPath(indexPath)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if let _ = self.viewModel?.dataSource.supplementaryItemOfKind(UICollectionElementKindSectionHeader, inSection: section) {
            return CGSizeMake(44, 44)
        } else {
            return CGSizeZero
        }
    }

}
{'fraction_non_alphanumeric': 0.07276062366248853, 'fraction_numerical': 0.006114338122898196, 'mean_word_length': 2.4509493670886076, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '8755', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1670, 'n_words': 324}