Skip to content

Commit

Permalink
Twelve: Add shuffle play
Browse files Browse the repository at this point in the history
Change-Id: Ifd0b2611f30190f96250bf5477708010ec5727e7
  • Loading branch information
trautamaki authored and luca020400 committed Jan 22, 2025
1 parent fa70c24 commit 0b2f5f8
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 67 deletions.
26 changes: 22 additions & 4 deletions app/src/main/java/org/lineageos/twelve/fragments/AlbumFragment.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-FileCopyrightText: 2024-2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -10,6 +10,7 @@ import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.core.view.ViewCompat
Expand Down Expand Up @@ -67,7 +68,11 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
private val playAllExtendedFloatingActionButton by getViewProperty<ExtendedFloatingActionButton>(
R.id.playAllExtendedFloatingActionButton
)
private val playButtonsLinearLayout by getViewProperty<LinearLayout>(R.id.playButtonsLinearLayout)
private val recyclerView by getViewProperty<RecyclerView>(R.id.recyclerView)
private val shufflePlayExtendedFloatingActionButton by getViewProperty<ExtendedFloatingActionButton>(
R.id.shufflePlayExtendedFloatingActionButton
)
private val thumbnailImageView by getViewProperty<ImageView>(R.id.thumbnailImageView)
private val toolbar by getViewProperty<MaterialToolbar>(R.id.toolbar)
private val tracksInfoTextView by getViewProperty<TextView>(R.id.tracksInfoTextView)
Expand Down Expand Up @@ -223,7 +228,7 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
}

ViewCompat.setOnApplyWindowInsetsListener(
playAllExtendedFloatingActionButton
playButtonsLinearLayout
) { v, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())

Expand All @@ -245,6 +250,12 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
findNavController().navigateSafe(R.id.action_albumFragment_to_fragment_now_playing)
}

shufflePlayExtendedFloatingActionButton.setOnClickListener {
viewModel.shufflePlayAlbum()

findNavController().navigateSafe(R.id.action_albumFragment_to_fragment_now_playing)
}

viewModel.loadAlbum(albumUri)

