First Screen!

This commit is contained in:
TheBrokenRail 2024-02-16 23:02:42 -05:00
parent 1d98619a3b
commit eef05a9883
7 changed files with 88 additions and 18 deletions

View File

@ -2,13 +2,16 @@ package com.thebrokenrail.mtudining.activity.list;
import android.os.Bundle; import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.appbar.MaterialToolbar;
import com.thebrokenrail.mtudining.R; import com.thebrokenrail.mtudining.R;
import com.thebrokenrail.mtudining.util.EdgeToEdgeUtil;
/** /**
* This activity lists the available food halls. * This activity lists the available food halls.
@ -20,8 +23,13 @@ public class ListActivity extends AppCompatActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Setup UI // Setup UI
EdgeToEdge.enable(this);
setContentView(R.layout.activity_list); setContentView(R.layout.activity_list);
// Toolbar
MaterialToolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Get View Model // Get View Model
ViewModelProvider viewModelProvider = new ViewModelProvider(this); ViewModelProvider viewModelProvider = new ViewModelProvider(this);
ListViewModel viewModel = viewModelProvider.get(ListViewModel.class); ListViewModel viewModel = viewModelProvider.get(ListViewModel.class);
@ -31,5 +39,6 @@ public class ListActivity extends AppCompatActivity {
RecyclerView recyclerView = findViewById(R.id.recycler_view); RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
EdgeToEdgeUtil.setup(this, recyclerView);
} }
} }

View File

@ -1,5 +1,6 @@
package com.thebrokenrail.mtudining.activity.list; package com.thebrokenrail.mtudining.activity.list;
import android.util.TypedValue;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -8,11 +9,10 @@ import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView; import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.card.MaterialCardView; import com.thebrokenrail.mtudining.R;
import com.thebrokenrail.mtudining.activity.task.Task; import com.thebrokenrail.mtudining.activity.task.Task;
import com.thebrokenrail.mtudining.activity.task.TaskAdapter; import com.thebrokenrail.mtudining.activity.task.TaskAdapter;
import com.thebrokenrail.mtudining.widget.CategoryView; import com.thebrokenrail.mtudining.widget.CategoryView;
import com.thebrokenrail.mtudining.widget.LoaderView;
/** /**
* Adapter for listing dining halls. * Adapter for listing dining halls.
@ -51,11 +51,26 @@ public class ListAdapter extends TaskAdapter<ListData, ListAdapter.ViewHolder> {
// Add Locations // Add Locations
holder.view.children.removeAllViews(); holder.view.children.removeAllViews();
for (ListData.Element location : data.locations) { for (ListData.Element location : data.locations) {
AppCompatTextView textView = new AppCompatTextView(holder.view.getContext()); AppCompatTextView item = new AppCompatTextView(holder.view.getContext());
textView.setText(location.name); // Text
MaterialCardView.LayoutParams innerLayoutParams = new MaterialCardView.LayoutParams(MaterialCardView.LayoutParams.MATCH_PARENT, MaterialCardView.LayoutParams.WRAP_CONTENT); item.setText(location.name);
textView.setLayoutParams(innerLayoutParams); // Text Size
holder.view.children.addView(textView); item.setTextSize(TypedValue.COMPLEX_UNIT_PX, item.getResources().getDimension(R.dimen.item_font_size));
// Padding
int margin = holder.view.getResources().getDimensionPixelSize(R.dimen.margin);
item.setPadding(margin, margin, margin, margin);
// Make Clickable
item.setClickable(true);
item.setFocusable(true);
// Ripple Effect
TypedValue outValue = new TypedValue();
holder.view.getContext().getTheme().resolveAttribute(androidx.appcompat.R.attr.selectableItemBackground, outValue, true);
item.setBackgroundResource(outValue.resourceId);
// Layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
item.setLayoutParams(layoutParams);
// Add To View
holder.view.children.addView(item);
} }
} }

View File

@ -35,7 +35,10 @@ public class ListViewModel extends ViewModel {
category.locations.add(new ListData.Element(location.id, location.name)); category.locations.add(new ListData.Element(location.id, location.name));
} }
} }
data.categories.add(category); // Skip Empty Category
if (category.locations.size() > 0) {
data.categories.add(category);
}
} }
} }
done(data); done(data);

View File

@ -0,0 +1,31 @@
package com.thebrokenrail.mtudining.util;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;
/**
* Utility class for handling edge-to-edge.
*/
public class EdgeToEdgeUtil {
/**
* Setup edge-to-edge on an activity and a RecyclerView.
* @param activity The activity
* @param recyclerView The RecyclerView
*/
public static void setup(AppCompatActivity activity, RecyclerView recyclerView) {
// Insets
ViewCompat.setOnApplyWindowInsetsListener(activity.getWindow().getDecorView(), (v, windowInsets) -> {
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(insets.left, 0, insets.right, 0);
return windowInsets;
});
ViewCompat.setOnApplyWindowInsetsListener(recyclerView, (v, windowInsets) -> {
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
recyclerView.setPadding(0, 0, 0, insets.bottom);
return windowInsets;
});
}
}

