<commit_before>//
//  DealerTableViewController.swift
//  Eurofurence
//
//  Copyright © 2017 Eurofurence. All rights reserved.
//

import UIKit
import Alamofire
import AlamofireImage
import ReactiveSwift

class DealerTableViewController: UITableViewController {
	let dataContext: DataContextProtocol = try! ContextResolver.container.resolve()
	var dealersByLetter: [String: [Dealer]] = [:]
	var sortedKeys: [String] = []
	var disposable = CompositeDisposable()

    deinit {
        disposable.dispose()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.backgroundColor =  UIColor(red: 35/255.0, green: 36/255.0, blue: 38/255.0, alpha: 1.0)
        self.refreshControl?.addTarget(self, action: #selector(DealerTableViewController.refresh(_:)), for: UIControlEvents.valueChanged)
        self.tableView.sectionIndexColor = UIColor.white

        disposable += dataContext.Dealers.signal
            .observe(on: QueueScheduler.concurrent).observe({ [weak self] _ in
                guard let strongSelf = self else { return }
                strongSelf.orderDealersAlphabeticaly(strongSelf.dataContext.Dealers.value)
                strongSelf.tableView.reloadData()
            })

        orderDealersAlphabeticaly(dataContext.Dealers.value)
        tableView.reloadData()

        guard let refreshControl = refreshControl else { return }

        let refreshControlVisibilityDelegate = RefreshControlDataStoreDelegate(refreshControl: refreshControl)
        DataStoreRefreshController.shared.add(refreshControlVisibilityDelegate)
    }

    func canRotate() -> Bool {
        return true
	}

	// TODO: Pull into super class for all refreshable ViewControllers
	/// Initiates sync with API via refreshControl
	func refresh(_ sender: AnyObject) {
		DataStoreRefreshController.shared.refreshStore()
	}

	override func viewWillDisappear(_ animated: Bool) {
		refreshControl?.endRefreshing()
	}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
	}

