Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch committed May 19, 2022
1 parent a538559 commit 0ecf910
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 181 deletions.
12 changes: 6 additions & 6 deletions lib/cubits/recipe_list_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ class FilteredListRecipeCubitState extends ListRecipeCubitState {
const FilteredListRecipeCubitState({
this.selectedTags = const {},
this.allRecipes = const [],
List<Recipe> recipes = const [],
Set<Tag> tags = const {},
}) : super(recipes: recipes, tags: tags);
super.recipes = const [],
super.tags = const {},
});

factory FilteredListRecipeCubitState.fromState(
ListRecipeCubitState state,
Expand Down Expand Up @@ -151,9 +151,9 @@ class SearchRecipeCubitState extends ListRecipeCubitState {

const SearchRecipeCubitState({
required this.query,
List<Recipe> recipes = const [],
Set<Tag> tags = const {},
}) : super(recipes: recipes, tags: tags);
super.recipes = const [],
super.tags = const {},
});

@override
List<Object?> get props => super.props + [query];
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/item_search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class _ItemSearchPageState extends State<ItemSearchPage> {
items: state.searchResults,
selected: (item) => state.selectedItems.contains(item),
onLongPressed: (_) {},
onPressed: (item) {
onPressed: (Item item) {
if (!widget.multiple) {
Navigator.of(context).pop([item]);
} else {
Expand Down
59 changes: 10 additions & 49 deletions lib/pages/item_selection_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import 'package:kitchenowl/cubits/item_selection_cubit.dart';
import 'package:kitchenowl/models/item.dart';
import 'package:kitchenowl/kitchenowl.dart';
import 'package:kitchenowl/models/recipe.dart';
import 'package:kitchenowl/widgets/shopping_item.dart';
import 'package:responsive_builder/responsive_builder.dart';

class ItemSelectionPage extends StatefulWidget {
final List<Recipe> recipes;
Expand Down Expand Up @@ -41,13 +39,6 @@ class _ItemSelectionPageState extends State<ItemSelectionPage> {

@override
Widget build(BuildContext context) {
final int crossAxisCount = getValueForScreenType<int>(
context: context,
mobile: 3,
tablet: 6,
desktop: 9,
);

return Scaffold(
appBar: AppBar(
title: Text(widget.title ?? AppLocalizations.of(context)!.itemsAdd),
Expand All @@ -67,26 +58,11 @@ class _ItemSelectionPageState extends State<ItemSelectionPage> {
),
),
),
SliverPadding(
padding: const EdgeInsets.symmetric(horizontal: 16),
sliver: SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
mainAxisSpacing: 4,
crossAxisSpacing: 4,
childAspectRatio: 1,
),
delegate: SliverChildBuilderDelegate(
(context, i) => ShoppingItemWidget<RecipeItem>(
onPressed: (item) => cubit.toggleItem(recipe, item),
selected: state.selectedItems[recipe]!.contains(
recipe.mandatoryItems.elementAt(i),
),
item: recipe.mandatoryItems.elementAt(i),
),
childCount: recipe.mandatoryItems.length,
),
),
SliverItemGridList(
items: recipe.mandatoryItems,
onPressed: (RecipeItem item) => cubit.toggleItem(recipe, item),
selected: (item) => state.selectedItems[recipe]!.contains(item),
onLongPressed: (_) {},
),
if (recipe.optionalItems.isNotEmpty)
SliverPadding(
Expand All @@ -98,26 +74,11 @@ class _ItemSelectionPageState extends State<ItemSelectionPage> {
),
),
),
SliverPadding(
padding: const EdgeInsets.symmetric(horizontal: 16),
sliver: SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
mainAxisSpacing: 4,
crossAxisSpacing: 4,
childAspectRatio: 1,
),
delegate: SliverChildBuilderDelegate(
(context, i) => ShoppingItemWidget<RecipeItem>(
onPressed: (item) => cubit.toggleItem(recipe, item),
selected: state.selectedItems[recipe]!.contains(
recipe.optionalItems.elementAt(i),
),
item: recipe.optionalItems.elementAt(i),
),
childCount: recipe.optionalItems.length,
),
),
SliverItemGridList(
items: recipe.optionalItems,
onPressed: (RecipeItem item) => cubit.toggleItem(recipe, item),
selected: (item) => state.selectedItems[recipe]!.contains(item),
onLongPressed: (_) {},
),
const SliverPadding(
padding: EdgeInsets.fromLTRB(16, 16, 16, 0),
Expand Down
74 changes: 14 additions & 60 deletions lib/pages/recipe_add_update_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import 'package:kitchenowl/models/update_value.dart';
import 'package:kitchenowl/pages/item_page.dart';
import 'package:kitchenowl/pages/item_search_page.dart';
import 'package:kitchenowl/kitchenowl.dart';
import 'package:kitchenowl/widgets/shopping_item.dart';
import 'package:responsive_builder/responsive_builder.dart';

class AddUpdateRecipePage extends StatefulWidget {
Expand Down Expand Up @@ -64,12 +63,6 @@ class _AddUpdateRecipePageState extends State<AddUpdateRecipePage> {

@override
Widget build(BuildContext context) {
final int crossAxisCount = getValueForScreenType<int>(
context: context,
mobile: 3,
tablet: 6,
desktop: 9,
);
final bool mobileLayout = getValueForScreenType<bool>(
context: context,
mobile: true,
Expand Down Expand Up @@ -311,33 +304,13 @@ class _AddUpdateRecipePageState extends State<AddUpdateRecipePage> {
bloc: cubit,
buildWhen: (previous, current) =>
!listEquals(previous.items, current.items),
builder: (context, state) => (state.items
.where((e) => !e.optional)
.isNotEmpty)
? SliverGrid(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
mainAxisSpacing: 4,
crossAxisSpacing: 4,
childAspectRatio: 1,
),
delegate: SliverChildBuilderDelegate(
(context, i) => ShoppingItemWidget(
selected: true,
item: state.items
.where((e) => !e.optional)
.elementAt(i),
onPressed: (RecipeItem item) =>
cubit.removeItem(item),
onLongPressed: (RecipeItem item) =>
_editItem(context, item),
),
childCount:
state.items.where((e) => !e.optional).length,
),
)
: const SliverToBoxAdapter(child: SizedBox(height: 0)),
builder: (context, state) => SliverItemGridList(
items: state.items.where((e) => !e.optional).toList(),
selected: (item) => true,
onPressed: (RecipeItem item) => cubit.removeItem(item),
onLongPressed: (RecipeItem item) =>
_editItem(context, item),
),
),
),
SliverPadding(
Expand Down Expand Up @@ -366,32 +339,13 @@ class _AddUpdateRecipePageState extends State<AddUpdateRecipePage> {
bloc: cubit,
buildWhen: (previous, current) =>
!listEquals(previous.items, current.items),
builder: (context, state) => (state.items
.where((e) => e.optional)
.isNotEmpty)
? SliverGrid(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
mainAxisSpacing: 4,
crossAxisSpacing: 4,
childAspectRatio: 1,
),
delegate: SliverChildBuilderDelegate(
(context, i) => ShoppingItemWidget(
item: state.items
.where((e) => e.optional)
.elementAt(i),
onPressed: (RecipeItem item) =>
cubit.removeItem(item),
onLongPressed: (RecipeItem item) =>
_editItem(context, item),
),
childCount:
state.items.where((e) => e.optional).length,
),
)
: const SliverToBoxAdapter(child: SizedBox(height: 0)),
builder: (context, state) => SliverItemGridList(
items: state.items.where((e) => e.optional).toList(),
selected: (item) => true,
onPressed: (RecipeItem item) => cubit.removeItem(item),
onLongPressed: (RecipeItem item) =>
_editItem(context, item),
),
),
),
if (isUpdate)
Expand Down
71 changes: 12 additions & 59 deletions lib/pages/recipe_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import 'package:kitchenowl/pages/photo_view_page.dart';
import 'package:kitchenowl/pages/recipe_add_update_page.dart';
import 'package:kitchenowl/kitchenowl.dart';
import 'package:kitchenowl/widgets/recipe_source_chip.dart';
import 'package:kitchenowl/widgets/shopping_item.dart';
import 'package:responsive_builder/responsive_builder.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:url_launcher/url_launcher_string.dart';

Expand Down Expand Up @@ -50,13 +48,6 @@ class _RecipePageState extends State<RecipePage> {

@override
Widget build(BuildContext context) {
final int crossAxisCount = getValueForScreenType<int>(
context: context,
mobile: 3,
tablet: 6,
desktop: 9,
);

return WillPopScope(
onWillPop: () async {
Navigator.of(context).pop(cubit.state.updateState);
Expand Down Expand Up @@ -229,31 +220,12 @@ class _RecipePageState extends State<RecipePage> {
),
),
if (state.recipe.items.where((e) => !e.optional).isNotEmpty)
SliverPadding(
padding: const EdgeInsets.symmetric(horizontal: 16),
sliver: SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
mainAxisSpacing: 4,
crossAxisSpacing: 4,
childAspectRatio: 1,
),
delegate: SliverChildBuilderDelegate(
(context, i) => ShoppingItemWidget(
onPressed: cubit.itemSelected,
selected: state.selectedItems.contains(state
.recipe.items
.where((e) => !e.optional)
.elementAt(i)),
item: state.recipe.items
.where((e) => !e.optional)
.elementAt(i),
),
childCount: state.recipe.items
.where((e) => !e.optional)
.length,
),
),
SliverItemGridList(
items:
state.recipe.items.where((e) => !e.optional).toList(),
selected: (item) => state.selectedItems.contains(item),
onPressed: cubit.itemSelected,
onLongPressed: (_) {},
),
if (state.recipe.items.where((e) => e.optional).isNotEmpty)
SliverPadding(
Expand All @@ -266,31 +238,12 @@ class _RecipePageState extends State<RecipePage> {
),
),
if (state.recipe.items.where((e) => e.optional).isNotEmpty)
SliverPadding(
padding: const EdgeInsets.symmetric(horizontal: 16),
sliver: SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
mainAxisSpacing: 4,
crossAxisSpacing: 4,
childAspectRatio: 1,
),
delegate: SliverChildBuilderDelegate(
(context, i) => ShoppingItemWidget(
onPressed: cubit.itemSelected,
selected: state.selectedItems.contains(state
.recipe.items
.where((e) => e.optional)
.elementAt(i)),
item: state.recipe.items
.where((e) => e.optional)
.elementAt(i),
),
childCount: state.recipe.items
.where((e) => e.optional)
.length,
),
),
SliverItemGridList(
items:
state.recipe.items.where((e) => e.optional).toList(),
selected: (item) => state.selectedItems.contains(item),
onPressed: cubit.itemSelected,
onLongPressed: (_) {},
),
SliverPadding(
padding: const EdgeInsets.all(16),
Expand Down
16 changes: 10 additions & 6 deletions lib/widgets/sliver_item_grid_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import 'package:kitchenowl/pages/item_page.dart';
import 'package:kitchenowl/widgets/shopping_item.dart';
import 'package:responsive_builder/responsive_builder.dart';

class SliverItemGridList extends StatelessWidget {
class SliverItemGridList<T extends Item> extends StatelessWidget {
final void Function()? onRefresh;
final void Function(Item)? onPressed;
final void Function(Item)? onLongPressed;
final List<Item> items;
final void Function(T)? onPressed;
final void Function(T)? onLongPressed;
final List<T> items;
final List<Category>? categories; // forwared to item page on long press
final bool isList;
final bool Function(Item)? selected;
final bool Function(T)? selected;

const SliverItemGridList({
Key? key,
Expand All @@ -29,6 +29,10 @@ class SliverItemGridList extends StatelessWidget {

@override
Widget build(BuildContext context) {
if (items.isEmpty) {
return const SliverToBoxAdapter(child: SizedBox(height: 0));
}

final int crossAxisCount = getValueForScreenType<int>(
context: context,
mobile: 3,
Expand All @@ -38,7 +42,7 @@ class SliverItemGridList extends StatelessWidget {

final delegate = SliverChildBuilderDelegate(
childCount: items.length,
(context, i) => ShoppingItemWidget(
(context, i) => ShoppingItemWidget<T>(
key: ObjectKey(items[i]),
item: items[i],
selected: selected?.call(items[i]) ?? false,
Expand Down

0 comments on commit 0ecf910

Please sign in to comment.