Implemented songs division by decades

This commit is contained in:
CappielloAntonio 2021-08-04 11:12:21 +02:00
parent bc51b0c9ca
commit 491fa4de3e
11 changed files with 112 additions and 54 deletions

View file

@ -7,11 +7,11 @@
<deviceKey> <deviceKey>
<Key> <Key>
<type value="VIRTUAL_DEVICE_PATH" /> <type value="VIRTUAL_DEVICE_PATH" />
<value value="$USER_HOME$/.android/avd/Pixel_3a_API_30_x86.avd" /> <value value="$USER_HOME$/.android/avd/Pixel_4_XL_API_31.avd" />
</Key> </Key>
</deviceKey> </deviceKey>
</Target> </Target>
</targetSelectedWithDropDown> </targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2021-08-04T06:28:38.102773Z" /> <timeTargetWasSelectedWithDropDown value="2021-08-04T07:08:43.793757Z" />
</component> </component>
</project> </project>

View file

@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.cappielloantonio.play.R; import com.cappielloantonio.play.R;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class YearAdapter extends RecyclerView.Adapter<YearAdapter.ViewHolder> { public class YearAdapter extends RecyclerView.Adapter<YearAdapter.ViewHolder> {
@ -19,10 +20,10 @@ public class YearAdapter extends RecyclerView.Adapter<YearAdapter.ViewHolder> {
private Context context; private Context context;
private ItemClickListener itemClickListener; private ItemClickListener itemClickListener;
public YearAdapter(Context context, List<Integer> years) { public YearAdapter(Context context) {
this.context = context; this.context = context;
this.mInflater = LayoutInflater.from(context); this.mInflater = LayoutInflater.from(context);
this.years = years; this.years = new ArrayList<>();
} }
@Override @Override

View file

@ -0,0 +1,6 @@
package com.cappielloantonio.play.interfaces;
public interface DecadesCallback {
void onLoadYear(int year);
}

View file

@ -6,6 +6,7 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.App; import com.cappielloantonio.play.App;
import com.cappielloantonio.play.interfaces.DecadesCallback;
import com.cappielloantonio.play.interfaces.MediaCallback; import com.cappielloantonio.play.interfaces.MediaCallback;
import com.cappielloantonio.play.model.Album; import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Song; import com.cappielloantonio.play.model.Song;
@ -14,7 +15,10 @@ import com.cappielloantonio.play.subsonic.models.SubsonicResponse;
import com.cappielloantonio.play.util.MappingUtil; import com.cappielloantonio.play.util.MappingUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
@ -37,7 +41,7 @@ public class AlbumRepository {
public LiveData<List<Album>> getAlbums(String type, int size) { public LiveData<List<Album>> getAlbums(String type, int size) {
App.getSubsonicClientInstance(application, false) App.getSubsonicClientInstance(application, false)
.getAlbumSongListClient() .getAlbumSongListClient()
.getAlbumList2(type, size, 0) .getAlbumList2(type, size, 0, null, null)
.enqueue(new Callback<SubsonicResponse>() { .enqueue(new Callback<SubsonicResponse>() {
@Override @Override
public void onResponse(Call<SubsonicResponse> call, Response<SubsonicResponse> response) { public void onResponse(Call<SubsonicResponse> call, Response<SubsonicResponse> response) {
@ -265,4 +269,68 @@ public class AlbumRepository {
} }
}); });
} }
public MutableLiveData<List<Integer>> getDecades() {
MutableLiveData<List<Integer>> decades = new MutableLiveData<>();
getFirstAlbum(first -> {
getLastAlbum(last -> {
List<Integer> decadeList = new ArrayList();
int startDecade = first - (first % 10);
int lastDecade = last - (last % 10);
while (startDecade <= lastDecade) {
decadeList.add(startDecade);
startDecade = startDecade + 10;
}
decades.setValue(decadeList);
});
});
return decades;
}
private void getFirstAlbum(DecadesCallback callback) {
App.getSubsonicClientInstance(application, false)
.getAlbumSongListClient()
.getAlbumList2("byYear", 1, 0, 0, Calendar.getInstance().get(Calendar.YEAR))
.enqueue(new Callback<SubsonicResponse>() {
@Override
public void onResponse(Call<SubsonicResponse> call, Response<SubsonicResponse> response) {
if (response.body().getStatus().getValue().equals(ResponseStatus.OK)) {
if(response.body().getAlbumList2().getAlbums().get(0) != null){
callback.onLoadYear(response.body().getAlbumList2().getAlbums().get(0).getYear());
}
}
}
@Override
public void onFailure(Call<SubsonicResponse> call, Throwable t) {
}
});
}
private void getLastAlbum(DecadesCallback callback) {
App.getSubsonicClientInstance(application, false)
.getAlbumSongListClient()
.getAlbumList2("byYear", 1, 0, Calendar.getInstance().get(Calendar.YEAR), 0)
.enqueue(new Callback<SubsonicResponse>() {
@Override
public void onResponse(Call<SubsonicResponse> call, Response<SubsonicResponse> response) {
if (response.body().getStatus().getValue().equals(ResponseStatus.OK)) {
if(response.body().getAlbumList2().getAlbums().get(0) != null){
callback.onLoadYear(response.body().getAlbumList2().getAlbums().get(0).getYear());
}
}
}
@Override
public void onFailure(Call<SubsonicResponse> call, Throwable t) {
}
});
}
} }

View file

@ -25,13 +25,13 @@ public class SongRepository {
private Application application; private Application application;
private MutableLiveData<List<Song>> starredSongs = new MutableLiveData<>();
public SongRepository(Application application) { public SongRepository(Application application) {
this.application = application; this.application = application;
} }
public MutableLiveData<List<Song>> getStarredSongs() { public MutableLiveData<List<Song>> getStarredSongs() {
MutableLiveData<List<Song>> starredSongs = new MutableLiveData<>();
App.getSubsonicClientInstance(application, false) App.getSubsonicClientInstance(application, false)
.getAlbumSongListClient() .getAlbumSongListClient()
.getStarred2() .getStarred2()
@ -80,27 +80,29 @@ public class SongRepository {
}); });
} }
public void getRandomSample(int number, MediaCallback callback) { public MutableLiveData<List<Song>> getRandomSample(int number, Integer fromYear, Integer toYear) {
MutableLiveData<List<Song>> randomSongsSample = new MutableLiveData<>(new ArrayList<>());
App.getSubsonicClientInstance(application, false) App.getSubsonicClientInstance(application, false)
.getAlbumSongListClient() .getAlbumSongListClient()
.getRandomSongs(number) .getRandomSongs(number, fromYear, toYear)
.enqueue(new Callback<SubsonicResponse>() { .enqueue(new Callback<SubsonicResponse>() {
@Override @Override
public void onResponse(Call<SubsonicResponse> call, Response<SubsonicResponse> response) { public void onResponse(Call<SubsonicResponse> call, Response<SubsonicResponse> response) {
List<Song> songs = new ArrayList<>();
if (response.body().getStatus().getValue().equals(ResponseStatus.OK)) { if (response.body().getStatus().getValue().equals(ResponseStatus.OK)) {
songs = new ArrayList<>(MappingUtil.mapSong(response.body().getRandomSongs().getSongs())); List<Song> songs = new ArrayList<>(MappingUtil.mapSong(response.body().getRandomSongs().getSongs()));
randomSongsSample.setValue(songs);
} }
callback.onLoadMedia(songs);
} }
@Override @Override
public void onFailure(Call<SubsonicResponse> call, Throwable t) { public void onFailure(Call<SubsonicResponse> call, Throwable t) {
callback.onError(new Exception(t.getMessage()));
} }
}); });
return randomSongsSample;
} }
public void scrobble(String id) { public void scrobble(String id) {

View file

@ -35,14 +35,14 @@ public class AlbumSongListClient {
return albumSongListService.getAlbumList(subsonic.getParams(), type, size, offset); return albumSongListService.getAlbumList(subsonic.getParams(), type, size, offset);
} }
public Call<SubsonicResponse> getAlbumList2(String type, int size, int offset) { public Call<SubsonicResponse> getAlbumList2(String type, int size, int offset, Integer fromYear, Integer toYear) {
Log.d(TAG, "getAlbumList2()"); Log.d(TAG, "getAlbumList2()");
return albumSongListService.getAlbumList2(subsonic.getParams(), type, size, offset); return albumSongListService.getAlbumList2(subsonic.getParams(), type, size, offset, fromYear, toYear);
} }
public Call<SubsonicResponse> getRandomSongs(int size) { public Call<SubsonicResponse> getRandomSongs(int size, Integer fromYear, Integer toYear) {
Log.d(TAG, "getRandomSongs()"); Log.d(TAG, "getRandomSongs()");
return albumSongListService.getRandomSongs(subsonic.getParams(), size); return albumSongListService.getRandomSongs(subsonic.getParams(), size, fromYear, toYear);
} }
public Call<SubsonicResponse> getSongsByGenre(String genre, int count, int offset) { public Call<SubsonicResponse> getSongsByGenre(String genre, int count, int offset) {

View file

@ -14,10 +14,10 @@ public interface AlbumSongListService {
Call<SubsonicResponse> getAlbumList(@QueryMap Map<String, String> params, @Query("type") String type, @Query("size") int size, @Query("offset") int offset); Call<SubsonicResponse> getAlbumList(@QueryMap Map<String, String> params, @Query("type") String type, @Query("size") int size, @Query("offset") int offset);
@GET("getAlbumList2") @GET("getAlbumList2")
Call<SubsonicResponse> getAlbumList2(@QueryMap Map<String, String> params, @Query("type") String type, @Query("size") int size, @Query("offset") int offset); Call<SubsonicResponse> getAlbumList2(@QueryMap Map<String, String> params, @Query("type") String type, @Query("size") int size, @Query("offset") int offset, @Query("fromYear") Integer fromYear, @Query("toYear") Integer toYear);
@GET("getRandomSongs") @GET("getRandomSongs")
Call<SubsonicResponse> getRandomSongs(@QueryMap Map<String, String> params, @Query("size") int size); Call<SubsonicResponse> getRandomSongs(@QueryMap Map<String, String> params, @Query("size") int size, @Query("fromYear") Integer fromYear, @Query("toYear") Integer toYear);
@GET("getSongsByGenre") @GET("getSongsByGenre")
Call<SubsonicResponse> getSongsByGenre(@QueryMap Map<String, String> params, @Query("genre") String genre, @Query("count") int count, @Query("offset") int offset); Call<SubsonicResponse> getSongsByGenre(@QueryMap Map<String, String> params, @Query("genre") String genre, @Query("count") int count, @Query("offset") int offset);

View file

@ -179,13 +179,10 @@ public class HomeFragment extends Fragment {
} }
private void initYearSongView() { private void initYearSongView() {
if (bind != null)
bind.homeFlashbackSector.setVisibility(!homeViewModel.getYearList().isEmpty() ? View.VISIBLE : View.GONE);
bind.yearsRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)); bind.yearsRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false));
bind.yearsRecyclerView.setHasFixedSize(true); bind.yearsRecyclerView.setHasFixedSize(true);
yearAdapter = new YearAdapter(requireContext(), homeViewModel.getYearList()); yearAdapter = new YearAdapter(requireContext());
yearAdapter.setClickListener((view, position) -> { yearAdapter.setClickListener((view, position) -> {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(Song.BY_YEAR, Song.BY_YEAR); bundle.putString(Song.BY_YEAR, Song.BY_YEAR);
@ -193,6 +190,10 @@ public class HomeFragment extends Fragment {
activity.navController.navigate(R.id.action_homeFragment_to_songListPageFragment, bundle); activity.navController.navigate(R.id.action_homeFragment_to_songListPageFragment, bundle);
}); });
bind.yearsRecyclerView.setAdapter(yearAdapter); bind.yearsRecyclerView.setAdapter(yearAdapter);
homeViewModel.getYearList().observe(requireActivity(), years -> {
if (bind != null) bind.homeFlashbackSector.setVisibility(!years.isEmpty() ? View.VISIBLE : View.GONE);
yearAdapter.setItems(years);
});
} }
private void initStarredTracksView() { private void initStarredTracksView() {

View file

@ -63,7 +63,7 @@ public class AlbumCatalogueViewModel extends AndroidViewModel {
private void retrieveAlbums(Context context, MediaCallback callback, int size, int offset) { private void retrieveAlbums(Context context, MediaCallback callback, int size, int offset) {
App.getSubsonicClientInstance(context, false) App.getSubsonicClientInstance(context, false)
.getAlbumSongListClient() .getAlbumSongListClient()
.getAlbumList2("alphabeticalByName", size, offset) .getAlbumList2("alphabeticalByName", size, offset, null, null)
.enqueue(new Callback<SubsonicResponse>() { .enqueue(new Callback<SubsonicResponse>() {
@Override @Override
public void onResponse(Call<SubsonicResponse> call, retrofit2.Response<SubsonicResponse> response) { public void onResponse(Call<SubsonicResponse> call, retrofit2.Response<SubsonicResponse> response) {

View file

@ -19,6 +19,7 @@ import com.cappielloantonio.play.repository.SongRepository;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
public class HomeViewModel extends AndroidViewModel { public class HomeViewModel extends AndroidViewModel {
private static final String TAG = "HomeViewModel"; private static final String TAG = "HomeViewModel";
@ -27,13 +28,12 @@ public class HomeViewModel extends AndroidViewModel {
private ArtistRepository artistRepository; private ArtistRepository artistRepository;
private DownloadRepository downloadRepository; private DownloadRepository downloadRepository;
private LiveData<List<Download>> downloadedSongSample; private LiveData<List<Song>> dicoverSongSample;
private List<Integer> years;
private MutableLiveData<List<Song>> dicoverSongSample;
private LiveData<List<Album>> mostPlayedAlbumSample; private LiveData<List<Album>> mostPlayedAlbumSample;
private LiveData<List<Album>> recentlyAddedAlbumSample; private LiveData<List<Album>> recentlyAddedAlbumSample;
private LiveData<List<Album>> recentlyPlayedAlbumSample; private LiveData<List<Album>> recentlyPlayedAlbumSample;
private LiveData<List<Download>> downloadedSongSample;
private LiveData<List<Integer>> years;
private LiveData<List<Song>> starredTracks; private LiveData<List<Song>> starredTracks;
private LiveData<List<Album>> starredAlbums; private LiveData<List<Album>> starredAlbums;
@ -47,18 +47,12 @@ public class HomeViewModel extends AndroidViewModel {
artistRepository = new ArtistRepository(application); artistRepository = new ArtistRepository(application);
downloadRepository = new DownloadRepository(application); downloadRepository = new DownloadRepository(application);
// favoritesSongSample = songRepository.getListLiveFavoritesSampleSong(20); dicoverSongSample = songRepository.getRandomSample(10, null, null);
// downloadedSongSample = songRepository.getListLiveDownloadedSampleSong(20);
// years = songRepository.getYearList();
setDicoverSongSample();
dicoverSongSample = new MutableLiveData<>();
downloadedSongSample = downloadRepository.getLiveDownloadSample(10);
years = new ArrayList<>();
mostPlayedAlbumSample = albumRepository.getAlbums("frequent", 20); mostPlayedAlbumSample = albumRepository.getAlbums("frequent", 20);
recentlyAddedAlbumSample = albumRepository.getAlbums("newest", 20); recentlyAddedAlbumSample = albumRepository.getAlbums("newest", 20);
recentlyPlayedAlbumSample = albumRepository.getAlbums("recent", 20); recentlyPlayedAlbumSample = albumRepository.getAlbums("recent", 20);
downloadedSongSample = downloadRepository.getLiveDownloadSample(10);
years = albumRepository.getDecades();
starredTracks = songRepository.getStarredSongs(); starredTracks = songRepository.getStarredSongs();
starredAlbums = albumRepository.getStarredAlbums(); starredAlbums = albumRepository.getStarredAlbums();
@ -73,7 +67,7 @@ public class HomeViewModel extends AndroidViewModel {
return dicoverSongSample; return dicoverSongSample;
} }
public List<Integer> getYearList() { public LiveData<List<Integer>> getYearList() {
return years; return years;
} }
@ -104,18 +98,4 @@ public class HomeViewModel extends AndroidViewModel {
public LiveData<List<Album>> getRecentlyPlayedAlbumList() { public LiveData<List<Album>> getRecentlyPlayedAlbumList() {
return recentlyPlayedAlbumSample; return recentlyPlayedAlbumSample;
} }
private void setDicoverSongSample() {
songRepository.getRandomSample(10, new MediaCallback() {
@Override
public void onError(Exception exception) {
}
@Override
public void onLoadMedia(List<?> media) {
dicoverSongSample.setValue((List<Song>) media);
}
});
}
} }

View file

@ -66,7 +66,7 @@ public class SongListPageViewModel extends AndroidViewModel {
songList = songRepository.getSongsByGenres(filters); songList = songRepository.getSongsByGenres(filters);
break; break;
case Song.BY_YEAR: case Song.BY_YEAR:
// songList = songRepository.getSongByYearListLive(year); songList = songRepository.getRandomSample(500, year, year + 10);
break; break;
case Song.STARRED: case Song.STARRED:
songList = songRepository.getStarredSongs(); songList = songRepository.getStarredSongs();