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>
<Key>
<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>
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2021-08-04T06:28:38.102773Z" />
<timeTargetWasSelectedWithDropDown value="2021-08-04T07:08:43.793757Z" />
</component>
</project>

View file

@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.cappielloantonio.play.R;
import java.util.ArrayList;
import java.util.List;
public class YearAdapter extends RecyclerView.Adapter<YearAdapter.ViewHolder> {
@ -19,10 +20,10 @@ public class YearAdapter extends RecyclerView.Adapter<YearAdapter.ViewHolder> {
private Context context;
private ItemClickListener itemClickListener;
public YearAdapter(Context context, List<Integer> years) {
public YearAdapter(Context context) {
this.context = context;
this.mInflater = LayoutInflater.from(context);
this.years = years;
this.years = new ArrayList<>();
}
@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 com.cappielloantonio.play.App;
import com.cappielloantonio.play.interfaces.DecadesCallback;
import com.cappielloantonio.play.interfaces.MediaCallback;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Song;
@ -14,7 +15,10 @@ import com.cappielloantonio.play.subsonic.models.SubsonicResponse;
import com.cappielloantonio.play.util.MappingUtil;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
@ -37,7 +41,7 @@ public class AlbumRepository {
public LiveData<List<Album>> getAlbums(String type, int size) {
App.getSubsonicClientInstance(application, false)
.getAlbumSongListClient()
.getAlbumList2(type, size, 0)
.getAlbumList2(type, size, 0, null, null)
.enqueue(new Callback<SubsonicResponse>() {
@Override
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 MutableLiveData<List<Song>> starredSongs = new MutableLiveData<>();
public SongRepository(Application application) {
this.application = application;
}
public MutableLiveData<List<Song>> getStarredSongs() {
MutableLiveData<List<Song>> starredSongs = new MutableLiveData<>();
App.getSubsonicClientInstance(application, false)
.getAlbumSongListClient()
.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)
.getAlbumSongListClient()
.getRandomSongs(number)
.getRandomSongs(number, fromYear, toYear)
.enqueue(new Callback<SubsonicResponse>() {
@Override
public void onResponse(Call<SubsonicResponse> call, Response<SubsonicResponse> response) {
List<Song> songs = new ArrayList<>();
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
public void onFailure(Call<SubsonicResponse> call, Throwable t) {
callback.onError(new Exception(t.getMessage()));
}
});
return randomSongsSample;
}
public void scrobble(String id) {

View file

@ -35,14 +35,14 @@ public class AlbumSongListClient {
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()");
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()");
return albumSongListService.getRandomSongs(subsonic.getParams(), size);
return albumSongListService.getRandomSongs(subsonic.getParams(), size, fromYear, toYear);
}
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);
@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")
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")
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() {
if (bind != null)
bind.homeFlashbackSector.setVisibility(!homeViewModel.getYearList().isEmpty() ? View.VISIBLE : View.GONE);
bind.yearsRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false));
bind.yearsRecyclerView.setHasFixedSize(true);
yearAdapter = new YearAdapter(requireContext(), homeViewModel.getYearList());
yearAdapter = new YearAdapter(requireContext());
yearAdapter.setClickListener((view, position) -> {
Bundle bundle = new Bundle();
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);
});
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() {

View file

@ -63,7 +63,7 @@ public class AlbumCatalogueViewModel extends AndroidViewModel {
private void retrieveAlbums(Context context, MediaCallback callback, int size, int offset) {
App.getSubsonicClientInstance(context, false)
.getAlbumSongListClient()
.getAlbumList2("alphabeticalByName", size, offset)
.getAlbumList2("alphabeticalByName", size, offset, null, null)
.enqueue(new Callback<SubsonicResponse>() {
@Override
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.List;
import java.util.Map;
public class HomeViewModel extends AndroidViewModel {
private static final String TAG = "HomeViewModel";
@ -27,13 +28,12 @@ public class HomeViewModel extends AndroidViewModel {
private ArtistRepository artistRepository;
private DownloadRepository downloadRepository;
private LiveData<List<Download>> downloadedSongSample;
private List<Integer> years;
private MutableLiveData<List<Song>> dicoverSongSample;
private LiveData<List<Song>> dicoverSongSample;
private LiveData<List<Album>> mostPlayedAlbumSample;
private LiveData<List<Album>> recentlyAddedAlbumSample;
private LiveData<List<Album>> recentlyPlayedAlbumSample;
private LiveData<List<Download>> downloadedSongSample;
private LiveData<List<Integer>> years;
private LiveData<List<Song>> starredTracks;
private LiveData<List<Album>> starredAlbums;
@ -47,18 +47,12 @@ public class HomeViewModel extends AndroidViewModel {
artistRepository = new ArtistRepository(application);
downloadRepository = new DownloadRepository(application);
// favoritesSongSample = songRepository.getListLiveFavoritesSampleSong(20);
// downloadedSongSample = songRepository.getListLiveDownloadedSampleSong(20);
// years = songRepository.getYearList();
setDicoverSongSample();
dicoverSongSample = new MutableLiveData<>();
downloadedSongSample = downloadRepository.getLiveDownloadSample(10);
years = new ArrayList<>();
dicoverSongSample = songRepository.getRandomSample(10, null, null);
mostPlayedAlbumSample = albumRepository.getAlbums("frequent", 20);
recentlyAddedAlbumSample = albumRepository.getAlbums("newest", 20);
recentlyPlayedAlbumSample = albumRepository.getAlbums("recent", 20);
downloadedSongSample = downloadRepository.getLiveDownloadSample(10);
years = albumRepository.getDecades();
starredTracks = songRepository.getStarredSongs();
starredAlbums = albumRepository.getStarredAlbums();
@ -73,7 +67,7 @@ public class HomeViewModel extends AndroidViewModel {
return dicoverSongSample;
}
public List<Integer> getYearList() {
public LiveData<List<Integer>> getYearList() {
return years;
}
@ -104,18 +98,4 @@ public class HomeViewModel extends AndroidViewModel {
public LiveData<List<Album>> getRecentlyPlayedAlbumList() {
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);
break;
case Song.BY_YEAR:
// songList = songRepository.getSongByYearListLive(year);
songList = songRepository.getRandomSample(500, year, year + 10);
break;
case Song.STARRED:
songList = songRepository.getStarredSongs();