Added download UI in home screen

This commit is contained in:
CappielloAntonio 2021-04-26 19:37:05 +02:00
parent e0569c3901
commit bf70863f84
8 changed files with 109 additions and 3 deletions

View file

@ -67,6 +67,12 @@ public interface SongDao {
@Query("SELECT * FROM song WHERE favorite = 1") @Query("SELECT * FROM song WHERE favorite = 1")
LiveData<List<Song>> getFavoriteSong(); LiveData<List<Song>> getFavoriteSong();
@Query("SELECT * FROM song WHERE offline = 1 ORDER BY RANDOM() LIMIT :number")
LiveData<List<Song>> getDownloadedSongSample(int number);
@Query("SELECT * FROM song WHERE offline = 1")
LiveData<List<Song>> getDownloadedSong();
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<Song> songs); void insertAll(List<Song> songs);

View file

@ -48,6 +48,9 @@ public class Song implements Parcelable {
@Ignore @Ignore
public static final String IS_FAVORITE = "IS_FAVORITE"; public static final String IS_FAVORITE = "IS_FAVORITE";
@Ignore
public static final String DOWNLOADED = "DOWNLOADED";
@Ignore @Ignore
public static final String RADIO = "RADIO"; public static final String RADIO = "RADIO";

View file

@ -1,7 +1,6 @@
package com.cappielloantonio.play.repository; package com.cappielloantonio.play.repository;
import android.app.Application; import android.app.Application;
import android.util.Log;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
@ -33,6 +32,8 @@ public class SongRepository {
private LiveData<List<Song>> listLiveSongByYear; private LiveData<List<Song>> listLiveSongByYear;
private LiveData<List<Song>> listLiveSampleFavoritesSong; private LiveData<List<Song>> listLiveSampleFavoritesSong;
private LiveData<List<Song>> listLiveFavoritesSong; private LiveData<List<Song>> listLiveFavoritesSong;
private LiveData<List<Song>> listLiveSampleDownloadedSong;
private LiveData<List<Song>> listLiveDownloadedSong;
public SongRepository(Application application) { public SongRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application); AppDatabase database = AppDatabase.getInstance(application);
@ -314,6 +315,16 @@ public class SongRepository {
return listLiveFavoritesSong; return listLiveFavoritesSong;
} }
public LiveData<List<Song>> getListLiveDownloadedSampleSong(int number) {
listLiveSampleDownloadedSong = songDao.getDownloadedSongSample(number);
return listLiveSampleDownloadedSong;
}
public LiveData<List<Song>> getListLiveDownloadedSong() {
listLiveDownloadedSong = songDao.getDownloadedSong();
return listLiveDownloadedSong;
}
public void insertAll(ArrayList<Song> songs) { public void insertAll(ArrayList<Song> songs) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(songDao, songGenreCrossDao, songs); InsertAllThreadSafe insertAll = new InsertAllThreadSafe(songDao, songGenreCrossDao, songs);
Thread thread = new Thread(insertAll); Thread thread = new Thread(insertAll);

View file

@ -42,6 +42,7 @@ public class HomeFragment extends Fragment {
private SongResultSearchAdapter favoriteSongAdapter; private SongResultSearchAdapter favoriteSongAdapter;
private RecentMusicAdapter recentlyPlayedMusicAdapter; private RecentMusicAdapter recentlyPlayedMusicAdapter;
private RecentMusicAdapter mostPlayedMusicAdapter; private RecentMusicAdapter mostPlayedMusicAdapter;
private RecentMusicAdapter dowanloadedMusicAdapter;
@Nullable @Nullable
@Override @Override
@ -67,6 +68,7 @@ public class HomeFragment extends Fragment {
initFavoritesSongView(); initFavoritesSongView();
initYearSongView(); initYearSongView();
initRecentAddedSongView(); initRecentAddedSongView();
initDownloadedSongView();
} }
@Override @Override
@ -106,6 +108,12 @@ public class HomeFragment extends Fragment {
bundle.putString(Song.IS_FAVORITE, Song.IS_FAVORITE); bundle.putString(Song.IS_FAVORITE, Song.IS_FAVORITE);
activity.navController.navigate(R.id.action_homeFragment_to_songListPageFragment, bundle); activity.navController.navigate(R.id.action_homeFragment_to_songListPageFragment, bundle);
}); });
bind.downloadedTracksTextViewClickable.setOnClickListener(v -> {
Bundle bundle = new Bundle();
bundle.putString(Song.DOWNLOADED, Song.DOWNLOADED);
activity.navController.navigate(R.id.action_homeFragment_to_songListPageFragment, bundle);
});
} }
private void initDiscoverSongSlideView() { private void initDiscoverSongSlideView() {
@ -180,6 +188,18 @@ public class HomeFragment extends Fragment {
homeViewModel.getRecentlyAddedSongList().observe(requireActivity(), songs -> recentlyAddedMusicAdapter.setItems(songs)); homeViewModel.getRecentlyAddedSongList().observe(requireActivity(), songs -> recentlyAddedMusicAdapter.setItems(songs));
} }
private void initDownloadedSongView() {
bind.downloadedTracksRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false));
bind.downloadedTracksRecyclerView.setHasFixedSize(true);
dowanloadedMusicAdapter = new RecentMusicAdapter(activity, requireContext(), getChildFragmentManager());
bind.downloadedTracksRecyclerView.setAdapter(dowanloadedMusicAdapter);
homeViewModel.getDownloaded().observe(requireActivity(), songs -> {
if(bind != null) bind.homeDownloadedTracksSector.setVisibility(!songs.isEmpty() ? View.VISIBLE : View.GONE);
dowanloadedMusicAdapter.setItems(songs);
});
}
private void setDiscoverSongSlideViewOffset(float pageOffset, float pageMargin) { private void setDiscoverSongSlideViewOffset(float pageOffset, float pageMargin) {
bind.discoverSongViewPager.setPageTransformer((page, position) -> { bind.discoverSongViewPager.setPageTransformer((page, position) -> {
float myOffset = position * -(2 * pageOffset + pageMargin); float myOffset = position * -(2 * pageOffset + pageMargin);
@ -197,10 +217,10 @@ public class HomeFragment extends Fragment {
/* /*
* Il layout di default prevede questa sequenza: * Il layout di default prevede questa sequenza:
* - Discovery - Most_played - Last_played - Year - Favorite - Recently_added * - Discovery - Most_played - Last_played - Year - Favorite - Downloaded - Recently_added
* *
* Se però non ho ancora ascoltato nessuna canzone e quindi Most_played e Last_played sono vuoti, modifico come segue * Se però non ho ancora ascoltato nessuna canzone e quindi Most_played e Last_played sono vuoti, modifico come segue
* - Discovery - Recently_added - Year - Favorite - Most_played - Last_played * - Discovery - Recently_added - Year - Favorite - Downloaded - Most_played - Last_played
*/ */
public void reorder() { public void reorder() {
if(bind != null) { if(bind != null) {
@ -209,6 +229,7 @@ public class HomeFragment extends Fragment {
bind.homeLinearLayoutContainer.addView(bind.homeRecentlyAddedTracksSector); bind.homeLinearLayoutContainer.addView(bind.homeRecentlyAddedTracksSector);
bind.homeLinearLayoutContainer.addView(bind.homeFlashbackSector); bind.homeLinearLayoutContainer.addView(bind.homeFlashbackSector);
bind.homeLinearLayoutContainer.addView(bind.homeFavoriteTracksSector); bind.homeLinearLayoutContainer.addView(bind.homeFavoriteTracksSector);
bind.homeLinearLayoutContainer.addView(bind.homeDownloadedTracksSector);
bind.homeLinearLayoutContainer.addView(bind.homeMostPlayedTracksSector); bind.homeLinearLayoutContainer.addView(bind.homeMostPlayedTracksSector);
bind.homeLinearLayoutContainer.addView(bind.homeRecentlyPlayedTracksSector); bind.homeLinearLayoutContainer.addView(bind.homeRecentlyPlayedTracksSector);
} }

View file

@ -89,6 +89,10 @@ public class SongListPageFragment extends Fragment {
songListPageViewModel.title = Song.IS_FAVORITE; songListPageViewModel.title = Song.IS_FAVORITE;
bind.pageTitleLabel.setText("Favourite song"); bind.pageTitleLabel.setText("Favourite song");
} }
else if(getArguments().getString(Song.DOWNLOADED) != null) {
songListPageViewModel.title = Song.DOWNLOADED;
bind.pageTitleLabel.setText("Downloaded");
}
else if(getArguments().getString(Song.RADIO) != null) { else if(getArguments().getString(Song.RADIO) != null) {
songListPageViewModel.title = Song.IS_FAVORITE; songListPageViewModel.title = Song.IS_FAVORITE;
songListPageViewModel.year = getArguments().getInt("radio_object"); songListPageViewModel.year = getArguments().getInt("radio_object");

View file

@ -20,6 +20,7 @@ public class HomeViewModel extends AndroidViewModel {
private LiveData<List<Song>> recentlyAddedSongSample; private LiveData<List<Song>> recentlyAddedSongSample;
private LiveData<List<Song>> mostPlayedSongSample; private LiveData<List<Song>> mostPlayedSongSample;
private LiveData<List<Song>> favoritesSongSample; private LiveData<List<Song>> favoritesSongSample;
private LiveData<List<Song>> downloadedSongSample;
private List<Integer> years; private List<Integer> years;
public HomeViewModel(@NonNull Application application) { public HomeViewModel(@NonNull Application application) {
@ -32,6 +33,7 @@ public class HomeViewModel extends AndroidViewModel {
recentlyAddedSongSample = songRepository.getListLiveRecentlyAddedSampleSong(20); recentlyAddedSongSample = songRepository.getListLiveRecentlyAddedSampleSong(20);
mostPlayedSongSample = songRepository.getListLiveMostPlayedSampleSong(20); mostPlayedSongSample = songRepository.getListLiveMostPlayedSampleSong(20);
favoritesSongSample = songRepository.getListLiveFavoritesSampleSong(20); favoritesSongSample = songRepository.getListLiveFavoritesSampleSong(20);
downloadedSongSample = songRepository.getListLiveDownloadedSampleSong(20);
years = songRepository.getYearList(); years = songRepository.getYearList();
} }
@ -63,4 +65,8 @@ public class HomeViewModel extends AndroidViewModel {
public LiveData<List<Song>> getFavorites() { public LiveData<List<Song>> getFavorites() {
return favoritesSongSample; return favoritesSongSample;
} }
public LiveData<List<Song>> getDownloaded() {
return downloadedSongSample;
}
} }

View file

@ -59,6 +59,9 @@ public class SongListPageViewModel extends AndroidViewModel {
case Song.IS_FAVORITE: case Song.IS_FAVORITE:
songList = songRepository.getListLiveFavoritesSong(); songList = songRepository.getListLiveFavoritesSong();
break; break;
case Song.DOWNLOADED:
songList = songRepository.getListLiveDownloadedSong();
break;
} }
return songList; return songList;

View file

@ -222,6 +222,58 @@
android:paddingBottom="8dp" /> android:paddingBottom="8dp" />
</LinearLayout> </LinearLayout>
<!-- Downloaded tracks -->
<LinearLayout
android:id="@+id/home_downloaded_tracks_sector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Label and button -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="8dp"
android:paddingTop="8dp"
android:paddingEnd="8dp">
<TextView
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="Downloaded" />
<TextView
android:id="@+id/downloaded_tracks_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="See all" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/downloaded_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>
<!-- Recently added tracks --> <!-- Recently added tracks -->
<LinearLayout <LinearLayout
android:id="@+id/home_recently_added_tracks_sector" android:id="@+id/home_recently_added_tracks_sector"