<commit_before>//
//  FeedFollowSuggestionsController.swift
//  Tempo
//
//  Created by <NAME> on 11/19/16.
//  Copyright © 2016 CUAppDev. All rights reserved.
//

import UIKit

protocol FeedFollowSuggestionsControllerDelegate: class {
	func feedFollowSuggestionsController(controller: FeedFollowSuggestionsController, wantsToShowProfileForUser user: User)
	func feedFollowSuggestionsControllerWantsToShowMoreSuggestions()
}

class FeedFollowSuggestionsController: NSObject, UITableViewDataSource, UITableViewDelegate, FollowUserDelegate {
	
	let suggestionsEdgePadding: CGFloat = 16
	let topInset: CGFloat = 40
	let smallerYOffset: CGFloat = 22
	let greaterYOffset: CGFloat = 123
	let cellHeight: CGFloat = 101
	
	var view: UIView!
	var suggestionsContainerView: UIView!
	var noMorePostsLabel: UILabel!
	var tableView: UITableView!
	var followMoreButton: UIButton!
	
	var suggestedPeopleToFollow = [User]()
	var showingNoMorePostsLabel: Bool = true
	var suggestionsContainerHeight: CGFloat!
	
	weak var delegate: FeedFollowSuggestionsControllerDelegate?
	
	init(frame: CGRect) {
		super.init()

		view = UIView(frame: frame)
		setupView()
		fetchSuggestedPeopleToFollow()
	}
	
	func showNoMorePostsLabel() {
		if showingNoMorePostsLabel { return }
		
		view.addSubview(noMorePostsLabel)
		showingNoMorePostsLabel = true
		updateViews()
	}
	
	func hideNoMorePostsLabel() {
		if !showingNoMorePostsLabel { return }
		
		noMorePostsLabel.removeFromSuperview()
		showingNoMorePostsLabel = false
		updateViews()
	}
	
	func reload() {
		fetchSuggestedPeopleToFollow()
	}
	
	func fetchSuggestedPeopleToFollow() {
		let completion: ([User]) -> Void = { (users: [User]) in
			DispatchQueue.main.async {
				self.suggestedPeopleToFollow = users
				self.tableView.reloadData()
				
				if self.suggestedPeopleToFollow.count == 0 {
					self.view.alpha = 0.0
				} else {
					self.view.alpha = 1.0
					self.tableView.backgroundView = nil
					self.tableView.frame.size.height = self.cellHeight * CGFloat(self.suggestedPeopleToFollow.count)
					self.followMoreButton.frame.origin.y = self.tableView.frame.maxY + self.suggestionsEdgePadding
				}
			}
		}
		
		API.sharedAPI.fetchFollowSuggestions(completion, length: 3, page: 0)
	}
	
