Skip to content

Commit 26f95bf

Browse files
committed
Load more tracks when scrolling
1 parent d7f4f75 commit 26f95bf

File tree

3 files changed

+122
-25
lines changed

3 files changed

+122
-25
lines changed

app/src/main/java/com/spotify/sdk/android/MainActivity.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class MainActivity extends AppCompatActivity {
3232
private SpotifyService mSpotifyService;
3333
private String mCurrentQuery;
3434
private Player mPlayer;
35+
3536
private ServiceConnection mServiceConnection = new ServiceConnection() {
3637
@Override
3738
public void onServiceConnected(ComponentName name, IBinder service) {
@@ -44,6 +45,23 @@ public void onServiceDisconnected(ComponentName name) {
4445
}
4546
};
4647

48+
private LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
49+
private ScrollListener mScrollListener = new ScrollListener(mLayoutManager);
50+
;
51+
52+
private class ScrollListener extends ResultListScrollListener {
53+
54+
public ScrollListener(LinearLayoutManager layoutManager) {
55+
super(layoutManager);
56+
}
57+
58+
@Override
59+
public void onLoadMore() {
60+
Log.d(TAG, "Load more...");
61+
mAdapter.getNextPage();
62+
}
63+
}
64+
4765
public static Intent createIntent(Context context) {
4866
return new Intent(context, MainActivity.class);
4967
}
@@ -92,7 +110,7 @@ private void setupSearch() {
92110
public boolean onQueryTextSubmit(String query) {
93111
logMessage("query text submit " + query);
94112
search(query);
95-
return false;
113+
return true;
96114
}
97115

98116
@Override
@@ -110,29 +128,16 @@ public void onItemSelected(View itemView, Track item) {
110128
}
111129
});
112130

113-
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
114-
mResultsList.setLayoutManager(layoutManager);
131+
mResultsList.setLayoutManager(mLayoutManager);
115132
mResultsList.setAdapter(mAdapter);
116-
mResultsList.addOnScrollListener(new RecyclerView.OnScrollListener() {
117-
@Override
118-
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
119-
super.onScrolled(recyclerView, dx, dy);
120-
Log.d(TAG, "scrolled " + dx + " " + dy);
121-
122-
int itemCount = layoutManager.getItemCount();
123-
int itemPosition = layoutManager.findLastVisibleItemPosition();
124-
125-
if (itemCount <= itemPosition) {
126-
Log.d(TAG, "bottom hit");
127-
}
128-
}
129-
});
133+
mResultsList.addOnScrollListener(mScrollListener);
130134
}
131135

132136
private void search(@NonNull String query) {
133-
if (!query.isEmpty()) {
134-
mAdapter.searchTracks(query);
137+
if (!query.isEmpty() && !query.equals(mCurrentQuery)) {
135138
mCurrentQuery = query;
139+
mScrollListener.reset();
140+
mAdapter.searchTracks(query, 20);
136141
}
137142
}
138143

@@ -196,4 +201,5 @@ private void logMessage(String msg) {
196201
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
197202
Log.d(TAG, msg);
198203
}
204+
199205
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2015 Spotify AB
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
23+
package com.spotify.sdk.android;
24+
25+
import android.support.v7.widget.LinearLayoutManager;
26+
import android.support.v7.widget.RecyclerView;
27+
import android.util.Log;
28+
29+
public abstract class ResultListScrollListener extends RecyclerView.OnScrollListener {
30+
31+
private static final String TAG = ResultListScrollListener.class.getSimpleName();
32+
33+
private final LinearLayoutManager mLayoutManager;
34+
35+
private int mItemCount;
36+
37+
private boolean mAwaitingItems = true;
38+
39+
public ResultListScrollListener(LinearLayoutManager layoutManager) {
40+
mLayoutManager = layoutManager;
41+
}
42+
43+
public void reset() {
44+
mItemCount = 0;
45+
}
46+
47+
@Override
48+
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
49+
super.onScrolled(recyclerView, dx, dy);
50+
51+
int itemCount = mLayoutManager.getItemCount();
52+
int itemPosition = mLayoutManager.findLastVisibleItemPosition();
53+
54+
if (mAwaitingItems && itemCount > mItemCount) {
55+
mItemCount = itemCount;
56+
mAwaitingItems = false;
57+
}
58+
59+
Log.d(TAG, String.format("loading %s, item count: %s/%s, itemPosition %s", mAwaitingItems, mItemCount, itemCount, itemPosition));
60+
61+
if (!mAwaitingItems && (itemCount <= itemPosition + 1)) {
62+
mAwaitingItems = true;
63+
onLoadMore();
64+
}
65+
}
66+
67+
public abstract void onLoadMore();
68+
}

app/src/main/java/com/spotify/sdk/android/SearchResultsAdapter.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import com.squareup.picasso.Picasso;
1414

1515
import java.util.ArrayList;
16+
import java.util.HashMap;
1617
import java.util.List;
18+
import java.util.Map;
1719

1820
import kaaes.spotify.webapi.android.SpotifyService;
1921
import kaaes.spotify.webapi.android.models.ArtistSimple;
@@ -33,6 +35,10 @@ public class SearchResultsAdapter extends RecyclerView.Adapter<SearchResultsAdap
3335
private final SpotifyService mSpotifyApi;
3436
private final ItemSelectedListener mListener;
3537
private int mOffset;
38+
private String mQuery;
39+
private int mLimit;
40+
41+
private boolean mHasMoreItems = true;
3642

3743
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
3844

@@ -65,20 +71,37 @@ public SearchResultsAdapter(Activity context, SpotifyService spotifyService, Ite
6571
mListener = listener;
6672
}
6773

68-
public void setData(List<Track> items) {
74+
private void clearData() {
6975
mItems.clear();
76+
}
77+
78+
private void addData(List<Track> items) {
7079
mItems.addAll(items);
7180
notifyDataSetChanged();
7281
}
7382

74-
public void searchTracks(String query) {
83+
public void getNextPage() {
84+
mOffset += mLimit;
85+
getData();
86+
}
87+
88+
public void searchTracks(String query, int pageSize) {
89+
mQuery = query;
90+
mLimit = pageSize;
7591
mOffset = 0;
76-
mSpotifyApi.searchTracks(query, new Callback<TracksPager>() {
92+
clearData();
93+
getData();
94+
}
95+
96+
private void getData( ) {
97+
Map<String, Object> options = new HashMap<>();
98+
options.put(SpotifyService.OFFSET, mOffset);
99+
options.put(SpotifyService.LIMIT, mLimit);
100+
mSpotifyApi.searchTracks(mQuery, options, new Callback<TracksPager>() {
77101
@Override
78102
public void success(TracksPager tracksPager, Response response) {
79-
setData(tracksPager.tracks.items);
80-
mOffset = tracksPager.tracks.offset + tracksPager.tracks.limit;
81-
Log.d(TAG, "Whoops success " + mOffset);
103+
mHasMoreItems = tracksPager.tracks.total > tracksPager.tracks.offset + tracksPager.tracks.limit;
104+
addData(tracksPager.tracks.items);
82105
}
83106

84107
@Override

0 commit comments

Comments
 (0)