Allow downloading whole albums.

This commit is contained in:
Antoine POPINEAU 2020-06-13 16:45:58 +02:00
parent 00fb833cfa
commit 4127421132
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
7 changed files with 95 additions and 41 deletions

View File

@ -1,7 +1,9 @@
package com.github.apognu.otter.fragments
import android.os.Bundle
import android.view.Gravity
import android.view.View
import androidx.appcompat.widget.PopupMenu
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.RecyclerView
import com.github.apognu.otter.R
@ -84,10 +86,28 @@ class PlaylistTracksFragment : FunkwhaleFragment<PlaylistTrack, PlaylistTracksAd
context.toast("All tracks were added to your queue")
}
queue.setOnClickListener {
CommandBus.send(Command.AddToQueue(adapter.data.map { it.track }))
context?.let { context ->
actions.setOnClickListener {
PopupMenu(context, actions, Gravity.START, R.attr.actionOverflowMenuStyle, 0).apply {
inflate(R.menu.album)
context.toast("All tracks were added to your queue")
setOnMenuItemClickListener {
when (it.itemId) {
R.id.add_to_queue -> {
CommandBus.send(Command.AddToQueue(adapter.data.map { it.track }))
context.toast("All tracks were added to your queue")
}
R.id.download -> CommandBus.send(Command.PinTracks(adapter.data.map { it.track }))
}
true
}
show()
}
}
}
}

View File

@ -1,7 +1,11 @@
package com.github.apognu.otter.fragments
import android.os.Bundle
import android.view.Gravity
import android.view.Menu
import android.view.MenuInflater
import android.view.View
import androidx.appcompat.widget.PopupMenu
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.RecyclerView
import com.github.apognu.otter.R
@ -89,10 +93,28 @@ class TracksFragment : FunkwhaleFragment<Track, TracksAdapter>() {
context.toast("All tracks were added to your queue")
}
queue.setOnClickListener {
CommandBus.send(Command.AddToQueue(adapter.data))
context?.let { context ->
actions.setOnClickListener {
PopupMenu(context, actions, Gravity.START, R.attr.actionOverflowMenuStyle, 0).apply {
inflate(R.menu.album)
context.toast("All tracks were added to your queue")
setOnMenuItemClickListener {
when (it.itemId) {
R.id.add_to_queue -> {
CommandBus.send(Command.AddToQueue(adapter.data))
context.toast("All tracks were added to your queue")
}
R.id.download -> CommandBus.send(Command.PinTracks(adapter.data))
}
true
}
show()
}
}
}
}

View File

@ -193,23 +193,8 @@ class PlayerService : Service() {
is Command.SetRepeatMode -> player.repeatMode = message.mode
is Command.PinTrack -> {
message.track.bestUpload()?.let { upload ->
val url = mustNormalizeUrl(upload.listen_url)
val data = Gson().toJson(
DownloadInfo(
url,
message.track.title,
message.track.artist.name,
null
)
).toByteArray()
DownloadRequest(url, DownloadRequest.TYPE_PROGRESSIVE, Uri.parse(url), Collections.emptyList(), null, data).also {
sendAddDownload(this@PlayerService, PinService::class.java, it, false)
}
}
}
is Command.PinTrack -> download(message.track)
is Command.PinTracks -> message.tracks.forEach { download(it) }
}
if (player.playWhenReady) {
@ -355,6 +340,24 @@ class PlayerService : Service() {
player.seekTo(duration.toLong())
}
private fun download(track: Track) {
track.bestUpload()?.let { upload ->
val url = mustNormalizeUrl(upload.listen_url)
val data = Gson().toJson(
DownloadInfo(
url,
track.title,
track.artist.name,
null
)
).toByteArray()
DownloadRequest(url, DownloadRequest.TYPE_PROGRESSIVE, Uri.parse(url), Collections.emptyList(), null, data).also {
sendAddDownload(this@PlayerService, PinService::class.java, it, false)
}
}
}
inner class PlayerEventListener : Player.EventListener {
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
super.onPlayerStateChanged(playWhenReady, playbackState)

View File

@ -31,6 +31,7 @@ sealed class Command {
class PlayTrack(val index: Int) : Command()
class PinTrack(val track: Track) : Command()
class PinTracks(val tracks: List<Track>) : Command()
}
sealed class Event {

View File

@ -180,16 +180,14 @@
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/queue"
style="@style/AppTheme.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
<ImageButton
android:id="@+id/actions"
style="@style/IconButton"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginEnd="16dp"
android:text="@string/playback_queue"
app:icon="@drawable/add" />
android:contentDescription="@string/alt_more_options"
android:src="@drawable/more" />
</LinearLayout>

View File

@ -183,16 +183,14 @@
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/queue"
style="@style/AppTheme.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
<ImageButton
android:id="@+id/actions"
style="@style/IconButton"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginEnd="16dp"
android:text="@string/playback_queue"
app:icon="@drawable/add" />
android:contentDescription="@string/alt_more_options"
android:src="@drawable/more" />
</LinearLayout>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add_to_queue"
android:title="@string/playback_queue" />
<item
android:id="@+id/download"
android:title="@string/playback_queue_download" />
</menu>