119 lines
3.9 KiB
Java
119 lines
3.9 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.Comparator;
|
|
import java.util.Date;
|
|
|
|
/**
|
|
* Task for loading menu information.
|
|
*/
|
|
public class MenuTask extends Task<MenuData> {
|
|
private final Connection connection;
|
|
|
|
private String locationId;
|
|
private Date date;
|
|
|
|
public MenuTask(Connection connection) {
|
|
this.connection = connection;
|
|
}
|
|
|
|
/**
|
|
* Setup task.
|
|
* @param locationId Location ID (or null to use existing)
|
|
* @param date Date
|
|
*/
|
|
void setup(String locationId, Date date) {
|
|
if (locationId != null) {
|
|
this.locationId = locationId;
|
|
}
|
|
this.date = date;
|
|
}
|
|
|
|
/**
|
|
* Check if task is setup.
|
|
* @return If the task is setup
|
|
*/
|
|
boolean isSetup() {
|
|
return locationId != null;
|
|
}
|
|
|
|
/**
|
|
* Get date.
|
|
* @return The date
|
|
*/
|
|
public Date getDate() {
|
|
return date;
|
|
}
|
|
|
|
@Override
|
|
protected void startImpl(long id) {
|
|
// Get Periods
|
|
Periods periods = new Periods(Constants.PLATFORM, locationId, date);
|
|
connection.send(periods, periodsResponse -> {
|
|
// Loaded Periods
|
|
|
|
// Sort Periods/Meals
|
|
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, date, 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);
|
|
}
|
|
// Set Selected Meal
|
|
data.selectedMeal = data.meals.get(0).name;
|
|
} else {
|
|
// No Periods To Load
|
|
done(id, data);
|
|
}
|
|
}, () -> {
|
|
// Error
|
|
done(id, null);
|
|
});
|
|
}
|
|
}
|