Skip to content

Commit

Permalink
playback: temp revert swipe to next behavior
Browse files Browse the repository at this point in the history
It's too buggy to be in a release currently, I have to disable it for
now.
  • Loading branch information
OxygenCobalt committed Nov 12, 2023
1 parent 917e6c0 commit df7ec27
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,18 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.widget.Toolbar
import androidx.core.view.updatePadding
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import dagger.hilt.android.AndroidEntryPoint
import java.lang.reflect.Field
import kotlin.math.abs
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.FragmentPlaybackPanelBinding
import org.oxycblt.auxio.detail.DetailViewModel
import org.oxycblt.auxio.list.ListViewModel
import org.oxycblt.auxio.list.adapter.UpdateInstructions
import org.oxycblt.auxio.music.MusicParent
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.playback.queue.QueueViewModel
import org.oxycblt.auxio.music.resolveNames
import org.oxycblt.auxio.playback.state.RepeatMode
import org.oxycblt.auxio.playback.ui.PlaybackPagerAdapter
import org.oxycblt.auxio.playback.ui.StyledSeekBar
import org.oxycblt.auxio.ui.ViewBindingFragment
import org.oxycblt.auxio.util.collectImmediately
import org.oxycblt.auxio.util.lazyReflectedField
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.overrideOnOverflowMenuClick
import org.oxycblt.auxio.util.showToast
Expand All @@ -66,14 +58,11 @@ import org.oxycblt.auxio.util.systemBarInsetsCompat
class PlaybackPanelFragment :
ViewBindingFragment<FragmentPlaybackPanelBinding>(),
Toolbar.OnMenuItemClickListener,
StyledSeekBar.Listener,
PlaybackPagerAdapter.Listener {
StyledSeekBar.Listener {
private val playbackModel: PlaybackViewModel by activityViewModels()
private val detailModel: DetailViewModel by activityViewModels()
private val queueModel: QueueViewModel by activityViewModels()
private val listModel: ListViewModel by activityViewModels()
private var equalizerLauncher: ActivityResultLauncher<Intent>? = null
private var coverAdapter: PlaybackPagerAdapter? = null

override fun onCreateBinding(inflater: LayoutInflater) =
FragmentPlaybackPanelBinding.inflate(inflater)
Expand Down Expand Up @@ -110,13 +99,19 @@ class PlaybackPanelFragment :
}
}

// cover carousel adapter
coverAdapter = PlaybackPagerAdapter(this)
binding.playbackCoverPager.apply {
adapter = coverAdapter
registerOnPageChangeCallback(OnCoverChangedCallback(queueModel))
val recycler = VP_RECYCLER_FIELD.get(this@apply) as RecyclerView
recycler.isNestedScrollingEnabled = false
// Set up marquee on song information, alongside click handlers that navigate to each
// respective item.
binding.playbackSong.apply {
isSelected = true
setOnClickListener { playbackModel.song.value?.let(detailModel::showAlbum) }
}
binding.playbackArtist.apply {
isSelected = true
setOnClickListener { navigateToCurrentArtist() }
}
binding.playbackAlbum.apply {
isSelected = true
setOnClickListener { navigateToCurrentAlbum() }
}

binding.playbackSeekBar.listener = this
Expand All @@ -136,14 +131,15 @@ class PlaybackPanelFragment :
collectImmediately(playbackModel.repeatMode, ::updateRepeat)
collectImmediately(playbackModel.isPlaying, ::updatePlaying)
collectImmediately(playbackModel.isShuffled, ::updateShuffled)
collectImmediately(queueModel.queue, ::updateQueue)
collectImmediately(queueModel.index, ::updateQueuePosition)
}

override fun onDestroyBinding(binding: FragmentPlaybackPanelBinding) {
equalizerLauncher = null
coverAdapter = null
binding.playbackToolbar.setOnMenuItemClickListener(null)
// Marquee elements leak if they are not disabled when the views are destroyed.
binding.playbackSong.isSelected = false
binding.playbackArtist.isSelected = false
binding.playbackAlbum.isSelected = false
}

override fun onMenuItemClick(item: MenuItem): Boolean {
Expand Down Expand Up @@ -174,26 +170,19 @@ class PlaybackPanelFragment :
playbackModel.seekTo(positionDs)
}

