Refresh Failed Canteen List
This commit is contained in:
@@ -8,10 +8,12 @@ import org.json.JSONObject;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import de.sebse.fuplanner.MainActivity;
|
||||||
import de.sebse.fuplanner.services.Canteen.types.Canteen;
|
import de.sebse.fuplanner.services.Canteen.types.Canteen;
|
||||||
import de.sebse.fuplanner.services.Canteen.types.Canteens;
|
import de.sebse.fuplanner.services.Canteen.types.Canteens;
|
||||||
import de.sebse.fuplanner.services.Canteen.types.Day;
|
import de.sebse.fuplanner.services.Canteen.types.Day;
|
||||||
import de.sebse.fuplanner.tools.AsyncQueue;
|
import de.sebse.fuplanner.tools.AsyncQueue;
|
||||||
|
import de.sebse.fuplanner.tools.MainAcitivityListener;
|
||||||
import de.sebse.fuplanner.tools.network.HTTPService;
|
import de.sebse.fuplanner.tools.network.HTTPService;
|
||||||
import de.sebse.fuplanner.tools.network.NetworkCallback;
|
import de.sebse.fuplanner.tools.network.NetworkCallback;
|
||||||
import de.sebse.fuplanner.tools.network.NetworkError;
|
import de.sebse.fuplanner.tools.network.NetworkError;
|
||||||
@@ -21,10 +23,15 @@ public class CanteenBrowser extends HTTPService {
|
|||||||
private Canteens canteens;
|
private Canteens canteens;
|
||||||
private final AsyncQueue queue = new AsyncQueue();
|
private final AsyncQueue queue = new AsyncQueue();
|
||||||
private final Context context;
|
private final Context context;
|
||||||
|
private MainAcitivityListener mListener;
|
||||||
|
|
||||||
public CanteenBrowser(Context context) {
|
public CanteenBrowser(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
if (context instanceof MainAcitivityListener)
|
||||||
|
mListener = (MainActivity) context;
|
||||||
|
else
|
||||||
|
throw new RuntimeException(context.toString() + "must implement MainActivityListener");
|
||||||
try {
|
try {
|
||||||
this.canteens = Canteens.load(context);
|
this.canteens = Canteens.load(context);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -46,15 +53,14 @@ public class CanteenBrowser extends HTTPService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.upgradeCanteens(success -> {
|
this.upgradeCanteens(success -> {
|
||||||
log.d("updated canteen list");
|
|
||||||
if (this.canteens == null)
|
if (this.canteens == null)
|
||||||
this.canteens = success;
|
this.canteens = success;
|
||||||
else
|
else
|
||||||
this.canteens.update(success);
|
this.canteens.update(success);
|
||||||
this.save();
|
this.save();
|
||||||
callback.onResponse(this.canteens);
|
saveOnCallback(callback, forceRefresh).onResponse(this.canteens);
|
||||||
queue.next("list");
|
queue.next("list");
|
||||||
}, queue.check("list", errorCallback));
|
}, queue.check("list", errorOnCallback(errorCallback)));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,12 +117,11 @@ public class CanteenBrowser extends HTTPService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.upgradeCanteen(canteen, success -> {
|
this.upgradeCanteen(canteen, success -> {
|
||||||
log.d("updated canteen");
|
|
||||||
canteen.update(success);
|
canteen.update(success);
|
||||||
this.save();
|
this.save();
|
||||||
callback.onResponse(canteen);
|
saveOnCallback(callback, forceRefresh).onResponse(canteen);
|
||||||
queue.next(hash);
|
queue.next(hash);
|
||||||
}, queue.check(hash, errorCallback));
|
}, queue.check(hash, errorOnCallback(errorCallback)));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,12 +164,11 @@ public class CanteenBrowser extends HTTPService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.upgradeDay(day, success -> {
|
this.upgradeDay(day, success -> {
|
||||||
log.d("updated day");
|
|
||||||
day.update(success);
|
day.update(success);
|
||||||
this.save();
|
this.save();
|
||||||
callback.onResponse(day);
|
saveOnCallback(callback, forceRefresh).onResponse(day);
|
||||||
queue.next(hash);
|
queue.next(hash);
|
||||||
}, queue.check(hash, errorCallback));
|
}, queue.check(hash, errorOnCallback(errorCallback)));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,4 +220,22 @@ public class CanteenBrowser extends HTTPService {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private<T> NetworkCallback<T> saveOnCallback(NetworkCallback<T> callback, boolean forceRefresh){
|
||||||
|
return (success -> {
|
||||||
|
if (forceRefresh)
|
||||||
|
mListener.refreshFailed(false);
|
||||||
|
callback.onResponse(success);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private NetworkErrorCallback errorOnCallback(NetworkErrorCallback errorCallback){
|
||||||
|
return (error -> {
|
||||||
|
if (error.getHttpStatus() == 401 || error.getHttpStatus() == 403)
|
||||||
|
mListener.loginTokenInvalid(false);
|
||||||
|
else
|
||||||
|
mListener.refreshFailed(true);
|
||||||
|
errorCallback.onError(error);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user