1
1
package com .d4rk .androidtutorials .java .data .source ;
2
2
3
+ import android .os .Handler ;
4
+ import android .os .Looper ;
3
5
import com .android .volley .Request ;
4
6
import com .android .volley .RequestQueue ;
5
7
import com .android .volley .toolbox .JsonObjectRequest ;
12
14
import java .util .ArrayList ;
13
15
import java .util .Collections ;
14
16
import java .util .List ;
17
+ import java .util .concurrent .Executor ;
18
+ import java .util .concurrent .Executors ;
15
19
16
20
/**
17
21
* Volley based implementation of {@link HomeRemoteDataSource}.
@@ -20,10 +24,14 @@ public class DefaultHomeRemoteDataSource implements HomeRemoteDataSource {
20
24
21
25
private final RequestQueue requestQueue ;
22
26
private final String apiUrl ;
27
+ private final Executor executor ;
28
+ private final Handler mainHandler ;
23
29
24
30
public DefaultHomeRemoteDataSource (RequestQueue requestQueue , String apiUrl ) {
25
31
this .requestQueue = requestQueue ;
26
32
this .apiUrl = apiUrl ;
33
+ this .executor = Executors .newSingleThreadExecutor ();
34
+ this .mainHandler = new Handler (Looper .getMainLooper ());
27
35
}
28
36
29
37
@ Override
@@ -32,29 +40,34 @@ public void fetchPromotedApps(PromotedAppsCallback callback) {
32
40
Request .Method .GET ,
33
41
apiUrl ,
34
42
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 ()))
57
48
);
58
49
requestQueue .add (request );
59
50
}
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
+ }
60
73
}
0 commit comments