<commit_before>//
//  AwesomeMediaMiniAudioView.swift
//  AwesomeMedia
//
//  Created by <NAME> on 4/25/18.
//

import UIKit
import AwesomeUIMagic

public class AwesomeMediaAudioPlayerView: UIView {

    @IBOutlet public weak var mainView: UIView!
    @IBOutlet public weak var coverImageView: UIImageView!
    @IBOutlet public weak var fullscreenButton: UIButton!
    @IBOutlet public weak var playButton: UIButton!
    @IBOutlet public weak var titleLabel: UILabel!
    @IBOutlet public weak var timeLabel: UILabel?
    
    // Public variables
    public var mediaParams = AwesomeMediaParams()
    public var isLocked = false
    public var canRemove = false
    public var trackingSource: AwesomeMediaTrackingSource = .audioMiniplayer
    
    // Private variables
    fileprivate var removeTimer: Timer?
    
    // Callbacks
    public var fullScreenCallback: FullScreenCallback?
    
    override public func awakeFromNib() {
        super.awakeFromNib()
        
        // add observers
        addObservers()
    }
    
    public func configure(withMediaParams mediaParams: AwesomeMediaParams, trackingSource: AwesomeMediaTrackingSource) {
        self.mediaParams = mediaParams
        self.trackingSource = trackingSource
        
        // Load cover Image
        loadCoverImage()
        
        // Set Media Information
        updateMediaInformation()
        
        // Update play button status
        updatePlayStatus()
        
        // check for loading state
        mainView.stopLoadingAnimation()
        if AwesomeMediaManager.shared.mediaIsLoading(withParams: mediaParams) {
            mainView.startLoadingAnimation()
        }
    }
    
    // MARK: - Events
    
    @IBAction func playButtonPressed(_ sender: Any) {
        AwesomeMediaPlayerType.type = .audio
        
        playButton.isSelected = !playButton.isSelected
        
        // Play/Stop media
        if playButton.isSelected {
            AwesomeMediaManager.shared.playMedia(withParams: self.mediaParams, viewController: parentViewController)
        } else {
            sharedAVPlayer.pause()
        }
        
        // tracking event
        if playButton.isSelected {
            track(event: .startedPlaying, source: trackingSource)
        } else {
            track(event: .stoppedPlaying, source: trackingSource)
        }
    }
    
    @IBAction func fullscreenButtonPressed(_ sender: Any) {
        fullScreenCallback?()
        
        track(event: .toggleFullscreen, source: trackingSource)
    }
    
}

// MARK: - Media Information

extension AwesomeMediaAudioPlayerView {
    
    public func updateMediaInformation() {
        titleLabel.text = mediaParams.title
        timeLabel?.text = mediaParams.duration.timeString.uppercased()
    }
    
    public func loadCoverImage() {
        guard let coverImageUrl = mediaParams.coverUrl else {
            return
        }
        
        // set the cover image
        coverImageView.setImage(coverImageUrl)
    }
    
    fileprivate func updatePlayStatus() {
        playButton.isSelected = sharedAVPlayer.isPlaying(withParams: mediaParams)
    }
}

// MARK: - Observers

extension AwesomeMediaAudioPlayerView: AwesomeMediaEventObserver {
    
    public func addObservers() {
        AwesomeMediaNotificationCenter.addObservers([.basic, .timedOut, .stopped], to: self)
    }
    
    public func removeObservers() {
        AwesomeMediaNotificationCenter.removeObservers(from: self)
    }
    
    public func startedPlaying() {
        guard sharedAVPlayer.isPlaying(withParams: mediaParams) else {
            return
        }
        
        // cancel remove timer
        cancelRemoveTimer()
        
        // change play button selection
        playButton.isSelected = true
        
        // update Control Center
        AwesomeMediaControlCenter.updateControlCenter(withParams: mediaParams)
        
        // remove media alert if present
        parentViewController?.removeAlertIfPresent()
    }
    
    public func pausedPlaying() {
        playButton.isSelected = sharedAVPlayer.isPlaying(withParams: mediaParams)
        
        // unlock buttons
        stoppedBuffering()
        
        // configure auto remove
        setupAutoRemove()
    }
    
    public func stoppedPlaying() {
        pausedPlaying()
        stoppedBuffering()
        finishedPlaying()
        
        // remove media alert if present
        parentViewController?.removeAlertIfPresent()
    }
    
    public func startedBuffering() {
        guard sharedAVPlayer.isCurrentItem(withParams: mediaParams) else {
            stoppedBuffering()
            return
        }
        
        mainView.startLoadingAnimation()
        
        lock(true, animated: true)
    }
    
    public func stoppedBuffering() {
        mainView.stopLoadingAnimation()
        
        lock(false, animated: true)
    }
    
    public func finishedPlaying() {
        playButton.isSelected = false
        
        // unlock buttons
        lock(false, animated: true)
        
        // configure auto remove
        setupAutoRemove()
    }
    
    public func timedOut() {
        parentViewController?.showMediaTimedOutAlert()
    }
}

// MARK: - States<commit_msg>observer added for hide mini player and when observed should remove the mini player.<commit_after>//
//  AwesomeMediaMiniAudioView.swift
//  AwesomeMedia
//
//  Created by <NAME> on 4/25/18.
//

