Skip to content

Commit 85951b4

Browse files
committed
Refactored attribute handling system
1 parent c83ef18 commit 85951b4

File tree

7 files changed

+127
-211
lines changed

7 files changed

+127
-211
lines changed

AndroidWoocommerceSDK/src/main/java/com/amirshiati/androidwoocommercesdk/AttributeManager.java

Lines changed: 0 additions & 145 deletions
This file was deleted.

AndroidWoocommerceSDK/src/main/java/com/amirshiati/androidwoocommercesdk/WooSDK.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.amirshiati.androidwoocommercesdk.enums.CategoryManagerType;
88
import com.amirshiati.androidwoocommercesdk.enums.OrderManageType;
99
import com.amirshiati.androidwoocommercesdk.enums.ProductManagerType;
10+
import com.amirshiati.androidwoocommercesdk.handler.AttributeHandler;
1011
import com.amirshiati.androidwoocommercesdk.handler.CategoryHandler;
1112
import com.amirshiati.androidwoocommercesdk.handler.OrderHandler;
1213
import com.amirshiati.androidwoocommercesdk.handler.ProductHandler;
@@ -38,15 +39,6 @@ public WooSDK(Context context, String ckKey, String csKey, String domainName) {
3839
uriBuilderSingleton = UriBuilderSingleton.getInstance(domainName);
3940
}
4041

41-
42-
public AttributeManager getAttributes() {
43-
return new AttributeManager(UriBuilder.getAttributes(domainName), AttributeManagerType.GETATTRIBUTES, volley);
44-
}
45-
46-
public AttributeManager getAttributes(long attributeId) {
47-
return new AttributeManager(UriBuilder.getAttribute(domainName, attributeId), AttributeManagerType.GETATTRIBUTE, volley);
48-
}
49-
5042
public void getProduct(long id, OnResponse onResponse) {
5143
ProductHandler productHandler = new ProductHandler(uriBuilderSingleton, volley);
5244
productHandler.get(id, onResponse);
@@ -78,4 +70,15 @@ public void getCategories(ParamBuilder paramBuilder, OnResponse onResponse) {
7870
categoryHandler.getList(paramBuilder, onResponse);
7971
}
8072

73+
public void getAttribute(long id, OnResponse onResponse) {
74+
AttributeHandler attributeHandler = new AttributeHandler(uriBuilderSingleton, volley);
75+
attributeHandler.get(id, onResponse);
76+
}
77+
78+
public void getAttributes(ParamBuilder paramBuilder, OnResponse onResponse) {
79+
AttributeHandler attributeHandler = new AttributeHandler(uriBuilderSingleton, volley);
80+
attributeHandler.getList(paramBuilder, onResponse);
81+
}
82+
83+
8184
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.amirshiati.androidwoocommercesdk.builder;
2+
3+
import android.net.Uri;
4+
5+
import com.amirshiati.androidwoocommercesdk.interfaces.ParamBuilder;
6+
7+
public class AttributeBuilder implements ParamBuilder {
8+
@Override
9+
public String buildParam(Uri.Builder builder) {
10+
return builder.build().toString();
11+
}
12+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.amirshiati.androidwoocommercesdk.handler;
2+
3+
import com.amirshiati.androidwoocommercesdk.helper.UriBuilderSingleton;
4+
import com.amirshiati.androidwoocommercesdk.helper.Utils;
5+
import com.amirshiati.androidwoocommercesdk.helper.Volley;
6+
import com.amirshiati.androidwoocommercesdk.interfaces.OnGetJsonArrayFinished;
7+
import com.amirshiati.androidwoocommercesdk.interfaces.OnGetJsonObjectFinished;
8+
import com.amirshiati.androidwoocommercesdk.interfaces.OnResponse;
9+
import com.amirshiati.androidwoocommercesdk.interfaces.ParamBuilder;
10+
import com.amirshiati.androidwoocommercesdk.interfaces.WooHandler;
11+
import com.amirshiati.androidwoocommercesdk.json.AttributeJsonConverter;
12+
import com.amirshiati.androidwoocommercesdk.model.Attribute;
13+
import com.android.volley.Request;
14+
import com.android.volley.VolleyError;
15+
16+
import org.json.JSONArray;
17+
import org.json.JSONObject;
18+
19+
import java.util.ArrayList;
20+
21+
public class AttributeHandler implements WooHandler {
22+
23+
private UriBuilderSingleton uriBuilder;
24+
private Volley volley;
25+
26+
27+
public AttributeHandler(UriBuilderSingleton uriBuilder, Volley volley) {
28+
this.uriBuilder = uriBuilder;
29+
this.volley = volley;
30+
}
31+
32+
@Override
33+
public void get(long id, OnResponse onResponse) {
34+
String url = uriBuilder.getAttribute(id).build().toString();
35+
volley.basicAuthJsonObjReq(Request.Method.GET, url, null, new OnGetJsonObjectFinished() {
36+
@Override
37+
public void onSuccess(JSONObject response) {
38+
try {
39+
if (onResponse != null)
40+
onResponse.onSuccess(AttributeJsonConverter.jsonToAttribute(response));
41+
42+
} catch (Exception e) {
43+
if (onResponse != null)
44+
onResponse.onFail(" Error: " + e.getMessage());
45+
}
46+
}
47+
48+
@Override
49+
public void onFail(VolleyError error) {
50+
if (onResponse != null)
51+
onResponse.onFail(" Error: " + Utils.getVolleyErrorBody(error));
52+
}
53+
});
54+
}
55+
56+
@Override
57+
public void getList(ParamBuilder builder, OnResponse onResponse) {
58+
String url = builder.buildParam(uriBuilder.getAttributes());
59+
final ArrayList<Attribute> result = new ArrayList<Attribute>();
60+
61+
volley.basicAuthJsonArrayReq(
62+
Request.Method.GET,
63+
url,
64+
null,
65+
new OnGetJsonArrayFinished() {
66+
@Override
67+
public void onSuccess(JSONArray response) {
68+
try {
69+
for (int i = 0; i < response.length(); i++)
70+
result.add(AttributeJsonConverter.jsonToAttribute(response.getJSONObject(i)));
71+
72+
if (onResponse != null)
73+
onResponse.onSuccess(result);
74+
75+
} catch (Exception e) {
76+
if (onResponse != null)
77+
onResponse.onFail(" Error: " + e.getMessage());
78+
}
79+
}
80+
81+
@Override
82+
public void onFail(VolleyError error) {
83+
if (onResponse != null)
84+
onResponse.onFail(" Error: " + Utils.getVolleyErrorBody(error));
85+
}
86+
});
87+
}
88+
89+
@Override
90+
public void delete(ParamBuilder builder) {
91+
92+
}
93+
94+
@Override
95+
public void add(ParamBuilder builder) {
96+
97+
}
98+
99+
@Override
100+
public void update(ParamBuilder builder) {
101+
102+
}
103+
}

AndroidWoocommerceSDK/src/main/java/com/amirshiati/androidwoocommercesdk/interfaces/OnGetAttributeFinished.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

AndroidWoocommerceSDK/src/main/java/com/amirshiati/androidwoocommercesdk/interfaces/OnGetAttributesFinished.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

app/src/main/java/com/amirshiati/woocommercesdk/MainActivity.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ protected void onCreate(Bundle savedInstanceState) {
3434
"domain"
3535
);
3636

37-
3837
wooSDK.getOrder(843, new OnResponse() {
3938
@Override
4039
public void onSuccess(Object object) {
@@ -121,36 +120,6 @@ public void onFail(String err) {
121120

122121
}
123122
});
124-
// wooSDK.getAttributes(1)
125-
// .addGetAttributeCallBack(new OnGetAttributeFinished() {
126-
// @Override
127-
// public void onSuccess(Attribute attribute) {
128-
// Log.i(TAG, attribute.getName());
129-
// }
130-
//
131-
// @Override
132-
// public void onFail(String message) {
133-
// Log.i(TAG, "error");
134-
// Log.i(TAG, message);
135-
// }
136-
// })
137-
// .start();
138-
139-
// wooSDK.getAttributes()
140-
// .addGetAttributesCallBack(new OnGetAttributesFinished() {
141-
// @Override
142-
// public void onSuccess(ArrayList<Attribute> attributes) {
143-
// for (Attribute attribute : attributes)
144-
// Log.i(TAG, attribute.getName());
145-
// }
146-
//
147-
// @Override
148-
// public void onFail(String message) {
149-
// Log.i(TAG, "error");
150-
// Log.i(TAG, message);
151-
// }
152-
// })
153-
// .start();
154123

155124
}
156125
}

0 commit comments

Comments
 (0)