<commit_before>//
//  TTAImagePickerController.swift
//  Pods
//
//  Created by TobyoTenma on 17/06/2017.
//  Copyright (c) 2017 TMTBO. All rights reserved.
//

import Photos

public protocol TTAImagePickerControllerDelegate: class {
    func imagePickerController(_ picker: TTAImagePickerController, didFinishPicking images: [UIImage], assets: [TTAAsset])
}

// MARK: - Option Functions
extension TTAImagePickerControllerDelegate {
    
}

public class TTAImagePickerController: UINavigationController {
    
    public weak var pickerDelegate: TTAImagePickerControllerDelegate?
    
    /// The max num image of the image picker can pick, default is 9
    public var maxPickerNum = 9 {
        didSet {
            configPicker { (albumVc, assetVc) in
                albumVc?.maxPickerNum = maxPickerNum
                assetVc?.maxPickerNum = maxPickerNum
            }
        }
    }
    
    /// The selected asset
    public var selectedAsset: [TTAAsset] = [] {
        didSet {
            configPicker { (_, assetVc) in
                assetVc?.selectedAsset = selectedAsset.map { $0.original }
            }
        }
    }
    
    /// The tint color which item was selected, default is `UIColor(colorLiteralRed: 0, green: 122.0 / 255.0, blue: 1, alpha: 1)`
    public var selectItemTintColor: UIColor? = UIColor(colorLiteralRed: 0, green: 122.0 / 255.0, blue: 1, alpha: 1) {
        didSet {
            configPicker { (albumVc, assetVc) in
                albumVc?.selectItemTintColor = selectItemTintColor
                assetVc?.selectItemTintColor = selectItemTintColor
            }
        }
    }
    
    /// NavigationBar tintColor
    public var tintColor: UIColor = UIColor(colorLiteralRed: 0, green: 122.0 / 255.0, blue: 1, alpha: 1) {
        didSet {
            navigationBar.tintColor = tintColor
            navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: tintColor]
            _ = splitController.viewControllers.map { (viewController) in
                guard let viewController = viewController as? UINavigationController else { return }
                viewController.toolbar.tintColor = tintColor
                viewController.navigationBar.tintColor = tintColor
                viewController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: tintColor]
            }
        }
    }
    
    /// NavigationBar barTintColor
    public var barTintColor: UIColor? {
        didSet {
            navigationBar.barTintColor = barTintColor
            _ = splitController.viewControllers.map { (viewController) in
                guard let viewController = viewController as? UINavigationController else { return }
                viewController.navigationBar.barTintColor = barTintColor
                viewController.toolbar.barTintColor = barTintColor
            }
        }
    }
    
    fileprivate let splitController = UISplitViewController()
    
    public convenience init(selectedAsset: [TTAAsset]) {
        type(of: self).prepareIconFont()
        let rootVc = UIViewController()
        self.init(rootViewController: rootVc)
        prepareNavigationItems()
        
        self.selectedAsset = selectedAsset

        addChildViewController(splitController)
        view.addSubview(splitController.view)
        splitController.view.backgroundColor = .clear
        splitController.preferredDisplayMode = .allVisible
        splitController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    }
    
    deinit {
        #if DEBUG
            print("TTAImagePickerController >>>>>> deinit")
        #endif
    }

}

// MARK: - UI

extension TTAImagePickerController {
    func prepareNavigationItems() {
        guard let topViewController = topViewController else { return }
        topViewController.navigationItem.title = "\(TTAImagePickerController.self)"
        topViewController.navigationItem.hidesBackButton = true
        let cancelItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(didClickCancelItem))
        topViewController.navigationItem.rightBarButtonItem = cancelItem
    }
}

// MARK: - Life Cycle

extension TTAImagePickerController {
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        checkPermission()
    }
    
    public override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        splitController.view.frame = view.bounds
    }<commit_msg>fixed: fix the seleted asset pass back problem<commit_after>//
//  TTAImagePickerController.swift
//  Pods
//
//  Created by TobyoTenma on 17/06/2017.
//  Copyright (c) 2017 TMTBO. All rights reserved.
//

