From 7d95618ff52b060e03869548e94be4bd4f18a023 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Sat, 11 Jul 2020 17:41:41 +0200 Subject: [PATCH] Allow track downloading from the search results. --- .../apognu/otter/activities/SearchActivity.kt | 31 +++++++++++++++++-- .../apognu/otter/adapters/SearchAdapter.kt | 10 ++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt b/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt index e6a553f..d0d7381 100644 --- a/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt +++ b/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt @@ -10,10 +10,13 @@ import com.github.apognu.otter.adapters.SearchAdapter import com.github.apognu.otter.fragments.AlbumsFragment import com.github.apognu.otter.fragments.ArtistsFragment import com.github.apognu.otter.repositories.* -import com.github.apognu.otter.utils.Album -import com.github.apognu.otter.utils.Artist -import com.github.apognu.otter.utils.untilNetwork +import com.github.apognu.otter.utils.* +import com.google.android.exoplayer2.offline.Download import kotlinx.android.synthetic.main.activity_search.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.net.URLEncoder import java.util.* @@ -44,6 +47,14 @@ class SearchActivity : AppCompatActivity() { override fun onResume() { super.onResume() + lifecycleScope.launch(Dispatchers.IO) { + EventBus.get().collect { message -> + when (message) { + is Event.DownloadChanged -> refreshDownloadedTrack(message.download) + } + } + } + artistsRepository = ArtistsSearchRepository(this@SearchActivity, "") albumsRepository = AlbumsSearchRepository(this@SearchActivity, "") tracksRepository = TracksSearchRepository(this@SearchActivity, "") @@ -114,6 +125,20 @@ class SearchActivity : AppCompatActivity() { } } + private suspend fun refreshDownloadedTrack(download: Download) { + if (download.state == Download.STATE_COMPLETED) { + download.getMetadata()?.let { info -> + adapter.tracks.withIndex().associate { it.value to it.index }.filter { it.key.id == info.id }.toList().getOrNull(0)?.let { match -> + log(match) + withContext(Dispatchers.Main) { + adapter.tracks[match.second].downloaded = true + adapter.notifyItemChanged(adapter.getPositionOf(SearchAdapter.ResultType.Track, match.second)) + } + } + } + } + } + inner class SearchResultClickListener : SearchAdapter.OnSearchResultClickListener { override fun onArtistClick(holder: View?, artist: Artist) { ArtistsFragment.openAlbums(this@SearchActivity, artist) diff --git a/app/src/main/java/com/github/apognu/otter/adapters/SearchAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/SearchAdapter.kt index 48c8d76..f146750 100644 --- a/app/src/main/java/com/github/apognu/otter/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/apognu/otter/adapters/SearchAdapter.kt @@ -214,6 +214,7 @@ class SearchAdapter(private val context: Context?, private val listener: OnSearc when (it.itemId) { R.id.track_add_to_queue -> CommandBus.send(Command.AddToQueue(listOf(track))) R.id.track_play_next -> CommandBus.send(Command.PlayNext(track)) + R.id.track_pin -> CommandBus.send(Command.PinTrack(track)) R.id.queue_remove -> CommandBus.send(Command.RemoveFromQueue(track)) } @@ -228,6 +229,15 @@ class SearchAdapter(private val context: Context?, private val listener: OnSearc } } + fun getPositionOf(type: ResultType, position: Int): Int { + return when (type) { + ResultType.Artist -> position + 1 + ResultType.Album -> position + artists.size + 2 + ResultType.Track -> artists.size + albums.size + SECTION_COUNT + position + else -> 0 + } + } + inner class ViewHolder(view: View, val context: Context?) : RecyclerView.ViewHolder(view), View.OnClickListener { val handle = view.handle val cover = view.cover