viewLifecycleOwner.lifecycleScope.launch {
Expand Down Expand Up @@ -350,8 +361,15 @@ class AlbumFragment : Fragment(R.layout.fragment_album) {
recyclerView.isVisible = !isEmpty
noElementsNestedScrollView.isVisible = isEmpty
when (isEmpty) {
true -> playAllExtendedFloatingActionButton.hide()
false -> playAllExtendedFloatingActionButton.show()
true -> {
playAllExtendedFloatingActionButton.hide()
shufflePlayExtendedFloatingActionButton.hide()
}

false -> {
playAllExtendedFloatingActionButton.show()
shufflePlayExtendedFloatingActionButton.show()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-FileCopyrightText: 2024-2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -10,6 +10,7 @@ import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.core.view.ViewCompat
Expand Down Expand Up @@ -67,7 +68,11 @@ class PlaylistFragment : Fragment(R.layout.fragment_playlist) {
R.id.playAllExtendedFloatingActionButton
)
private val playlistNameTextView by getViewProperty<TextView>(R.id.playlistNameTextView)
private val playButtonsLinearLayout by getViewProperty<LinearLayout>(R.id.playButtonsLinearLayout)
private val recyclerView by getViewProperty<RecyclerView>(R.id.recyclerView)
private val shufflePlayExtendedFloatingActionButton by getViewProperty<ExtendedFloatingActionButton>(
R.id.shufflePlayExtendedFloatingActionButton
)
private val thumbnailImageView by getViewProperty<ImageView>(R.id.thumbnailImageView)
private val toolbar by getViewProperty<MaterialToolbar>(R.id.toolbar)
private val tracksInfoTextView by getViewProperty<TextView>(R.id.tracksInfoTextView)
Expand Down Expand Up @@ -178,7 +183,7 @@ class PlaylistFragment : Fragment(R.layout.fragment_playlist) {
}

ViewCompat.setOnApplyWindowInsetsListener(
playAllExtendedFloatingActionButton
playButtonsLinearLayout
) { v, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())

Expand Down Expand Up @@ -216,6 +221,12 @@ class PlaylistFragment : Fragment(R.layout.fragment_playlist) {
findNavController().navigateSafe(R.id.action_playlistFragment_to_fragment_now_playing)
}

shufflePlayExtendedFloatingActionButton.setOnClickListener {
viewModel.shufflePlayPlaylist()

findNavController().navigateSafe(R.id.action_playlistFragment_to_fragment_now_playing)
}

viewModel.loadPlaylist(playlistUri)

viewLifecycleOwner.lifecycleScope.launch {
Expand Down Expand Up @@ -279,8 +290,14 @@ class PlaylistFragment : Fragment(R.layout.fragment_playlist) {
recyclerView.isVisible = !isEmpty
noElementsNestedScrollView.isVisible = isEmpty
when (isEmpty) {
true -> playAllExtendedFloatingActionButton.hide()
false -> playAllExtendedFloatingActionButton.show()
true -> {
playAllExtendedFloatingActionButton.hide()
shufflePlayExtendedFloatingActionButton.hide()
}
false -> {
playAllExtendedFloatingActionButton.show()
shufflePlayExtendedFloatingActionButton.show()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-FileCopyrightText: 2024-2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -147,4 +147,12 @@ class AlbumViewModel(application: Application) : TwelveViewModel(application) {
playAudio(audios, startFrom?.let { audios.indexOf(it) } ?: 0)
}
}

fun shufflePlayAlbum() {
albumContent.value.mapNotNull {
(it as? AlbumContent.AudioItem)?.audio
}.takeUnless { it.isEmpty() }?.let { audios ->
playAudio(audios.shuffled(), 0)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-FileCopyrightText: 2024-2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -62,4 +62,12 @@ class PlaylistViewModel(application: Application) : TwelveViewModel(application)
playAudio(it, position)
}
}

fun shufflePlayPlaylist() {
(playlist.value as? RequestStatus.Success)?.data?.second?.takeUnless {
it.isEmpty()
}?.let {
playAudio(it.shuffled(), 0)
}
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/drawable/ic_shuffle_play.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: Material Design Authors / Google LLC
SPDX-License-Identifier: Apache-2.0
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="#000000">
<path
android:fillColor="@android:color/white"
android:pathData="M560,800L560,720L664,720L537,593L594,536L720,662L720,560L800,560L800,800L560,800ZM216,800L160,744L664,240L560,240L560,160L800,160L800,400L720,400L720,296L216,800ZM367,423L160,216L216,160L423,367L367,423Z" />
</vector>
17 changes: 3 additions & 14 deletions app/src/main/res/layout-land/fragment_album.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2024 The LineageOS Project
SPDX-FileCopyrightText: 2024-2025 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down Expand Up @@ -96,24 +96,13 @@
android:indeterminate="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<FrameLayout
<include layout="@layout/play_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:paddingBottom="16dp"
app:layout_anchor="@+id/recyclerView"
app:layout_anchorGravity="bottom|end"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior">

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/playAllExtendedFloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="@string/play_all"
app:icon="@drawable/ic_play_arrow"
tools:visibility="visible" />

</FrameLayout>
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
17 changes: 3 additions & 14 deletions app/src/main/res/layout-land/fragment_playlist.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2024 The LineageOS Project
SPDX-FileCopyrightText: 2024-2025 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down Expand Up @@ -96,25 +96,14 @@
android:indeterminate="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<FrameLayout
<include layout="@layout/play_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:paddingBottom="16dp"
app:layout_anchor="@+id/recyclerView"
app:layout_anchorGravity="bottom|end"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior">

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/playAllExtendedFloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="@string/play_all"
app:icon="@drawable/ic_play_arrow"
tools:visibility="visible" />

</FrameLayout>
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" />

<org.lineageos.twelve.ui.views.FullscreenLoadingProgressBar
android:id="@+id/fullscreenLoadingProgressBar"
Expand Down
17 changes: 3 additions & 14 deletions app/src/main/res/layout/fragment_album.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?><!--
SPDX-FileCopyrightText: 2024 The LineageOS Project
SPDX-FileCopyrightText: 2024-2025 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down Expand Up @@ -96,24 +96,13 @@
android:indeterminate="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<FrameLayout
<include layout="@layout/play_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:paddingBottom="16dp"
app:layout_anchor="@+id/recyclerView"
app:layout_anchorGravity="bottom|end"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior">

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/playAllExtendedFloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="@string/play_all"
app:icon="@drawable/ic_play_arrow"
tools:visibility="visible" />

</FrameLayout>
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
18 changes: 4 additions & 14 deletions app/src/main/res/layout/fragment_playlist.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2024 The LineageOS Project
SPDX-FileCopyrightText: 2024-2025 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down Expand Up @@ -98,25 +98,15 @@
android:indeterminate="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<FrameLayout
<include
layout="@layout/play_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:paddingBottom="16dp"
app:layout_anchor="@+id/recyclerView"
app:layout_anchorGravity="bottom|end"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior">

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/playAllExtendedFloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="@string/play_all"
app:icon="@drawable/ic_play_arrow"
tools:visibility="visible" />

</FrameLayout>
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" />

<org.lineageos.twelve.ui.views.FullscreenLoadingProgressBar
android:id="@+id/fullscreenLoadingProgressBar"
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/res/layout/play_buttons.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2025 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/playButtonsLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/shufflePlayExtendedFloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:tooltipText="@string/shuffle_play"
android:layout_gravity="end"
android:layout_marginBottom="12dp"
app:backgroundTint="?attr/colorTertiaryContainer"
app:collapsedSize="46dp"
app:icon="@drawable/ic_shuffle_play"
tools:visibility="visible" />

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/playAllExtendedFloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="@string/play_all"
app:icon="@drawable/ic_play_arrow"
tools:visibility="visible" />
</LinearLayout>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,9 @@
<string name="content_description_play_pause">Play/pause</string>
<string name="content_description_thumbnail">Thumbnail</string>

<!-- Play all -->
<!-- Play buttons -->
<string name="play_all">Play all</string>
<string name="shuffle_play">Shuffle play</string>

<!-- Media repository tree -->
<string name="library_item_no_permissions">No permissions</string>
Expand Down

0 comments on commit 0b2f5f8

Please sign in to comment.