<commit_before>//
//  ViewController.swift
//  enpit-space
//
//  Created by <NAME> on 2015/07/17.
//  Copyright (c) 2015年 <NAME>. All rights reserved.
//

import UIKit
import MediaPlayer

class ViewController: UIViewController, MPMediaPickerControllerDelegate {

    @IBOutlet private weak var musicTitleLabel: UILabel!
    @IBOutlet private weak var teamLabel: UILabel!
    @IBOutlet private weak var keywordLabel: UILabel!
    
    private var mediaItemCollection: MPMediaItemCollection?
    private var centralManager = CentralManager.alloc()
    private var nexturnTimers: [NSTimer]?
    private let musicPlayer = MPMusicPlayerController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        configureNexturn()
        
        configurePayloadNotification()
        configureNexturnNotification()
        configureMusicNotification()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private func configurePayloadNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "parsePayload:", name: "didReceivePayloadNotification", object: nil)
    }
    
    private func configureMusicNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "playMusic:", name: "didReceiveMusicNotification", object: nil)        
    }

    private func configureNexturnNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "playNexturn:", name: "didReceiveNexturnNotification", object: nil)
    }
    
    private func configureMediaPicker() {
        let mediaPickerController = MPMediaPickerController()
        mediaPickerController.delegate = self
        mediaPickerController.allowsPickingMultipleItems = false
        presentViewController(mediaPickerController, animated: true, completion: nil)
    }
    
    private func configureNexturn() {
        let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        centralManager = CentralManager(delegate: self.centralManager, queue: queue, options: nil)
    }
    
    func mediaPickerDidCancel(mediaPicker: MPMediaPickerController!) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func mediaPicker(mediaPicker: MPMediaPickerController!, didPickMediaItems mediaItemCollection: MPMediaItemCollection!) {
        let item = mediaItemCollection.representativeItem
        
        let title = item.valueForProperty(MPMediaItemPropertyTitle) as! String
        let artistName = item.valueForProperty(MPMediaItemPropertyArtist) as! String
        let albumName = item.valueForProperty(MPMediaItemPropertyAlbumTitle) as! String
        
        musicTitleLabel.text = title
        self.mediaItemCollection = mediaItemCollection

        dismissViewControllerAnimated(true, completion: nil)<commit_msg>:heavy_plus_sign: 前回選択した曲を保存するようにした<commit_after>//
//  ViewController.swift
//  enpit-space
//
//  Created by <NAME> on 2015/07/17.
//  Copyright (c) 2015年 <NAME>. All rights reserved.
//

import UIKit
import MediaPlayer

class ViewController: UIViewController, MPMediaPickerControllerDelegate {

    @IBOutlet private weak var musicTitleLabel: UILabel!
    @IBOutlet private weak var teamLabel: UILabel!
    @IBOutlet private weak var keywordLabel: UILabel!
    
    private var mediaItemCollection: MPMediaItemCollection?
    private var centralManager = CentralManager.alloc()
    private var nexturnTimers: [NSTimer]?
    private let musicPlayer = MPMusicPlayerController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        configureMusic()
        configureNexturn()
        
        configurePayloadNotification()
        configureNexturnNotification()
        configureMusicNotification()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private func configurePayloadNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "parsePayload:", name: "didReceivePayloadNotification", object: nil)
    }
    
    private func configureMusicNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "playMusic:", name: "didReceiveMusicNotification", object: nil)        
    }

    private func configureNexturnNotification() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "playNexturn:", name: "didReceiveNexturnNotification", object: nil)
    }
    
    private func configureMediaPicker() {
        let mediaPickerController = MPMediaPickerController()
        mediaPickerController.delegate = self
        mediaPickerController.allowsPickingMultipleItems = false
        presentViewController(mediaPickerController, animated: true, completion: nil)
    }
    
    private func configureMusic() {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        if let mediaItemCollectionArchivedData = userDefaults.objectForKey("mediaItemCollectionArchivedData") as? NSData {
            let mediaItemCollection = NSKeyedUnarchiver.unarchiveObjectWithData(mediaItemCollectionArchivedData) as! MPMediaItemCollection
            let item = mediaItemCollection.representativeItem
            let title = item.valueForProperty(MPMediaItemPropertyTitle) as! String
            let artistName = item.valueForProperty(MPMediaItemPropertyArtist) as! String
            let albumName = item.valueForProperty(MPMediaItemPropertyAlbumTitle) as! String
            
            self.mediaItemCollection = mediaItemCollection
            musicTitleLabel.text = title
        }
    }
    
    private func configureNexturn() {
        let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        centralManager = CentralManager(delegate: self.centralManager, queue: queue, options: nil)
    }
    
    func mediaPickerDidCancel(mediaPicker: MPMediaPickerController!) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func mediaPicker(mediaPicker: MPMediaPickerController!, didPickMediaItems mediaItemCollection: MPMediaItemCollection!) {
        let item = mediaItemCollection.representativeItem
        
        let title = item.valueForProperty(MPMediaItemPropertyTitle) as! String
        let artistName = item.valueForProperty(MPMediaItemPropertyArtist) as! String
        let albumName = item.valueForProperty(MPMediaItemPropertyAlbumTitle) as! String
        
        musicTitleLabel.text = title
        self.mediaItemCollection = mediaItemCollection

        let userDefaults = NSUserDefaults.standardUserDefaults()
        let mediaItemCollectionArchivedData = NSKeyedArchiver.archivedDataWithRootObject(mediaItemCollection)
        userDefaults.setObject(mediaItemCollectionArchivedData, forKey: "mediaItemCollectionArchivedData")
        userDefaults.synchronize()
        
        dismissViewControllerAnimated(true, completion: nil)
{'fraction_non_alphanumeric': 0.07081722805080067, 'fraction_numerical': 0.003589177250138045, 'mean_word_length': 3.5883470550981635, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5194422', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1784, 'n_words': 420}