import UIKit
import AwesomeUIMagic

public class AwesomeMediaAudioPlayerView: UIView {

    @IBOutlet public weak var mainView: UIView!
    @IBOutlet public weak var coverImageView: UIImageView!
    @IBOutlet public weak var fullscreenButton: UIButton!
    @IBOutlet public weak var playButton: UIButton!
    @IBOutlet public weak var titleLabel: UILabel!
    @IBOutlet public weak var timeLabel: UILabel?
    
    // Public variables
    public var mediaParams = AwesomeMediaParams()
    public var isLocked = false
    public var canRemove = false
    public var trackingSource: AwesomeMediaTrackingSource = .audioMiniplayer
    
    // Private variables
    fileprivate var removeTimer: Timer?
    
    // Callbacks
    public var fullScreenCallback: FullScreenCallback?
    
    override public func awakeFromNib() {
        super.awakeFromNib()
        
        // add observers
        addObservers()
    }
    
    public func configure(withMediaParams mediaParams: AwesomeMediaParams, trackingSource: AwesomeMediaTrackingSource) {
        self.mediaParams = mediaParams
        self.trackingSource = trackingSource
        
        // Load cover Image
        loadCoverImage()
        
        // Set Media Information
        updateMediaInformation()
        
        // Update play button status
        updatePlayStatus()
        
        // check for loading state
        mainView.stopLoadingAnimation()
        if AwesomeMediaManager.shared.mediaIsLoading(withParams: mediaParams) {
            mainView.startLoadingAnimation()
        }
    }
    
    // MARK: - Events
    
    @IBAction func playButtonPressed(_ sender: Any) {
        AwesomeMediaPlayerType.type = .audio
        
        playButton.isSelected = !playButton.isSelected
        
        // Play/Stop media
        if playButton.isSelected {
            AwesomeMediaManager.shared.playMedia(withParams: self.mediaParams, viewController: parentViewController)
        } else {
            sharedAVPlayer.pause()
        }
        
        // tracking event
        if playButton.isSelected {
            track(event: .startedPlaying, source: trackingSource)
        } else {
            track(event: .stoppedPlaying, source: trackingSource)
        }
    }
    
    @IBAction func fullscreenButtonPressed(_ sender: Any) {
        fullScreenCallback?()
        
        track(event: .toggleFullscreen, source: trackingSource)
    }
    
}

// MARK: - Media Information

extension AwesomeMediaAudioPlayerView {
    
    public func updateMediaInformation() {
        titleLabel.text = mediaParams.title
        timeLabel?.text = mediaParams.duration.timeString.uppercased()
    }
    
    public func loadCoverImage() {
        guard let coverImageUrl = mediaParams.coverUrl else {
            return
        }
        
        // set the cover image
        coverImageView.setImage(coverImageUrl)
    }
    
    fileprivate func updatePlayStatus() {
        playButton.isSelected = sharedAVPlayer.isPlaying(withParams: mediaParams)
    }
}

// MARK: - Observers

extension AwesomeMediaAudioPlayerView: AwesomeMediaEventObserver {
    
    public func addObservers() {
        AwesomeMediaNotificationCenter.addObservers([.basic, .timedOut, .stopped, .hideMiniPlayer], to: self)
    }
    
    public func removeObservers() {
        AwesomeMediaNotificationCenter.removeObservers(from: self)
    }
    
    public func startedPlaying() {
        guard sharedAVPlayer.isPlaying(withParams: mediaParams) else {
            return
        }
        
        // cancel remove timer
        cancelRemoveTimer()
        
        // change play button selection
        playButton.isSelected = true
        
        // update Control Center
        AwesomeMediaControlCenter.updateControlCenter(withParams: mediaParams)
        
        // remove media alert if present
        parentViewController?.removeAlertIfPresent()
    }
    
    public func pausedPlaying() {
        playButton.isSelected = sharedAVPlayer.isPlaying(withParams: mediaParams)
        
        // unlock buttons
        stoppedBuffering()
        
        // configure auto remove
        setupAutoRemove()
    }
    
    public func stoppedPlaying() {
        pausedPlaying()
        stoppedBuffering()
        finishedPlaying()
        
        // remove media alert if present
        parentViewController?.removeAlertIfPresent()
    }
    
    public func startedBuffering() {
        guard sharedAVPlayer.isCurrentItem(withParams: mediaParams) else {
            stoppedBuffering()
            return
        }
        
        mainView.startLoadingAnimation()
        
        lock(true, animated: true)
    }
    
    public func stoppedBuffering() {
        mainView.stopLoadingAnimation()
        
        lock(false, animated: true)
    }
    
    public func finishedPlaying() {
        playButton.isSelected = false
        
        // unlock buttons
        lock(false, animated: true)
        
        // configure auto remove
        setupAutoRemove()
    }
    
    public func timedOut() {
        parentViewController?.showMediaTimedOutAlert()
    }
    
    public func hideMiniPlayer(_ notification: NSNotification) {
        remove(animated: false)
    }
}

// MARK: - States
{'fraction_non_alphanumeric': 0.07145532871324219, 'fraction_numerical': 0.0009365049634763064, 'mean_word_length': 2.1216018707980124, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4553303', 'n_tokens_mistral': 2780, 'n_tokens_neox': 2712, 'n_words': 642}