128 lines
4.6 KiB
Java
128 lines
4.6 KiB
Java
package com.thebrokenrail.mtudining.activity.menu;
|
|
|
|
import com.thebrokenrail.mtudining.activity.task.Task;
|
|
import com.thebrokenrail.mtudining.api.Connection;
|
|
import com.thebrokenrail.mtudining.api.method.PeriodDetail;
|
|
import com.thebrokenrail.mtudining.api.method.Periods;
|
|
import com.thebrokenrail.mtudining.util.Category;
|
|
import com.thebrokenrail.mtudining.util.Constants;
|
|
|
|
import java.util.Collections;
|
|
import java.util.Comparator;
|
|
|
|
/**
|
|
* Task for loading menu information.
|
|
*/
|
|
public class MenuTask extends Task<MenuData> {
|
|
private final Connection connection;
|
|
|
|
private String locationId;
|
|
private final MenuState state;
|
|
|
|
public MenuTask(Connection connection, MenuState state) {
|
|
this.connection = connection;
|
|
this.state = state;
|
|
}
|
|
|
|
/**
|
|
* Setup task.
|
|
* @param locationId Location ID
|
|
*/
|
|
void setup(String locationId) {
|
|
this.locationId = locationId;
|
|
}
|
|
|
|
@Override
|
|
protected void startImpl(long id) {
|
|
// Cancel Existing HTTP Calls
|
|
connection.cancelAll();
|
|
// Get Periods
|
|
Periods periods = new Periods(Constants.PLATFORM, locationId, state.getDate());
|
|
connection.send(periods, periodsResponse -> {
|
|
// Loaded Periods
|
|
|
|
// Sort Periods/Meals
|
|
if (periodsResponse.periods == null) {
|
|
periodsResponse.periods = Collections.emptyList();
|
|
}
|
|
periodsResponse.periods.sort(Comparator.comparingInt(a -> a.sort_order));
|
|
|
|
// Load Menu For Each Period
|
|
MenuData data = new MenuData();
|
|
int[] remaining = {periodsResponse.periods.size()};
|
|
if (remaining[0] > 0) {
|
|
// Start Loading Menus
|
|
for (Periods.Response.Period period : periodsResponse.periods) {
|
|
// Load Period Details
|
|
MenuData.Meal meal = new MenuData.Meal(period.id, period.name);
|
|
PeriodDetail periodDetail = new PeriodDetail(Constants.PLATFORM, locationId, state.getDate(), period.id);
|
|
connection.send(periodDetail, periodDetailResponse -> {
|
|
// Loaded Period Details
|
|
|
|
// Sort Categories
|
|
periodDetailResponse.menu.periods.categories.sort(Comparator.comparingInt(a -> a.sort_order));
|
|
|
|
// Add Data
|
|
for (PeriodDetail.Response.Menu.PeriodData.MenuCategory category : periodDetailResponse.menu.periods.categories) {
|
|
Category<PeriodDetail.Response.Menu.PeriodData.MenuCategory.MenuItem> menuCategory = new Category<>(category.name);
|
|
|
|
// Sort Items
|
|
category.items.sort(Comparator.comparingInt(a -> a.sort_order));
|
|
|
|
// Add Items To Category
|
|
menuCategory.items.addAll(category.items);
|
|
|
|
// Skip Empty Category
|
|
if (menuCategory.items.size() > 0) {
|
|
meal.categories.add(menuCategory);
|
|
}
|
|
}
|
|
|
|
// Check If All Data Has Been Loaded
|
|
remaining[0]--;
|
|
if (remaining[0] == 0) {
|
|
// All Data Loaded
|
|
done(id, data);
|
|
}
|
|
}, () -> {
|
|
// Error
|
|
done(id, null);
|
|
});
|
|
data.meals.add(meal);
|
|
}
|
|
} else {
|
|
// No Periods To Load
|
|
done(id, data);
|
|
}
|
|
}, () -> {
|
|
// Error
|
|
done(id, null);
|
|
});
|
|
}
|
|
|
|
@Override
|
|
protected void done(long id, MenuData obj) {
|
|
// Update Selected Meal If Needed
|
|
if (obj != null && obj.meals.size() > 0) {
|
|
boolean currentSelectedMealIsValid = false;
|
|
String selectedMeal = state.getSelectedMeal();
|
|
if (selectedMeal != null) {
|
|
// Check If Current Selection Is Valid
|
|
for (MenuData.Meal meal : obj.meals) {
|
|
if (selectedMeal.equals(meal.name)) {
|
|
currentSelectedMealIsValid = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (!currentSelectedMealIsValid) {
|
|
// Current Selection Is Invalid, Change It
|
|
state.setSelectedMeal(obj.meals.get(0).name);
|
|
}
|
|
}
|
|
|
|
// Call Parent
|
|
super.done(id, obj);
|
|
}
|
|
}
|