Skip to content

Commit

Permalink
Video loading
Browse files Browse the repository at this point in the history
  • Loading branch information
twocentstudios committed Jun 11, 2017
1 parent f0aae48 commit 8c414fa
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions tinykittenstv/VideoViewController.swift
Expand Up @@ -8,6 +8,8 @@

import UIKit
import ReactiveSwift
import ReactiveCocoa
import Result
import XCDYouTubeKit
import AVFoundation
import Mortar
Expand All @@ -25,9 +27,11 @@ final class VideoViewController: UIViewController {

lazy var playerView: PlayerView = PlayerView()
lazy var descriptionView: VideoDescriptionView = VideoDescriptionView()
lazy var loadingView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)

let userPlayState = MutableProperty(UserPlayState.play)
let viewState = MutableProperty(ViewState.inactive)
private let loading: Property<Bool>
private let player: Property<AVPlayer?>

private let fetchAction: Action<(), XCDYouTubeVideo, NSError>
Expand All @@ -41,8 +45,7 @@ final class VideoViewController: UIViewController {
.start(on: QueueScheduler())
}

// TODO: loading/error?
// let loadings = fetchAction.isExecuting.signal.filter({ $0 }).map({ _ in ??? })
// TODO: error?
// let errors = fetchAction.errors.map { $0 }
let values = fetchAction.values
.map { (video: XCDYouTubeVideo) -> URL? in
Expand All @@ -57,6 +60,18 @@ final class VideoViewController: UIViewController {

self.player = ReactiveSwift.Property(initial: nil, then: values)

let infoLoading = fetchAction.isExecuting.producer.prefix(value: false)
let playerUnavailable = player.producer.map({ $0 == nil }).prefix(value: false)
let playerLoading = player.producer
.map { $0?.currentItem }
.skipNil()
.flatMap(.latest) { (playerItem: AVPlayerItem) -> Signal<Bool, NoError> in
return playerItem.reactive.signal(forKeyPath: #keyPath(AVPlayerItem.isPlaybackBufferEmpty)).skipNil().map({ $0 as! Bool })
}
.prefix(value: false)
let combinedLoading = SignalProducer.combineLatest(infoLoading, playerUnavailable, playerLoading).map({ $0 || $1 || $2 })
self.loading = Property<Bool>(initial: false, then: combinedLoading)

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

self.player.producer
Expand Down Expand Up @@ -97,12 +112,16 @@ final class VideoViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

loadingView.color = Color.gray60

view |+| [
playerView,
loadingView,
descriptionView
]

[playerView, descriptionView] |=| view
loadingView.m_center |=| view

descriptionView.viewData = VideoDescriptionViewData(title: videoInfo.title, description: videoInfo.description)

Expand All @@ -120,5 +139,10 @@ final class VideoViewController: UIViewController {
descriptionView.alpha = toAlpha
})
}

loading.producer
.startWithValues { [weak loadingView] (isLoading: Bool) in
isLoading ? loadingView?.startAnimating() : loadingView?.stopAnimating()
}
}
}

0 comments on commit 8c414fa

Please sign in to comment.