Skip to content

Commit

Permalink
feat: Improve recipe search (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch authored Dec 9, 2022
1 parent 7833ed7 commit 007a533
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
19 changes: 13 additions & 6 deletions lib/pages/home_page/recipe_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ class _RecipeListPageState extends State<RecipeListPage> {
Widget build(BuildContext context) {
final cubit = BlocProvider.of<RecipeListCubit>(context);

final bool useBottomNavigationBar = getValueForScreenType<bool>(
final int hintOffset = getValueForScreenType<int>(
context: context,
mobile: true,
tablet: false,
desktop: false,
mobile: 0,
tablet: 80,
desktop: 256,
);

return SafeArea(
Expand Down Expand Up @@ -213,8 +213,15 @@ class _RecipeListPageState extends State<RecipeListPage> {
color: Theme.of(context).colorScheme.onPrimary,
),
indexHintAlignment: Alignment.centerLeft,
indexHintOffset:
Offset(useBottomNavigationBar ? 0 : 216, 0),
indexHintOffset: Offset(
((MediaQuery.of(context).size.width -
hintOffset -
1600) /
2)
.clamp(0, double.infinity) +
hintOffset.toDouble(),
0,
),
),
hapticFeedback: true,
itemBuilder: (context, i) {
Expand Down
8 changes: 3 additions & 5 deletions lib/services/api/recipe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ extension RecipeApi on ApiService {
return body.map((e) => Recipe.fromJson(e)).toList();
}

Future<List<Recipe>?> searchRecipe(String query) async {
final res = await get('$baseRoute/search?query=$query');
Future<List<int>?> searchRecipe(String query) async {
final res = await get('$baseRoute/search?only_ids=true&query=$query');
if (res.statusCode != 200) return null;

final body = List.from(jsonDecode(res.body));

return body.map((e) => Recipe.fromJson(e)).toList();
return List.from(jsonDecode(res.body));
}

Future<Recipe?> getRecipe(Recipe recipe) async {
Expand Down
7 changes: 6 additions & 1 deletion lib/services/transactions/recipe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class TransactionRecipeSearchRecipes extends Transaction<List<Recipe>> {

@override
Future<List<Recipe>?> runOnline() async {
return await ApiService.getInstance().searchRecipe(query);
final ids = await ApiService.getInstance().searchRecipe(query);
if (ids == null) return [];
final recipes = (await TempStorage.getInstance().readRecipes() ?? [])
..retainWhere((e) => ids.contains(e.id));

return recipes;
}
}

0 comments on commit 007a533

Please sign in to comment.