View File

@ -1,6 +1,7 @@
package com.thebrokenrail.mtudining.widget; package com.thebrokenrail.mtudining.widget;
import android.content.Context; import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue; import android.util.TypedValue;
import android.widget.FrameLayout; import android.widget.FrameLayout;
@ -17,13 +18,13 @@ import com.thebrokenrail.mtudining.R;
* Widget that shows a category of items. * Widget that shows a category of items.
*/ */
public class CategoryView extends FrameLayout { public class CategoryView extends FrameLayout {
private final LinearLayout inner; private final MaterialCardView card;
public final MaterialCardView children; public final LinearLayout children;
private final AppCompatTextView title; private final AppCompatTextView title;
public CategoryView(@NonNull Context context, @Nullable AttributeSet attrs) { public CategoryView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs); super(context, attrs);
inner = new LinearLayout(context); LinearLayout inner = new LinearLayout(context);
inner.setOrientation(LinearLayout.VERTICAL); inner.setOrientation(LinearLayout.VERTICAL);
addView(inner); addView(inner);
@ -38,18 +39,27 @@ public class CategoryView extends FrameLayout {
TypedValue value = new TypedValue(); TypedValue value = new TypedValue();
context.getTheme().resolveAttribute(androidx.appcompat.R.attr.colorPrimary, value, true); context.getTheme().resolveAttribute(androidx.appcompat.R.attr.colorPrimary, value, true);
title.setTextColor(value.data); title.setTextColor(value.data);
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.category_font_size));
title.setTypeface(null, Typeface.BOLD);
title.setClickable(true); title.setClickable(true);
title.setFocusable(true); title.setFocusable(true);
inner.addView(title); inner.addView(title);
LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
title.setLayoutParams(innerLayoutParams); title.setLayoutParams(innerLayoutParams);
// Setup Children // Setup Card
children = new MaterialCardView(context, null, com.google.android.material.R.attr.materialCardViewElevatedStyle); card = new MaterialCardView(context, null, com.google.android.material.R.attr.materialCardViewElevatedStyle);
inner.addView(children); inner.addView(card);
innerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); innerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
innerLayoutParams.setMargins(0, margin, 0, 0); innerLayoutParams.setMargins(0, margin, 0, 0);
children.setLayoutParams(innerLayoutParams); card.setLayoutParams(innerLayoutParams);
// Setup Children
children = new LinearLayout(context);
children.setOrientation(LinearLayout.VERTICAL);
card.addView(children);
MaterialCardView.LayoutParams cardLayoutParams = new MaterialCardView.LayoutParams(MaterialCardView.LayoutParams.MATCH_PARENT, MaterialCardView.LayoutParams.WRAP_CONTENT);
children.setLayoutParams(cardLayoutParams);
} }
/** /**
@ -61,7 +71,7 @@ public class CategoryView extends FrameLayout {
public void setup(boolean isOpen, String titleText, Runnable onClickTitle) { public void setup(boolean isOpen, String titleText, Runnable onClickTitle) {
titleText = (isOpen ? "" : "") + " " + titleText; titleText = (isOpen ? "" : "") + " " + titleText;
title.setText(titleText); title.setText(titleText);
children.setVisibility(isOpen ? VISIBLE : GONE); card.setVisibility(isOpen ? VISIBLE : GONE);
title.setOnClickListener(v -> onClickTitle.run()); title.setOnClickListener(v -> onClickTitle.run());
} }
} }

View File

@ -4,7 +4,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
tools:context=".activity.list.ListActivity"> tools:context=".activity.list.ListActivity">
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
@ -24,6 +23,7 @@
android:id="@+id/recycler_view" android:id="@+id/recycler_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<dimen name="margin">8dp</dimen> <dimen name="margin">8dp</dimen>
<dimen name="category_font_size">20sp</dimen>
<dimen name="item_font_size">18sp</dimen>
</resources> </resources>