Skip to content

Commit

Permalink
Select item sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch committed Aug 4, 2021
1 parent d784330 commit 6d9e4dc
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 11 deletions.
73 changes: 68 additions & 5 deletions lib/cubits/shoppinglist_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:kitchenowl/models/item.dart';
import 'package:kitchenowl/services/transactions/shoppinglist.dart';
import 'package:kitchenowl/services/transaction_handler.dart';

enum ShoppinglistSorting { alphabetical, algorithmic }

class ShoppinglistCubit extends Cubit<ShoppinglistCubitState> {
String get query => (state is SearchShoppinglistCubitState)
? (state as SearchShoppinglistCubitState).query
Expand All @@ -27,13 +29,28 @@ class ShoppinglistCubit extends Cubit<ShoppinglistCubitState> {
await refresh();
}

void incrementSorting() {
setSorting(ShoppinglistSorting
.values[(state.sorting.index + 1) % ShoppinglistSorting.values.length]);
}

void setSorting(ShoppinglistSorting sorting) {
if (!(state is SearchShoppinglistCubitState))
_sortShoppinglistItems(state.listItems, sorting);
emit(state.copyWith(sorting: sorting));
}

Future<void> refresh([String query]) async {
// Get required information
final state = this.state;
if (state is SearchShoppinglistCubitState) query = query ?? state.query;
final sorting = state.sorting;
List<ShoppinglistItem> shoppinglist = await TransactionHandler.getInstance()
.runTransaction(TransactionShoppingListGetItems()) ??
const [];

if (query != null && query.isNotEmpty) {
// Split query into name and description
final splitIndex = query.indexOf(',');
String queryName = query;
String queryDescription = '';
Expand Down Expand Up @@ -64,9 +81,13 @@ class ShoppinglistCubit extends Cubit<ShoppinglistCubitState> {
listItems: shoppinglist,
));
} else {
// Sort if needed
if (sorting != ShoppinglistSorting.alphabetical)
_sortShoppinglistItems(shoppinglist, sorting);

final recent = await TransactionHandler.getInstance()
.runTransaction(TransactionShoppingListGetRecentItems());
emit(ShoppinglistCubitState(shoppinglist, recent));
emit(ShoppinglistCubitState(shoppinglist, recent, sorting));
}
}

Expand All @@ -82,17 +103,44 @@ class ShoppinglistCubit extends Cubit<ShoppinglistCubitState> {
}
}
}

void _sortShoppinglistItems(
List<ShoppinglistItem> shoppinglist, ShoppinglistSorting sorting) {
switch (sorting) {
case ShoppinglistSorting.alphabetical:
shoppinglist.sort((a, b) => a.name.compareTo(b.name));
break;
case ShoppinglistSorting.algorithmic:
shoppinglist.sort((a, b) => a.ordering.compareTo(b.ordering));
break;
}
}
}

class ShoppinglistCubitState extends Equatable {
final List<ShoppinglistItem> listItems;
final List<Item> recentItems;
final ShoppinglistSorting sorting;

const ShoppinglistCubitState(
[this.listItems = const [], this.recentItems = const []]);
const ShoppinglistCubitState([
this.listItems = const [],
this.recentItems = const [],
this.sorting = ShoppinglistSorting.alphabetical,
]);

ShoppinglistCubitState copyWith({
List<ShoppinglistItem> listItems,
List<Item> recentItems,
ShoppinglistSorting sorting,
}) =>
ShoppinglistCubitState(
listItems ?? this.listItems,
recentItems ?? this.recentItems,
sorting ?? this.sorting,
);

@override
List<Object> get props => listItems.cast<Object>();
List<Object> get props => listItems.cast<Object>() + recentItems + [sorting];
}

class SearchShoppinglistCubitState extends ShoppinglistCubitState {
Expand All @@ -102,9 +150,24 @@ class SearchShoppinglistCubitState extends ShoppinglistCubitState {
const SearchShoppinglistCubitState({
List<ShoppinglistItem> listItems = const [],
List<ShoppinglistItem> recentItems = const [],
ShoppinglistSorting sorting = ShoppinglistSorting.alphabetical,
this.query = "",
this.result = const [],
}) : super(listItems, recentItems);
}) : super(listItems, recentItems, sorting);

