<commit_before>//
//  MorphSegue.swift
//
//  Created by <NAME> on 26/10/2015.
//  Copyright (c) 2015 BareFeetWare. Free to use and modify, without warranty.
//

// Work in progress. Unfinished.

import UIKit

class MorphSegue: UIStoryboardSegue {

    // MARK: - Public variables

    @IBOutlet lazy var fromView: UIView? = {
        return self.sourceViewController.view
    }()
    
    var duration: NSTimeInterval = 1.0
    var animationOptions = UIViewAnimationOptions.CurveEaseInOut
    
    // MARK: - UIStoryboardSegue
    
    override func perform() {
        let destinationVCView = destinationViewController.view
        let sourceVCView = sourceViewController.view
        
        // Force frames to match constraints, in case misplaced.
        destinationVCView.setNeedsLayout()
        destinationVCView.layoutIfNeeded()
        
        destinationVCView.frame = sourceVCView.frame
        if let cell = fromView as? UITableViewCell {
            cell.selected = false
        }
        
        var morphingView: UIView?
        var contentView: UIView?
        let isMorphingCopy = true
        if isMorphingCopy {
            // Create a copy of the view hierarchy for morphing, so the original is not changed.
            if let cell = fromView as? UITableViewCell {
                morphingView = cell.copyWithSubviews(nil, includeConstraints: false)
                morphingView?.copySubviews(cell.contentView.subviews, includeConstraints: false)
            } else {
                morphingView = fromView?.copyWithSubviews(fromView?.subviews, includeConstraints: false)
            }
            if let morphingView = morphingView {
                sourceVCView.addSubview(morphingView)
                contentView = morphingView
            }
        } else {
            // morphingView = fromView
            // if let cell = fromView as? UITableViewCell {
            //     contentView = cell.contentView
            // }
            // TODO: finish
        }
        
        // Add destination constraints, which will animate frames when layout is updated, inside animation block below.
//        NSLayoutConstraint.copyDescendantConstraintsFromSourceView(sourceVCView, toDestinationView: destinationVCView)
        
        if let morphingView = morphingView {
            UIView.animateWithDuration(
                duration,
                delay: 0.0,
                options: animationOptions,
                animations: {
                    morphingView.frame = sourceVCView.bounds;
//                    morphingView.setNeedsLayout()
//                    morphingView.layoutIfNeeded()
                    if let _ = self.fromView as? UITableViewCell {
                        contentView?.frame = morphingView.bounds
                    }
                    if let subviews = contentView?.subviews {
                        for subview in subviews {
                            if let destinationSubview = destinationVCView.subviewMatchingView(subview) {
                                subview.frame = destinationSubview.frame
                                subview.copyAnimatablePropertiesFromView(destinationSubview)
                            } else {
                                subview.alpha = 0.0
                            }
                        }
                    }
                })<commit_msg>MorphSegue: Enabled copying of constraints (crashes)<commit_after>//
//  MorphSegue.swift
//
//  Created by <NAME> on 26/10/2015.
//  Copyright (c) 2015 BareFeetWare. Free to use and modify, without warranty.
//

// Work in progress. Unfinished.

import UIKit

class MorphSegue: UIStoryboardSegue {

    // MARK: - Public variables

    @IBOutlet lazy var fromView: UIView? = {
        return self.sourceViewController.view
    }()
    
    var duration: NSTimeInterval = 1.0
    var animationOptions = UIViewAnimationOptions.CurveEaseInOut
    
    // MARK: - UIStoryboardSegue
    
    override func perform() {
        let destinationVCView = destinationViewController.view
        let sourceVCView = sourceViewController.view
        
        // Force frames to match constraints, in case misplaced.
        destinationVCView.setNeedsLayout()
        destinationVCView.layoutIfNeeded()
        
        destinationVCView.frame = sourceVCView.frame
        if let cell = fromView as? UITableViewCell {
            cell.selected = false
        }
        
        var morphingView: UIView?
        var contentView: UIView?
        let isMorphingCopy = true
        if isMorphingCopy {
            // Create a copy of the view hierarchy for morphing, so the original is not changed.
            if let cell = fromView as? UITableViewCell {
                morphingView = cell.copyWithSubviews(nil, includeConstraints: true)
                morphingView?.copySubviews(cell.contentView.subviews, includeConstraints: true)
            } else {
                morphingView = fromView?.copyWithSubviews(fromView?.subviews, includeConstraints: true)
            }
            if let morphingView = morphingView {
                sourceVCView.addSubview(morphingView)
                contentView = morphingView
            }
        } else {
            // morphingView = fromView
            // if let cell = fromView as? UITableViewCell {
            //     contentView = cell.contentView
            // }
            // TODO: finish
        }
        
        // Add destination constraints, which will animate frames when layout is updated, inside animation block below.
        NSLayoutConstraint.copyDescendantConstraintsFromSourceView(sourceVCView, toDestinationView: destinationVCView)
        
        if let morphingView = morphingView {
            UIView.animateWithDuration(
                duration,
                delay: 0.0,
                options: animationOptions,
                animations: {
                    morphingView.frame = sourceVCView.bounds;
                    morphingView.setNeedsLayout()
                    morphingView.layoutIfNeeded()
                    if let _ = self.fromView as? UITableViewCell {
                        contentView?.frame = morphingView.bounds
                    }
                    if let subviews = contentView?.subviews {
                        for subview in subviews {
                            if let destinationSubview = destinationVCView.subviewMatchingView(subview) {
                                subview.frame = destinationSubview.frame
                                subview.copyAnimatablePropertiesFromView(destinationSubview)
                            } else {
                                subview.alpha = 0.0
                            }
                        }
                    }
                })
{'fraction_non_alphanumeric': 0.06496004735128737, 'fraction_numerical': 0.005327019828351583, 'mean_word_length': 1.5758384146341464, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2976574', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1608, 'n_words': 399}