<commit_before>//
//  ViewController.swift
//  SWAutoLayoutScrollView
//
//  Created by DonMag on 5/18/16.
//  Copyright © 2016 DonMag. All rights reserved.
//

import UIKit

extension MutableCollectionType where Self.Index == Int {
	func shuffle() -> Self {
		var r = self
		let c = self.count
		for i in 0..<(c - 1) {
			let j = Int(arc4random_uniform(UInt32(c - i))) + i
			if i != j {
				swap(&r[i], &r[j])
			}
		}
		return r
	}
}

class ViewController: UIViewController {

	@IBOutlet weak var theScrollView: UIScrollView!
	@IBOutlet weak var pScrollView: UIScrollView!
	@IBOutlet weak var theLabel: UILabel!
	
	var imgURLs = ["a", "b", "c", "d", "e", "f"]
	
	
	override func viewDidLoad() {
		super.viewDidLoad()
		// Do any additional setup after loading the view, typically from a nib.

		theScrollView.removeFromSuperview()
		theLabel.removeFromSuperview()
		
		theScrollView.translatesAutoresizingMaskIntoConstraints = false
		theLabel.translatesAutoresizingMaskIntoConstraints = false
		
		pScrollView.addSubview(theScrollView)
		pScrollView.addSubview(theLabel)
		
		var d = [String:AnyObject]()
		d["theScrollView"] = theScrollView
		d["theLabel"] = theLabel
		d["sv"] = pScrollView
		
		
		var hFmt = "H:|[theScrollView(==sv)]|"
		
		pScrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(hFmt, options: [], metrics: nil, views: d))

		hFmt = "V:|[theScrollView(250)]-700-[theLabel]|"

		pScrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(hFmt, options: [], metrics: nil, views: d))

		hFmt = "H:|-40-[theLabel]"
		
		pScrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(hFmt, options: [], metrics: nil, views: d))

		
		// add simple subviews
		self.addMyViews(8, vWidth: 200, vHeight: 200, vSpacing: 10, bCentered: true)
		
		// or, add images as subviews
