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

@ -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) {