Skip to content

Commit d3fc36f

Browse files
committed
Fixed bug where app would crash if you didn't have an internet connection and were trying to load a schedule in a bus stop on the bus route details view
1 parent 7f2903c commit d3fc36f

File tree

4 files changed

+97
-46
lines changed

4 files changed

+97
-46
lines changed

app/src/main/java/com/example/carrismobile/RouteDetailsFragment.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,17 @@ public void run() {
179179
currentDirectionIndex = adapterView.getSelectedItemPosition();
180180
stopList.clear();
181181
if (currentCarreira.getDirectionList().get(currentDirectionIndex).getPathList().get(0).getStop().getScheduleList() == null){
182-
currentCarreira.updateSchedulesOnStopOnGivenDirectionAndStop(currentDirectionIndex, 0);
182+
try{
183+
currentCarreira.updateSchedulesOnStopOnGivenDirectionAndStop(currentDirectionIndex, 0);
184+
}catch (Exception ignore){
185+
getActivity().runOnUiThread(new Runnable() {
186+
@Override
187+
public void run() {
188+
dialog.show();
189+
}
190+
});
191+
connected = false;
192+
}
183193
}
184194
List<Stop> toAdd = new ArrayList<>();
185195
currentCarreira.getDirectionList().get(currentDirectionIndex).getPathList().forEach(path -> toAdd.add(path.getStop()));
@@ -222,7 +232,17 @@ public void run() {
222232
stopList.addAll(toAdd);
223233
}
224234
Stop currentStop = stopList.get(currentStopIndex);
225-
currentCarreira.updateSchedulesOnStopOnGivenDirectionAndStop(currentDirectionIndex, currentStopIndex);
235+
try{
236+
currentCarreira.updateSchedulesOnStopOnGivenDirectionAndStop(currentDirectionIndex, currentStopIndex);
237+
}catch (Exception ignore){
238+
getActivity().runOnUiThread(new Runnable() {
239+
@Override
240+
public void run() {
241+
dialog.show();
242+
}
243+
});
244+
connected = false;
245+
}
226246
double[] coordinates = currentStop.getCoordinates();
227247
scheduleList.clear();
228248
scheduleList.addAll(currentStop.getScheduleList());
@@ -423,7 +443,18 @@ public void loadCarreira(Carreira carreira){
423443
@Override
424444
public void run() {
425445
startWaitingAnimation();
426-
carreira.updateSchedulesOnStopOnGivenDirectionAndStop(currentDirectionIndex, 0);
446+
try{
447+
carreira.updateSchedulesOnStopOnGivenDirectionAndStop(currentDirectionIndex, 0);
448+
}catch (Exception ignore){
449+
getActivity().runOnUiThread(new Runnable() {
450+
@Override
451+
public void run() {
452+
dialog.show();
453+
}
454+
});
455+
connected = false;
456+
457+
}
427458
currentCarreira = carreira;
428459
currentCarreiraId = carreira.getRouteId();
429460
currentStopIndex = 0;

app/src/main/java/com/example/carrismobile/RouteFavoritesFragment.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,29 +125,35 @@ public void onClick(View view) {
125125
}
126126

127127
private void init(){
128-
mPrefs = getActivity().getSharedPreferences("RouteFavoritesFragment", MODE_PRIVATE);
129-
String size;
130-
size = (String)loadObject("key_carreiraList_size", String.class);
131-
if (size == null){
132-
Log.d("STOP FAVORITES INIT", "SIZE WAS NULL");
133-
storeObject(new Gson().toJson("0"), "key_carreiraList_size");
134-
init();
135-
return;
136-
}
137-
for (int i = 0; i < Integer.parseInt(size); i++){
138-
Carreira carreiraToAdd = (Carreira)loadObject("key_carreiraList_carreira_" + i, Carreira.class);
139-
carreiraList.add(carreiraToAdd);
140-
Log.d("Carreira Recovered", carreiraToAdd.getName());
141-
}
142-
currentCarreiraList.addAll(carreiraList);
143-
currentCarreiraList.sort(Comparator.comparing(Carreira::getRouteId));
144-
getActivity().runOnUiThread(new Runnable() {
128+
Thread initThread = new Thread(new Runnable() {
145129
@Override
146130
public void run() {
147-
carreiraListAdapter = new RouteImageListAdaptor(getActivity(), currentCarreiraList, 0);
148-
list.setAdapter(carreiraListAdapter);
131+
mPrefs = getActivity().getSharedPreferences("RouteFavoritesFragment", MODE_PRIVATE);
132+
String size;
133+
size = (String)loadObject("key_carreiraList_size", String.class);
134+
if (size == null){
135+
Log.d("STOP FAVORITES INIT", "SIZE WAS NULL");
136+
storeObject(new Gson().toJson("0"), "key_carreiraList_size");
137+
init();
138+
return;
139+
}
140+
for (int i = 0; i < Integer.parseInt(size); i++){
141+
Carreira carreiraToAdd = (Carreira)loadObject("key_carreiraList_carreira_" + i, Carreira.class);
142+
carreiraList.add(carreiraToAdd);
143+
Log.d("Carreira Recovered", carreiraToAdd.getName());
144+
}
145+
currentCarreiraList.addAll(carreiraList);
146+
currentCarreiraList.sort(Comparator.comparing(Carreira::getRouteId));
147+
getActivity().runOnUiThread(new Runnable() {
148+
@Override
149+
public void run() {
150+
carreiraListAdapter = new RouteImageListAdaptor(getActivity(), currentCarreiraList, 0);
151+
list.setAdapter(carreiraListAdapter);
152+
}
153+
});
149154
}
150155
});
156+
initThread.start();
151157
}
152158

153159
public void addCarreiraToFavorites(Carreira carreira){

app/src/main/java/com/example/carrismobile/StopFavoritesFragment.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,30 +136,37 @@ public void onClick(View view) {
136136
}
137137

138138
private void init(){
139-
mPrefs = getActivity().getSharedPreferences("StopFavoritesFragment", MODE_PRIVATE);
140-
//mPrefs.edit().clear().apply();
141-
String size;
142-
size = (String)loadObject("key_stopList_size", String.class);
143-
if (size == null){
144-
Log.d("STOP FAVORITES INIT", "SIZE WAS NULL");
145-
storeObject(new Gson().toJson("0"), "key_stopList_size");
146-
init();
147-
return;
148-
}
149-
for (int i = 0; i < Integer.parseInt(size); i++){
150-
Stop stopToAdd = (Stop)loadObject("key_stopList_stop_" + i, Stop.class);
151-
stopList.add(stopToAdd);
152-
Log.d("Stop Recovered", stopToAdd.getTts_name());
153-
}
154-
currentStopList.addAll(stopList);
155-
currentStopList.sort(Comparator.comparing(Stop::getTts_name));
156-
getActivity().runOnUiThread(new Runnable() {
139+
Thread initThread = new Thread(new Runnable() {
157140
@Override
158141
public void run() {
159-
stopImageListAdaptor = new StopImageListAdaptor(getActivity(), currentStopList);
160-
list.setAdapter(stopImageListAdaptor);
142+
mPrefs = getActivity().getSharedPreferences("StopFavoritesFragment", MODE_PRIVATE);
143+
//mPrefs.edit().clear().apply();
144+
String size;
145+
size = (String)loadObject("key_stopList_size", String.class);
146+
if (size == null){
147+
Log.d("STOP FAVORITES INIT", "SIZE WAS NULL");
148+
storeObject(new Gson().toJson("0"), "key_stopList_size");
149+
init();
150+
return;
151+
}
152+
for (int i = 0; i < Integer.parseInt(size); i++){
153+
Stop stopToAdd = (Stop)loadObject("key_stopList_stop_" + i, Stop.class);
154+
stopList.add(stopToAdd);
155+
Log.d("Stop Recovered", stopToAdd.getTts_name());
156+
}
157+
currentStopList.addAll(stopList);
158+
currentStopList.sort(Comparator.comparing(Stop::getTts_name));
159+
getActivity().runOnUiThread(new Runnable() {
160+
@Override
161+
public void run() {
162+
stopImageListAdaptor = new StopImageListAdaptor(getActivity(), currentStopList);
163+
list.setAdapter(stopImageListAdaptor);
164+
}
165+
});
161166
}
162167
});
168+
initThread.start();
169+
163170
}
164171

165172
public void addStopToFavorites(Stop stop){

app/src/main/java/data_structure/Carreira.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package data_structure;
22

3+
import android.net.http.HttpException;
34
import android.util.Log;
45

56
import androidx.annotation.Nullable;
67

8+
import org.jsoup.HttpStatusException;
9+
710
import java.io.File;
811
import java.io.FileInputStream;
912
import java.io.FileOutputStream;
1013
import java.io.ObjectInputStream;
1114
import java.io.ObjectOutputStream;
1215
import java.io.Serializable;
16+
import java.net.HttpRetryException;
1317
import java.util.ArrayList;
1418
import java.util.List;
1519

@@ -53,7 +57,7 @@ public void init(){
5357
}
5458
}
5559

56-
public void updateSchedulesOnStopOnGivenDirectionAndStop(int directionIndex, int stopIndex){
60+
public void updateSchedulesOnStopOnGivenDirectionAndStop(int directionIndex, int stopIndex) throws IllegalStateException{
5761
Carreira currentCarreira = this;
5862
Direction currentDirection = currentCarreira.getDirectionList().get(directionIndex);
5963
//TODO still not working
@@ -62,15 +66,18 @@ public void updateSchedulesOnStopOnGivenDirectionAndStop(int directionIndex, int
6266
stopToUpdate.init();
6367
List<Schedule> schedules = stopToUpdate.getScheduleList();
6468
List<RealTimeSchedule> realTimeSchedules = new ArrayList<>();
65-
realTimeSchedules.addAll(getSchedules(stopToUpdate.getStopID(), 5));
69+
realTimeSchedules.addAll(getSchedules(stopToUpdate.getStopID(), 0,5));
6670
//RouteDetails will make sure to catch the error (I hope so)
6771
realTimeSchedules.removeIf(realTimeSchedule -> !realTimeSchedule.getPattern_id().equals(currentDirection.getDirectionId()));
6872
//TODO travel_time not being assigned correct
6973
realTimeSchedules.forEach(realTimeSchedule -> schedules.add(new Schedule(stopToUpdate.getStopID(), realTimeSchedule.getScheduled_arrival(), "")));
7074
//Log.e("Stop Schedule added", schedules.toString());
7175
}
7276

73-
public static List<RealTimeSchedule> getSchedules(int stopId, int attempt){
77+
public static List<RealTimeSchedule> getSchedules(int stopId, int attempt, int maxAttempt) throws IllegalArgumentException {
78+
if (attempt == maxAttempt){
79+
throw new IllegalArgumentException("Attempt has reached maxAttempt");
80+
}
7481
String newStopId = stopId+"";
7582
while(newStopId.length() < 6){
7683
newStopId = "0" + newStopId;
@@ -79,7 +86,7 @@ public static List<RealTimeSchedule> getSchedules(int stopId, int attempt){
7986
try{
8087
realTimeScheduleList = Api.getRealTimeStops(newStopId);
8188
}catch (Exception e){
82-
return getSchedules(stopId, attempt + 1);
89+
return getSchedules(stopId, attempt + 1, maxAttempt);
8390
}
8491
return realTimeScheduleList;
8592
}

0 commit comments

Comments
 (0)