import Photos

public protocol TTAImagePickerControllerDelegate: class {
    func imagePickerController(_ picker: TTAImagePickerController, didFinishPicking images: [UIImage], assets: [TTAAsset])
}

// MARK: - Option Functions
extension TTAImagePickerControllerDelegate {
    
}

public class TTAImagePickerController: UINavigationController {
    
    public weak var pickerDelegate: TTAImagePickerControllerDelegate?
    
    /// The max num image of the image picker can pick, default is 9
    public var maxPickerNum = 9 {
        didSet {
            configPicker { (albumVc, assetVc) in
                albumVc?.maxPickerNum = maxPickerNum
                assetVc?.maxPickerNum = maxPickerNum
            }
        }
    }
    
    /// The selected asset
    public var selectedAsset: [TTAAsset] = [] {
        didSet {
            updateSelectedAsset()
        }
    }
    
    /// The tint color which item was selected, default is `UIColor(colorLiteralRed: 0, green: 122.0 / 255.0, blue: 1, alpha: 1)`
    public var selectItemTintColor: UIColor? = UIColor(colorLiteralRed: 0, green: 122.0 / 255.0, blue: 1, alpha: 1) {
        didSet {
            configPicker { (albumVc, assetVc) in
                albumVc?.selectItemTintColor = selectItemTintColor
                assetVc?.selectItemTintColor = selectItemTintColor
            }
        }
    }
    
    /// NavigationBar tintColor
    public var tintColor: UIColor = UIColor(colorLiteralRed: 0, green: 122.0 / 255.0, blue: 1, alpha: 1) {
        didSet {
            navigationBar.tintColor = tintColor
            navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: tintColor]
            _ = splitController.viewControllers.map { (viewController) in
                guard let viewController = viewController as? UINavigationController else { return }
                viewController.toolbar.tintColor = tintColor
                viewController.navigationBar.tintColor = tintColor
                viewController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: tintColor]
            }
        }
    }
    
    /// NavigationBar barTintColor
    public var barTintColor: UIColor? {
        didSet {
            navigationBar.barTintColor = barTintColor
            _ = splitController.viewControllers.map { (viewController) in
                guard let viewController = viewController as? UINavigationController else { return }
                viewController.navigationBar.barTintColor = barTintColor
                viewController.toolbar.barTintColor = barTintColor
            }
        }
    }
    
    fileprivate let splitController = UISplitViewController()
    
    public convenience init(selectedAsset: [TTAAsset]) {
        type(of: self).prepareIconFont()
        let rootVc = UIViewController()
        self.init(rootViewController: rootVc)
        prepareNavigationItems()
        
        self.selectedAsset = selectedAsset
        updateSelectedAsset()
        
        addChildViewController(splitController)
        view.addSubview(splitController.view)
        splitController.view.backgroundColor = .clear
        splitController.preferredDisplayMode = .allVisible
        splitController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    }
    
    deinit {
        #if DEBUG
            print("TTAImagePickerController >>>>>> deinit")
        #endif
    }

}

// MARK: - UI

extension TTAImagePickerController {
    func prepareNavigationItems() {
        guard let topViewController = topViewController else { return }
        topViewController.navigationItem.title = "\(TTAImagePickerController.self)"
        topViewController.navigationItem.hidesBackButton = true
        let cancelItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(didClickCancelItem))
        topViewController.navigationItem.rightBarButtonItem = cancelItem
    }
    
    func updateSelectedAsset() {
        configPicker { (_, assetVc) in
            assetVc?.selectedAsset = selectedAsset.map { $0.original }
        }
    }
}

// MARK: - Life Cycle

extension TTAImagePickerController {
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        checkPermission()
    }
    
    public override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        splitController.view.frame = view.bounds
    }
{'fraction_non_alphanumeric': 0.07833899419305358, 'fraction_numerical': 0.010518242576969431, 'mean_word_length': 2.4562665656948126, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1692716', 'n_tokens_mistral': 2557, 'n_tokens_neox': 2437, 'n_words': 543}