//
//  LoadingViewController.swift
//  Tusker
//
//  Created by Shadowfacts on 10/1/18.
//  Copyright © 2018 Shadowfacts. All rights reserved.
//

import UIKit

// Based on John Sundell's example: https://www.swiftbysundell.com/posts/using-child-view-controllers-as-plugins-in-swift
class LoadingViewController: UIViewController {

    private var activityIndicator: UIActivityIndicatorView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .clear
        
        activityIndicator = UIActivityIndicatorView(style: .large)
        activityIndicator.color = .secondaryLabel
        activityIndicator.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(activityIndicator)
        
        activityIndicator.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
        activityIndicator.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // 0.5s delay so indicator is not shown if data loads very quickly
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
            self?.activityIndicator.startAnimating()
        }
    }

}
