11
11
import com .kc .unsplash .api .HeaderInterceptor ;
12
12
import com .kc .unsplash .api .Order ;
13
13
import com .kc .unsplash .api .endpoints .PhotosEndpointInterface ;
14
+ import com .kc .unsplash .models .SearchResults ;
14
15
import com .kc .unsplash .models .Stats ;
15
16
16
17
import java .util .List ;
@@ -30,6 +31,7 @@ public class Unsplash {
30
31
private PhotosEndpointInterface photosApiService ;
31
32
private CollectionsEndpointInterface collectionsApiService ;
32
33
private StatsEndpointInterface statsApiService ;
34
+ private String TAG = "Unsplash" ;
33
35
34
36
public Unsplash (String clientId ) {
35
37
this .clientId = clientId ;
@@ -79,9 +81,13 @@ public void getRandomPhotos(@Nullable String collections,
79
81
call .enqueue (getMultiplePhotoCallback (listener ));
80
82
}
81
83
82
- public void searchPhotos (@ NonNull String query , @ Nullable Integer page , @ Nullable Integer perPage , OnPhotosLoadedListener listener ){
83
- Call <List <Photo >> call = photosApiService .searchPhotos (query , page , perPage );
84
- call .enqueue (getMultiplePhotoCallback (listener ));
84
+ public void searchPhotos (@ NonNull String query , OnSearchCompleteListener listener ){
85
+ searchPhotos (query , null , null , listener );
86
+ }
87
+
88
+ public void searchPhotos (@ NonNull String query , @ Nullable Integer page , @ Nullable Integer perPage , OnSearchCompleteListener listener ){
89
+ Call <SearchResults > call = photosApiService .searchPhotos (query , page , perPage );
90
+ call .enqueue (getSearchResultsCallback (listener ));
85
91
}
86
92
87
93
public void getPhotoDownloadLink (@ NonNull String id , final OnLinkLoadedListener listener ){
@@ -94,7 +100,7 @@ public void onResponse(Call<String> call, Response<String> response) {
94
100
listener .onComplete (response .body ());
95
101
}
96
102
else if (statusCode == 401 ) {
97
- Log .d ("Unsplash" , "Unauthorized, Check your client Id" );
103
+ Log .d (TAG , "Unauthorized, Check your client Id" );
98
104
}
99
105
}
100
106
@@ -157,11 +163,12 @@ public void getStats(final OnStatsLoadedListener listener){
157
163
@ Override
158
164
public void onResponse (Call <Stats > call , Response <Stats > response ) {
159
165
int statusCode = response .code ();
166
+ Log .d (TAG , "Status Code = " + statusCode );
160
167
if (statusCode == 200 ) {
161
168
listener .onComplete (response .body ());
162
169
}
163
170
else if (statusCode == 401 ) {
164
- Log .d ("Unsplash" , "Unauthorized, Check your client Id" );
171
+ Log .d (TAG , "Unauthorized, Check your client Id" );
165
172
}
166
173
}
167
174
@@ -180,12 +187,13 @@ private Callback<Photo> getSinglePhotoCallback(final OnPhotoLoadedListener liste
180
187
@ Override
181
188
public void onResponse (Call <Photo > call , Response <Photo > response ) {
182
189
int statusCode = response .code ();
190
+ Log .d (TAG , "Status Code = " + statusCode );
183
191
if (statusCode == 200 ) {
184
192
Photo photo = response .body ();
185
193
listener .onComplete (photo );
186
194
}
187
195
else if (statusCode == 401 ) {
188
- Log .d ("Unsplash" , "Unauthorized, Check your client Id" );
196
+ Log .d (TAG , "Unauthorized, Check your client Id" );
189
197
}
190
198
}
191
199
@@ -201,17 +209,20 @@ private Callback<List<Photo>> getMultiplePhotoCallback(final OnPhotosLoadedListe
201
209
@ Override
202
210
public void onResponse (Call <List <Photo >> call , Response <List <Photo >> response ) {
203
211
int statusCode = response .code ();
212
+ Log .d (TAG , "Url = " + call .request ().url ());
213
+ Log .d (TAG , "Status Code = " + statusCode );
204
214
if (statusCode == 200 ) {
205
215
List <Photo > photos = response .body ();
206
216
listener .onComplete (photos );
207
217
}
208
218
else if (statusCode == 401 ) {
209
- Log .d ("Unsplash" , "Unauthorized, Check your client Id" );
219
+ Log .d (TAG , "Unauthorized, Check your client Id" );
210
220
}
211
221
}
212
222
213
223
@ Override
214
224
public void onFailure (Call <List <Photo >> call , Throwable t ) {
225
+ Log .d (TAG , "Url = " + call .request ().url ());
215
226
listener .onError (t .getMessage ());
216
227
}
217
228
};
@@ -222,12 +233,13 @@ private Callback<Collection> getSingleCollectionCallback(final OnCollectionLoade
222
233
@ Override
223
234
public void onResponse (Call <Collection > call , Response <Collection > response ) {
224
235
int statusCode = response .code ();
236
+ Log .d (TAG , "Status Code = " + statusCode );
225
237
if (statusCode == 200 ) {
226
238
Collection collections = response .body ();
227
239
listener .onComplete (collections );
228
240
}
229
241
else if (statusCode == 401 ) {
230
- Log .d ("Unsplash" , "Unauthorized, Check your client Id" );
242
+ Log .d (TAG , "Unauthorized, Check your client Id" );
231
243
}
232
244
}
233
245
@@ -238,17 +250,40 @@ public void onFailure(Call<Collection> call, Throwable t) {
238
250
};
239
251
}
240
252
253
+ private Callback <SearchResults > getSearchResultsCallback (final OnSearchCompleteListener listener ){
254
+ return new Callback <SearchResults >() {
255
+ @ Override
256
+ public void onResponse (Call <SearchResults > call , Response <SearchResults > response ) {
257
+ int statusCode = response .code ();
258
+ Log .d (TAG , "Status Code = " + statusCode );
259
+ if (statusCode == 200 ) {
260
+ SearchResults results = response .body ();
261
+ listener .onComplete (results );
262
+ }
263
+ else if (statusCode == 401 ) {
264
+ Log .d (TAG , "Unauthorized, Check your client Id" );
265
+ }
266
+ }
267
+
268
+ @ Override
269
+ public void onFailure (Call <SearchResults > call , Throwable t ) {
270
+ listener .onError (t .getMessage ());
271
+ }
272
+ };
273
+ }
274
+
241
275
private Callback <List <Collection >> getMultipleCollectionsCallback (final OnCollectionsLoadedListener listener ){
242
276
return new Callback <List <Collection >>() {
243
277
@ Override
244
278
public void onResponse (Call <List <Collection >> call , Response <List <Collection >> response ) {
245
279
int statusCode = response .code ();
280
+ Log .d (TAG , "Status Code = " + statusCode );
246
281
if (statusCode == 200 ) {
247
282
List <Collection > collections = response .body ();
248
283
listener .onComplete (collections );
249
284
}
250
285
else if (statusCode == 401 ) {
251
- Log .d ("Unsplash" , "Unauthorized, Check your client Id" );
286
+ Log .d (TAG , "Unauthorized, Check your client Id" );
252
287
}
253
288
}
254
289
@@ -260,15 +295,21 @@ public void onFailure(Call<List<Collection>> call, Throwable t) {
260
295
}
261
296
262
297
public interface OnPhotosLoadedListener {
263
- public void onComplete (List <Photo > photos );
298
+ void onComplete (List <Photo > photos );
264
299
265
- public void onError (String error );
300
+ void onError (String error );
301
+ }
302
+
303
+ public interface OnSearchCompleteListener {
304
+ void onComplete (SearchResults results );
305
+
306
+ void onError (String error );
266
307
}
267
308
268
309
public interface OnPhotoLoadedListener {
269
- public void onComplete (Photo photo );
310
+ void onComplete (Photo photo );
270
311
271
- public void onError (String error );
312
+ void onError (String error );
272
313
}
273
314
274
315
public interface OnLinkLoadedListener {
0 commit comments