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 {
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 {
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()
}
holder.delete.setOnClickListener {
DownloadService.sendRemoveDownload(context, PinService::class.java, download.id, false)
DownloadService.sendRemoveDownload(context, PinService::class.java, download.contentId, false)
listener.refresh()
}

View File

@ -94,6 +94,11 @@ class TracksAdapter(private val context: Context?, private val favoriteListener:
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 {

View File

@ -2,8 +2,6 @@ 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
@ -124,6 +122,18 @@ class TracksFragment : FunkwhaleFragment<Track, TracksAdapter>() {
when (message) {
is Event.TrackPlayed -> 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 com.github.apognu.otter.Otter
import com.github.apognu.otter.R
import com.github.apognu.otter.utils.AppContext
import com.github.apognu.otter.utils.Request
import com.github.apognu.otter.utils.RequestBus
import com.github.apognu.otter.utils.Response
import com.github.apognu.otter.utils.*
import com.google.android.exoplayer2.offline.*
import com.google.android.exoplayer2.scheduler.Scheduler
import com.google.android.exoplayer2.ui.DownloadNotificationHelper
@ -44,7 +41,16 @@ class PinService : DownloadService(AppContext.NOTIFICATION_DOWNLOADS) {
override fun getScheduler(): Scheduler? = null
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()

View File

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

View File

@ -1,11 +1,10 @@
package com.github.apognu.otter.repositories
import android.content.Context
import com.github.apognu.otter.utils.FunkwhaleResponse
import com.github.apognu.otter.utils.Track
import com.github.apognu.otter.utils.TracksCache
import com.github.apognu.otter.utils.TracksResponse
import com.github.apognu.otter.utils.*
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 kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList
@ -25,9 +24,30 @@ class TracksRepository(override val context: Context?, albumId: Int) : Repositor
.toList()
.flatten()
val downloaded = getDownloadedIds() ?: listOf()
data.map { track ->
track.favorite = favorites.contains(track.id)
track.downloaded = downloaded.contains(track.id)
track
}.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 RadioStarted : Event()
object ListingsChanged : Event()
object DownloadChanged : Event()
}
sealed class Request(var channel: Channel<Response>? = null) {

View File

@ -100,6 +100,7 @@ data class Track(
) : SearchResult {
var current: Boolean = false
var favorite: Boolean = false
var downloaded: Boolean = false
data class Upload(
val listen_url: String,
@ -149,7 +150,8 @@ data class Radio(
)
data class DownloadInfo(
val id: String,
val id: Int,
val contentId: String,
val title: String,
val artist: String,
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
android:id="@+id/title"
style="@style/AppTheme.ItemTitle"
android:layout_width="match_parent"
android:layout_width="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:lines="1"
tools:text="Absolution" />

View File

@ -98,4 +98,8 @@
<item quantity="other">%1$d pistes • %2$s</item>
</plurals>
<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>

View File

@ -99,4 +99,8 @@
<item quantity="other">%1$d tracks • %2$s"</item>
</plurals>
<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>