<commit_before>import UIKit

final class MediaPickerViewController: UIViewController, MediaPickerViewInput {
    private var isBeingRotated: Bool = false
    private let mediaPickerView = MediaPickerView()
    private var layoutSubviewsPromise = Promise<Void>()
    
    // MARK: - UIViewController
    
    override func loadView() {
        view = UIView()
        view.backgroundColor = .black
        view.addSubview(mediaPickerView)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        automaticallyAdjustsScrollViewInsets = false
        onViewDidLoad?()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.setNavigationBarHidden(true, animated: animated)
        UIApplication.shared.setStatusBarHidden(true, with: .fade)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        mediaPickerView.alpha = 0
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        // 1. Open this view controller
        // 2. Push another view controller
        // 3. Rotate device
        // 4. Pop to this view controller
        //
        // Without the following lines views go wild, it looks like they are chaotically placed.
        //
        // UIDevice.current.userInterfaceIdiom restricts the klusge to iPad. It is only an iPad issue.
        //
        // I've spent about 4-5 hours fixing it.
        //
        if mediaPickerView.alpha == 0 && UIDevice.current.userInterfaceIdiom == .pad {
            DispatchQueue.main.async {
                self.view.setNeedsLayout()
                self.view.layoutIfNeeded()
                
                UIView.animate(withDuration: 0.2, animations: {
                    self.mediaPickerView.alpha = 1
                })
            }
        }
        
        // AI-3326: костыль для iOS 8, на котором после дисмисса модального окна или возврата с предыдущего экрана
        // OpenGL рандомно (не каждый раз) прекращает отрисовку
        mediaPickerView.reloadCamera()
        
        onViewDidAppear?(animated)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        if !isBeingRotated {
            mediaPickerView.frame = view.bounds
            mediaPickerView.transform = CGAffineTransform(interfaceOrientation: interfaceOrientation)
        }
        onPreviewSizeDetermined?(mediaPickerView.previewSize)
        layoutSubviewsPromise.fulfill()
    }
    
    override func willAnimateRotation(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
        if shouldAutorotate {
            // Compensation animation for rotation.<commit_msg>AI-4278: Fix returning to media picker VC on iPhone<commit_after>import UIKit

final class MediaPickerViewController: UIViewController, MediaPickerViewInput {
    private var isBeingRotated: Bool = false
    private let mediaPickerView = MediaPickerView()
    private var layoutSubviewsPromise = Promise<Void>()
    
    // MARK: - UIViewController
    
    override func loadView() {
        view = UIView()
        view.backgroundColor = .black
        view.addSubview(mediaPickerView)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        automaticallyAdjustsScrollViewInsets = false
        onViewDidLoad?()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.setNavigationBarHidden(true, animated: animated)
        UIApplication.shared.setStatusBarHidden(true, with: .fade)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        // See viewDidAppear
        // UIDevice.current.userInterfaceIdiom restricts the klusge to iPad. It is only an iPad issue.
        if UIDevice.current.userInterfaceIdiom == .pad {
            mediaPickerView.alpha = 0
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        // 1. Open this view controller
        // 2. Push another view controller
        // 3. Rotate device
        // 4. Pop to this view controller
        //
        // Without the following lines views go wild, it looks like they are chaotically placed.
        //
        // I've spent about 4-5 hours fixing it.
        //
        // Note that there is no check for iPad here. If alpha is 0 we must animate fade in in any case
        //
        if mediaPickerView.alpha == 0 {
            DispatchQueue.main.async {
                self.view.setNeedsLayout()
                self.view.layoutIfNeeded()
                
                UIView.animate(withDuration: 0.2, animations: {
                    self.mediaPickerView.alpha = 1
                })
            }
        }
        
        // AI-3326: костыль для iOS 8, на котором после дисмисса модального окна или возврата с предыдущего экрана
        // OpenGL рандомно (не каждый раз) прекращает отрисовку
        mediaPickerView.reloadCamera()
        
        onViewDidAppear?(animated)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        if !isBeingRotated {
            mediaPickerView.frame = view.bounds
            mediaPickerView.transform = CGAffineTransform(interfaceOrientation: interfaceOrientation)
        }
        onPreviewSizeDetermined?(mediaPickerView.previewSize)
        layoutSubviewsPromise.fulfill()
    }
    
    override func willAnimateRotation(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
        if shouldAutorotate {
            // Compensation animation for rotation.
{'fraction_non_alphanumeric': 0.07100085543199315, 'fraction_numerical': 0.006330196749358426, 'mean_word_length': 2.076842105263158, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2407859', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1555, 'n_words': 389}