<commit_before>//
//  GalleryDetailViewController.swift
//  Squizit
//
//  Created by <NAME> on 9/5/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import UIKit
import Lilliput

let DebugLayout = false

class GalleryDetailCollectionViewCell : UICollectionViewCell {
	@IBOutlet weak var imageView: ImagePresenterView!
	@IBOutlet weak var imageViewHeightConstraint: NSLayoutConstraint!
	@IBOutlet weak var imageViewWidthConstraint: NSLayoutConstraint!
	@IBOutlet weak var imageViewCenterYAlignmentConstraint: NSLayoutConstraint!
	@IBOutlet weak var playerNamesLabel: UILabel!
	@IBOutlet weak var matchDateLabel: UILabel!

	var renderAction:CancelableAction<UIImage>?

	class func identifier() ->String { return "GalleryDetailCollectionViewCell" }

	override func awakeFromNib() {
		super.awakeFromNib()

		imageView.backgroundColor = SquizitTheme.paperBackgroundColor()

		// for some reason I can't set Baskerville in IB
		playerNamesLabel.font = UIFont(name: "Baskerville", size: playerNamesLabel.font.pointSize)
		matchDateLabel.font = UIFont(name:"Baskerville-Italic", size: matchDateLabel.font.pointSize)

		imageView.layer.shadowColor = UIColor.blackColor().CGColor
		imageView.layer.shadowOffset = CGSize(width: 0, height: 2)
		imageView.layer.shadowOpacity = 1
		imageView.layer.shadowRadius = 5

		prepareForReuse()
	}

	override func prepareForReuse() {

		if let action = renderAction {
			action.cancel()
			renderAction = nil
		}

		imageView.image = nil

		if DebugLayout {
			backgroundColor = UIColor(hue: CGFloat(drand48()), saturation: 0.5 + CGFloat(drand48()/2), brightness: 0.5 + CGFloat(drand48()/2), alpha: CGFloat(0.5))
		}
	}

}

class GalleryDetailCollectionViewDataSource : BasicGalleryCollectionViewDataSource {

	private var _renderQueue = dispatch_queue_create("com.zakariya.squizit.GalleryDetailRenderQueue", nil)
	private var _drawingBackgroundColor = SquizitTheme.paperBackgroundColor()

	override var cellIdentifier:String {
		return GalleryDetailCollectionViewCell.identifier()
	}

	override var fetchBatchSize:Int {
		return 3
	}

