<commit_before>//
//  SessionActionsViewController.swift
//  WWDC
//
//  Created by <NAME> on 06/05/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Cocoa
import PlayerUI
import ConfCore
import RxSwift
import RxCocoa

protocol SessionActionsViewControllerDelegate: class {

    func sessionActionsDidSelectSlides(_ sender: NSView?)
    func sessionActionsDidSelectFavorite(_ sender: NSView?)
    func sessionActionsDidSelectDownload(_ sender: NSView?)
    func sessionActionsDidSelectCalendar(_ sender: NSView?)
    func sessionActionsDidSelectDeleteDownload(_ sender: NSView?)
    func sessionActionsDidSelectCancelDownload(_ sender: NSView?)
    func sessionActionsDidSelectShare(_ sender: NSView?)
}

class SessionActionsViewController: NSViewController {

    init() {
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private var disposeBag = DisposeBag()

    var viewModel: SessionViewModel? = nil {
        didSet {
            resetDownloadButton()
            updateBindings()
        }
    }

    weak var delegate: SessionActionsViewControllerDelegate?

    private lazy var favoriteButton: PUIButton = {
        let b = PUIButton(frame: .zero)

        b.image = #imageLiteral(resourceName: "favorite")
        b.alternateImage = #imageLiteral(resourceName: "favorite-filled")
        b.target = self
        b.action = #selector(toggleFavorite)
        b.isToggle = true
        b.shouldAlwaysDrawHighlighted = true

        return b
    }()

    private lazy var slidesButton: PUIButton = {
        let b = PUIButton(frame: .zero)

        b.image = #imageLiteral(resourceName: "slides")
        b.target = self
        b.action = #selector(showSlides)
        b.shouldAlwaysDrawHighlighted = true
        b.toolTip = "Open slides"

        return b
    }()

    private lazy var downloadButton: PUIButton = {
        let b = PUIButton(frame: .zero)

        b.image = #imageLiteral(resourceName: "download")
        b.target = self
        b.action = #selector(download)
        b.shouldAlwaysDrawHighlighted = true

        return b
    }()
    
    private lazy var calendarButton: PUIButton = {
        let b = PUIButton(frame: .zero)
        
        b.image = #imageLiteral(resourceName: "calendar")
        b.target = self
        b.action = #selector(addCalendar(_:))
        b.shouldAlwaysDrawHighlighted = true
        b.toolTip = "Add to Calendar"
        
        return b
    }()
    
    private lazy var downloadIndicator: NSProgressIndicator = {
        let pi = NSProgressIndicator(frame: NSRect(x: 0, y: 0, width: 24, height: 24))

        pi.style = .spinning
        pi.isIndeterminate = false
        pi.minValue = 0
        pi.maxValue = 1
        pi.doubleValue = 0
        pi.translatesAutoresizingMaskIntoConstraints = false
        pi.widthAnchor.constraint(equalToConstant: 24).isActive = true
        pi.heightAnchor.constraint(equalToConstant: 24).isActive = true
        pi.isHidden = true
        pi.addGestureRecognizer(NSClickGestureRecognizer(target: self, action: #selector(cancelDownload)))
        pi.toolTip = "Click to cancel"

        return pi
    }()

    private lazy var shareButton: PUIButton = {
        let b = PUIButton(frame: .zero)

        b.image = #imageLiteral(resourceName: "share")
        b.target = self
        b.action = #selector(share)
        b.shouldAlwaysDrawHighlighted = true
        b.sendsActionOnMouseDown = true
        b.toolTip = "Share session"

        return b
    }()

    private lazy var stackView: NSStackView = {
        let v = NSStackView(views: [
            self.slidesButton,
            self.favoriteButton,
            self.downloadButton,
            self.downloadIndicator,
            self.shareButton,
            self.calendarButton
            ])

        v.orientation = .horizontal
        v.spacing = 22
        v.alignment = .centerY
        v.distribution = .equalSpacing

        return v
    }()

    override func loadView() {
        view = stackView
        view.wantsLayer = true
        view.translatesAutoresizingMaskIntoConstraints = false

        view.setContentHuggingPriority(NSLayoutConstraint.Priority.defaultHigh, for: .horizontal)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        updateBindings()
    }

    private func updateBindings() {
        disposeBag = DisposeBag()

        guard let viewModel = viewModel else { return }

        slidesButton.isHidden = (viewModel.session.asset(of: .slides) == nil)
        calendarButton.isHidden = (viewModel.sessionInstance.startTime == viewModel.sessionInstance.endTime)
        
        viewModel.rxIsFavorite.subscribe(onNext: { [weak self] isFavorite in
            self?.favoriteButton.state = isFavorite ? .on : .off

            if isFavorite {
                self?.favoriteButton.toolTip = "Remove from favorites"
            } else {
                self?.favoriteButton.toolTip = "Add to favorites"
            }
        }).disposed(by: disposeBag)

        if let rxDownloadState = DownloadManager.shared.downloadStatusObservable(for: viewModel.session) {
            rxDownloadState.throttle(0.8, scheduler: MainScheduler.instance).subscribe(onNext: { [weak self] status in
                switch status {
                case .downloading(let progress):
                    self?.downloadIndicator.isHidden = false
                    self?.downloadButton.isHidden = true

                    if progress < 0 {
                        self?.downloadIndicator.isIndeterminate = true
                        self?.downloadIndicator.startAnimation(nil)
                    } else {
                        self?.downloadIndicator.isIndeterminate = false
                        self?.downloadIndicator.doubleValue = progress
                    }
                case .paused, .cancelled, .none, .failed:
                    self?.resetDownloadButton()
                    self?.downloadIndicator.isHidden = true
                    self?.downloadButton.isHidden = false
                case .finished:<commit_msg>Calendar button won't appear on events once they have finished<commit_after>//
//  SessionActionsViewController.swift
//  WWDC
//
//  Created by <NAME> on 06/05/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Cocoa
import PlayerUI
import ConfCore
import RxSwift
import RxCocoa

protocol SessionActionsViewControllerDelegate: class {

    func sessionActionsDidSelectSlides(_ sender: NSView?)
    func sessionActionsDidSelectFavorite(_ sender: NSView?)
    func sessionActionsDidSelectDownload(_ sender: NSView?)
    func sessionActionsDidSelectCalendar(_ sender: NSView?)
    func sessionActionsDidSelectDeleteDownload(_ sender: NSView?)
    func sessionActionsDidSelectCancelDownload(_ sender: NSView?)
    func sessionActionsDidSelectShare(_ sender: NSView?)
}

class SessionActionsViewController: NSViewController {

    init() {
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private var disposeBag = DisposeBag()

    var viewModel: SessionViewModel? = nil {
        didSet {
            resetDownloadButton()
            updateBindings()
        }
    }

    weak var delegate: SessionActionsViewControllerDelegate?

    private lazy var favoriteButton: PUIButton = {
        let b = PUIButton(frame: .zero)

        b.image = #imageLiteral(resourceName: "favorite")
        b.alternateImage = #imageLiteral(resourceName: "favorite-filled")
        b.target = self
        b.action = #selector(toggleFavorite)
        b.isToggle = true
        b.shouldAlwaysDrawHighlighted = true

        return b
    }()

    private lazy var slidesButton: PUIButton = {
        let b = PUIButton(frame: .zero)

        b.image = #imageLiteral(resourceName: "slides")
        b.target = self
        b.action = #selector(showSlides)
        b.shouldAlwaysDrawHighlighted = true
        b.toolTip = "Open slides"

        return b
    }()

    private lazy var downloadButton: PUIButton = {
        let b = PUIButton(frame: .zero)

        b.image = #imageLiteral(resourceName: "download")
        b.target = self
        b.action = #selector(download)
        b.shouldAlwaysDrawHighlighted = true

        return b
    }()
    
    private lazy var calendarButton: PUIButton = {
        let b = PUIButton(frame: .zero)
        
        b.image = #imageLiteral(resourceName: "calendar")
        b.target = self
        b.action = #selector(addCalendar(_:))
        b.shouldAlwaysDrawHighlighted = true
        b.toolTip = "Add to Calendar"
        
        return b
    }()
    
    private lazy var downloadIndicator: NSProgressIndicator = {
        let pi = NSProgressIndicator(frame: NSRect(x: 0, y: 0, width: 24, height: 24))

        pi.style = .spinning
        pi.isIndeterminate = false
        pi.minValue = 0
        pi.maxValue = 1
        pi.doubleValue = 0
        pi.translatesAutoresizingMaskIntoConstraints = false
        pi.widthAnchor.constraint(equalToConstant: 24).isActive = true
        pi.heightAnchor.constraint(equalToConstant: 24).isActive = true
        pi.isHidden = true
        pi.addGestureRecognizer(NSClickGestureRecognizer(target: self, action: #selector(cancelDownload)))
        pi.toolTip = "Click to cancel"

        return pi
    }()

    private lazy var shareButton: PUIButton = {
        let b = PUIButton(frame: .zero)

        b.image = #imageLiteral(resourceName: "share")
        b.target = self
        b.action = #selector(share)
        b.shouldAlwaysDrawHighlighted = true
        b.sendsActionOnMouseDown = true
        b.toolTip = "Share session"

        return b
    }()

    private lazy var stackView: NSStackView = {
        let v = NSStackView(views: [
            self.slidesButton,
            self.favoriteButton,
            self.downloadButton,
            self.downloadIndicator,
            self.shareButton,
            self.calendarButton
            ])

        v.orientation = .horizontal
        v.spacing = 22
        v.alignment = .centerY
        v.distribution = .equalSpacing

        return v
    }()

    override func loadView() {
        view = stackView
        view.wantsLayer = true
        view.translatesAutoresizingMaskIntoConstraints = false

        view.setContentHuggingPriority(NSLayoutConstraint.Priority.defaultHigh, for: .horizontal)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        updateBindings()
    }

    private func updateBindings() {
        disposeBag = DisposeBag()

        guard let viewModel = viewModel else { return }

        slidesButton.isHidden = (viewModel.session.asset(of: .slides) == nil)
        calendarButton.isHidden = (viewModel.sessionInstance.endTime < Date())
        
        viewModel.rxIsFavorite.subscribe(onNext: { [weak self] isFavorite in
            self?.favoriteButton.state = isFavorite ? .on : .off

            if isFavorite {
                self?.favoriteButton.toolTip = "Remove from favorites"
            } else {
                self?.favoriteButton.toolTip = "Add to favorites"
            }
        }).disposed(by: disposeBag)

        if let rxDownloadState = DownloadManager.shared.downloadStatusObservable(for: viewModel.session) {
            rxDownloadState.throttle(0.8, scheduler: MainScheduler.instance).subscribe(onNext: { [weak self] status in
                switch status {
                case .downloading(let progress):
                    self?.downloadIndicator.isHidden = false
                    self?.downloadButton.isHidden = true

                    if progress < 0 {
                        self?.downloadIndicator.isIndeterminate = true
                        self?.downloadIndicator.startAnimation(nil)
                    } else {
                        self?.downloadIndicator.isIndeterminate = false
                        self?.downloadIndicator.doubleValue = progress
                    }
                case .paused, .cancelled, .none, .failed:
                    self?.resetDownloadButton()
                    self?.downloadIndicator.isHidden = true
                    self?.downloadButton.isHidden = false
                case .finished:
{'fraction_non_alphanumeric': 0.08705519980546324, 'fraction_numerical': 0.00453919105130907, 'mean_word_length': 2.1522738886050075, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2750870', 'n_tokens_mistral': 3445, 'n_tokens_neox': 3241, 'n_words': 758}