@override
ShoppinglistCubitState copyWith({
List<ShoppinglistItem> listItems,
List<Item> recentItems,
ShoppinglistSorting sorting,
}) =>
SearchShoppinglistCubitState(
listItems: listItems ?? this.listItems,
recentItems: recentItems ?? this.recentItems,
sorting: sorting ?? this.sorting,
query: this.query,
result: this.result,
);

@override
List<Object> get props => super.props + result + <Object>[query];
Expand Down
2 changes: 2 additions & 0 deletions lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"shoppingList": "Einkaufsliste",
"shoppingLists": "Einkaufslisten",
"start": "Start",
"sortingAlphabetical": "Alphabetisch",
"sortingAlgorithmic": "Algorithmisch",
"swipeToDeleteUser": "Wische zum löschen",
"underConstruction": "Baustelle",
"unreachableMessage": "Hmmmm... der Server wurde nicht erreicht",
Expand Down
2 changes: 2 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"shoppingList": "Shopping list",
"shoppingLists": "Shopping lists",
"start": "Start",
"sortingAlphabetical": "Alphabetical",
"sortingAlgorithmic": "Algorithmic",
"swipeToDeleteUser": "Swipe to delete user",
"underConstruction": "Under Construction",
"unreachableMessage": "Hmmmm... coulnd't reach server",
Expand Down
16 changes: 10 additions & 6 deletions lib/models/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class Item extends Model {
class ItemWithDescription extends Item {
final String description;

const ItemWithDescription({int id, String name, this.description})
: super(id: id, name: name);
const ItemWithDescription(
{int id, String name, int ordering, this.description})
: super(id: id, name: name, ordering: ordering);

factory ItemWithDescription.fromJson(Map<String, dynamic> map) =>
ItemWithDescription(
Expand Down Expand Up @@ -73,14 +74,16 @@ class ItemWithDescription extends Item {
}

class ShoppinglistItem extends ItemWithDescription {
const ShoppinglistItem({int id, String name, String description})
: super(id: id, name: name, description: description);
const ShoppinglistItem(
{int id, String name, String description, int ordering})
: super(id: id, name: name, description: description, ordering: ordering);

factory ShoppinglistItem.fromJson(Map<String, dynamic> map) =>
ShoppinglistItem(
id: map['id'],
name: map['name'],
description: map['description'],
ordering: map['ordering'],
);

factory ShoppinglistItem.fromItem({
Expand Down Expand Up @@ -113,8 +116,9 @@ class ShoppinglistItem extends ItemWithDescription {
class RecipeItem extends ItemWithDescription {
final bool optional;

const RecipeItem({int id, String name, String description, this.optional})
: super(id: id, name: name, description: description);
const RecipeItem(
{int id, String name, String description, int ordering, this.optional})
: super(id: id, name: name, description: description, ordering: ordering);

factory RecipeItem.fromJson(Map<String, dynamic> map) => RecipeItem(
id: map['id'],
Expand Down
28 changes: 28 additions & 0 deletions lib/pages/home_page/shoppinglist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,34 @@ class _ShoppinglistPageState extends State<ShoppinglistPage> {
);
return CustomScrollView(
slivers: [
SliverPadding(
padding: const EdgeInsets.symmetric(horizontal: 16),
sliver: SliverToBoxAdapter(
child: Align(
alignment: Alignment.centerRight,
child: TextButton(
onPressed: cubit.incrementSorting,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(state.sorting ==
ShoppinglistSorting.alphabetical
? AppLocalizations.of(context)
.sortingAlphabetical
: AppLocalizations.of(context)
.sortingAlgorithmic),
const SizedBox(width: 4),
Icon(Icons.sort),
],
),
),
),
),
),
),
SliverPadding(
padding: const EdgeInsets.symmetric(horizontal: 16),
sliver: SliverGrid(
Expand Down

0 comments on commit 6d9e4dc

Please sign in to comment.