<commit_before>//
//  PlayerManager.swift
//  CocoaSwiftPlayer
//
//  Created by <NAME> on 3/2/2016.
//  Copyright © 2016 STAY REAL. All rights reserved.
//

import Cocoa
import AVFoundation
import RealmSwift

class PlayerManager: NSObject, AVAudioPlayerDelegate {
    
    static var sharedManager = PlayerManager()
    
    var isPlaying: Bool {
        if let player = player {
            return player.playing
        } else {
            return false
        }
    }
    
    var player: AVAudioPlayer?
    
    var currentSong: Song? = nil {
        didSet {
            if let currentSong = currentSong {
                if currentSong.location != player?.url?.path {
                    player = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: currentSong.location))
                    player?.volume = volume
                }
            }
        }
    }
    var currentPlayList: [Song] = []
    
    var isRepeated = false
    var isShuffle = false
    var volume: Float = 0.5
    
    // MARK: - Lifecycle Methods
    
    override init() {
        super.init()
    }
    
    deinit {
        
    }
    
    // MARK: - Player Control
    
    func play() {
        if isPlaying {
            pause()
            return
        }
        
        if currentPlayList.isEmpty {
            loadAllSongs()
        }
        
        if currentSong == nil {
            currentSong = currentPlayList[0]
        }
        
        player?.play()
    }
    
    func pause() {
        player?.pause()
    }
    
    func next() {
        
    }
    
    func rewind() {
        
    }
    
    func shuffle() {
        
    }
    
    func repeatPlaylist() {
        
    }
    
    private func loadAllSongs() {
        let realm = try! Realm()
        currentPlayList = realm.objects(Song).map { song in return song}
    }

}<commit_msg>Implements next/rewind features<commit_after>//
//  PlayerManager.swift
//  CocoaSwiftPlayer
//
//  Created by <NAME> on 3/2/2016.
//  Copyright © 2016 STAY REAL. All rights reserved.
//

import Cocoa
import AVFoundation
import RealmSwift

class PlayerManager: NSObject, AVAudioPlayerDelegate {
    
    static var sharedManager = PlayerManager()
    
    var isPlaying: Bool {
        if let player = player {
            return player.playing
        } else {
            return false
        }
    }
    
    var player: AVAudioPlayer?
    
    var currentIndex: Int?
    var currentSong: Song? = nil {
        didSet {
            if let currentSong = currentSong {
                currentIndex = currentPlayList.indexOf({ song -> Bool in
                    return song.location == currentSong.location
                })
                
                if currentSong.location != player?.url?.path {
                    player = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: currentSong.location))
                    player?.volume = volume
                }
            } else {
                stop()
                player = nil
            }
        }
    }
    var currentPlayList: [Song] = []
    
    var isRepeated = false
    var isShuffle = false
    var volume: Float = 0.5
    
    // MARK: - Lifecycle Methods
    
    override init() {
        super.init()
    }
    
    deinit {
        
    }
    
    // MARK: - Player Control
    
    func play() {
        if isPlaying {
            pause()
            return
        }
        
        if currentPlayList.isEmpty {
            loadAllSongs()
        }
        
        if currentSong == nil {
            currentSong = currentPlayList[0]
        }
        
        player?.play()
    }
    
    func pause() {
        player?.pause()
    }
    
    func stop() {
        player?.stop()
    }
    
    func next() {
        guard let currentIndex = currentIndex else { return }
        
        if currentIndex == currentPlayList.count - 1 {
            currentSong = nil
        } else {
            currentSong = currentPlayList[currentIndex + 1]
            play()
        }
    }
    
    func rewind() {
        guard let currentIndex = currentIndex else { return }
        
        if currentIndex == 0 {
            currentSong = nil
        } else {
            currentSong = currentPlayList[currentIndex - 1]
            play()
        }
    }
    
    func shuffle() {
        
    }
    
    func repeatPlaylist() {
        
    }
    
    private func loadAllSongs() {
        let realm = try! Realm()
        currentPlayList = realm.objects(Song).map { song in return song}
    }

}
{'fraction_non_alphanumeric': 0.08674592690444738, 'fraction_numerical': 0.0066050198150594455, 'mean_word_length': 1.2932862190812722, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '6261955', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1231, 'n_words': 305}