//		self.setOfferImages(imgURLs)

		
	}

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


	func addMyViews(n: Int, vWidth: Int, vHeight: Int, vSpacing: Int, bCentered: Bool) {
		
		var curView: AnyObject?
		var prevView: AnyObject?
		var vConstraint: NSLayoutConstraint?
		
		for i in 1...n {
			
			// load an instance of our SampleView... would have proper error handling in real-world-usage
			guard let v = UINib(nibName: "SampleView", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as? UIView else { return }
			
			v.translatesAutoresizingMaskIntoConstraints = false
			
			// the UILabel in our SampleView was assigned a Tag of 99 in IB... would have proper error handling in real-world-usage
			guard let lbl = v.viewWithTag(99) as? UILabel else { return }
			lbl.text = "\(i)"
			
			// add this view to the ScrollView
			self.theScrollView.addSubview(v)
			
			curView = v
			
			if (prevView == nil) {
				
				vConstraint = NSLayoutConstraint(item: v,
				                                    attribute: .Leading,
				                                    relatedBy: .Equal,
				                                    toItem: self.theScrollView,
				                                    attribute: .Leading,
				                                    multiplier: 1.0,
				                                    constant: 12.0)
				
			} else {
				
				vConstraint = NSLayoutConstraint(item: v,
				                                    attribute: .Leading,
				                                    relatedBy: .Equal,
				                                    toItem: prevView,
				                                    attribute: .Trailing,
				                                    multiplier: 1.0,
				                                    constant: 12.0)
				
			}
			
			self.theScrollView.addConstraint(vConstraint!)

			// set the width & height
			
			vConstraint = NSLayoutConstraint(item: v,
			                                 attribute: .Width,
			                                 relatedBy: .Equal,
			                                 toItem: nil,
			                                 attribute: .Width,
			                                 multiplier: 1.0,
			                                 constant: CGFloat(vWidth))
			
			self.theScrollView.addConstraint(vConstraint!)

			
			vConstraint = NSLayoutConstraint(item: v,
			                                 attribute: .Height,
			                                 relatedBy: .Equal,
			                                 toItem: nil,
			                                 attribute: .Height,
			                                 multiplier: 1.0,
			                                 constant: CGFloat(vHeight))
			
			self.theScrollView.addConstraint(vConstraint!)
			

			if bCentered {
				
				
				// now set the CenterY of our SampleView equal to the CenterY of the ScrollView

				vConstraint = NSLayoutConstraint(item: v,
				                                 attribute: .CenterY,
				                                 relatedBy: .Equal,
				                                 toItem: self.theScrollView,
				                                 attribute: .CenterY,
				                                 multiplier: 1.0,
				                                 constant: 0)
				
				self.theScrollView.addConstraint(vConstraint!)
				
				
			}
			
			// assign the "current SampleView" reference to the "previous" reference
		
			prevView = v
			
		}
		
		// add a "closing" Horizontal constraint, to pin the right edge of the last view to the right-edge of the ScrollView - sort of...
		// because the First added view is pinned to the left, and the Last added view is pinned to the right, the contentSize is "auto-magically" handled

		vConstraint = NSLayoutConstraint(item: self.theScrollView,
		                                 attribute: .Trailing,
		                                 relatedBy: .Equal,
		                                 toItem: curView!,
		                                 attribute: .Trailing,
		                                 multiplier: 1.0,
		                                 constant: 12.0)

		self.theScrollView.addConstraint(vConstraint!)
		

	}
	
	
	func addMyViewsVFL(n: Int, vWidth: Int, vHeight: Int, vSpacing: Int, bCentered: Bool) {
		
		var prevRef: String
		var currRef: String
		var vFmt: String
		var hFmt: String

		var dViews = [String:AnyObject]()
		
		let dMetrics: [String:Int] = [
			"width" : vWidth,<commit_msg>replaced hard-coded spacing with passed value in new func<commit_after>//
//  ViewController.swift
//  SWAutoLayoutScrollView
//
//  Created by DonMag on 5/18/16.
//  Copyright © 2016 DonMag. All rights reserved.
//

import UIKit

extension MutableCollectionType where Self.Index == Int {
	func shuffle() -> Self {
		var r = self
		let c = self.count
		for i in 0..<(c - 1) {
			let j = Int(arc4random_uniform(UInt32(c - i))) + i
			if i != j {
				swap(&r[i], &r[j])
			}
		}
		return r
	}
}

class ViewController: UIViewController {

	@IBOutlet weak var theScrollView: UIScrollView!
	@IBOutlet weak var pScrollView: UIScrollView!
	@IBOutlet weak var theLabel: UILabel!
	
	var imgURLs = ["a", "b", "c", "d", "e", "f"]
	
	
	override func viewDidLoad() {
		super.viewDidLoad()
		// Do any additional setup after loading the view, typically from a nib.

		theScrollView.removeFromSuperview()
		theLabel.removeFromSuperview()
		
		theScrollView.translatesAutoresizingMaskIntoConstraints = false
		theLabel.translatesAutoresizingMaskIntoConstraints = false
		
		pScrollView.addSubview(theScrollView)
		pScrollView.addSubview(theLabel)
		
		var d = [String:AnyObject]()
		d["theScrollView"] = theScrollView
		d["theLabel"] = theLabel
		d["sv"] = pScrollView
		
		
		var hFmt = "H:|[theScrollView(==sv)]|"
		
		pScrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(hFmt, options: [], metrics: nil, views: d))

		hFmt = "V:|[theScrollView(250)]-700-[theLabel]|"

		pScrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(hFmt, options: [], metrics: nil, views: d))

		hFmt = "H:|-40-[theLabel]"
		
		pScrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(hFmt, options: [], metrics: nil, views: d))

		
		// add simple subviews
		self.addMyViews(8, vWidth: 200, vHeight: 200, vSpacing: 16, bCentered: true)
		
		// or, add images as subviews
//		self.setOfferImages(imgURLs)

		
	}

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


	func addMyViews(n: Int, vWidth: Int, vHeight: Int, vSpacing: Int, bCentered: Bool) {
		
		var curView: AnyObject?
		var prevView: AnyObject?
		var vConstraint: NSLayoutConstraint?
		
		for i in 1...n {
			
			// load an instance of our SampleView... would have proper error handling in real-world-usage
			guard let v = UINib(nibName: "SampleView", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as? UIView else { return }
			
			v.translatesAutoresizingMaskIntoConstraints = false
			
			// the UILabel in our SampleView was assigned a Tag of 99 in IB... would have proper error handling in real-world-usage
			guard let lbl = v.viewWithTag(99) as? UILabel else { return }
			lbl.text = "\(i)"
			
			// add this view to the ScrollView
			self.theScrollView.addSubview(v)
			
			curView = v
			
			if (prevView == nil) {
				
				vConstraint = NSLayoutConstraint(item: v,
				                                    attribute: .Leading,
				                                    relatedBy: .Equal,
				                                    toItem: self.theScrollView,
				                                    attribute: .Leading,
				                                    multiplier: 1.0,
				                                    constant: CGFloat(vSpacing))
				
			} else {
				
				vConstraint = NSLayoutConstraint(item: v,
				                                    attribute: .Leading,
				                                    relatedBy: .Equal,
				                                    toItem: prevView,
				                                    attribute: .Trailing,
				                                    multiplier: 1.0,
				                                    constant: CGFloat(vSpacing))
				
			}
			
			self.theScrollView.addConstraint(vConstraint!)

			// set the width & height
			
			vConstraint = NSLayoutConstraint(item: v,
			                                 attribute: .Width,
			                                 relatedBy: .Equal,
			                                 toItem: nil,
			                                 attribute: .Width,
			                                 multiplier: 1.0,
			                                 constant: CGFloat(vWidth))
			
			self.theScrollView.addConstraint(vConstraint!)

			
			vConstraint = NSLayoutConstraint(item: v,
			                                 attribute: .Height,
			                                 relatedBy: .Equal,
			                                 toItem: nil,
			                                 attribute: .Height,
			                                 multiplier: 1.0,
			                                 constant: CGFloat(vHeight))
			
			self.theScrollView.addConstraint(vConstraint!)
			

			if bCentered {
				
				
				// now set the CenterY of our SampleView equal to the CenterY of the ScrollView

				vConstraint = NSLayoutConstraint(item: v,
				                                 attribute: .CenterY,
				                                 relatedBy: .Equal,
				                                 toItem: self.theScrollView,
				                                 attribute: .CenterY,
				                                 multiplier: 1.0,
				                                 constant: 0)
				
				self.theScrollView.addConstraint(vConstraint!)
				
				
			}
			
			// assign the "current SampleView" reference to the "previous" reference
		
			prevView = v
			
		}
		
		// add a "closing" Horizontal constraint, to pin the right edge of the last view to the right-edge of the ScrollView - sort of...
		// because the First added view is pinned to the left, and the Last added view is pinned to the right, the contentSize is "auto-magically" handled

		guard let v = curView else { return }
		
		vConstraint = NSLayoutConstraint(item: self.theScrollView,
		                                 attribute: .Trailing,
		                                 relatedBy: .Equal,
		                                 toItem: v,
		                                 attribute: .Trailing,
		                                 multiplier: 1.0,
		                                 constant: CGFloat(vSpacing))

		self.theScrollView.addConstraint(vConstraint!)
		

	}
	
	
	func addMyViewsVFL(n: Int, vWidth: Int, vHeight: Int, vSpacing: Int, bCentered: Bool) {
		
		var prevRef: String
		var currRef: String
		var vFmt: String
		var hFmt: String

		var dViews = [String:AnyObject]()
		
		let dMetrics: [String:Int] = [
			"width" : vWidth,
{'fraction_non_alphanumeric': 0.08820079756040347, 'fraction_numerical': 0.008522949409648917, 'mean_word_length': 1.6289825282631039, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 72, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4658457', 'n_tokens_mistral': 4333, 'n_tokens_neox': 3432, 'n_words': 935}