MTU-Dining/app/src/main/java/com/thebrokenrail/mtudining/activity/list/ListViewModel.java
2024-02-16 22:04:50 -05:00

53 lines
2.2 KiB
Java

package com.thebrokenrail.mtudining.activity.list;
import androidx.lifecycle.ViewModel;
import com.thebrokenrail.mtudining.activity.task.Task;
import com.thebrokenrail.mtudining.api.Connection;
import com.thebrokenrail.mtudining.api.method.AllLocations;
import com.thebrokenrail.mtudining.api.method.Info;
import com.thebrokenrail.mtudining.util.Constants;
/**
* Data preserved between screen rotations for {@link ListActivity}.
*/
public class ListViewModel extends ViewModel {
private final Connection connection = new Connection();
public final Task<ListData> task = new Task<ListData>() {
@Override
protected void startImpl() {
// Load Site Info
Info info = new Info();
connection.send(info, infoResponse -> {
// Load Locations
AllLocations allLocations = new AllLocations(Constants.PLATFORM, infoResponse.site.id, true, false, true);
connection.send(allLocations, allLocationsResponse -> {
// Success
ListData data = new ListData(infoResponse.site.id);
// Find Active Locations
for (AllLocations.Response.Building building : allLocationsResponse.buildings) {
if (building.active) {
// Found Active Building
ListData.Category category = new ListData.Category(building.name);
for (AllLocations.Response.Location location : building.locations) {
if (location.active) {
// Found Active Location
category.locations.add(new ListData.Element(location.id, location.name));
}
}
data.categories.add(category);
}
}
done(data);
}, () -> {
// Failed Fetching Location Info
done(null);
});
}, () -> {
// Failed Fetching Site Info
done(null);
});
}
};
}