	func setupView() {
		// Create no posts in last 24 hours label
		noMorePostsLabel = UILabel(frame: CGRect(x: 0, y: 65, width: view.frame.width, height: 23))
		noMorePostsLabel.font = UIFont(name: "AvenirNext-Regular", size: 17.0)
		noMorePostsLabel.text = "No posts in the last 24 hours."
		noMorePostsLabel.textColor = .descriptionGrey
		noMorePostsLabel.textAlignment = .center
		noMorePostsLabel.isHidden = !showingNoMorePostsLabel
		
		// Create suggestions container view
		let currContainerYOffset = showingNoMorePostsLabel ? greaterYOffset : smallerYOffset
		let followSuggestionsLabelHeight: CGFloat = 18
		let tableViewHeight: CGFloat = cellHeight * CGFloat(suggestedPeopleToFollow.count)
		let followMoreButtonHeight: CGFloat = 36
		suggestionsContainerHeight = followSuggestionsLabelHeight + suggestionsEdgePadding/2 + tableViewHeight + suggestionsEdgePadding + followMoreButtonHeight
		
		suggestionsContainerView = UIView(frame: CGRect(x: 0, y: currContainerYOffset, width: view.bounds.width, height: suggestionsContainerHeight))

		let followSuggestionsLabel = UILabel(frame: CGRect(x: suggestionsEdgePadding, y: 0, width: view.frame.width - 2*suggestionsEdgePadding, height: 18))
		followSuggestionsLabel.font = UIFont(name: "AvenirNext-Regular", size: 13.0)
		followSuggestionsLabel.text = "SUGGESTED PEOPLE TO FOLLOW"
		followSuggestionsLabel.textColor = .sectionTitleGrey
		followSuggestionsLabel.textAlignment = .left
		
		tableView = UITableView(frame: CGRect(x: suggestionsEdgePadding, y: followSuggestionsLabel.frame.maxY + suggestionsEdgePadding/2, width: view.frame.width - 2*suggestionsEdgePadding, height: tableViewHeight), style: .plain)
		tableView.dataSource = self
		tableView.delegate = self
		tableView.rowHeight = cellHeight
		tableView.isScrollEnabled = false
		tableView.showsVerticalScrollIndicator = false
		tableView.register(UINib(nibName: "FollowTableViewCell", bundle: nil), forCellReuseIdentifier: "FollowCell")
		
		followMoreButton = UIButton(frame: CGRect(x: 0, y: tableView.frame.maxY + suggestionsEdgePadding, width: 200, height: 36))
		followMoreButton.center.x = view.center.x
		followMoreButton.backgroundColor = .tempoRed
		followMoreButton.setTitle("Follow more friends", for: UIControlState())
		followMoreButton.setTitleColor(UIColor.white.withAlphaComponent(0.87), for: UIControlState())
		followMoreButton.titleLabel?.font = UIFont(name: "AvenirNext-Demibold", size: 16.0)
		followMoreButton.layer.cornerRadius = 3<commit_msg>Change follow suggestions tableview back to accommodate 3 followers<commit_after>//
//  FeedFollowSuggestionsController.swift
//  Tempo
//
//  Created by <NAME> on 11/19/16.
//  Copyright © 2016 CUAppDev. All rights reserved.
//

import UIKit

protocol FeedFollowSuggestionsControllerDelegate: class {
	func feedFollowSuggestionsController(controller: FeedFollowSuggestionsController, wantsToShowProfileForUser user: User)
	func feedFollowSuggestionsControllerWantsToShowMoreSuggestions()
}

class FeedFollowSuggestionsController: NSObject, UITableViewDataSource, UITableViewDelegate, FollowUserDelegate {
	
	let suggestionsEdgePadding: CGFloat = 16
	let topInset: CGFloat = 40
	let smallerYOffset: CGFloat = 22
	let greaterYOffset: CGFloat = 123
	let cellHeight: CGFloat = 101
	let numFollowSuggestions: Int = 3
	
	var view: UIView!
	var suggestionsContainerView: UIView!
	var noMorePostsLabel: UILabel!
	var tableView: UITableView!
	
	var suggestedPeopleToFollow = [User]()
	var showingNoMorePostsLabel: Bool = true
	var suggestionsContainerHeight: CGFloat!
	
	weak var delegate: FeedFollowSuggestionsControllerDelegate?
	
	init(frame: CGRect) {
		super.init()

		view = UIView(frame: frame)
		setupView()
		fetchSuggestedPeopleToFollow()
	}
	
	func showNoMorePostsLabel() {
		if showingNoMorePostsLabel { return }
		
		view.addSubview(noMorePostsLabel)
		showingNoMorePostsLabel = true
		updateViews()
	}
	
	func hideNoMorePostsLabel() {
		if !showingNoMorePostsLabel { return }
		
		noMorePostsLabel.removeFromSuperview()
		showingNoMorePostsLabel = false
		updateViews()
	}
	
	func reload() {
		fetchSuggestedPeopleToFollow()
	}
	
	func fetchSuggestedPeopleToFollow() {
		let completion: ([User]) -> Void = { (users: [User]) in
			DispatchQueue.main.async {
				self.suggestedPeopleToFollow = users
				self.tableView.reloadData()
				
				if self.suggestedPeopleToFollow.count == 0 {
					self.view.alpha = 0.0
				} else {
					self.view.alpha = 1.0
					self.tableView.backgroundView = nil
				}
			}
		}
		
		API.sharedAPI.fetchFollowSuggestions(completion, length: numFollowSuggestions, page: 0)
	}
	
	func setupView() {
		// Create no posts in last 24 hours label
		noMorePostsLabel = UILabel(frame: CGRect(x: 0, y: 65, width: view.frame.width, height: 23))
		noMorePostsLabel.font = UIFont(name: "AvenirNext-Regular", size: 17.0)
		noMorePostsLabel.text = "No posts in the last 24 hours."
		noMorePostsLabel.textColor = .descriptionGrey
		noMorePostsLabel.textAlignment = .center
		noMorePostsLabel.isHidden = !showingNoMorePostsLabel
		
		// Create suggestions container view
		let currContainerYOffset = showingNoMorePostsLabel ? greaterYOffset : smallerYOffset
		let followSuggestionsLabelHeight: CGFloat = 18
		let tableViewHeight: CGFloat = cellHeight * CGFloat(numFollowSuggestions)
		let followMoreButtonHeight: CGFloat = 36
		suggestionsContainerHeight = followSuggestionsLabelHeight + suggestionsEdgePadding/2 + tableViewHeight + suggestionsEdgePadding + followMoreButtonHeight
		
		suggestionsContainerView = UIView(frame: CGRect(x: 0, y: currContainerYOffset, width: view.bounds.width, height: suggestionsContainerHeight))

		let followSuggestionsLabel = UILabel(frame: CGRect(x: suggestionsEdgePadding, y: 0, width: view.frame.width - 2*suggestionsEdgePadding, height: 18))
		followSuggestionsLabel.font = UIFont(name: "AvenirNext-Regular", size: 13.0)
		followSuggestionsLabel.text = "SUGGESTED PEOPLE TO FOLLOW"
		followSuggestionsLabel.textColor = .sectionTitleGrey
		followSuggestionsLabel.textAlignment = .left
		
		tableView = UITableView(frame: CGRect(x: suggestionsEdgePadding, y: followSuggestionsLabel.frame.maxY + suggestionsEdgePadding/2, width: view.frame.width - 2*suggestionsEdgePadding, height: tableViewHeight), style: .plain)
		tableView.dataSource = self
		tableView.delegate = self
		tableView.rowHeight = cellHeight
		tableView.isScrollEnabled = false
		tableView.showsVerticalScrollIndicator = false
		tableView.register(UINib(nibName: "FollowTableViewCell", bundle: nil), forCellReuseIdentifier: "FollowCell")
		
		let followMoreButton = UIButton(frame: CGRect(x: 0, y: tableView.frame.maxY + suggestionsEdgePadding, width: 200, height: 36))
		followMoreButton.center.x = view.center.x
		followMoreButton.backgroundColor = .tempoRed
		followMoreButton.setTitle("Follow more friends", for: UIControlState())
		followMoreButton.setTitleColor(UIColor.white.withAlphaComponent(0.87), for: UIControlState())
		followMoreButton.titleLabel?.font = UIFont(name: "AvenirNext-Demibold", size: 16.0)
		followMoreButton.layer.cornerRadius = 3
{'fraction_non_alphanumeric': 0.08895110919664011, 'fraction_numerical': 0.014968770191686409, 'mean_word_length': 5.275, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5523138', 'n_tokens_mistral': 3291, 'n_tokens_neox': 2755, 'n_words': 621}