2019-08-19 16:50:33 +02:00
|
|
|
package com.github.apognu.otter.fragments
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2020-06-25 01:26:15 +02:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2019-08-19 16:50:33 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import com.github.apognu.otter.R
|
|
|
|
import com.github.apognu.otter.adapters.FavoritesAdapter
|
|
|
|
import com.github.apognu.otter.repositories.FavoritesRepository
|
2020-06-13 19:50:02 +02:00
|
|
|
import com.github.apognu.otter.repositories.TracksRepository
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.github.apognu.otter.utils.*
|
2020-06-14 20:40:08 +02:00
|
|
|
import com.google.android.exoplayer2.offline.Download
|
2019-08-19 16:50:33 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_favorites.*
|
2020-06-14 20:40:08 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
2019-08-19 16:50:33 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
2019-10-31 16:17:37 +01:00
|
|
|
import kotlinx.coroutines.flow.collect
|
2019-08-19 16:50:33 +02:00
|
|
|
import kotlinx.coroutines.launch
|
2020-06-14 20:40:08 +02:00
|
|
|
import kotlinx.coroutines.withContext
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2019-10-23 20:21:18 +02:00
|
|
|
class FavoritesFragment : FunkwhaleFragment<Track, FavoritesAdapter>() {
|
2019-08-19 16:50:33 +02:00
|
|
|
override val viewRes = R.layout.fragment_favorites
|
|
|
|
override val recycler: RecyclerView get() = favorites
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
|
|
adapter = FavoritesAdapter(context, FavoriteListener())
|
|
|
|
repository = FavoritesRepository(context)
|
|
|
|
|
|
|
|
watchEventBus()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
|
2020-06-25 01:26:15 +02:00
|
|
|
lifecycleScope.launch(IO) {
|
2019-08-19 16:50:33 +02:00
|
|
|
RequestBus.send(Request.GetCurrentTrack).wait<Response.CurrentTrack>()?.let { response ->
|
2020-06-14 20:40:08 +02:00
|
|
|
withContext(Main) {
|
|
|
|
adapter.currentTrack = response.track
|
|
|
|
adapter.notifyDataSetChanged()
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
2020-06-14 20:40:08 +02:00
|
|
|
|
|
|
|
refreshDownloadedTracks()
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
play.setOnClickListener {
|
2019-10-23 20:21:18 +02:00
|
|
|
CommandBus.send(Command.ReplaceQueue(adapter.data.shuffled()))
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun watchEventBus() {
|
2020-06-25 01:26:15 +02:00
|
|
|
lifecycleScope.launch(Main) {
|
2019-10-31 16:17:37 +01:00
|
|
|
EventBus.get().collect { message ->
|
2019-08-19 16:50:33 +02:00
|
|
|
when (message) {
|
2020-06-14 20:40:08 +02:00
|
|
|
is Event.DownloadChanged -> refreshDownloadedTrack(message.download)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 14:54:13 +02:00
|
|
|
|
2020-06-25 01:26:15 +02:00
|
|
|
lifecycleScope.launch(Main) {
|
2020-06-24 14:54:13 +02:00
|
|
|
CommandBus.get().collect { command ->
|
|
|
|
when (command) {
|
|
|
|
is Command.RefreshTrack -> refreshCurrentTrack(command.track)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 20:40:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private suspend fun refreshDownloadedTracks() {
|
|
|
|
val downloaded = TracksRepository.getDownloadedIds() ?: listOf()
|
2020-06-13 19:50:02 +02:00
|
|
|
|
2020-06-14 20:40:08 +02:00
|
|
|
withContext(Main) {
|
|
|
|
adapter.data = adapter.data.map {
|
|
|
|
it.downloaded = downloaded.contains(it.id)
|
|
|
|
it
|
|
|
|
}.toMutableList()
|
|
|
|
|
|
|
|
adapter.notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
}
|
2020-06-13 19:50:02 +02:00
|
|
|
|
2020-06-14 20:40:08 +02:00
|
|
|
private suspend fun refreshDownloadedTrack(download: Download) {
|
|
|
|
if (download.state == Download.STATE_COMPLETED) {
|
|
|
|
download.getMetadata()?.let { info ->
|
|
|
|
adapter.data.withIndex().associate { it.value to it.index }.filter { it.key.id == info.id }.toList().getOrNull(0)?.let { match ->
|
|
|
|
withContext(Main) {
|
|
|
|
adapter.data[match.second].downloaded = true
|
|
|
|
adapter.notifyItemChanged(match.second)
|
2020-06-13 19:50:02 +02:00
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 14:54:13 +02:00
|
|
|
private fun refreshCurrentTrack(track: Track?) {
|
|
|
|
track?.let {
|
2020-06-26 19:42:10 +02:00
|
|
|
adapter.currentTrack?.current = false
|
|
|
|
adapter.currentTrack = track.apply {
|
|
|
|
current = true
|
|
|
|
}
|
2020-06-24 14:54:13 +02:00
|
|
|
adapter.notifyDataSetChanged()
|
2020-06-01 21:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
inner class FavoriteListener : FavoritesAdapter.OnFavoriteListener {
|
|
|
|
override fun onToggleFavorite(id: Int, state: Boolean) {
|
2019-11-23 14:39:19 +01:00
|
|
|
(repository as? FavoritesRepository)?.let { repository ->
|
|
|
|
when (state) {
|
|
|
|
true -> repository.addFavorite(id)
|
|
|
|
false -> repository.deleteFavorite(id)
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|