More Tweaks

This commit is contained in:
TheBrokenRail 2024-02-17 16:51:16 -05:00
parent efacd10fb5
commit fd2afd5bba
15 changed files with 113 additions and 103 deletions

View File

@ -3,20 +3,7 @@
<component name="deploymentTargetDropDown">
<value>
<entry key="app">
<State>
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="R5CRB1GE0RY" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-02-17T18:39:39.847936576Z" />
</State>
<State />
</entry>
</value>
</component>

View File

@ -10,6 +10,8 @@ import androidx.recyclerview.widget.RecyclerView;
import com.thebrokenrail.mtudining.activity.menu.MenuActivity;
import com.thebrokenrail.mtudining.activity.task.Task;
import com.thebrokenrail.mtudining.activity.task.TaskAdapter;
import com.thebrokenrail.mtudining.api.method.AllLocations;
import com.thebrokenrail.mtudining.util.Category;
import com.thebrokenrail.mtudining.widget.CategoryView;
/**
@ -31,18 +33,14 @@ class ListAdapter extends TaskAdapter<ListData> {
@Override
protected void bindItemView(View view, int position) {
ListData.Category data = getResult().categories.get(position - getFirstElementPosition());
Category<AllLocations.Response.Location> data = getResult().categories.get(position - getFirstElementPosition());
// Setup View
CategoryView category = (CategoryView) view;
category.setup(data.isOpen, data.name, () -> {
category.setup(data, () -> {
// Open/Close Category
data.isOpen = !data.isOpen;
notifyItemChanged(getResult().categories.indexOf(data) + getFirstElementPosition());
});
// Add Locations
category.clearItems();
for (ListData.Category.Element location : data.locations) {
category.addItem(location.name, () -> {
}, location -> {
// Open Menu
Intent intent = new Intent(category.getContext(), MenuActivity.class);
intent.putExtra(MenuActivity.ID_EXTRA, location.id);
@ -50,7 +48,6 @@ class ListAdapter extends TaskAdapter<ListData> {
category.getContext().startActivity(intent);
});
}
}
@Override
protected int getDataSize() {

View File

@ -1,5 +1,8 @@
package com.thebrokenrail.mtudining.activity.list;
import com.thebrokenrail.mtudining.api.method.AllLocations;
import com.thebrokenrail.mtudining.util.Category;
import java.util.ArrayList;
import java.util.List;
@ -7,28 +10,8 @@ import java.util.List;
* Data to be displayed in {@link ListActivity}.
*/
class ListData {
public static class Category {
public static class Element {
public final String id;
public final String name;
public Element(String id, String name) {
this.id = id;
this.name = name;
}
}
public final String name;
public boolean isOpen = true;
public final List<Element> locations = new ArrayList<>();
public Category(String name) {
this.name = name;
}
}
public final String siteId;
public final List<Category> categories = new ArrayList<>();
public final List<Category<AllLocations.Response.Location>> categories = new ArrayList<>();
ListData(String siteId) {
this.siteId = siteId;

View File

@ -4,6 +4,7 @@ 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.Category;
import com.thebrokenrail.mtudining.util.Constants;
/**
@ -30,15 +31,15 @@ class ListTask extends Task<ListData> {
for (AllLocations.Response.Building building : allLocationsResponse.buildings) {
if (building.active) {
// Found Active Building
ListData.Category category = new ListData.Category(building.name);
Category<AllLocations.Response.Location> category = new Category<>(building.name);
for (AllLocations.Response.Location location : building.locations) {
if (location.active) {
// Found Active Location
category.locations.add(new ListData.Category.Element(location.id, location.name));
category.items.add(location);
}
}
// Skip Empty Category
if (category.locations.size() > 0) {
if (category.items.size() > 0) {
data.categories.add(category);
}
}

View File

@ -0,0 +1,10 @@
package com.thebrokenrail.mtudining.activity.menu;
/**
* Dialog for a food item.
*/
public class ItemDialog {
public void show() {
}
}

View File

@ -12,6 +12,8 @@ import com.google.android.material.textfield.TextInputLayout;
import com.thebrokenrail.mtudining.R;
import com.thebrokenrail.mtudining.activity.task.Task;
import com.thebrokenrail.mtudining.activity.task.TaskAdapter;
import com.thebrokenrail.mtudining.api.method.PeriodDetail;
import com.thebrokenrail.mtudining.util.Category;
import com.thebrokenrail.mtudining.util.DateUtil;
import com.thebrokenrail.mtudining.widget.CategoryView;
import com.thebrokenrail.mtudining.widget.CustomDropDownView;
@ -56,22 +58,17 @@ class MenuAdapter extends TaskAdapter<MenuData> {
protected void bindItemView(View view, int position) {
MenuData.Meal meal = getMeal();
assert meal != null;
MenuData.Meal.Category data = meal.categories.get(position - getFirstElementPosition());
Category<PeriodDetail.Response.Menu.PeriodData.MenuCategory.MenuItem> data = meal.categories.get(position - getFirstElementPosition());
// Setup View
CategoryView category = (CategoryView) view;
category.setup(data.isOpen, data.name, () -> {
category.setup(data, () -> {
// Open/Close Category
data.isOpen = !data.isOpen;
notifyItemChanged(meal.categories.indexOf(data) + getFirstElementPosition());
});
// Add Locations
category.clearItems();
for (MenuData.Meal.Category.Element item : data.items) {
category.addItem(item.name, () -> {
}, item -> {
// Do Something!
});
}
}
@Override
protected int getDataSize() {

View File

@ -1,33 +1,16 @@
package com.thebrokenrail.mtudining.activity.menu;
import com.thebrokenrail.mtudining.api.method.PeriodDetail;
import com.thebrokenrail.mtudining.util.Category;
import java.util.ArrayList;
import java.util.List;
class MenuData {
public static class Meal {
public static class Category {
public static class Element {
public final String name;
public final String description;
public Element(String name, String description) {
this.name = name;
this.description = description;
}
}
public final String name;
public boolean isOpen = true;
public final List<Element> items = new ArrayList<>();
public Category(String name) {
this.name = name;
}
}
public final String id;
public final String name;
public final List<Category> categories = new ArrayList<>();
public final List<Category<PeriodDetail.Response.Menu.PeriodData.MenuCategory.MenuItem>> categories = new ArrayList<>();
public Meal(String id, String name) {
this.id = id;

View File

@ -4,6 +4,7 @@ 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;
@ -77,15 +78,13 @@ public class MenuTask extends Task<MenuData> {
// Add Data
for (PeriodDetail.Response.Menu.PeriodData.MenuCategory category : periodDetailResponse.menu.periods.categories) {
MenuData.Meal.Category menuCategory = new MenuData.Meal.Category(category.name);
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
for (PeriodDetail.Response.Menu.PeriodData.MenuCategory.MenuItem item : category.items) {
menuCategory.items.add(new MenuData.Meal.Category.Element(item.name, item.desc));
}
menuCategory.items.addAll(category.items);
// Skip Empty Category
if (menuCategory.items.size() > 0) {

View File

@ -202,7 +202,7 @@ public abstract class TaskAdapter<E> extends RecyclerView.Adapter<RecyclerView.V
*/
protected void reloadUI(int oldItemCount) {
// Reload Header Without Animation
notifyItemChanged(0, new Object());
notifyItemChanged(0, new Object()); // https://stackoverflow.com/a/45590003
// Remove Existing Items
notifyItemRangeRemoved(1, oldItemCount - 1);
// Add Items

View File

@ -49,7 +49,6 @@ public class Connection {
public <T> void send(Method<T> method, Consumer<T> success, Runnable error) {
// Build URL
String url = Constants.API_BASE + method.getPath();
System.out.println("GO: " + url);
// Build Request
Request request = new Request.Builder()

View File

@ -1,6 +1,7 @@
package com.thebrokenrail.mtudining.api.method;
import com.thebrokenrail.mtudining.api.Method;
import com.thebrokenrail.mtudining.util.Category;
import java.util.List;
@ -38,10 +39,15 @@ public class AllLocations implements Method<AllLocations.Response> {
public List<Location> locations;
}
public List<Building> buildings;
public static class Location {
public static class Location implements Category.Named {
public String id;
public String name;
public boolean active;
@Override
public String getName() {
return name;
}
}
public List<Location> locations;
}

View File

@ -1,6 +1,7 @@
package com.thebrokenrail.mtudining.api.method;
import com.thebrokenrail.mtudining.api.Method;
import com.thebrokenrail.mtudining.util.Category;
import com.thebrokenrail.mtudining.util.DateUtil;
import java.util.Date;
@ -33,10 +34,15 @@ public class PeriodDetail implements Method<PeriodDetail.Response> {
public static class Menu {
public static class PeriodData {
public static class MenuCategory {
public static class MenuItem {
public static class MenuItem implements Category.Named {
public String name;
public String desc;
public int sort_order;
@Override
public String getName() {
return name;
}
}
public String name;
public List<MenuItem> items;

View File

@ -0,0 +1,30 @@
package com.thebrokenrail.mtudining.util;
import java.util.ArrayList;
import java.util.List;
/**
* Simple class representing a category.
*/
public class Category<T extends Category.Named> {
public Category(String name) {
this.name = name;
}
public interface Named {
String getName();
}
/**
* Category name.
*/
public final String name;
/**
* If category is open.
*/
public boolean isOpen = true;
/**
* Category items.
*/
public final List<T> items = new ArrayList<>();
}

View File

@ -13,6 +13,9 @@ import androidx.appcompat.widget.AppCompatTextView;
import com.google.android.material.card.MaterialCardView;
import com.thebrokenrail.mtudining.R;
import com.thebrokenrail.mtudining.util.Category;
import java.util.function.Consumer;
/**
* Widget that shows a category of items.
@ -64,21 +67,26 @@ public class CategoryView extends FrameLayout {
/**
* Setup widget.
* @param isOpen If category is open
* @param titleText The category's title
* @param category The category
* @param onClickTitle Callback when clicking on title
* @param onClickItem Callback when clicking on an item
*/
public void setup(boolean isOpen, String titleText, Runnable onClickTitle) {
titleText = (isOpen ? "" : "") + " " + titleText;
public <T extends Category.Named> void setup(Category<T> category, Runnable onClickTitle, Consumer<T> onClickItem) {
String titleText = (category.isOpen ? "" : "") + " " + category.name;
title.setText(titleText);
card.setVisibility(isOpen ? VISIBLE : GONE);
card.setVisibility(category.isOpen ? VISIBLE : GONE);
title.setOnClickListener(v -> onClickTitle.run());
// Add Items
clearItems();
for (T item : category.items) {
addItem(item.getName(), () -> onClickItem.accept(item));
}
}
/**
* Clear category.
*/
public void clearItems() {
private void clearItems() {
children.removeAllViews();
}
@ -87,7 +95,7 @@ public class CategoryView extends FrameLayout {
* @param name Item name
* @param onClick Click handler
*/
public void addItem(String name, Runnable onClick) {
private void addItem(String name, Runnable onClick) {
AppCompatTextView item = new AppCompatTextView(getContext());
// Text
item.setText(name);

View File

@ -3,14 +3,15 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
android:orientation="horizontal"
android:padding="@dimen/margin">
<!-- Date -->
<com.thebrokenrail.mtudining.widget.CustomDropDownView
android:id="@+id/menu_date_field"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:layout_weight="1"
android:hint="@string/date"
style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
app:endIconMode="custom"
@ -24,14 +25,17 @@
</com.thebrokenrail.mtudining.widget.CustomDropDownView>
<!-- Padding -->
<View
android:layout_width="@dimen/margin"
android:layout_height="0dp" />
<!-- Meal -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/menu_meal_field"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin"
android:layout_marginStart="@dimen/margin"
android:layout_marginEnd="@dimen/margin"
android:layout_weight="1"
android:hint="@string/meal"
style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu">