Skip to content

Commit

Permalink
made better constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-shiati committed Dec 16, 2020
1 parent c96c341 commit 1256e46
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.amirshiati.androidwoocommercesdk;

import android.net.Uri;

import com.amirshiati.androidwoocommercesdk.enums.Order;
import com.amirshiati.androidwoocommercesdk.enums.OrderBy;
import com.amirshiati.androidwoocommercesdk.enums.ProductManagerType;
import com.amirshiati.androidwoocommercesdk.helper.ProductJsonConverter;
import com.amirshiati.androidwoocommercesdk.helper.UriBuilder;
import com.amirshiati.androidwoocommercesdk.helper.Utils;
import com.amirshiati.androidwoocommercesdk.helper.Volley;
import com.amirshiati.androidwoocommercesdk.interfaces.OnGetJsonArrayFinished;
import com.amirshiati.androidwoocommercesdk.interfaces.OnGetProductsFinished;
Expand All @@ -16,20 +21,116 @@

public class ProductManager {

private String domainName;
private Uri.Builder builder;
private ProductManagerType type;
private Volley volley;

public ProductManager(String domainName, Volley volley) {
this.domainName = domainName;
private OnGetProductsFinished onGetProductsFinished;
private int page = 1;
private int perPage = 10;
private String search = "";
private Order order;
private OrderBy orderBy;


public ProductManager(Uri.Builder builder, ProductManagerType type, Volley volley) {
this.builder = builder;
this.type = type;
this.volley = volley;
}

public void getProducts(long page, long perPage, OnGetProductsFinished onGetProductFinished) {
public ProductManager addCallBack(OnGetProductsFinished onGetProductFinished) {
this.onGetProductsFinished = onGetProductFinished;
return this;
}

public ProductManager setPage(int page) {
this.page = page;
return this;
}

public ProductManager setPerPage(int perPage) {
this.perPage = perPage;
return this;
}

public ProductManager setOrder(Order order) {
this.order = order;
return this;
}

public ProductManager setOrderBy(OrderBy orderBy) {
this.orderBy = orderBy;
return this;
}

public ProductManager search(String search) {
this.search = search;
return this;
}

public void start() {
switch (type) {
case GETPRODUCTS:
setBuilder(this);
getProducts(this);
break;

default:
return;
}
}

private void setBuilder(final ProductManager productManager) {
builder.appendQueryParameter("page", String.valueOf(productManager.page));
builder.appendQueryParameter("per_page", String.valueOf(productManager.perPage));

if (!Utils.stringEmpty(productManager.search))
builder.appendQueryParameter("search", productManager.search);

if (productManager.order != null)
builder.appendQueryParameter("order", productManager.setBuilderOrder(productManager.order));

if (productManager.orderBy != null)
builder.appendQueryParameter("orderby", productManager.setBuilderOrderBy(productManager.orderBy));

}

private String setBuilderOrder(Order order) {
switch (order) {
case ASC:
return "asc";

default:
return "desc";
}
}

private String setBuilderOrderBy(OrderBy orderBy) {
switch (orderBy) {
case ID:
return "id";

case SLUG:
return "slug";

case TITLE:
return "title";

case INCLUDE:
return "include";

default:
return "date";
}
}

private void getProducts(final ProductManager productManager) {
final ArrayList<Product> result = new ArrayList<Product>();

volley.basicAuthJsonArrayReq(
Request.Method.GET,
UriBuilder.getProducts(domainName, page, perPage),
builder.build().toString(),
null,
new OnGetJsonArrayFinished() {
@Override
Expand All @@ -38,10 +139,12 @@ public void onSuccess(JSONArray response) {
for (int i = 0; i < response.length(); i++)
result.add(ProductJsonConverter.jsonToProduct(response.getJSONObject(i)));

onGetProductFinished.onSuccess(result);
if (productManager.onGetProductsFinished != null)
productManager.onGetProductsFinished.onSuccess(result);

} catch (Exception e) {
onGetProductFinished.onFail(e.getMessage());
if (productManager.onGetProductsFinished != null)
productManager.onGetProductsFinished.onFail(e.getMessage());
}
}

Expand All @@ -58,8 +161,11 @@ public void onFail(VolleyError error) {
}
}

onGetProductFinished.onFail(body);
if (productManager.onGetProductsFinished != null)
productManager.onGetProductsFinished.onFail(body);
}
});
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import android.content.Context;

import com.amirshiati.androidwoocommercesdk.enums.ProductManagerType;
import com.amirshiati.androidwoocommercesdk.helper.ProductJsonConverter;
import com.amirshiati.androidwoocommercesdk.helper.UriBuilder;
import com.amirshiati.androidwoocommercesdk.helper.Volley;
Expand All @@ -28,16 +29,17 @@ public class WooSDK {

private Volley volley;

public ProductManager productManager;

public WooSDK(Context context, String ckKey, String csKey, String domainName) {
this.context = context;
this.ckKey = ckKey;
this.csKey = csKey;
this.domainName = domainName;

volley = new Volley(context, ckKey, csKey, timeOut);
productManager = new ProductManager(domainName, volley);
}

public ProductManager getProducts() {
return new ProductManager(UriBuilder.getProducts(domainName), ProductManagerType.GETPRODUCTS, volley);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.amirshiati.androidwoocommercesdk.enums;

public enum Order {

ASC,
DESC

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.amirshiati.androidwoocommercesdk.enums;

public enum OrderBy {

DATE,
ID,
INCLUDE,
TITLE,
SLUG

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.amirshiati.androidwoocommercesdk.enums;

public enum ProductManagerType {

GETPRODUCTS
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static ArrayList<ProductDownloadProperties> getDownloadProperties(JSONObj
for (int i = 0; i < toGet.getJSONArray("downloads").length(); i++) {
JSONObject object = toGet.getJSONArray("downloads").getJSONObject(i);
result.add(new ProductDownloadProperties(
object.getLong("id"),
getString(toGet, "id"),
getString(object, "name"),
getString(object, "file"))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

public class UriBuilder {

public static String getProducts(String domainName, long page, long perPage) {
public static Uri.Builder getProducts(String domainName) {
Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
.authority(domainName)
.appendPath("wp-json")
.appendPath("wc")
.appendPath("v3")
.appendPath("products")
.appendQueryParameter("page", String.valueOf(page))
.appendQueryParameter("per_page", String.valueOf(perPage));
.appendPath("products");

return builder.build().toString();
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public static Date stringToDate(String date) {
}

public static boolean stringEmpty(String str) {
return str == null || str.equals("") || str.length() <= 0;
return str == null || str.equals("") || str.isEmpty() || str.length() <= 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

public class ProductDownloadProperties implements Serializable {

private Long id;
private String id;
private String name;
private String fileUrl;

public ProductDownloadProperties(Long id, String name, String fileUrl) {
public ProductDownloadProperties(String id, String name, String fileUrl) {
this.id = id;
this.name = name;
this.fileUrl = fileUrl;
}

public Long getId() {
public String getId() {
return id;
}

public void setId(Long id) {
public void setId(String id) {
this.id = id;
}

Expand Down
47 changes: 30 additions & 17 deletions app/src/main/java/com/amirshiati/woocommercesdk/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.util.Log;

import com.amirshiati.androidwoocommercesdk.WooSDK;
import com.amirshiati.androidwoocommercesdk.enums.Order;
import com.amirshiati.androidwoocommercesdk.enums.OrderBy;
import com.amirshiati.androidwoocommercesdk.interfaces.OnGetProductsFinished;
import com.amirshiati.androidwoocommercesdk.model.Product;

Expand All @@ -19,23 +21,34 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

WooSDK wooSDK = new WooSDK(this,
"ck_40d3e92eeff1eb8d6df19ddc9a5d72add570c41a",
"cs_87214e0b3128b84792407443077db0090bad8967",
"testforapp.webgram.app");
WooSDK wooSDK = new WooSDK(
this,
"ck_key",
"cs_key",
"domain"
);

wooSDK.getProducts()
.setPage(3)
.setPerPage(25)
.search("Hoodie")
.setOrder(Order.ASC)
.setOrderBy(OrderBy.SLUG)

.addCallBack(new OnGetProductsFinished() {
@Override
public void onSuccess(ArrayList<Product> products) {
for (Product product : products)
Log.i(TAG, product.getName());
}

@Override
public void onFail(String message) {
Log.i(TAG, message);
}
})

.start();


wooSDK.productManager.getProducts(1, 15, new OnGetProductsFinished() {
@Override
public void onSuccess(ArrayList<Product> products) {
for (Product product : products)
Log.i(TAG, product.getName());
}

@Override
public void onFail(String message) {
Log.i(TAG, message);
}
});
}
}

0 comments on commit 1256e46

Please sign in to comment.