	override func configureCell( cell:UICollectionViewCell, atIndexPath indexPath:NSIndexPath ) {
		let store = self.store
		let backgroundColor = _drawingBackgroundColor
		let flowLayout = self.collectionView.collectionViewLayout as UICollectionViewFlowLayout
		let itemSize = flowLayout.itemSize
		let thumbnailHeight = itemSize.height * 0.8

		if let drawing = self.fetchedResultsController.objectAtIndexPath(indexPath) as? GalleryDrawing {

			//
			//	Now, we have to render the thumbnail - it comes from the store as a transparent PNG, 
			//	with pen/brush strokes in black, and eraser in white. We need to multiply composite
			//	it over the paper texture to make a viable thumbnail image
			//

			var galleryCell = cell as GalleryDetailCollectionViewCell

			galleryCell.playerNamesLabel.text = drawing.artistDisplayNames
			galleryCell.matchDateLabel.text = dateFormatter.stringFromDate(NSDate(timeIntervalSinceReferenceDate: drawing.date))


			// use the thumbnail's size to set the cell image size & aspect ratio
			let thumbnailActualHeight = CGFloat(drawing.thumbnailHeight)
			let thumbnailActualWidth = CGFloat(drawing.thumbnailWidth)
			let thumbnailWidth = thumbnailActualWidth * (thumbnailHeight/thumbnailActualHeight)
			galleryCell.imageViewHeightConstraint.constant = thumbnailHeight
			galleryCell.imageViewWidthConstraint.constant = thumbnailWidth

			// offset the vertical centering constraint
			galleryCell.imageViewCenterYAlignmentConstraint.constant = galleryCell.playerNamesLabel.intrinsicContentSize().height + galleryCell.matchDateLabel.intrinsicContentSize().height

			let queue = _renderQueue
			galleryCell.renderAction = CancelableAction<UIImage>(action: { done, canceled in

				dispatch_async( queue ) {

					if let buffer = ByteBuffer.fromNSData( drawing.match ) {
						if !canceled() {
							var matchLoadResult = buffer.getMatch()
							if let error = matchLoadResult.error {
								NSLog("Unable to load match from data, error: %@", error.message )
								assertionFailure("Unable to load match from data, bailing" )
							}

							var match = matchLoadResult.value
							if !canceled() {
								var rendering = match.render( backgroundColor: backgroundColor )
								done( result:rendering )
							}
						}
					}
				}
<commit_msg>Minor improvement to detail sizing<commit_after>//
//  GalleryDetailViewController.swift
//  Squizit
//
//  Created by <NAME> on 9/5/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import UIKit
import Lilliput

let DebugLayout = false

class GalleryDetailCollectionViewCell : UICollectionViewCell {
	@IBOutlet weak var imageView: ImagePresenterView!
	@IBOutlet weak var imageViewHeightConstraint: NSLayoutConstraint!
	@IBOutlet weak var imageViewWidthConstraint: NSLayoutConstraint!
	@IBOutlet weak var imageViewCenterYAlignmentConstraint: NSLayoutConstraint!
	@IBOutlet weak var playerNamesLabel: UILabel!
	@IBOutlet weak var matchDateLabel: UILabel!

	var renderAction:CancelableAction<UIImage>?

	class func identifier() ->String { return "GalleryDetailCollectionViewCell" }

	override func awakeFromNib() {
		super.awakeFromNib()

		imageView.backgroundColor = SquizitTheme.paperBackgroundColor()

		// for some reason I can't set Baskerville in IB
		playerNamesLabel.font = UIFont(name: "Baskerville", size: playerNamesLabel.font.pointSize)
		matchDateLabel.font = UIFont(name:"Baskerville-Italic", size: matchDateLabel.font.pointSize)

		imageView.layer.shadowColor = UIColor.blackColor().CGColor
		imageView.layer.shadowOffset = CGSize(width: 0, height: 2)
		imageView.layer.shadowOpacity = 1
		imageView.layer.shadowRadius = 5

		prepareForReuse()
	}

	override func prepareForReuse() {

		if let action = renderAction {
			action.cancel()
			renderAction = nil
		}

		imageView.image = nil

		if DebugLayout {
			backgroundColor = UIColor(hue: CGFloat(drand48()), saturation: 0.5 + CGFloat(drand48()/2), brightness: 0.5 + CGFloat(drand48()/2), alpha: CGFloat(0.5))
		}
	}

}

class GalleryDetailCollectionViewDataSource : BasicGalleryCollectionViewDataSource {

	private var _renderQueue = dispatch_queue_create("com.zakariya.squizit.GalleryDetailRenderQueue", nil)
	private var _drawingBackgroundColor = SquizitTheme.paperBackgroundColor()

	override var cellIdentifier:String {
		return GalleryDetailCollectionViewCell.identifier()
	}

	override var fetchBatchSize:Int {
		return 3
	}

	override func configureCell( cell:UICollectionViewCell, atIndexPath indexPath:NSIndexPath ) {
		let store = self.store
		let backgroundColor = _drawingBackgroundColor
		let flowLayout = self.collectionView.collectionViewLayout as UICollectionViewFlowLayout
		let itemSize = flowLayout.itemSize
		let thumbnailHeight = itemSize.height * 0.85

		if let drawing = self.fetchedResultsController.objectAtIndexPath(indexPath) as? GalleryDrawing {

			//
			//	Now, we have to render the thumbnail - it comes from the store as a transparent PNG, 
			//	with pen/brush strokes in black, and eraser in white. We need to multiply composite
			//	it over the paper texture to make a viable thumbnail image
			//

			var galleryCell = cell as GalleryDetailCollectionViewCell

			galleryCell.playerNamesLabel.text = drawing.artistDisplayNames
			galleryCell.matchDateLabel.text = dateFormatter.stringFromDate(NSDate(timeIntervalSinceReferenceDate: drawing.date))


			// use the thumbnail's size to set the cell image size & aspect ratio
			let thumbnailActualHeight = CGFloat(drawing.thumbnailHeight)
			let thumbnailActualWidth = CGFloat(drawing.thumbnailWidth)
			let thumbnailWidth = thumbnailActualWidth * (thumbnailHeight/thumbnailActualHeight)
			galleryCell.imageViewHeightConstraint.constant = thumbnailHeight
			galleryCell.imageViewWidthConstraint.constant = thumbnailWidth

			// offset the vertical centering constraint
			galleryCell.imageViewCenterYAlignmentConstraint.constant = (galleryCell.playerNamesLabel.intrinsicContentSize().height + galleryCell.matchDateLabel.intrinsicContentSize().height) / 2

			let queue = _renderQueue
			galleryCell.renderAction = CancelableAction<UIImage>(action: { done, canceled in

				dispatch_async( queue ) {

					if let buffer = ByteBuffer.fromNSData( drawing.match ) {
						if !canceled() {
							var matchLoadResult = buffer.getMatch()
							if let error = matchLoadResult.error {
								NSLog("Unable to load match from data, error: %@", error.message )
								assertionFailure("Unable to load match from data, bailing" )
							}

							var match = matchLoadResult.value
							if !canceled() {
								var rendering = match.render( backgroundColor: backgroundColor )
								done( result:rendering )
							}
						}
					}
				}

{'fraction_non_alphanumeric': 0.08070776255707762, 'fraction_numerical': 0.00684931506849315, 'mean_word_length': 4.537926675094817, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '2366117', 'n_tokens_mistral': 2924, 'n_tokens_neox': 2500, 'n_words': 629}