private fun updateQueue(queue: List<Song>) {
coverAdapter?.update(queue, UpdateInstructions.Replace(0))
}

private fun updateQueuePosition(position: Int) {
val pager = requireBinding().playbackCoverPager
val distance = abs(pager.currentItem - position)
if (distance != 0) {
pager.setCurrentItem(position, distance == 1)
}
}

private fun updateSong(song: Song?) {
if (song == null) {
// Nothing to do.
return
}

val binding = requireBinding()
val context = requireContext()
logD("Updating song display: $song")
binding.playbackCover.bind(song)
binding.playbackSong.text = song.name.resolve(context)
binding.playbackArtist.text = song.artists.resolveNames(context)
binding.playbackAlbum.text = song.album.name.resolve(context)
binding.playbackSeekBar.durationDs = song.durationMs.msToDs()
}

Expand Down Expand Up @@ -223,43 +212,11 @@ class PlaybackPanelFragment :
requireBinding().playbackShuffle.isActivated = isShuffled
}

override fun navigateToCurrentSong() {
playbackModel.song.value?.let(detailModel::showAlbum)
}

override fun navigateToCurrentArtist() {
private fun navigateToCurrentArtist() {
playbackModel.song.value?.let(detailModel::showArtist)
}

override fun navigateToCurrentAlbum() {
private fun navigateToCurrentAlbum() {
playbackModel.song.value?.let { detailModel.showAlbum(it.album) }
}

override fun navigateToMenu() {
// TODO
}

private class OnCoverChangedCallback(private val queueViewModel: QueueViewModel) :
OnPageChangeCallback() {

private var targetPosition = RecyclerView.NO_POSITION

override fun onPageSelected(position: Int) {
super.onPageSelected(position)
targetPosition = position
}

override fun onPageScrollStateChanged(state: Int) {
super.onPageScrollStateChanged(state)
if (state == ViewPager2.SCROLL_STATE_IDLE &&
targetPosition != RecyclerView.NO_POSITION &&
targetPosition != queueViewModel.index.value) {
queueViewModel.goto(targetPosition)
}
}
}

private companion object {
val VP_RECYCLER_FIELD: Field by lazyReflectedField(ViewPager2::class, "mRecyclerView")
}
}

This file was deleted.

52 changes: 46 additions & 6 deletions app/src/main/res/layout-h480dp/fragment_playback_panel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,55 @@
app:title="@string/lbl_playback"
tools:subtitle="@string/lbl_all_songs" />

<androidx.viewpager2.widget.ViewPager2
android:id="@+id/playback_cover_pager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/playback_seek_bar"
<org.oxycblt.auxio.image.CoverView
android:id="@+id/playback_cover"
style="@style/Widget.Auxio.Image.Full"
android:layout_margin="@dimen/spacing_medium"
app:enablePlaybackIndicator="false"
app:enableSelectionBadge="false"
app:layout_constraintBottom_toTopOf="@+id/playback_song"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/playback_toolbar" />

<TextView
android:id="@+id/playback_song"
style="@style/Widget.Auxio.TextView.Primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/spacing_medium"
android:layout_marginEnd="@dimen/spacing_medium"
app:layout_constraintBottom_toTopOf="@+id/playback_artist"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_chainStyle="packed"
tools:text="Song Name" />

<TextView
android:id="@+id/playback_artist"
style="@style/Widget.Auxio.TextView.Secondary.Marquee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/spacing_medium"
android:layout_marginEnd="@dimen/spacing_medium"
app:layout_constraintBottom_toTopOf="@+id/playback_album"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="Artist Name" />

<TextView
android:id="@+id/playback_album"
style="@style/Widget.Auxio.TextView.Secondary.Marquee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/spacing_medium"
android:layout_marginEnd="@dimen/spacing_medium"
app:layout_constraintBottom_toTopOf="@+id/playback_seek_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="Album Name" />


<org.oxycblt.auxio.playback.ui.StyledSeekBar
android:id="@+id/playback_seek_bar"
android:layout_width="match_parent"
Expand Down Expand Up @@ -114,4 +154,4 @@

</org.oxycblt.auxio.playback.ui.ForcedLTRFrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit df7ec27

Please sign in to comment.