mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Dynamically added views of pinned playlists to the homepage
This commit is contained in:
parent
e2f1212e58
commit
72712d6feb
7 changed files with 195 additions and 14 deletions
|
|
@ -7,6 +7,7 @@ import android.view.MenuInflater;
|
|||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
|
@ -16,6 +17,7 @@ import androidx.lifecycle.ViewModelProvider;
|
|||
import androidx.recyclerview.widget.GridLayoutManager;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.PagerSnapHelper;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import androidx.recyclerview.widget.SnapHelper;
|
||||
import androidx.viewpager2.widget.ViewPager2;
|
||||
|
||||
|
|
@ -26,15 +28,16 @@ import com.cappielloantonio.play.adapter.ArtistHorizontalAdapter;
|
|||
import com.cappielloantonio.play.adapter.DiscoverSongAdapter;
|
||||
import com.cappielloantonio.play.adapter.SimilarTrackAdapter;
|
||||
import com.cappielloantonio.play.adapter.SongHorizontalAdapter;
|
||||
import com.cappielloantonio.play.adapter.TrackAdapter;
|
||||
import com.cappielloantonio.play.adapter.YearAdapter;
|
||||
import com.cappielloantonio.play.databinding.FragmentHomeBinding;
|
||||
import com.cappielloantonio.play.helper.recyclerview.CustomLinearSnapHelper;
|
||||
import com.cappielloantonio.play.helper.recyclerview.DotsIndicatorDecoration;
|
||||
import com.cappielloantonio.play.model.Album;
|
||||
import com.cappielloantonio.play.model.Artist;
|
||||
import com.cappielloantonio.play.model.Playlist;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.ui.activity.MainActivity;
|
||||
import com.cappielloantonio.play.util.MusicUtil;
|
||||
import com.cappielloantonio.play.util.UIUtil;
|
||||
import com.cappielloantonio.play.viewmodel.HomeViewModel;
|
||||
|
||||
|
|
@ -97,6 +100,7 @@ public class HomeFragment extends Fragment {
|
|||
initStarredArtistsView();
|
||||
initYearSongView();
|
||||
initRecentAddedAlbumView();
|
||||
initPinnedPlaylistsView();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -479,7 +483,59 @@ public class HomeFragment extends Fragment {
|
|||
}
|
||||
}
|
||||
|
||||
public void addPinnedPlaylistsView() {
|
||||
public void initPinnedPlaylistsView() {
|
||||
homeViewModel.getPinnedPlaylistList(requireActivity()).observe(requireActivity(), playlists -> {
|
||||
if (bind != null && playlists != null) {
|
||||
for (Playlist playlist : playlists) {
|
||||
int playlistViewHashCode = playlist.getId().hashCode();
|
||||
if (requireView().findViewById(playlistViewHashCode) == null) {
|
||||
View genericPlaylistView = activity.getLayoutInflater().inflate(R.layout.generic_playlist_sector, null);
|
||||
genericPlaylistView.setId(playlistViewHashCode);
|
||||
genericPlaylistView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||
|
||||
TextView genericPlaylistTitleTextView = genericPlaylistView.findViewById(R.id.generic_playlist_title_text_view);
|
||||
TextView genericPlaylistCickableTextView = genericPlaylistView.findViewById(R.id.generic_playlist_text_view_clickable);
|
||||
RecyclerView genericPlaylistRecyclerView = genericPlaylistView.findViewById(R.id.generic_playlist_recycler_view);
|
||||
|
||||
genericPlaylistTitleTextView.setText(MusicUtil.getReadableString(playlist.getName()));
|
||||
genericPlaylistRecyclerView.setHasFixedSize(true);
|
||||
|
||||
SongHorizontalAdapter trackAdapter = new SongHorizontalAdapter(activity, requireContext(), true);
|
||||
genericPlaylistRecyclerView.setAdapter(trackAdapter);
|
||||
|
||||
homeViewModel.getPlaylistSongLiveList(playlist.getId()).observe(requireActivity(), songs -> {
|
||||
if (songs.size() > 0) {
|
||||
int songsNumber = Math.min(20, songs.size());
|
||||
|
||||
genericPlaylistRecyclerView.setLayoutManager(new GridLayoutManager(requireContext(), UIUtil.getSpanCount(songsNumber, 5), GridLayoutManager.HORIZONTAL, false));
|
||||
trackAdapter.setItems(songs.subList(0, songsNumber));
|
||||
}
|
||||
});
|
||||
|
||||
genericPlaylistCickableTextView.setOnClickListener(view -> {
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putParcelable("playlist_object", playlist);
|
||||
bundle.putBoolean("is_offline", false);
|
||||
activity.navController.navigate(R.id.action_homeFragment_to_playlistPageFragment, bundle);
|
||||
});
|
||||
|
||||
SnapHelper genericPlaylistSnapHelper = new PagerSnapHelper();
|
||||
genericPlaylistSnapHelper.attachToRecyclerView(genericPlaylistRecyclerView);
|
||||
|
||||
genericPlaylistRecyclerView.addItemDecoration(
|
||||
new DotsIndicatorDecoration(
|
||||
getResources().getDimensionPixelSize(R.dimen.radius),
|
||||
getResources().getDimensionPixelSize(R.dimen.radius) * 4,
|
||||
getResources().getDimensionPixelSize(R.dimen.dots_height),
|
||||
requireContext().getResources().getColor(R.color.titleTextColor, null),
|
||||
requireContext().getResources().getColor(R.color.titleTextColor, null))
|
||||
);
|
||||
|
||||
|
||||
bind.homeLinearLayoutContainer.addView(genericPlaylistView);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
package com.cappielloantonio.play.util;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.google.android.exoplayer2.database.DatabaseProvider;
|
||||
import com.google.android.exoplayer2.database.ExoDatabaseProvider;
|
||||
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
|
||||
import com.google.android.exoplayer2.upstream.HttpDataSource;
|
||||
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
|
||||
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
|
||||
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.CookieHandler;
|
||||
import java.net.CookieManager;
|
||||
import java.net.CookiePolicy;
|
||||
|
||||
public final class CacheUtil {
|
||||
|
||||
private static final String CACHE_CONTENT_DIRECTORY = "cache";
|
||||
|
||||
private static HttpDataSource.Factory httpDataSourceFactory;
|
||||
private static File cacheDirectory;
|
||||
private static SimpleCache simpleCache;
|
||||
private static ExoDatabaseProvider databaseProvider;
|
||||
|
||||
public static synchronized HttpDataSource.Factory getHttpDataSourceFactory() {
|
||||
if (httpDataSourceFactory == null) {
|
||||
CookieManager cookieManager = new CookieManager();
|
||||
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
|
||||
CookieHandler.setDefault(cookieManager);
|
||||
httpDataSourceFactory = new DefaultHttpDataSourceFactory();
|
||||
}
|
||||
return httpDataSourceFactory;
|
||||
}
|
||||
|
||||
public static synchronized SimpleCache getCache(Context context) {
|
||||
if (simpleCache == null) {
|
||||
File downloadContentDirectory = new File(getCacheDirectory(context), CACHE_CONTENT_DIRECTORY);
|
||||
|
||||
long cacheSize = PreferenceUtil.getInstance(context).getMediaCacheSize();
|
||||
LeastRecentlyUsedCacheEvictor cacheEvictor = new LeastRecentlyUsedCacheEvictor(cacheSize);
|
||||
ExoDatabaseProvider databaseProvider = getDatabaseProvider(context);
|
||||
|
||||
simpleCache = new SimpleCache(downloadContentDirectory, cacheEvictor, databaseProvider);
|
||||
}
|
||||
return simpleCache;
|
||||
}
|
||||
|
||||
private static synchronized ExoDatabaseProvider getDatabaseProvider(Context context) {
|
||||
if (databaseProvider == null) {
|
||||
databaseProvider = new ExoDatabaseProvider(context);
|
||||
}
|
||||
return databaseProvider;
|
||||
}
|
||||
|
||||
private static synchronized File getCacheDirectory(Context context) {
|
||||
if (cacheDirectory == null) {
|
||||
cacheDirectory = context.getExternalFilesDir(null);
|
||||
if (cacheDirectory == null) {
|
||||
cacheDirectory = context.getFilesDir();
|
||||
}
|
||||
}
|
||||
return cacheDirectory;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package com.cappielloantonio.play.viewmodel;
|
|||
import android.app.Application;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
|
@ -10,12 +11,17 @@ import androidx.lifecycle.MutableLiveData;
|
|||
|
||||
import com.cappielloantonio.play.model.Album;
|
||||
import com.cappielloantonio.play.model.Artist;
|
||||
import com.cappielloantonio.play.model.Playlist;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.repository.AlbumRepository;
|
||||
import com.cappielloantonio.play.repository.ArtistRepository;
|
||||
import com.cappielloantonio.play.repository.PlaylistRepository;
|
||||
import com.cappielloantonio.play.repository.SongRepository;
|
||||
import com.cappielloantonio.play.util.MappingUtil;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class HomeViewModel extends AndroidViewModel {
|
||||
private static final String TAG = "HomeViewModel";
|
||||
|
|
@ -23,6 +29,7 @@ public class HomeViewModel extends AndroidViewModel {
|
|||
private final SongRepository songRepository;
|
||||
private final AlbumRepository albumRepository;
|
||||
private final ArtistRepository artistRepository;
|
||||
private final PlaylistRepository playlistRepository;
|
||||
|
||||
private final MutableLiveData<List<Song>> dicoverSongSample = new MutableLiveData<>(null);
|
||||
private final MutableLiveData<List<Song>> starredTracksSample = new MutableLiveData<>(null);
|
||||
|
|
@ -34,6 +41,7 @@ public class HomeViewModel extends AndroidViewModel {
|
|||
private final MutableLiveData<List<Album>> starredAlbums = new MutableLiveData<>(null);
|
||||
private final MutableLiveData<List<Artist>> starredArtists = new MutableLiveData<>(null);
|
||||
private final MutableLiveData<List<Album>> recentlyAddedAlbumSample = new MutableLiveData<>(null);
|
||||
private final MutableLiveData<List<Playlist>> pinnedPlaylists = new MutableLiveData<>(null);
|
||||
|
||||
public HomeViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
|
|
@ -41,6 +49,7 @@ public class HomeViewModel extends AndroidViewModel {
|
|||
songRepository = new SongRepository(application);
|
||||
albumRepository = new AlbumRepository(application);
|
||||
artistRepository = new ArtistRepository(application);
|
||||
playlistRepository = new PlaylistRepository(application);
|
||||
|
||||
songRepository.getRandomSample(10, null, null).observeForever(dicoverSongSample::postValue);
|
||||
songRepository.getStarredSongs(true, 10).observeForever(starredTracksSample::postValue);
|
||||
|
|
@ -89,6 +98,15 @@ public class HomeViewModel extends AndroidViewModel {
|
|||
return recentlyPlayedAlbumSample;
|
||||
}
|
||||
|
||||
public LiveData<List<Playlist>> getPinnedPlaylistList(LifecycleOwner owner) {
|
||||
playlistRepository.getPinnedPlaylists().observe(owner, pinnedPlaylists::postValue);
|
||||
return pinnedPlaylists;
|
||||
}
|
||||
|
||||
public LiveData<List<Song>> getPlaylistSongLiveList(String playlistId) {
|
||||
return playlistRepository.getPlaylistSongs(playlistId);
|
||||
}
|
||||
|
||||
public void refreshDiscoverySongSample(LifecycleOwner owner) {
|
||||
songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue);
|
||||
}
|
||||
|
|
|
|||
48
app/src/main/res/layout/generic_playlist_sector.xml
Normal file
48
app/src/main/res/layout/generic_playlist_sector.xml
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:paddingStart="8dp"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingEnd="8dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/generic_playlist_title_text_view"
|
||||
style="@style/HeadlineTextView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:paddingStart="8dp"
|
||||
android:paddingTop="12dp"
|
||||
android:paddingEnd="8dp"
|
||||
android:text="@string/label_placeholder" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/generic_playlist_text_view_clickable"
|
||||
style="@style/SubheadTextView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="8dp"
|
||||
android:paddingTop="12dp"
|
||||
android:paddingEnd="8dp"
|
||||
android:text="@string/generic_playlist_see_all_button" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/generic_playlist_recycler_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:clipToPadding="false"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingBottom="8dp" />
|
||||
</LinearLayout>
|
||||
|
|
@ -14,16 +14,6 @@
|
|||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/for_you_image_view"
|
||||
android:layout_width="20dp"
|
||||
android:layout_height="20dp"
|
||||
android:layout_gravity="center"
|
||||
android:background="@drawable/ic_shuffle"
|
||||
android:layout_marginTop="8dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/track_cover_image_view"
|
||||
app:layout_constraintStart_toStartOf="parent"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title_track_label"
|
||||
style="@style/ItemTitleTextView"
|
||||
|
|
@ -32,9 +22,8 @@
|
|||
android:ellipsize="end"
|
||||
android:maxLines="2"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingLeft="4dp"
|
||||
android:singleLine="false"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@id/for_you_image_view"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/track_cover_image_view" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
|
@ -62,6 +62,9 @@
|
|||
<action
|
||||
android:id="@+id/action_homeFragment_to_searchFragment"
|
||||
app:destination="@id/searchFragment" />
|
||||
<action
|
||||
android:id="@+id/action_homeFragment_to_playlistPageFragment"
|
||||
app:destination="@id/playlistPageFragment" />
|
||||
</fragment>
|
||||
<fragment
|
||||
android:id="@+id/libraryFragment"
|
||||
|
|
|
|||
|
|
@ -68,6 +68,7 @@
|
|||
<string name="filter_info_selection">Select two or more filters</string>
|
||||
<string name="filter_title">Filter</string>
|
||||
<string name="filter_title_expanded">Filter Genres</string>
|
||||
<string name="generic_playlist_see_all_button">See all</string>
|
||||
<string name="genre_catalogue_title">Genre Catalogue</string>
|
||||
<string name="genre_catalogue_title_expanded">Browse Genres</string>
|
||||
<string name="home_subtitle_made_for_you">Start mix from a song you liked</string>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue