Last active
July 11, 2017 12:17
-
-
Save clackbib/21add57d617aa4d8f7d22f23206d1a18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PanelHandleViewModel { | |
// Gives us player status information | |
private lateinit var player: Player | |
// Multicasts emissions of what's currently selected for playback. | |
private lateinit var provider: PodcastProvider | |
fun handleMetaState(): Observable<PlayerHandleMetaState> { | |
return provider.currentlyPlaying() | |
.map { podcast -> | |
//Convert your model into information relevant to the View. | |
PlayerHandleMetaState( | |
podcast.title, | |
podcast.author, | |
podcast.albumArtUrl, | |
podcast.accentColor) | |
} | |
} | |
fun handleIconState(): Observable<PlayerHandleIconState> { | |
val icon = player.state() | |
.map { state -> if (state == Player.State.PLAYING) R.drawable.ic_pause else R.drawable.ic_play } | |
val color = provider.currentlyPlaying() | |
.map { podcast -> podcast.accentColor } | |
//Combine both data streams, since a change from either source needs to result into a UI update. | |
return Observable.combineLatest(icon, color, BiFunction { i, c -> PlayerHandleIconState(i, c) }) | |
} | |
fun handleProgress(): Observable<PlayerHandleProgressState> { | |
val timeElapsed = player.timeElapsed() | |
val trackLength = provider.currentlyPlaying().map { podcast -> podcast.length } | |
//Make a point to keep any sort of formatting, control flow, or calculation out of the View, since it's easier to test. | |
return Observable.combineLatest(timeElapsed, trackLength, | |
BiFunction { elapsed, duration -> PlayerHandleProgressState(elapsed.times(100).div(duration)) }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment