update adapters on query change
This commit is contained in:
parent
1809a5d1dd
commit
a88e851c4f
9 changed files with 68 additions and 20 deletions
|
|
@ -26,8 +26,7 @@ public class MusicPlayerRemote {
|
|||
|
||||
private static final WeakHashMap<Context, ServiceBinder> mConnectionMap = new WeakHashMap<>();
|
||||
|
||||
public static ServiceToken bindToService(@NonNull final Context context,
|
||||
final ServiceConnection callback) {
|
||||
public static ServiceToken bindToService(@NonNull final Context context, final ServiceConnection callback) {
|
||||
Activity realActivity = ((Activity) context).getParent();
|
||||
if (realActivity == null) {
|
||||
realActivity = (Activity) context;
|
||||
|
|
@ -42,6 +41,7 @@ public class MusicPlayerRemote {
|
|||
mConnectionMap.put(contextWrapper, binder);
|
||||
return new ServiceToken(contextWrapper);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
@ -49,11 +49,13 @@ public class MusicPlayerRemote {
|
|||
if (token == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ContextWrapper mContextWrapper = token.mWrappedContext;
|
||||
final ServiceBinder mBinder = mConnectionMap.remove(mContextWrapper);
|
||||
if (mBinder == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
mContextWrapper.unbindService(mBinder);
|
||||
if (mConnectionMap.isEmpty()) {
|
||||
musicService = null;
|
||||
|
|
@ -81,6 +83,7 @@ public class MusicPlayerRemote {
|
|||
if (mCallback != null) {
|
||||
mCallback.onServiceDisconnected(className);
|
||||
}
|
||||
|
||||
musicService = null;
|
||||
}
|
||||
}
|
||||
|
|
@ -178,6 +181,7 @@ public class MusicPlayerRemote {
|
|||
if (musicService != null) {
|
||||
return musicService.getCurrentSong();
|
||||
}
|
||||
|
||||
return Song.EMPTY_SONG;
|
||||
}
|
||||
|
||||
|
|
@ -185,6 +189,7 @@ public class MusicPlayerRemote {
|
|||
if (musicService != null) {
|
||||
return musicService.getPosition();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -192,6 +197,7 @@ public class MusicPlayerRemote {
|
|||
if (musicService != null) {
|
||||
return musicService.getPlayingQueue();
|
||||
}
|
||||
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
|
|
@ -199,6 +205,7 @@ public class MusicPlayerRemote {
|
|||
if (musicService != null) {
|
||||
return musicService.getSongProgressMillis();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -206,6 +213,7 @@ public class MusicPlayerRemote {
|
|||
if (musicService != null) {
|
||||
return musicService.getSongDurationMillis();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -213,6 +221,7 @@ public class MusicPlayerRemote {
|
|||
if (musicService != null) {
|
||||
return musicService.getQueueDurationMillis(position);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -220,6 +229,7 @@ public class MusicPlayerRemote {
|
|||
if (musicService != null) {
|
||||
return musicService.seek(millis);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -227,6 +237,7 @@ public class MusicPlayerRemote {
|
|||
if (musicService != null) {
|
||||
return musicService.getRepeatMode();
|
||||
}
|
||||
|
||||
return MusicService.REPEAT_MODE_NONE;
|
||||
}
|
||||
|
||||
|
|
@ -234,6 +245,7 @@ public class MusicPlayerRemote {
|
|||
if (musicService != null) {
|
||||
return musicService.getShuffleMode();
|
||||
}
|
||||
|
||||
return MusicService.SHUFFLE_MODE_NONE;
|
||||
}
|
||||
|
||||
|
|
@ -242,6 +254,7 @@ public class MusicPlayerRemote {
|
|||
musicService.cycleRepeatMode();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -250,6 +263,7 @@ public class MusicPlayerRemote {
|
|||
musicService.toggleShuffle();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.ContentObserver;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
|
@ -24,7 +23,6 @@ import android.os.Message;
|
|||
import android.os.PowerManager;
|
||||
import android.os.Process;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.v4.media.MediaMetadataCompat;
|
||||
import android.support.v4.media.session.MediaSessionCompat;
|
||||
import android.support.v4.media.session.PlaybackStateCompat;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,10 @@ import android.os.Bundle;
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.dkanada.gramophone.App;
|
||||
import com.dkanada.gramophone.databinding.FragmentLibraryBinding;
|
||||
import com.dkanada.gramophone.model.Song;
|
||||
import com.dkanada.gramophone.util.QueryUtil;
|
||||
import com.google.android.material.appbar.AppBarLayout;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.viewpager.widget.ViewPager;
|
||||
|
|
@ -44,6 +47,14 @@ import com.dkanada.gramophone.util.ThemeUtil;
|
|||
import com.dkanada.gramophone.util.PreferenceUtil;
|
||||
import com.dkanada.gramophone.util.Util;
|
||||
|
||||
import org.jellyfin.apiclient.interaction.Response;
|
||||
import org.jellyfin.apiclient.model.dto.BaseItemDto;
|
||||
import org.jellyfin.apiclient.model.querying.ItemQuery;
|
||||
import org.jellyfin.apiclient.model.querying.ItemsResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LibraryFragment extends AbsMainActivityFragment implements CabHolder, MainActivity.MainActivityFragmentCallbacks, ViewPager.OnPageChangeListener, SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
private FragmentLibraryBinding binding;
|
||||
|
|
|
|||
|
|
@ -60,14 +60,14 @@ public abstract class AbsLibraryPagerRecyclerViewFragment<A extends RecyclerView
|
|||
|
||||
initAdapter();
|
||||
initLayoutManager();
|
||||
initQuery();
|
||||
|
||||
initRecyclerView();
|
||||
loadItems(0);
|
||||
}
|
||||
|
||||
private void initAdapter() {
|
||||
adapter = createAdapter();
|
||||
query = createQuery();
|
||||
|
||||
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
|
||||
@Override
|
||||
public void onChanged() {
|
||||
|
|
@ -75,14 +75,16 @@ public abstract class AbsLibraryPagerRecyclerViewFragment<A extends RecyclerView
|
|||
checkIsEmpty();
|
||||
}
|
||||
});
|
||||
|
||||
loadItems();
|
||||
}
|
||||
|
||||
private void initLayoutManager() {
|
||||
layoutManager = createLayoutManager();
|
||||
}
|
||||
|
||||
private void initQuery() {
|
||||
query = createQuery();
|
||||
}
|
||||
|
||||
private void initRecyclerView() {
|
||||
if (recyclerView instanceof FastScrollRecyclerView) {
|
||||
ViewUtil.setUpFastScrollRecyclerViewColor(getActivity(), ((FastScrollRecyclerView) recyclerView), ThemeStore.accentColor(getActivity()));
|
||||
|
|
@ -94,7 +96,10 @@ public abstract class AbsLibraryPagerRecyclerViewFragment<A extends RecyclerView
|
|||
|
||||
protected void invalidateAdapter() {
|
||||
initAdapter();
|
||||
initQuery();
|
||||
|
||||
recyclerView.setAdapter(adapter);
|
||||
loadItems(0);
|
||||
}
|
||||
|
||||
protected void invalidateLayoutManager() {
|
||||
|
|
@ -137,7 +142,7 @@ public abstract class AbsLibraryPagerRecyclerViewFragment<A extends RecyclerView
|
|||
@NonNull
|
||||
protected abstract Q createQuery();
|
||||
|
||||
protected abstract void loadItems();
|
||||
protected abstract void loadItems(int index);
|
||||
|
||||
@Override
|
||||
public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
|
||||
|
|
@ -157,7 +162,7 @@ public abstract class AbsLibraryPagerRecyclerViewFragment<A extends RecyclerView
|
|||
if (last > total - page / 2 && total < size) {
|
||||
query = createQuery();
|
||||
loading = true;
|
||||
loadItems();
|
||||
loadItems(getAdapter().getItemCount());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -59,10 +59,14 @@ public class AlbumsFragment extends AbsLibraryPagerRecyclerViewCustomGridSizeFra
|
|||
return query;
|
||||
}
|
||||
|
||||
protected void loadItems() {
|
||||
App.getApiClient().GetItemsAsync(getQuery(), new Response<ItemsResult>() {
|
||||
protected void loadItems(int index) {
|
||||
ItemQuery query = getQuery();
|
||||
query.setStartIndex(index);
|
||||
|
||||
App.getApiClient().GetItemsAsync(query, new Response<ItemsResult>() {
|
||||
@Override
|
||||
public void onResponse(ItemsResult result) {
|
||||
if (index == 0) getAdapter().getDataSet().clear();
|
||||
for (BaseItemDto itemDto : result.getItems()) {
|
||||
getAdapter().getDataSet().add(new Album(itemDto));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,10 +62,14 @@ public class ArtistsFragment extends AbsLibraryPagerRecyclerViewCustomGridSizeFr
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void loadItems() {
|
||||
App.getApiClient().GetAlbumArtistsAsync(getQuery(), new Response<ItemsResult>() {
|
||||
protected void loadItems(int index) {
|
||||
ArtistsQuery query = getQuery();
|
||||
query.setStartIndex(index);
|
||||
|
||||
App.getApiClient().GetAlbumArtistsAsync(query, new Response<ItemsResult>() {
|
||||
@Override
|
||||
public void onResponse(ItemsResult result) {
|
||||
if (index == 0) getAdapter().getDataSet().clear();
|
||||
for (BaseItemDto itemDto : result.getItems()) {
|
||||
getAdapter().getDataSet().add(new Artist(itemDto));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,10 +53,14 @@ public class GenresFragment extends AbsLibraryPagerRecyclerViewFragment<GenreAda
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void loadItems() {
|
||||
App.getApiClient().GetGenresAsync(getQuery(), new Response<ItemsResult>() {
|
||||
protected void loadItems(int index) {
|
||||
ItemsByNameQuery query = getQuery();
|
||||
query.setStartIndex(index);
|
||||
|
||||
App.getApiClient().GetGenresAsync(query, new Response<ItemsResult>() {
|
||||
@Override
|
||||
public void onResponse(ItemsResult result) {
|
||||
if (index == 0) getAdapter().getDataSet().clear();
|
||||
for (BaseItemDto itemDto : result.getItems()) {
|
||||
getAdapter().getDataSet().add(new Genre(itemDto));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,10 +55,14 @@ public class PlaylistsFragment extends AbsLibraryPagerRecyclerViewFragment<Playl
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void loadItems() {
|
||||
App.getApiClient().GetItemsAsync(getQuery(), new Response<ItemsResult>() {
|
||||
protected void loadItems(int index) {
|
||||
ItemQuery query = getQuery();
|
||||
query.setStartIndex(index);
|
||||
|
||||
App.getApiClient().GetItemsAsync(query, new Response<ItemsResult>() {
|
||||
@Override
|
||||
public void onResponse(ItemsResult result) {
|
||||
if (index == 0) getAdapter().getDataSet().clear();
|
||||
for (BaseItemDto itemDto : result.getItems()) {
|
||||
getAdapter().getDataSet().add(new Playlist(itemDto));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,10 +81,14 @@ public class SongsFragment extends AbsLibraryPagerRecyclerViewCustomGridSizeFrag
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void loadItems() {
|
||||
App.getApiClient().GetItemsAsync(getQuery(), new Response<ItemsResult>() {
|
||||
protected void loadItems(int index) {
|
||||
ItemQuery query = getQuery();
|
||||
query.setStartIndex(index);
|
||||
|
||||
App.getApiClient().GetItemsAsync(query, new Response<ItemsResult>() {
|
||||
@Override
|
||||
public void onResponse(ItemsResult result) {
|
||||
if (index == 0) getAdapter().getDataSet().clear();
|
||||
for (BaseItemDto itemDto : result.getItems()) {
|
||||
getAdapter().getDataSet().add(new Song(itemDto));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue