Added downloaded indicator on track rows.

This commit is contained in:
Antoine POPINEAU 2020-06-13 17:44:50 +02:00
parent 4127421132
commit a2caba8bd1
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
12 changed files with 80 additions and 17 deletions

View File

@ -60,16 +60,16 @@ class DownloadsAdapter(private val context: Context, private val listener: OnRef
holder.toggle.setOnClickListener { holder.toggle.setOnClickListener {
if (state.state == Download.STATE_DOWNLOADING) { if (state.state == Download.STATE_DOWNLOADING) {
DownloadService.sendSetStopReason(context, PinService::class.java, download.id, 1, false) DownloadService.sendSetStopReason(context, PinService::class.java, download.contentId, 1, false)
} else { } else {
DownloadService.sendSetStopReason(context, PinService::class.java, download.id, Download.STOP_REASON_NONE, false) DownloadService.sendSetStopReason(context, PinService::class.java, download.contentId, Download.STOP_REASON_NONE, false)
} }
listener.refresh() listener.refresh()
} }
holder.delete.setOnClickListener { holder.delete.setOnClickListener {
DownloadService.sendRemoveDownload(context, PinService::class.java, download.id, false) DownloadService.sendRemoveDownload(context, PinService::class.java, download.contentId, false)
listener.refresh() listener.refresh()
} }

View File

@ -94,6 +94,11 @@ class TracksAdapter(private val context: Context?, private val favoriteListener:
notifyItemChanged(position) notifyItemChanged(position)
} }
} }
when (track.downloaded) {
true -> holder.title.setCompoundDrawablesWithIntrinsicBounds(R.drawable.downloaded, 0, 0, 0)
false -> holder.title.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
}
} }
holder.actions.setOnClickListener { holder.actions.setOnClickListener {

View File

@ -2,8 +2,6 @@ package com.github.apognu.otter.fragments
import android.os.Bundle import android.os.Bundle
import android.view.Gravity import android.view.Gravity
import android.view.Menu
import android.view.MenuInflater
import android.view.View import android.view.View
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
@ -124,6 +122,18 @@ class TracksFragment : FunkwhaleFragment<Track, TracksAdapter>() {
when (message) { when (message) {
is Event.TrackPlayed -> refreshCurrentTrack() is Event.TrackPlayed -> refreshCurrentTrack()
is Event.RefreshTrack -> refreshCurrentTrack() is Event.RefreshTrack -> refreshCurrentTrack()
is Event.DownloadChanged -> {
(repository as? TracksRepository)?.let { repository ->
val downloaded = repository.getDownloadedIds() ?: listOf()
adapter.data = adapter.data.map {
it.downloaded = downloaded.contains(it.id)
it
}.toMutableList()
adapter.notifyDataSetChanged()
}
}
} }
} }
} }

View File

@ -4,10 +4,7 @@ import android.app.Notification
import android.content.Intent import android.content.Intent
import com.github.apognu.otter.Otter import com.github.apognu.otter.Otter
import com.github.apognu.otter.R import com.github.apognu.otter.R
import com.github.apognu.otter.utils.AppContext import com.github.apognu.otter.utils.*
import com.github.apognu.otter.utils.Request
import com.github.apognu.otter.utils.RequestBus
import com.github.apognu.otter.utils.Response
import com.google.android.exoplayer2.offline.* import com.google.android.exoplayer2.offline.*
import com.google.android.exoplayer2.scheduler.Scheduler import com.google.android.exoplayer2.scheduler.Scheduler
import com.google.android.exoplayer2.ui.DownloadNotificationHelper import com.google.android.exoplayer2.ui.DownloadNotificationHelper
@ -44,7 +41,16 @@ class PinService : DownloadService(AppContext.NOTIFICATION_DOWNLOADS) {
override fun getScheduler(): Scheduler? = null override fun getScheduler(): Scheduler? = null
override fun getForegroundNotification(downloads: MutableList<Download>?): Notification { override fun getForegroundNotification(downloads: MutableList<Download>?): Notification {
return DownloadNotificationHelper(this, AppContext.NOTIFICATION_CHANNEL_DOWNLOADS).buildProgressNotification(R.drawable.downloads, null, "Hello, world", downloads) val quantity = downloads?.size ?: 0
val description = resources.getQuantityString(R.plurals.downloads_description, quantity, quantity)
return DownloadNotificationHelper(this, AppContext.NOTIFICATION_CHANNEL_DOWNLOADS).buildProgressNotification(R.drawable.downloads, null, description, downloads)
}
override fun onDownloadChanged(download: Download?) {
super.onDownloadChanged(download)
EventBus.send(Event.DownloadChanged)
} }
private fun getDownloads() = manager.downloadIndex.getDownloads() private fun getDownloads() = manager.downloadIndex.getDownloads()

View File

@ -345,6 +345,7 @@ class PlayerService : Service() {
val url = mustNormalizeUrl(upload.listen_url) val url = mustNormalizeUrl(upload.listen_url)
val data = Gson().toJson( val data = Gson().toJson(
DownloadInfo( DownloadInfo(
track.id,
url, url,
track.title, track.title,
track.artist.name, track.artist.name,

View File

@ -1,11 +1,10 @@
package com.github.apognu.otter.repositories package com.github.apognu.otter.repositories
import android.content.Context import android.content.Context
import com.github.apognu.otter.utils.FunkwhaleResponse import com.github.apognu.otter.utils.*
import com.github.apognu.otter.utils.Track
import com.github.apognu.otter.utils.TracksCache
import com.github.apognu.otter.utils.TracksResponse
import com.github.kittinunf.fuel.gson.gsonDeserializerOf import com.github.kittinunf.fuel.gson.gsonDeserializerOf
import com.google.android.exoplayer2.offline.Download
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList import kotlinx.coroutines.flow.toList
@ -25,9 +24,30 @@ class TracksRepository(override val context: Context?, albumId: Int) : Repositor
.toList() .toList()
.flatten() .flatten()
val downloaded = getDownloadedIds() ?: listOf()
data.map { track -> data.map { track ->
track.favorite = favorites.contains(track.id) track.favorite = favorites.contains(track.id)
track.downloaded = downloaded.contains(track.id)
track track
}.sortedBy { it.position } }.sortedBy { it.position }
} }
suspend fun getDownloadedIds(): List<Int>? {
return RequestBus.send(Request.GetDownloads).wait<com.github.apognu.otter.utils.Response.Downloads>()?.let { response ->
val ids: MutableList<Int> = mutableListOf()
while (response.cursor.moveToNext()) {
val download = response.cursor.download
Gson().fromJson(String(download.request.data), DownloadInfo::class.java)?.let {
if (download.state == Download.STATE_COMPLETED) {
ids.add(it.id)
}
}
}
ids
}
}
} }

View File

@ -47,6 +47,7 @@ sealed class Event {
object QueueChanged : Event() object QueueChanged : Event()
object RadioStarted : Event() object RadioStarted : Event()
object ListingsChanged : Event() object ListingsChanged : Event()
object DownloadChanged : Event()
} }
sealed class Request(var channel: Channel<Response>? = null) { sealed class Request(var channel: Channel<Response>? = null) {

View File

@ -100,6 +100,7 @@ data class Track(
) : SearchResult { ) : SearchResult {
var current: Boolean = false var current: Boolean = false
var favorite: Boolean = false var favorite: Boolean = false
var downloaded: Boolean = false
data class Upload( data class Upload(
val listen_url: String, val listen_url: String,
@ -149,7 +150,8 @@ data class Radio(
) )
data class DownloadInfo( data class DownloadInfo(
val id: String, val id: Int,
val contentId: String,
val title: String, val title: String,
val artist: String, val artist: String,
var download: Download?) var download: Download?)

View File

@ -0,0 +1,8 @@
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="16dp"
android:height="16dp"
android:drawable="@drawable/downloads" />
</layer-list>

View File

@ -39,9 +39,11 @@
<TextView <TextView
android:id="@+id/title" android:id="@+id/title"
style="@style/AppTheme.ItemTitle" style="@style/AppTheme.ItemTitle"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="4dp" android:drawableStart="@drawable/downloaded"
android:drawableTint="@color/controlColor"
android:drawablePadding="8dp"
android:ellipsize="end" android:ellipsize="end"
android:lines="1" android:lines="1"
tools:text="Absolution" /> tools:text="Absolution" />

View File

@ -98,4 +98,8 @@
<item quantity="other">%1$d pistes • %2$s</item> <item quantity="other">%1$d pistes • %2$s</item>
</plurals> </plurals>
<string name="only_my_music">Ma musique seulement</string> <string name="only_my_music">Ma musique seulement</string>
<plurals name="downloads_description">
<item quantity="one">Téléchargement de %1$d piste</item>
<item quantity="other">Téléchargement de %1$d pistes</item>
</plurals>
</resources> </resources>

View File

@ -99,4 +99,8 @@
<item quantity="other">%1$d tracks • %2$s"</item> <item quantity="other">%1$d tracks • %2$s"</item>
</plurals> </plurals>
<string name="only_my_music">Only my music</string> <string name="only_my_music">Only my music</string>
<plurals name="downloads_description">
<item quantity="one">Downloading %1$d track</item>
<item quantity="other">Downloading %1$d tracks</item>
</plurals>
</resources> </resources>