-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBusController.java
115 lines (90 loc) · 3.65 KB
/
BusController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.asuc.asucmobile.controllers;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import com.asuc.asucmobile.domain.models.BusDeparture;
import com.asuc.asucmobile.domain.models.BusInfo;
import com.asuc.asucmobile.domain.models.PTBusResponse;
import com.asuc.asucmobile.utilities.Callback;
import com.asuc.asucmobile.utilities.JSONUtilities;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.json.JSONException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
/**
* Created by alexthomas on 5/28/17.
*/
public class BusController implements Controller {
private static final String URL = BASE_URL + "/pt_routes?code=";
private static BusController instance;
private String title, id;
private HashMap<String, HashMap<String, Collection<PTBusResponse>>> PT_times;
private ArrayList<ArrayList<BusDeparture>> times = new ArrayList<>();
private Type testType;
private Callback callback;
private final Gson gson = new Gson();
public static Controller getInstance(String id, String title) {
if (instance == null || id == null || (!id.equals(instance.id))) {
instance = new BusController(id, title);
}
return instance;
}
public BusController(String id, String title) {
this.id = id;
this.title = title;
testType = new TypeToken<HashMap<String, HashMap<String, Collection<PTBusResponse>>>>() {
}.getType();
}
@Override
public void setResources(@NonNull final Context context, final JSONArray json) {
try {
if (json == null) {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
callback.onRetrievalFailed();
}
});
return;
}
new Thread(new Runnable() {
@Override
public void run() {
try {
PT_times = gson.fromJson((String)json.get(0), testType);
} catch (JSONException e) {
e.printStackTrace();
}
ArrayList<PTBusResponse> listOfItems = (ArrayList<PTBusResponse>) PT_times.get("ptbus_response").get("values");
final ArrayList<BusInfo> busInfos = new ArrayList<>();
for(PTBusResponse busResponse: listOfItems){
busInfos.add(new BusInfo(title, (ArrayList<BusDeparture>) busResponse.getValues(), busResponse.getRoute().getTitle(), ((ArrayList<BusDeparture>) busResponse.getValues()).get(0).getBusRouteName()));
}
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
callback.onDataRetrieved(busInfos);
}
});
}
}).start();
} catch (Exception e) {
e.printStackTrace();
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
callback.onRetrievalFailed();
}
});
}
}
@Override
public void refreshInBackground(@NonNull Context context, Callback callback) {
this.callback = callback;
JSONUtilities.readJSONFromUrl(context, URL + id, "ptbus_response", BusController.getInstance(id, title));
}
}