Added canteens, made Canteen plan more stable

This commit is contained in:
Caesar2011
2018-10-20 00:27:06 +02:00
parent 6e45f1f660
commit e70daa33cc
6 changed files with 48 additions and 9 deletions

View File

@@ -113,6 +113,7 @@ public class DaySwitcherFragment extends Fragment implements DaySwitcherListener
CanteenBrowser browser = ((MainActivity) getActivity()).getCanteenBrowser(); CanteenBrowser browser = ((MainActivity) getActivity()).getCanteenBrowser();
browser.getCanteens(canteens -> { browser.getCanteens(canteens -> {
Canteen canteen = canteens.getCanteen(mCanteenId); Canteen canteen = canteens.getCanteen(mCanteenId);
canteen.cleanUpDays();
adapterViewPager.setModule(canteen); adapterViewPager.setModule(canteen);
browser.getCanteen(canteen, success -> { browser.getCanteen(canteen, success -> {
adapterViewPager.setModule(); adapterViewPager.setModule();

View File

@@ -58,16 +58,40 @@ class MealAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
String value; String value;
switch (Preferences.getString(mContext, R.array.pref_price_group)) { switch (Preferences.getString(mContext, R.array.pref_price_group)) {
case "student": case "student":
if (meal.getPriceStdnt() < 0)
value = mContext.getResources().getString(R.string.no_price_available);
else
value = mContext.getString(R.string.price, meal.getPriceStdnt()); value = mContext.getString(R.string.price, meal.getPriceStdnt());
break; break;
case "employee": case "employee":
if (meal.getPriceEmply() < 0)
value = mContext.getResources().getString(R.string.no_price_available);
else
value = mContext.getString(R.string.price, meal.getPriceEmply()); value = mContext.getString(R.string.price, meal.getPriceEmply());
break; break;
case "other": case "other":
if (meal.getPriceOther() < 0)
value = mContext.getResources().getString(R.string.no_price_available);
else
value = mContext.getString(R.string.price, meal.getPriceOther()); value = mContext.getString(R.string.price, meal.getPriceOther());
break; break;
default: default:
value = mContext.getString(R.string.prices, meal.getPriceStdnt(), meal.getPriceEmply(), meal.getPriceOther()); String value1;
if (meal.getPriceStdnt() < 0)
value1 = " -/- ";
else
value1 = mContext.getString(R.string.price, meal.getPriceStdnt());
String value2;
if (meal.getPriceEmply() < 0)
value2 = " -/- ";
else
value2 = mContext.getString(R.string.price, meal.getPriceEmply());
String value3;
if (meal.getPriceOther() < 0)
value3 = " -/- ";
else
value3 = mContext.getString(R.string.price, meal.getPriceOther());
value = mContext.getString(R.string.prices, value1, value2, value3);
} }
viewHolder.mSubTitle.setText(value); viewHolder.mSubTitle.setText(value);
StringBuilder string = new StringBuilder(); StringBuilder string = new StringBuilder();

View File

@@ -65,7 +65,7 @@ public class CanteenBrowser extends HTTPService {
} }
private void upgradeCanteens(final NetworkCallback<Canteens> callback, final NetworkErrorCallback errorCallback) { private void upgradeCanteens(final NetworkCallback<Canteens> callback, final NetworkErrorCallback errorCallback) {
get("https://openmensa.org/api/v2/canteens", null, response -> { get("https://openmensa.org/api/v2/canteens?near[lat]=52.449743&near[lng]=13.282245&near[dist]=7", null, response -> {
String body = response.getParsed(); String body = response.getParsed();
if (body == null) { if (body == null) {
errorCallback.onError(new NetworkError(201101, 403, "No canteen list retrieved!")); errorCallback.onError(new NetworkError(201101, 403, "No canteen list retrieved!"));
@@ -192,9 +192,9 @@ public class CanteenBrowser extends HTTPService {
double priceEmply = 0; double priceEmply = 0;
double priceOther = 0; double priceOther = 0;
if (prices != null) { if (prices != null) {
priceStdnt = prices.getDouble("students"); priceOther = prices.optDouble("others", -1);
priceEmply = prices.getDouble("employees"); priceEmply = prices.optDouble("employees", priceOther);
priceOther = prices.getDouble("others"); priceStdnt = prices.optDouble("students", priceEmply);
} }
JSONArray noteArray = meal.getJSONArray("notes"); JSONArray noteArray = meal.getJSONArray("notes");
String[] notes = new String[noteArray.length()]; String[] notes = new String[noteArray.length()];
@@ -206,6 +206,7 @@ public class CanteenBrowser extends HTTPService {
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
log.d(body);
errorCallback.onError(new NetworkError(201302, 403, "Cannot parse meal list!")); errorCallback.onError(new NetworkError(201302, 403, "Cannot parse meal list!"));
return; return;
} }

View File

@@ -50,6 +50,17 @@ public class Canteen implements Serializable, Iterable<Day> {
} }
} }
public void cleanUpDays() {
SortedListDay newList = new SortedListDay();
Calendar cal = Calendar.getInstance();
for (Day day : list) {
if (Canteen.calendarToKey(day.getCalendar()).compareTo(Canteen.calendarToKey(cal)) >= 0) {
newList.add(day);
}
}
list = newList;
}
public int size() { public int size() {
return this.list.size(); return this.list.size();
} }

View File

@@ -62,4 +62,5 @@
<string name="ErrorFileDownloadText">Beim Herunterladen der Datei ist ein Fehler aufgetreten. Prüfe, ob Du mit dem Internet verbunden bist und der App Zugriff auf den Speicher gewährt hast.</string> <string name="ErrorFileDownloadText">Beim Herunterladen der Datei ist ein Fehler aufgetreten. Prüfe, ob Du mit dem Internet verbunden bist und der App Zugriff auf den Speicher gewährt hast.</string>
<string name="invalid_credentials">Ungültiger Benutzername oder Password!</string> <string name="invalid_credentials">Ungültiger Benutzername oder Password!</string>
<string name="error_occurred_code">Fehler %1$d aufgetreten!</string> <string name="error_occurred_code">Fehler %1$d aufgetreten!</string>
<string name="no_price_available">Kein Preis verfügbar!</string>
</resources> </resources>

View File

@@ -40,7 +40,7 @@
<string name="refresh">Refresh</string> <string name="refresh">Refresh</string>
<string name="go_to_today">Go to today</string> <string name="go_to_today">Go to today</string>
<string name="canteens">Canteens</string> <string name="canteens">Canteens</string>
<string name="prices" translatable="false">%1$.2f€ / %2$.2f€ / %3$.2f€</string> <string name="prices" translatable="false">%1$s / %2$s / %3$s</string>
<string name="price" translatable="false">%1$.2f€</string> <string name="price" translatable="false">%1$.2f€</string>
<string name="grade_separator" translatable="false">/</string> <string name="grade_separator" translatable="false">/</string>
<string name="location_name">Location: %1$s</string> <string name="location_name">Location: %1$s</string>
@@ -69,4 +69,5 @@
<string name="ErrorFileDownloadText">An error occurred while downloading the file. Please check if you are connected to the internet and if you have granted storage access to the app.</string> <string name="ErrorFileDownloadText">An error occurred while downloading the file. Please check if you are connected to the internet and if you have granted storage access to the app.</string>
<string name="invalid_credentials">Invalid username or password!</string> <string name="invalid_credentials">Invalid username or password!</string>
<string name="error_occurred_code">Error occurred: %1$d</string> <string name="error_occurred_code">Error occurred: %1$d</string>
<string name="no_price_available">No price available!</string>
</resources> </resources>