Skip to content

Commit

Permalink
Add more error callbacks (#21)
Browse files Browse the repository at this point in the history
* Add more error callbacks

* Fix a bug in returning a error message

* Expose a variable to provide information whether to load more data

* Remove unnecessary configs
  • Loading branch information
SungjunApp authored Feb 13, 2020
1 parent b4fc688 commit 54c85f2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
5 changes: 0 additions & 5 deletions pixleesdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ android {
targetSdkVersion 28
versionCode 1
versionName "1.2.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}
buildTypes {
release {
Expand All @@ -26,8 +23,6 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.json:json:20160810'

Expand Down
12 changes: 7 additions & 5 deletions pixleesdk/src/main/java/com/pixlee/pixleesdk/PXLAlbum.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.pixlee.pixleesdk;

import android.util.Log;

import com.pixlee.pixleesdk.data.PhotoResult;
import com.pixlee.pixleesdk.data.repository.AnalyticsDataSource;
import com.pixlee.pixleesdk.data.repository.BasicDataSource;

import java.util.ArrayList;

import retrofit2.Call;

/***
Expand Down Expand Up @@ -47,18 +47,20 @@ public PXLAlbum(String id, PXLClient client) {
* This is for unit test. Not for the use
* @return
*/
Call<PhotoResult> makeGetAlbumCall() {
@Override
Call<PhotoResult> makeGetAlbumCall(RequestHandlers<ArrayList<PXLPhoto>> handlers) {
if (album_id == null) {
Log.w(TAG, "No album id specified");
handlers.onError("No album id specified");
return null;
}
if (!this.hasMore) {
handlers.onError("No need to load more");
return null;
}

int desiredPage = this.lastPageLoaded + 1;
if (pagesLoading.get(desiredPage) != null && pagesLoading.get(desiredPage)) {
Log.d(TAG, String.format("page %s already loading", desiredPage));
handlers.onError(String.format("page %s already loading", desiredPage));
return null;
}
this.pagesLoading.put(desiredPage, true);
Expand Down
14 changes: 10 additions & 4 deletions pixleesdk/src/main/java/com/pixlee/pixleesdk/PXLBaseAlbum.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
Expand Down Expand Up @@ -65,6 +64,14 @@ public PXLBaseAlbum(BasicDataSource basicRepo, AnalyticsDataSource analyticsRepo
this.pagesLoading = new HashMap<>();
}

/**
* Gives hint for you whether to use loadNextPageOfPhotos()
* @return hasMore true: you can fire loadNextPageOfPhotos() to get more data, false: you cannot load more data
*/
public boolean isHasMore() {
return hasMore;
}

/***
* Sets the amount of photos fetched per call of 'loadNextPageOfPhotos'. Will purge previously
* fetched photos. Call 'loadNextPageOfPhotos' after setting.
Expand Down Expand Up @@ -136,16 +143,15 @@ public <T> void processReponse(Response response, RequestHandlers handlers){
*
* @return retrofit2.Call
*/
abstract Call<PhotoResult> makeGetAlbumCall();
abstract Call<PhotoResult> makeGetAlbumCall(RequestHandlers<ArrayList<PXLPhoto>> handlers);

/***
* Requests the next page of photos from the Pixlee album. Make sure to set perPage,
* sort order, and filter options before calling.
* @param handlers - called upon success/failure of the request
* @return true if the request was attempted, false if aborted before the attempt was made
*/
public void loadNextPageOfPhotos(final RequestHandlers<ArrayList<PXLPhoto>> handlers) {
Call<PhotoResult> call = makeGetAlbumCall();
Call<PhotoResult> call = makeGetAlbumCall(handlers);

if (call == null)
return;
Expand Down
12 changes: 6 additions & 6 deletions pixleesdk/src/main/java/com/pixlee/pixleesdk/PXLPdpAlbum.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.pixlee.pixleesdk;

import android.util.Log;

import com.pixlee.pixleesdk.data.PhotoResult;
import com.pixlee.pixleesdk.data.repository.AnalyticsDataSource;
import com.pixlee.pixleesdk.data.repository.BasicDataSource;

import java.util.ArrayList;

import retrofit2.Call;

/**
Expand Down Expand Up @@ -42,20 +42,20 @@ public PXLPdpAlbum(String sku, PXLClient client) {
* @return
*/
@Override
Call<PhotoResult> makeGetAlbumCall() {
Call<PhotoResult> makeGetAlbumCall(RequestHandlers<ArrayList<PXLPhoto>> handlers) {
if (sku == null) {
Log.w(TAG, "No sku specified");
handlers.onError("No sku specified");
return null;
}
if (!this.hasMore) {
Log.w(TAG, "no need to load more");
handlers.onError("No need to load more");
return null;
}

int desiredPage = this.lastPageLoaded + 1;
Boolean isPageLoading = pagesLoading.get(desiredPage);
if (isPageLoading != null && isPageLoading) {
Log.d(TAG, String.format("page %s already loading", desiredPage));
handlers.onError(String.format("page %s already loading", desiredPage));
return null;
}

Expand Down

0 comments on commit 54c85f2

Please sign in to comment.