mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Add Made for you section in home
This commit is contained in:
parent
ec7f9d3002
commit
c2782d7e49
7 changed files with 218 additions and 17 deletions
|
|
@ -0,0 +1,120 @@
|
|||
package com.cappielloantonio.play.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.navigation.Navigation;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
|
||||
import com.cappielloantonio.play.App;
|
||||
import com.cappielloantonio.play.R;
|
||||
import com.cappielloantonio.play.glide.CustomGlideRequest;
|
||||
import com.cappielloantonio.play.interfaces.MediaCallback;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.repository.QueueRepository;
|
||||
import com.cappielloantonio.play.repository.SongRepository;
|
||||
import com.cappielloantonio.play.service.MusicPlayerRemote;
|
||||
import com.cappielloantonio.play.ui.activity.MainActivity;
|
||||
import com.cappielloantonio.play.util.MusicUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class SimilarTrackAdapter extends RecyclerView.Adapter<SimilarTrackAdapter.ViewHolder> {
|
||||
private static final String TAG = "SimilarTrackAdapter";
|
||||
|
||||
private final MainActivity mainActivity;
|
||||
private final Context context;
|
||||
private final LayoutInflater mInflater;
|
||||
|
||||
private List<Song> songs;
|
||||
|
||||
public SimilarTrackAdapter(MainActivity mainActivity, Context context) {
|
||||
this.mainActivity = mainActivity;
|
||||
this.context = context;
|
||||
this.mInflater = LayoutInflater.from(context);
|
||||
this.songs = new ArrayList<>();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = mInflater.inflate(R.layout.item_home_track, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
||||
Song song = songs.get(position);
|
||||
|
||||
holder.textTitle.setText(MusicUtil.getReadableString(song.getTitle()));
|
||||
holder.textAlbum.setText(MusicUtil.getReadableString(song.getAlbumName()));
|
||||
|
||||
CustomGlideRequest.Builder
|
||||
.from(context, song.getPrimary(), CustomGlideRequest.SONG_PIC, null)
|
||||
.build()
|
||||
.transform(new RoundedCorners(CustomGlideRequest.CORNER_RADIUS))
|
||||
.into(holder.cover);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return songs.size();
|
||||
}
|
||||
|
||||
public void setItems(List<Song> songs) {
|
||||
this.songs = songs;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||
TextView textTitle;
|
||||
TextView textAlbum;
|
||||
ImageView cover;
|
||||
|
||||
ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
|
||||
textTitle = itemView.findViewById(R.id.title_track_label);
|
||||
textAlbum = itemView.findViewById(R.id.album_track_label);
|
||||
cover = itemView.findViewById(R.id.track_cover_image_view);
|
||||
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
List<Song> opener = new ArrayList<>();
|
||||
opener.add(songs.get(getBindingAdapterPosition()));
|
||||
MusicPlayerRemote.openQueue(opener, 0, true);
|
||||
|
||||
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
||||
queueRepository.insertAllAndStartNew(opener);
|
||||
|
||||
mainActivity.isBottomSheetInPeek(true);
|
||||
mainActivity.setBottomSheetMusicInfo(songs.get(getBindingAdapterPosition()));
|
||||
|
||||
SongRepository songRepository = new SongRepository(App.getInstance());
|
||||
songRepository.getInstantMix(songs.get(getBindingAdapterPosition()), 20, new MediaCallback() {
|
||||
@Override
|
||||
public void onError(Exception exception) {
|
||||
Log.e(TAG, "onError: " + exception.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadMedia(List<?> media) {
|
||||
MusicPlayerRemote.enqueue((List<Song>) media);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@ package com.cappielloantonio.play.adapter;
|
|||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.text.Html;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
|
@ -10,7 +9,6 @@ import android.widget.ImageView;
|
|||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.navigation.Navigation;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
|
|
@ -30,7 +28,7 @@ import java.util.List;
|
|||
/**
|
||||
* Adapter per i brani recenti in home
|
||||
*/
|
||||
public class RecentMusicAdapter extends RecyclerView.Adapter<RecentMusicAdapter.ViewHolder> {
|
||||
public class TrackAdapter extends RecyclerView.Adapter<TrackAdapter.ViewHolder> {
|
||||
private static final String TAG = "RecentMusicAdapter";
|
||||
|
||||
private final MainActivity mainActivity;
|
||||
|
|
@ -39,7 +37,7 @@ public class RecentMusicAdapter extends RecyclerView.Adapter<RecentMusicAdapter.
|
|||
|
||||
private List<Song> songs;
|
||||
|
||||
public RecentMusicAdapter(MainActivity mainActivity, Context context) {
|
||||
public TrackAdapter(MainActivity mainActivity, Context context) {
|
||||
this.mainActivity = mainActivity;
|
||||
this.context = context;
|
||||
this.mInflater = LayoutInflater.from(context);
|
||||
|
|
@ -29,7 +29,7 @@ public class SongRepository {
|
|||
this.application = application;
|
||||
}
|
||||
|
||||
public MutableLiveData<List<Song>> getStarredSongs() {
|
||||
public MutableLiveData<List<Song>> getStarredSongs(boolean random, int size) {
|
||||
MutableLiveData<List<Song>> starredSongs = new MutableLiveData<>();
|
||||
|
||||
App.getSubsonicClientInstance(application, false)
|
||||
|
|
@ -40,7 +40,13 @@ public class SongRepository {
|
|||
public void onResponse(Call<SubsonicResponse> call, Response<SubsonicResponse> response) {
|
||||
if (response.body().getStatus().getValue().equals(ResponseStatus.OK)) {
|
||||
List<Song> songs = new ArrayList<>(MappingUtil.mapSong(response.body().getStarred2().getSongs()));
|
||||
starredSongs.setValue(songs);
|
||||
|
||||
if (!random) {
|
||||
starredSongs.setValue(songs);
|
||||
} else {
|
||||
Collections.shuffle(songs);
|
||||
starredSongs.setValue(songs.subList(0, Math.min(size, songs.size())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,8 @@ import com.cappielloantonio.play.adapter.AlbumAdapter;
|
|||
import com.cappielloantonio.play.adapter.AlbumHorizontalAdapter;
|
||||
import com.cappielloantonio.play.adapter.ArtistHorizontalAdapter;
|
||||
import com.cappielloantonio.play.adapter.DiscoverSongAdapter;
|
||||
import com.cappielloantonio.play.adapter.RecentMusicAdapter;
|
||||
import com.cappielloantonio.play.adapter.SimilarTrackAdapter;
|
||||
import com.cappielloantonio.play.adapter.TrackAdapter;
|
||||
import com.cappielloantonio.play.adapter.SongHorizontalAdapter;
|
||||
import com.cappielloantonio.play.adapter.YearAdapter;
|
||||
import com.cappielloantonio.play.databinding.FragmentHomeBinding;
|
||||
|
|
@ -49,7 +50,8 @@ public class HomeFragment extends Fragment {
|
|||
private SongHorizontalAdapter starredSongAdapter;
|
||||
private AlbumHorizontalAdapter starredAlbumAdapter;
|
||||
private ArtistHorizontalAdapter starredArtistAdapter;
|
||||
private RecentMusicAdapter dowanloadedMusicAdapter;
|
||||
private TrackAdapter dowanloadedMusicAdapter;
|
||||
private SimilarTrackAdapter similarMusicAdapter;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
|
|
@ -70,6 +72,7 @@ public class HomeFragment extends Fragment {
|
|||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
initDiscoverSongSlideView();
|
||||
initSimilarSongView();
|
||||
initMostPlayedAlbumView();
|
||||
initRecentPlayedAlbumView();
|
||||
initStarredTracksView();
|
||||
|
|
@ -143,6 +146,11 @@ public class HomeFragment extends Fragment {
|
|||
return true;
|
||||
});
|
||||
|
||||
bind.similarTracksTextViewRefreshable.setOnLongClickListener(v -> {
|
||||
homeViewModel.refreshSimilarSongSample(requireActivity());
|
||||
return true;
|
||||
});
|
||||
|
||||
bind.recentlyPlayedAlbumsTextViewRefreshable.setOnLongClickListener(v -> {
|
||||
homeViewModel.refreshRecentlyPlayedAlbumList(requireActivity());
|
||||
return true;
|
||||
|
|
@ -195,6 +203,28 @@ public class HomeFragment extends Fragment {
|
|||
setDiscoverSongSlideViewOffset(20, 16);
|
||||
}
|
||||
|
||||
private void initSimilarSongView() {
|
||||
bind.similarTracksRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false));
|
||||
bind.similarTracksRecyclerView.setHasFixedSize(true);
|
||||
|
||||
similarMusicAdapter = new SimilarTrackAdapter(activity, requireContext());
|
||||
bind.similarTracksRecyclerView.setAdapter(similarMusicAdapter);
|
||||
homeViewModel.getStarredTracksSample().observe(requireActivity(), songs -> {
|
||||
if (songs == null) {
|
||||
if (bind != null) bind.homeSimilarTracksPlaceholder.placeholder.setVisibility(View.VISIBLE);
|
||||
if (bind != null) bind.homeSimilarTracksSector.setVisibility(View.GONE);
|
||||
} else {
|
||||
if (bind != null) bind.homeSimilarTracksPlaceholder.placeholder.setVisibility(View.GONE);
|
||||
if (bind != null) bind.homeSimilarTracksSector.setVisibility(!songs.isEmpty() ? View.VISIBLE : View.GONE);
|
||||
|
||||
similarMusicAdapter.setItems(songs);
|
||||
}
|
||||
});
|
||||
|
||||
CustomLinearSnapHelper similarSongSnapHelper = new CustomLinearSnapHelper();
|
||||
similarSongSnapHelper.attachToRecyclerView(bind.similarTracksRecyclerView);
|
||||
}
|
||||
|
||||
private void initMostPlayedAlbumView() {
|
||||
bind.mostPlayedAlbumsRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false));
|
||||
bind.mostPlayedAlbumsRecyclerView.setHasFixedSize(true);
|
||||
|
|
@ -360,7 +390,7 @@ public class HomeFragment extends Fragment {
|
|||
bind.downloadedTracksRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false));
|
||||
bind.downloadedTracksRecyclerView.setHasFixedSize(true);
|
||||
|
||||
dowanloadedMusicAdapter = new RecentMusicAdapter(activity, requireContext());
|
||||
dowanloadedMusicAdapter = new TrackAdapter(activity, requireContext());
|
||||
bind.downloadedTracksRecyclerView.setAdapter(dowanloadedMusicAdapter);
|
||||
homeViewModel.getDownloaded(requireActivity()).observe(requireActivity(), downloads -> {
|
||||
if (downloads == null) {
|
||||
|
|
@ -404,6 +434,7 @@ public class HomeFragment extends Fragment {
|
|||
if (bind != null) {
|
||||
bind.homeLinearLayoutContainer.removeAllViews();
|
||||
bind.homeLinearLayoutContainer.addView(bind.homeDiscoverSector);
|
||||
bind.homeLinearLayoutContainer.addView(bind.homeSimilarTracksSector);
|
||||
bind.homeLinearLayoutContainer.addView(bind.homeRecentlyAddedAlbumsSector);
|
||||
bind.homeLinearLayoutContainer.addView(bind.homeFlashbackSector);
|
||||
bind.homeLinearLayoutContainer.addView(bind.homeStarredTracksSector);
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ public class HomeViewModel extends AndroidViewModel {
|
|||
private final DownloadRepository downloadRepository;
|
||||
|
||||
private final MutableLiveData<List<Song>> dicoverSongSample = new MutableLiveData<>(null);
|
||||
private final MutableLiveData<List<Song>> starredTracksSample = new MutableLiveData<>(null);
|
||||
|
||||
private final MutableLiveData<List<Album>> mostPlayedAlbumSample = new MutableLiveData<>(null);
|
||||
private final MutableLiveData<List<Album>> recentlyPlayedAlbumSample = new MutableLiveData<>(null);
|
||||
private final MutableLiveData<List<Integer>> years = new MutableLiveData<>(null);
|
||||
|
|
@ -46,19 +48,24 @@ public class HomeViewModel extends AndroidViewModel {
|
|||
downloadRepository = new DownloadRepository(application);
|
||||
|
||||
songRepository.getRandomSample(10, null, null).observeForever(dicoverSongSample::postValue);
|
||||
songRepository.getStarredSongs(true, 10).observeForever(starredTracksSample::postValue);
|
||||
}
|
||||
|
||||
public LiveData<List<Song>> getDiscoverSongSample() {
|
||||
return dicoverSongSample;
|
||||
}
|
||||
|
||||
public LiveData<List<Song>> getStarredTracksSample() {
|
||||
return starredTracksSample;
|
||||
}
|
||||
|
||||
public LiveData<List<Integer>> getYearList(LifecycleOwner owner) {
|
||||
albumRepository.getDecades().observe(owner, years::postValue);
|
||||
return years;
|
||||
}
|
||||
|
||||
public LiveData<List<Song>> getStarredTracks(LifecycleOwner owner) {
|
||||
songRepository.getStarredSongs().observe(owner, starredTracks::postValue);
|
||||
songRepository.getStarredSongs(false, -1).observe(owner, starredTracks::postValue);
|
||||
return starredTracks;
|
||||
}
|
||||
|
||||
|
|
@ -96,8 +103,12 @@ public class HomeViewModel extends AndroidViewModel {
|
|||
songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue);
|
||||
}
|
||||
|
||||
public void refreshSimilarSongSample(LifecycleOwner owner) {
|
||||
songRepository.getStarredSongs(true, 10).observe(owner, starredTracksSample::postValue);
|
||||
}
|
||||
|
||||
public void refreshStarredTracks(LifecycleOwner owner) {
|
||||
songRepository.getStarredSongs().observe(owner, starredTracks::postValue);
|
||||
songRepository.getStarredSongs(false, -1).observe(owner, starredTracks::postValue);
|
||||
}
|
||||
|
||||
public void refreshStarredAlbums(LifecycleOwner owner) {
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ public class SongListPageViewModel extends AndroidViewModel {
|
|||
songList = songRepository.getRandomSample(500, year, year + 10);
|
||||
break;
|
||||
case Song.STARRED:
|
||||
songList = songRepository.getStarredSongs();
|
||||
songList = songRepository.getStarredSongs(false, -1);
|
||||
break;
|
||||
case Song.DOWNLOADED:
|
||||
songList.setValue(MappingUtil.mapDownload(downloadRepository.getLiveDownload()));
|
||||
|
|
|
|||
|
|
@ -71,6 +71,41 @@
|
|||
layout="@layout/item_placehoder_discovery"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<!-- Similar tracks -->
|
||||
<LinearLayout
|
||||
android:id="@+id/home_similar_tracks_sector"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/similar_tracks_text_view_refreshable"
|
||||
style="@style/HeadlineTextView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingTop="20dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:text="Made for you" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/similar_tracks_recycler_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:clipToPadding="false"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingEnd="8dp"
|
||||
android:paddingBottom="8dp" />
|
||||
</LinearLayout>
|
||||
|
||||
<include
|
||||
android:id="@+id/home_similar_tracks_placeholder"
|
||||
layout="@layout/item_placeholder_album"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<!-- Most played albums -->
|
||||
<LinearLayout
|
||||
android:id="@+id/home_most_played_albums_sector"
|
||||
|
|
@ -427,11 +462,6 @@
|
|||
android:text="See all" />
|
||||
</LinearLayout>
|
||||
|
||||
<include
|
||||
android:id="@+id/home_downloaded_tracks_placeholder"
|
||||
layout="@layout/item_placeholder_album"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/downloaded_tracks_recycler_view"
|
||||
android:layout_width="match_parent"
|
||||
|
|
@ -445,6 +475,11 @@
|
|||
android:paddingBottom="8dp" />
|
||||
</LinearLayout>
|
||||
|
||||
<include
|
||||
android:id="@+id/home_downloaded_tracks_placeholder"
|
||||
layout="@layout/item_placeholder_album"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<!-- Recently added albums -->
|
||||
<LinearLayout
|
||||
android:id="@+id/home_recently_added_albums_sector"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue