<commit_before>//
//  AppCoordinator+SessionTableViewContextMenuActions.swift
//  WWDC
//
//  Created by <NAME> on 6/11/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Cocoa
import RealmSwift
import RxSwift
import ConfCore
import PlayerUI
import EventKit

extension AppCoordinator: SessionsTableViewControllerDelegate  {
    
    func sessionTableViewContextMenuActionWatch(viewModels: [SessionViewModel]) {
        backgroundUpdate(objects: viewModels.map({ $0.session })) { session in
            if let instance = session.instances.first {
                guard !instance.isCurrentlyLive else { return }
                
                guard instance.type == .session || instance.type == .video else { return }
            }
            
            session.setCurrentPosition(1, 1)
        }
    }
    
    func sessionTableViewContextMenuActionUnWatch(viewModels: [SessionViewModel]) {
        backgroundUpdate(objects: viewModels.map({ $0.session })) { session in
            session.resetProgress()
        }
    }
    
    func sessionTableViewContextMenuActionFavorite(viewModels: [SessionViewModel]) {
        backgroundUpdate(objects: viewModels.map({ $0.session })) { session in
            session.favorites.append(Favorite())
        }
    }
    
    func sessionTableViewContextMenuActionRemoveFavorite(viewModels: [SessionViewModel]) {
        backgroundUpdate(objects: viewModels.map({ $0.session })) { session in
            session.favorites.removeAll()
        }
    }
    
    func sessionTableViewContextMenuActionDownload(viewModels: [SessionViewModel]) {
        viewModels.forEach { viewModel in
            guard let videoAsset = viewModel.session.assets.filter({ $0.assetType == .hdVideo }).first else { return }
            
            DownloadManager.shared.download(videoAsset)
        }
    }
    
    func sessionTableViewContextMenuActionCancelDownload(viewModels: [SessionViewModel]) {
        viewModels.forEach { viewModel in
            guard let videoAsset = viewModel.session.assets.filter({ $0.assetType == .hdVideo }).first else { return }
            
            guard DownloadManager.shared.isDownloading(videoAsset.remoteURL) else { return }
                
            DownloadManager.shared.deleteDownload(for: videoAsset)
        }
    }
    
    private func backgroundUpdate<T: ThreadConfined>(objects inputObjects: [T], updateBlock: @escaping (T) -> Void) {
        let safeReferences = inputObjects.map({ ThreadSafeReference(to: $0) })
        let config = storage.realmConfig
        
        DispatchQueue.global(qos: .userInitiated).async {
            do {
                let realm = try Realm(configuration: config)
                let objects = safeReferences.flatMap({ realm.resolve($0) })
                
                try realm.write {
                    objects.forEach(updateBlock)
                }<commit_msg>Added warning when trying to download lots of videos at once<commit_after>//
//  AppCoordinator+SessionTableViewContextMenuActions.swift
//  WWDC
//
//  Created by <NAME> on 6/11/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Cocoa
import RealmSwift
import RxSwift
import ConfCore
import PlayerUI
import EventKit

extension AppCoordinator: SessionsTableViewControllerDelegate  {
    
    func sessionTableViewContextMenuActionWatch(viewModels: [SessionViewModel]) {
        backgroundUpdate(objects: viewModels.map({ $0.session })) { session in
            if let instance = session.instances.first {
                guard !instance.isCurrentlyLive else { return }
                
                guard instance.type == .session || instance.type == .video else { return }
            }
            
            session.setCurrentPosition(1, 1)
        }
    }
    
    func sessionTableViewContextMenuActionUnWatch(viewModels: [SessionViewModel]) {
        backgroundUpdate(objects: viewModels.map({ $0.session })) { session in
            session.resetProgress()
        }
    }
    
    func sessionTableViewContextMenuActionFavorite(viewModels: [SessionViewModel]) {
        backgroundUpdate(objects: viewModels.map({ $0.session })) { session in
            session.favorites.append(Favorite())
        }
    }
    
    func sessionTableViewContextMenuActionRemoveFavorite(viewModels: [SessionViewModel]) {
        backgroundUpdate(objects: viewModels.map({ $0.session })) { session in
            session.favorites.removeAll()
        }
    }
    
    func sessionTableViewContextMenuActionDownload(viewModels: [SessionViewModel]) {
        if viewModels.count > 5 {
            // asking to download many videos, warn
            let alert = WWDCAlert.create()
            
            alert.messageText = "Download several videos"
            alert.informativeText = "You're about to download \(viewModels.count) videos. This can consume several gigabytes of internet bandwidth and disk space. Are you sure you want to download all \(viewModels.count) videos at once?"
            
            alert.addButton(withTitle: "No")
            alert.addButton(withTitle: "Yes")
            
            enum Choice: Int {
                case yes = 1001
                case no = 1000
            }
            
            guard let choice = Choice(rawValue: alert.runModal()) else { return }
            
            guard case .yes = choice else { return }
        }
        
        viewModels.forEach { viewModel in
            guard let videoAsset = viewModel.session.assets.filter({ $0.assetType == .hdVideo }).first else { return }
            
            DownloadManager.shared.download(videoAsset)
        }
    }
    
    func sessionTableViewContextMenuActionCancelDownload(viewModels: [SessionViewModel]) {
        viewModels.forEach { viewModel in
            guard let videoAsset = viewModel.session.assets.filter({ $0.assetType == .hdVideo }).first else { return }
            
            guard DownloadManager.shared.isDownloading(videoAsset.remoteURL) else { return }
                
            DownloadManager.shared.deleteDownload(for: videoAsset)
        }
    }
    
    private func backgroundUpdate<T: ThreadConfined>(objects inputObjects: [T], updateBlock: @escaping (T) -> Void) {
        let safeReferences = inputObjects.map({ ThreadSafeReference(to: $0) })
        let config = storage.realmConfig
        
        DispatchQueue.global(qos: .userInitiated).async {
            do {
                let realm = try Realm(configuration: config)
                let objects = safeReferences.flatMap({ realm.resolve($0) })
                
                try realm.write {
                    objects.forEach(updateBlock)
                }
{'fraction_non_alphanumeric': 0.08956339712918661, 'fraction_numerical': 0.007027511961722488, 'mean_word_length': 2.2096928982725528, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5998401', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1652, 'n_words': 386}