feat: Implemented shuffle feature for downloaded songs based on the set filter

This commit is contained in:
antonio 2024-01-28 18:02:48 +01:00
parent 1d3a32be5d
commit 2faba71df0
6 changed files with 58 additions and 7 deletions

5
.idea/gradle.xml generated
View file

@ -4,16 +4,15 @@
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="17" />
<option name="gradleJvm" value="#GRADLE_LOCAL_JAVA_HOME" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
</set>
</option>
<option name="resolveExternalAnnotations" value="false" />
</GradleProjectSettings>
</option>
</component>

View file

@ -33,6 +33,7 @@ public class DownloadHorizontalAdapter extends RecyclerView.Adapter<DownloadHori
private String filterValue;
private List<Child> songs;
private List<Child> shuffling;
private List<Child> grouped;
public DownloadHorizontalAdapter(ClickCallback click) {
@ -82,6 +83,7 @@ public class DownloadHorizontalAdapter extends RecyclerView.Adapter<DownloadHori
this.songs = songs;
this.grouped = groupSong(songs);
this.shuffling = shufflingSong(new ArrayList<>(songs));
notifyDataSetChanged();
}
@ -90,6 +92,10 @@ public class DownloadHorizontalAdapter extends RecyclerView.Adapter<DownloadHori
return grouped.get(id);
}
public List<Child> getShuffling() {
return shuffling;
}
@Override
public int getItemViewType(int position) {
return position;
@ -136,6 +142,27 @@ public class DownloadHorizontalAdapter extends RecyclerView.Adapter<DownloadHori
return songs;
}
private List<Child> shufflingSong(List<Child> songs) {
if (filterValue == null) {
return songs;
}
switch (filterKey) {
case Constants.DOWNLOAD_TYPE_TRACK:
return songs.stream().filter(child -> child.getId().equals(filterValue)).collect(Collectors.toList());
case Constants.DOWNLOAD_TYPE_ALBUM:
return songs.stream().filter(child -> Objects.equals(child.getAlbumId(), filterValue)).collect(Collectors.toList());
case Constants.DOWNLOAD_TYPE_GENRE:
return songs.stream().filter(child -> Objects.equals(child.getGenre(), filterValue)).collect(Collectors.toList());
case Constants.DOWNLOAD_TYPE_YEAR:
return songs.stream().filter(child -> Objects.equals(child.getYear(), Integer.valueOf(filterValue))).collect(Collectors.toList());
case Constants.DOWNLOAD_TYPE_ARTIST:
return songs.stream().filter(child -> Objects.equals(child.getArtistId(), filterValue)).collect(Collectors.toList());
default:
return songs;
}
}
private String countSong(String filterKey, String filterValue, List<Child> songs) {
if (filterValue != null) {
switch (filterKey) {

View file

@ -33,6 +33,7 @@ import com.cappielloantonio.tempo.viewmodel.DownloadViewModel;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
@ -165,6 +166,19 @@ public class DownloadFragment extends Fragment implements ClickCallback {
bind.downloadedGoBackImageView.setVisibility(stack.size() > 1 ? View.VISIBLE : View.GONE);
setupBackPressing(stack.size());
setupShuffleButton();
});
}
private void setupShuffleButton() {
bind.shuffleDownloadedTextViewClickable.setOnClickListener(view -> {
List<Child> songs = downloadHorizontalAdapter.getShuffling();
if (songs != null && !songs.isEmpty()) {
Collections.shuffle(songs);
MediaManager.startQueue(mediaBrowserListenableFuture, songs, 0);
activity.setBottomSheetInPeek(true);
}
});
}

View file

@ -76,11 +76,20 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/download_title_section"
android:layout_marginBottom="16dp"
app:layout_constraintEnd_toStartOf="@+id/downloaded_go_back_image_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/shuffle_downloaded_text_view_clickable"
style="@style/TitleMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/download_shuffle_all_subtitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/downloaded_text_view_refreshable"/>
<ImageView
android:id="@+id/downloaded_go_back_image_view"
android:layout_width="24dp"
@ -108,12 +117,12 @@
android:layout_height="wrap_content"
android:clipToPadding="false"
android:nestedScrollingEnabled="false"
android:paddingTop="8dp"
android:paddingTop="12dp"
android:paddingBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/downloaded_text_view_refreshable" />
app:layout_constraintTop_toBottomOf="@id/shuffle_downloaded_text_view_clickable" />
<include
android:id="@+id/download_downloaded_placeholder"

View file

@ -73,7 +73,8 @@
android:id="@+id/downloaded_item_more_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:paddingVertical="12dp"
android:paddingStart="12dp"
app:layout_constraintBottom_toBottomOf="@+id/item_cover_image_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/item_cover_image_view" >

View file

@ -65,6 +65,7 @@
<string name="download_info_empty_title">No downloads yet!</string>
<string name="download_item_multiple_subtitle_formatter">%1$s • %2$s items</string>
<string name="download_item_single_subtitle_formatter">%1$s items</string>
<string name="download_shuffle_all_subtitle">Shuffle all</string>
<string name="download_storage_dialog_sub_summary">For the changes to take effect, restart the app.</string>
<string name="download_storage_dialog_summary">Changing the destination of downloaded files from one storage to another will result in the immediate deletion of any previously downloaded files in the other storage.</string>
<string name="download_storage_dialog_title">Select storage option</string>