	private func orderDealersAlphabeticaly(_ dealers: [Dealer]) {
		// TODO: Insert sync barrier
		dealersByLetter.removeAll()
		for dealer in dealers {
			var dealerName: String?

			if !dealer.DisplayName.isEmpty {
				dealerName = dealer.DisplayName
			} else if !dealer.AttendeeNickname.isEmpty {
				dealerName = dealer.AttendeeNickname
			}

			if let dealerName = dealerName {
				let dealerLetter: String
				if #available(iOS 9.0, *) {
					dealerLetter = String(dealerName.localizedUppercase[dealerName.startIndex])
				} else {
					dealerLetter = String(dealerName.uppercased()[dealerName.startIndex])
				}
				var dealersWithLetter: [Dealer]
				if let _dealersWithLetter = dealersByLetter[dealerLetter] {
					dealersWithLetter = _dealersWithLetter
				} else {
					dealersWithLetter = []
				}
				dealersWithLetter.append(dealer)
				dealersByLetter.updateValue(dealersWithLetter, forKey: dealerLetter)
			}
		}
		sortedKeys = dealersByLetter.keys.sorted()
	}

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return dealersByLetter.keys.count
	}

	override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
		return sortedKeys
	}

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let letter = sortedKeys[section]
        if let dealersWithLetter = dealersByLetter[letter] {
            return (dealersWithLetter.count)
        } else {
            return 0
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DealersTableViewCell", for: indexPath) as! DealersTableViewCell
        cell.dealer = self.dealersByLetter[sortedKeys[(indexPath as NSIndexPath).section]]?[(indexPath as NSIndexPath).row]
        return cell
    }

    override func tableView( _ tableView: UITableView, titleForHeaderInSection section: Int) -> String {
        return sortedKeys[section]
    }

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView //recast your view as a UITableViewHeaderFooterView
        header.contentView.backgroundColor = UIColor(red: 68/255, green: 69/255, blue: 72/255, alpha: 1.0) //make the background color light blue
        header.textLabel!.textColor = UIColor.white //make the text white
        header.alpha = 0.8 //make the header transparent
    }<commit_msg>Add tiled background to dealer table view [#109]<commit_after>//
//  DealerTableViewController.swift
//  Eurofurence
//
//  Copyright © 2017 Eurofurence. All rights reserved.
//

import UIKit
import Alamofire
import AlamofireImage
import ReactiveSwift

class DealerTableViewController: UITableViewController {
	let dataContext: DataContextProtocol = try! ContextResolver.container.resolve()
	var dealersByLetter: [String: [Dealer]] = [:]
	var sortedKeys: [String] = []
	var disposable = CompositeDisposable()

    deinit {
        disposable.dispose()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.backgroundColor =  UIColor(patternImage: #imageLiteral(resourceName: "Background Tile"))
        refreshControl?.addTarget(self, action: #selector(DealerTableViewController.refresh(_:)),
                                  for: UIControlEvents.valueChanged)
        //tableView.sectionIndexColor = UIColor.white
		//tableView.sectionIndexBackgroundColor = UIColor.darkGray.withAlphaComponent(0.5)

        disposable += dataContext.Dealers.signal
            .observe(on: QueueScheduler.concurrent).observe({ [weak self] _ in
                guard let strongSelf = self else { return }
                strongSelf.orderDealersAlphabeticaly(strongSelf.dataContext.Dealers.value)
                strongSelf.tableView.reloadData()
            })

        orderDealersAlphabeticaly(dataContext.Dealers.value)
        tableView.reloadData()

        guard let refreshControl = refreshControl else { return }

        let refreshControlVisibilityDelegate = RefreshControlDataStoreDelegate(refreshControl: refreshControl)
        DataStoreRefreshController.shared.add(refreshControlVisibilityDelegate)
    }

    func canRotate() -> Bool {
        return true
	}

	// TODO: Pull into super class for all refreshable ViewControllers
	/// Initiates sync with API via refreshControl
	func refresh(_ sender: AnyObject) {
		DataStoreRefreshController.shared.refreshStore()
	}

	override func viewWillDisappear(_ animated: Bool) {
		refreshControl?.endRefreshing()
	}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
	}

	private func orderDealersAlphabeticaly(_ dealers: [Dealer]) {
		// TODO: Insert sync barrier
		dealersByLetter.removeAll()
		for dealer in dealers {
			var dealerName: String?

			if !dealer.DisplayName.isEmpty {
				dealerName = dealer.DisplayName
			} else if !dealer.AttendeeNickname.isEmpty {
				dealerName = dealer.AttendeeNickname
			}

			if let dealerName = dealerName {
				let dealerLetter: String
				if #available(iOS 9.0, *) {
					dealerLetter = String(dealerName.localizedUppercase[dealerName.startIndex])
				} else {
					dealerLetter = String(dealerName.uppercased()[dealerName.startIndex])
				}
				var dealersWithLetter: [Dealer]
				if let _dealersWithLetter = dealersByLetter[dealerLetter] {
					dealersWithLetter = _dealersWithLetter
				} else {
					dealersWithLetter = []
				}
				dealersWithLetter.append(dealer)
				dealersByLetter.updateValue(dealersWithLetter, forKey: dealerLetter)
			}
		}
		sortedKeys = dealersByLetter.keys.sorted()
	}

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return dealersByLetter.keys.count
	}

	override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
		return sortedKeys
	}

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let letter = sortedKeys[section]
        if let dealersWithLetter = dealersByLetter[letter] {
            return (dealersWithLetter.count)
        } else {
            return 0
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DealersTableViewCell", for: indexPath) as! DealersTableViewCell
        cell.dealer = self.dealersByLetter[sortedKeys[(indexPath as NSIndexPath).section]]?[(indexPath as NSIndexPath).row]
        return cell
    }

    override func tableView( _ tableView: UITableView, titleForHeaderInSection section: Int) -> String {
        return sortedKeys[section]
    }

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
		guard let header = view as? UITableViewHeaderFooterView else { return }
        header.contentView.backgroundColor = UIColor.darkGray
        header.textLabel!.textColor = UIColor.white
        header.alpha = 0.75
    }
{'fraction_non_alphanumeric': 0.08579976493215087, 'fraction_numerical': 0.006517790362218186, 'mean_word_length': 3.438122332859175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '530463', 'n_tokens_mistral': 2991, 'n_tokens_neox': 2599, 'n_words': 610}