Otter-App-Android-Funkwhale/app/src/main/java/com/github/apognu/otter/fragments/PlaylistTracksFragment.kt

166 lines
5.1 KiB
Kotlin
Raw Normal View History

2019-08-19 16:50:33 +02:00
package com.github.apognu.otter.fragments
import android.os.Bundle
2020-06-13 16:45:58 +02:00
import android.view.Gravity
2019-08-19 16:50:33 +02:00
import android.view.View
2020-06-13 16:45:58 +02:00
import androidx.appcompat.widget.PopupMenu
2019-08-19 16:50:33 +02:00
import androidx.core.os.bundleOf
2020-07-13 23:32:42 +02:00
import androidx.lifecycle.LiveData
import androidx.lifecycle.lifecycleScope
2020-07-13 23:32:42 +02:00
import androidx.lifecycle.observe
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.PlaylistTracksAdapter
2020-07-13 23:32:42 +02:00
import com.github.apognu.otter.models.api.FunkwhalePlaylistTrack
import com.github.apognu.otter.models.dao.PlaylistEntity
import com.github.apognu.otter.repositories.FavoritesRepository
2019-08-19 16:50:33 +02:00
import com.github.apognu.otter.repositories.PlaylistTracksRepository
import com.github.apognu.otter.utils.*
2020-07-13 23:32:42 +02:00
import com.github.apognu.otter.viewmodels.PlayerStateViewModel
import com.github.apognu.otter.viewmodels.PlaylistViewModel
import com.github.apognu.otter.models.domain.Track
2019-08-19 16:50:33 +02:00
import com.squareup.picasso.Picasso
2020-06-14 00:42:45 +02:00
import jp.wasabeef.picasso.transformations.RoundedCornersTransformation
2019-08-19 16:50:33 +02:00
import kotlinx.android.synthetic.main.fragment_tracks.*
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch
2020-07-13 23:32:42 +02:00
class PlaylistTracksFragment : LiveOtterFragment<FunkwhalePlaylistTrack, Track, PlaylistTracksAdapter>() {
override lateinit var liveData: LiveData<List<Track>>
2019-08-19 16:50:33 +02:00
override val viewRes = R.layout.fragment_tracks
override val recycler: RecyclerView get() = tracks
lateinit var favoritesRepository: FavoritesRepository
2020-07-13 23:32:42 +02:00
var playlistId = 0
var playlistName = ""
2019-08-19 16:50:33 +02:00
companion object {
2020-07-13 23:32:42 +02:00
fun new(playlist: PlaylistEntity): PlaylistTracksFragment {
2019-08-19 16:50:33 +02:00
return PlaylistTracksFragment().apply {
arguments = bundleOf(
2020-07-13 23:32:42 +02:00
"playlistId" to playlist.id,
"playlistName" to playlist.name
2019-08-19 16:50:33 +02:00
)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
arguments?.apply {
2020-07-13 23:32:42 +02:00
playlistId = getInt("playlistId")
playlistName = getString("playlistName") ?: "N/A"
2019-08-19 16:50:33 +02:00
}
2020-07-13 23:32:42 +02:00
liveData = PlaylistViewModel(playlistId).tracks
super.onCreate(savedInstanceState)
adapter = PlaylistTracksAdapter(context, FavoriteListener())
2020-07-13 23:32:42 +02:00
repository = PlaylistTracksRepository(context, playlistId)
favoritesRepository = FavoritesRepository(context)
2019-08-19 16:50:33 +02:00
2020-07-13 23:32:42 +02:00
PlayerStateViewModel.get().track.observe(this) { track ->
adapter.currentTrack = track
adapter.notifyDataSetChanged()
}
2019-08-19 16:50:33 +02:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
cover.visibility = View.INVISIBLE
covers.visibility = View.VISIBLE
artist.text = "Playlist"
2020-07-13 23:32:42 +02:00
title.text = playlistName
2019-08-19 16:50:33 +02:00
}
override fun onResume() {
super.onResume()
var coverHeight: Float? = null
scroller.setOnScrollChangeListener { _: View?, _: Int, scrollY: Int, _: Int, _: Int ->
if (coverHeight == null) {
coverHeight = covers.measuredHeight.toFloat()
}
covers.translationY = (scrollY / 2).toFloat()
coverHeight?.let { height ->
covers.alpha = (height - scrollY.toFloat()) / height
}
}
2019-08-19 16:50:33 +02:00
play.setOnClickListener {
2020-07-13 23:32:42 +02:00
CommandBus.send(Command.ReplaceQueue(adapter.data.shuffled()))
2019-08-19 16:50:33 +02:00
context.toast("All tracks were added to your queue")
}
2020-06-13 16:45:58 +02:00
context?.let { context ->
actions.setOnClickListener {
PopupMenu(context, actions, Gravity.START, R.attr.actionOverflowMenuStyle, 0).apply {
inflate(R.menu.album)
2019-08-19 16:50:33 +02:00
2020-06-13 16:45:58 +02:00
setOnMenuItemClickListener {
when (it.itemId) {
R.id.add_to_queue -> {
2020-07-13 23:32:42 +02:00
CommandBus.send(Command.AddToQueue(adapter.data))
2020-06-13 16:45:58 +02:00
context.toast("All tracks were added to your queue")
}
2020-07-13 23:32:42 +02:00
R.id.download -> CommandBus.send(Command.PinTracks(adapter.data))
2020-06-13 16:45:58 +02:00
}
true
}
show()
}
}
2019-08-19 16:50:33 +02:00
}
}
2020-07-13 23:32:42 +02:00
override fun onDataFetched(data: List<FunkwhalePlaylistTrack>) {
data.map { it.track.album }.toSet().map { it?.cover?.urls?.original }.take(4).forEachIndexed { index, url ->
2019-08-19 16:50:33 +02:00
val imageView = when (index) {
0 -> cover_top_left
1 -> cover_top_right
2 -> cover_bottom_left
3 -> cover_bottom_right
else -> cover_top_left
}
2020-06-14 00:42:45 +02:00
val corner = when (index) {
0 -> RoundedCornersTransformation.CornerType.TOP_LEFT
1 -> RoundedCornersTransformation.CornerType.TOP_RIGHT
2 -> RoundedCornersTransformation.CornerType.BOTTOM_LEFT
3 -> RoundedCornersTransformation.CornerType.BOTTOM_RIGHT
else -> RoundedCornersTransformation.CornerType.TOP_LEFT
}
imageView?.let { view ->
lifecycleScope.launch(Main) {
Picasso.get()
.maybeLoad(maybeNormalizeUrl(url))
2020-06-14 00:42:45 +02:00
.fit()
.centerCrop()
.transform(RoundedCornersTransformation(16, 0, corner))
.into(view)
}
}
2019-08-19 16:50:33 +02:00
}
}
inner class FavoriteListener : PlaylistTracksAdapter.OnFavoriteListener {
override fun onToggleFavorite(id: Int, state: Boolean) {
when (state) {
true -> favoritesRepository.addFavorite(id)
false -> favoritesRepository.deleteFavorite(id)
}
}
}
2019-08-19 16:50:33 +02:00
}