mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
First experiment with on-scrolling pagination on track list by genre
This commit is contained in:
parent
b345695fa2
commit
35bc02e6d9
4 changed files with 83 additions and 16 deletions
|
|
@ -0,0 +1,36 @@
|
|||
package com.cappielloantonio.play.helper.recyclerview;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
|
||||
public abstract class PaginationScrollListener extends RecyclerView.OnScrollListener {
|
||||
private static final String TAG = "PaginationScrollListener";
|
||||
|
||||
private LinearLayoutManager layoutManager;
|
||||
|
||||
protected PaginationScrollListener(LinearLayoutManager layoutManager) {
|
||||
this.layoutManager = layoutManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||||
super.onScrolled(recyclerView, dx, dy);
|
||||
|
||||
int visibleItemCount = layoutManager.getChildCount();
|
||||
int totalItemCount = layoutManager.getItemCount();
|
||||
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
|
||||
|
||||
if (!isLoading()) {
|
||||
if (firstVisibleItemPosition >= 0 && (visibleItemCount + firstVisibleItemPosition) >= totalItemCount) {
|
||||
loadMoreItems();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void loadMoreItems();
|
||||
|
||||
public abstract boolean isLoading();
|
||||
|
||||
}
|
||||
|
|
@ -11,7 +11,6 @@ import com.cappielloantonio.play.subsonic.models.Child;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
import retrofit2.Call;
|
||||
|
|
@ -171,27 +170,19 @@ public class SongRepository {
|
|||
});
|
||||
}
|
||||
|
||||
public MutableLiveData<List<Child>> getSongsByGenre(String id) {
|
||||
public MutableLiveData<List<Child>> getSongsByGenre(String id, int page) {
|
||||
MutableLiveData<List<Child>> songsByGenre = new MutableLiveData<>();
|
||||
|
||||
Log.d(TAG, "onScrolled PAGE: " + page);
|
||||
|
||||
App.getSubsonicClientInstance(false)
|
||||
.getAlbumSongListClient()
|
||||
.getSongsByGenre(id, 500, 0)
|
||||
.getSongsByGenre(id, 100, 100 * page)
|
||||
.enqueue(new Callback<ApiResponse>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
|
||||
if (response.isSuccessful() && response.body() != null && response.body().getSubsonicResponse().getSongsByGenre() != null) {
|
||||
List<Child> newSongs = response.body().getSubsonicResponse().getSongsByGenre().getSongs();
|
||||
List<Child> songs = songsByGenre.getValue();
|
||||
|
||||
if (songs == null) songs = new ArrayList<>();
|
||||
songs.addAll(newSongs);
|
||||
Collections.shuffle(songs);
|
||||
|
||||
LinkedHashSet<Child> hashSet = new LinkedHashSet<>(songs);
|
||||
ArrayList<Child> songsWithoutDuplicates = new ArrayList<>(hashSet);
|
||||
|
||||
songsByGenre.setValue(songsWithoutDuplicates);
|
||||
songsByGenre.setValue(response.body().getSubsonicResponse().getSongsByGenre().getSongs());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
|
|||
|
||||
import com.cappielloantonio.play.R;
|
||||
import com.cappielloantonio.play.databinding.FragmentSongListPageBinding;
|
||||
import com.cappielloantonio.play.helper.recyclerview.PaginationScrollListener;
|
||||
import com.cappielloantonio.play.interfaces.ClickCallback;
|
||||
import com.cappielloantonio.play.service.MediaManager;
|
||||
import com.cappielloantonio.play.service.MediaService;
|
||||
|
|
@ -32,6 +33,8 @@ import java.util.Collections;
|
|||
|
||||
@UnstableApi
|
||||
public class SongListPageFragment extends Fragment implements ClickCallback {
|
||||
private static final String TAG = "SongListPageFragment";
|
||||
|
||||
private FragmentSongListPageBinding bind;
|
||||
private MainActivity activity;
|
||||
private SongListPageViewModel songListPageViewModel;
|
||||
|
|
@ -40,6 +43,8 @@ public class SongListPageFragment extends Fragment implements ClickCallback {
|
|||
|
||||
private ListenableFuture<MediaBrowser> mediaBrowserListenableFuture;
|
||||
|
||||
private boolean isLoading = true;
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
activity = (MainActivity) getActivity();
|
||||
|
|
@ -163,7 +168,23 @@ public class SongListPageFragment extends Fragment implements ClickCallback {
|
|||
|
||||
songHorizontalAdapter = new SongHorizontalAdapter(this, true);
|
||||
bind.songListRecyclerView.setAdapter(songHorizontalAdapter);
|
||||
songListPageViewModel.getSongList().observe(getViewLifecycleOwner(), songs -> songHorizontalAdapter.setItems(songs));
|
||||
songListPageViewModel.getSongList().observe(getViewLifecycleOwner(), songs -> {
|
||||
isLoading = false;
|
||||
songHorizontalAdapter.setItems(songs);
|
||||
});
|
||||
|
||||
bind.songListRecyclerView.addOnScrollListener(new PaginationScrollListener((LinearLayoutManager) bind.songListRecyclerView.getLayoutManager()) {
|
||||
@Override
|
||||
protected void loadMoreItems() {
|
||||
isLoading = true;
|
||||
songListPageViewModel.getSongsByPage(getViewLifecycleOwner());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLoading() {
|
||||
return isLoading;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void initializeMediaBrowser() {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import android.text.TextUtils;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
|
|
@ -48,7 +49,7 @@ public class SongListPageViewModel extends AndroidViewModel {
|
|||
|
||||
switch (title) {
|
||||
case Constants.MEDIA_BY_GENRE:
|
||||
songList = songRepository.getSongsByGenre(genre.getGenre());
|
||||
songList = songRepository.getSongsByGenre(genre.getGenre(), 0);
|
||||
break;
|
||||
case Constants.MEDIA_BY_ARTIST:
|
||||
songList = artistRepository.getTopSongs(artist.getName(), 50);
|
||||
|
|
@ -67,6 +68,24 @@ public class SongListPageViewModel extends AndroidViewModel {
|
|||
return songList;
|
||||
}
|
||||
|
||||
public void getSongsByPage(LifecycleOwner owner) {
|
||||
switch (title) {
|
||||
case Constants.MEDIA_BY_GENRE:
|
||||
int page = (songList.getValue() != null ? songList.getValue().size() : 0) / 100;
|
||||
songRepository.getSongsByGenre(genre.getGenre(), page).observe(owner, children -> {
|
||||
List<Child> currentMedia = songList.getValue();
|
||||
currentMedia.addAll(children);
|
||||
songList.setValue(currentMedia);
|
||||
});
|
||||
break;
|
||||
case Constants.MEDIA_BY_ARTIST:
|
||||
case Constants.MEDIA_BY_GENRES:
|
||||
case Constants.MEDIA_BY_YEAR:
|
||||
case Constants.MEDIA_STARRED:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public String getFiltersTitle() {
|
||||
return TextUtils.join(", ", filterNames);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue