Skip to content

Commit a1ba63a

Browse files
Merge pull request #103 from MihaiCristianCondrea/codex/move-json-parsing-to-background-executor
Offload promoted app parsing to background thread
2 parents d6b9d90 + 2d533c4 commit a1ba63a

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed
Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.d4rk.androidtutorials.java.data.source;
22

3+
import android.os.Handler;
4+
import android.os.Looper;
35
import com.android.volley.Request;
46
import com.android.volley.RequestQueue;
57
import com.android.volley.toolbox.JsonObjectRequest;
@@ -12,6 +14,8 @@
1214
import java.util.ArrayList;
1315
import java.util.Collections;
1416
import java.util.List;
17+
import java.util.concurrent.Executor;
18+
import java.util.concurrent.Executors;
1519

1620
/**
1721
* Volley based implementation of {@link HomeRemoteDataSource}.
@@ -20,10 +24,14 @@ public class DefaultHomeRemoteDataSource implements HomeRemoteDataSource {
2024

2125
private final RequestQueue requestQueue;
2226
private final String apiUrl;
27+
private final Executor executor;
28+
private final Handler mainHandler;
2329

2430
public DefaultHomeRemoteDataSource(RequestQueue requestQueue, String apiUrl) {
2531
this.requestQueue = requestQueue;
2632
this.apiUrl = apiUrl;
33+
this.executor = Executors.newSingleThreadExecutor();
34+
this.mainHandler = new Handler(Looper.getMainLooper());
2735
}
2836

2937
@Override
@@ -32,29 +40,34 @@ public void fetchPromotedApps(PromotedAppsCallback callback) {
3240
Request.Method.GET,
3341
apiUrl,
3442
null,
35-
response -> {
36-
List<PromotedApp> result = new ArrayList<>();
37-
try {
38-
JSONArray apps = response.getJSONObject("data").getJSONArray("apps");
39-
for (int i = 0; i < apps.length(); i++) {
40-
JSONObject obj = apps.getJSONObject(i);
41-
String pkg = obj.getString("packageName");
42-
if (pkg.contains("com.d4rk.androidtutorials")) {
43-
continue;
44-
}
45-
result.add(new PromotedApp(
46-
obj.getString("name"),
47-
pkg,
48-
obj.getString("iconLogo")
49-
));
50-
}
51-
} catch (JSONException e) {
52-
result = Collections.emptyList();
53-
}
54-
callback.onResult(result);
55-
},
56-
error -> callback.onResult(Collections.emptyList())
43+
response -> executor.execute(() -> {
44+
List<PromotedApp> result = parseResponse(response);
45+
mainHandler.post(() -> callback.onResult(result));
46+
}),
47+
error -> mainHandler.post(() -> callback.onResult(Collections.emptyList()))
5748
);
5849
requestQueue.add(request);
5950
}
51+
52+
private List<PromotedApp> parseResponse(JSONObject response) {
53+
List<PromotedApp> result = new ArrayList<>();
54+
try {
55+
JSONArray apps = response.getJSONObject("data").getJSONArray("apps");
56+
for (int i = 0; i < apps.length(); i++) {
57+
JSONObject obj = apps.getJSONObject(i);
58+
String pkg = obj.getString("packageName");
59+
if (pkg.contains("com.d4rk.androidtutorials")) {
60+
continue;
61+
}
62+
result.add(new PromotedApp(
63+
obj.getString("name"),
64+
pkg,
65+
obj.getString("iconLogo")
66+
));
67+
}
68+
} catch (JSONException e) {
69+
result = Collections.emptyList();
70+
}
71+
return result;
72+
}
6073
}

0 commit comments

Comments
 (0)