Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
AomineD committed Jul 25, 2022
1 parent 724dd73 commit 3f07cac
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion Pexels/src/main/java/com/wine/pexels/Pexels.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import com.wine.pexels.network.PexelsApi;

import java.lang.reflect.Type;

public class Pexels {
static String key_api = "563492ad6f91700001000001260162bc2b22489eb68b31976376d648";

Expand Down Expand Up @@ -36,10 +38,23 @@ public Pexels search(String by){
return this;
}



private TypeOrientation t = null;
public Pexels orientation(TypeOrientation typeOrientation){
t = typeOrientation;
return this;
}

public void load(int page,LoadImages listener){

if(t == null)
pexelsApi.loadImagesSearch(search, page, listener);

else{
pexelsApi.setOrientation(t);
pexelsApi.loadByOrientation(search, page, listener);
t = null;
}

}

Expand Down
7 changes: 7 additions & 0 deletions Pexels/src/main/java/com/wine/pexels/TypeOrientation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wine.pexels;

public enum TypeOrientation {
PORTRAIT,
LANDSCAPE,
SQUARE
}
72 changes: 72 additions & 0 deletions Pexels/src/main/java/com/wine/pexels/network/PexelsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.google.gson.Gson;
import com.wine.pexels.LoadImages;
import com.wine.pexels.Pexels;
import com.wine.pexels.TypeOrientation;
import com.wine.pexels.models.PexelImage;

import org.json.JSONArray;
Expand All @@ -37,6 +38,22 @@ public PexelsApi(Context c){
queue = Volley.newRequestQueue(c);
}

private String actualOrientation = "";

public void setOrientation(TypeOrientation orientation){
switch (orientation){
case SQUARE:
actualOrientation = "square";
break;
case PORTRAIT:
actualOrientation = "portrait";
break;
case LANDSCAPE:
actualOrientation = "landscape";
break;
}
}


public void loadImagesSearch(String query, int page, LoadImages loadImages){

Expand Down Expand Up @@ -88,5 +105,60 @@ public Map<String, String> getHeaders() throws AuthFailureError {

}

public void loadByOrientation(String query, int page, LoadImages loadImages){

String cUrl =baseURl+search+"query="+query+"&page="+page+"&per_page=15";

if(!actualOrientation.equals("")){

cUrl = cUrl+"&orientation="+actualOrientation;
actualOrientation = "";
}

StringRequest request = new StringRequest(Request.Method.GET, cUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {

try {
JSONObject obj = new JSONObject(response);

JSONArray arr = obj.getJSONArray("photos");
ArrayList<PexelImage> pexelImages = new ArrayList<>();

for (int i = 0; i < arr.length(); i++) {

PexelImage px = new Gson().fromJson(arr.getJSONObject(i).toString(), PexelImage.class);
pexelImages.add(px);

}


loadImages.OnLoadImages(pexelImages);

} catch (JSONException e) {
loadImages.OnError(e.getMessage());
e.printStackTrace();
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loadImages.OnError(error.getMessage());
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();

headers.put("Authorization", Pexels.getPexels().getApi());
return headers;
}
};

queue.add(request);


}

}

0 comments on commit 3f07cac

Please sign in to comment.