Skip to content

Commit

Permalink
Convert from event-based to mvc
Browse files Browse the repository at this point in the history
  • Loading branch information
gjedeer committed Jun 13, 2017
1 parent b2c4687 commit b28cc32
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 31 deletions.
11 changes: 10 additions & 1 deletion app/src/main/kotlin/com/github/funkyg/funkytunes/Model.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.funkyg.funkytunes

import android.view.View

data class Album(val title: String, val artist: String, val year: Int?, val image: Image) {
fun getQuery(): String {
// Exclude additions like "(Original Motion Picture Soundtrack)" or "(Deluxe Edition)" from
Expand All @@ -10,6 +12,13 @@ data class Album(val title: String, val artist: String, val year: Int?, val imag
}
}

data class Song(val name: String, val artist: String?, val image: Image, val duration: Int?)
data class Song(var name: String, var artist: String?, val image: Image, var duration: Int?, var isPlaying: Boolean = false, var isQueued: Boolean = false, var progress: Float = 0.0f) {
val songPlayingVisible: Int
get() = if (isPlaying && !isQueued) View.VISIBLE else View.GONE
val songProgressVisible: Int
get() = if (isQueued && !isPlaying && progress > 0.1) View.VISIBLE else View.GONE
val songLoadingVisible: Int
get() = if (isQueued && !isPlaying && progress <= 0.1) View.VISIBLE else View.GONE
}

data class Image(val url: String)
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class PlayingQueueActivity : BaseActivity(), PlaybackInterface {
private val Tag = "PlayingQueueActivity"

private lateinit var binding: ActivityPlayingQueueBinding
private val albumBindings = mutableMapOf<Int, ItemPlaylistBinding>()

private var adapter: LastAdapter? = null
private var currentPlaylist: List<Song>? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -40,7 +42,6 @@ class PlayingQueueActivity : BaseActivity(), PlaybackInterface {
super.onDestroy()
service!!.removePlaybackInterface(this)
service?.removePlaybackInterface(binding.bottomControl.root as PlaybackInterface)
albumBindings.clear()
}

override fun onPlayAlbum(album: Album) {
Expand All @@ -52,56 +53,75 @@ class PlayingQueueActivity : BaseActivity(), PlaybackInterface {
}

override fun onCancelAlbum() {
albumBindings.clear()
finish()
}

override fun onProgress(index: Int, progress: Int) {
if(albumBindings.get(index)?.songplaying?.visibility != View.VISIBLE) {
albumBindings.get(index)?.songplaying?.visibility = View.GONE
albumBindings.get(index)?.songloading?.visibility = View.GONE

albumBindings.get(index)?.songprogress?.setProgress(progress.toFloat())
albumBindings.get(index)?.songprogress?.visibility = View.VISIBLE
val playlist: List<Song>? = currentPlaylist
if (playlist == null) {
Log.v(Tag, "Progress received when playlist is null")
return
}
val song = playlist[index]
if (!song.isPlaying) {
song.isPlaying = false
song.isQueued = true
song.progress = progress.toFloat()

runOnUiThread {
binding.recycler.getAdapter().notifyDataSetChanged()
}
} else {
Log.v(Tag, "Progress received when song is playing")
}
}

override fun onPlaySong(song: Song, index: Int) {
for(binding in albumBindings.values) {
binding.songplaying.visibility = View.GONE
val playlist: List<Song>? = currentPlaylist
if (playlist != null) {
for(s in playlist) {
s.isPlaying = false
}
} else {
Log.v(Tag, "onPlaySong received when playlist is empty")
}
song.isPlaying = true
song.isQueued = false

runOnUiThread {
binding.recycler.getAdapter().notifyDataSetChanged()
}
albumBindings.get(index)?.songplaying?.visibility = View.VISIBLE
albumBindings.get(index)?.songloading?.visibility = View.GONE
albumBindings.get(index)?.songprogress?.visibility = View.GONE
Log.i(Tag, "Playing track $index")
Log.i(Tag, "Playing track $index, adapter: $adapter")
}

override fun onEnqueueTrack(index: Int) {
for(binding in albumBindings.values) {
binding.songloading.visibility = View.GONE
val playlist: List<Song>? = currentPlaylist
if (playlist == null) {
return
}
val song = playlist[index]
song.isPlaying = false
song.isQueued = true

runOnUiThread {
binding.recycler.getAdapter().notifyDataSetChanged()
}
albumBindings.get(index)?.songplaying?.visibility = View.GONE
albumBindings.get(index)?.songprogress?.visibility = View.GONE
albumBindings.get(index)?.songloading?.visibility = View.VISIBLE
albumBindings.get(index)?.songloading?.setIndeterminate(true)
Log.i(Tag, "Enqueued track $index")
}

override fun onPlaylistLoaded(playlist: List<Song>) {
runOnUiThread {
albumBindings.clear()
currentPlaylist = playlist

val itemBinder = object : ItemType<ItemPlaylistBinding>(R.layout.item_playlist) {
override fun onBind(holder: Holder<ItemPlaylistBinding>) {
albumBindings[holder.adapterPosition] = holder.binding

holder.binding.root.setOnClickListener {
service!!.playTrack(holder.adapterPosition)
}
}
}

LastAdapter(playlist, BR.song)
adapter = LastAdapter(playlist, BR.song)
.map(Song::class.java, itemBinder)
.into(binding.recycler)
binding.recycler.visibility = View.VISIBLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@ class MusicService : Service() {
mmr.setDataSource(file.absolutePath)
val title = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) ?: ""
val artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST) ?: ""
currentSongInfo = Song(title, artist, playlist!![currentTrack].image,
mediaPlayer?.duration?.div(1000))
val song = playlist!![currentTrack]
song.name = title
song.artist = artist
song.duration = mediaPlayer?.duration?.div(1000)
currentSongInfo = song

Handler(Looper.getMainLooper()).post({
playbackListeners.forEach { l ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class NotificationHandler(private val service: MusicService) : BroadcastReceiver
handler.removeCallbacks(StopForegroundRunnable)
}

if (currentSong == null) {
return
}

Thread(Runnable {
val notificationBuilder = NotificationCompat.Builder(service)

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/layout/item_playlist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
android:layout_alignParentTop="true"
android:layout_gravity="right"
android:src="@drawable/ic_play_arrow_accent_24dp"
android:visibility="gone" />
android:visibility="@{song.songPlayingVisible}" />

<ProgressBar
android:id="@+id/songloading"
Expand All @@ -60,7 +60,7 @@
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:indeterminate="true"
android:visibility="gone" />
android:visibility="@{song.songLoadingVisible}" />

<com.github.lzyzsd.circleprogress.DonutProgress
android:id="@+id/songprogress"
Expand All @@ -72,7 +72,7 @@
custom:donut_unfinished_stroke_width="3dp"
custom:donut_text_color="#00000000"
custom:donut_finished_color="#FE4083"
android:visibility="gone" />
android:visibility="@{song.songProgressVisible}" />

</RelativeLayout>

Expand Down

0 comments on commit b28cc32

Please sign in to comment.