Skip to content
This repository was archived by the owner on Sep 14, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor image handling
  • Loading branch information
Nikolas Rimikis committed Apr 3, 2023
commit 30d595bd29cc32b9e839a60e8960fa1f3c715451
11 changes: 11 additions & 0 deletions lib/src/models/image_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:flutter/foundation.dart';

class ImageResponse {
final Uint8List data;
final bool isSvg;

const ImageResponse({
required this.data,
required this.isSvg,
});
}
51 changes: 24 additions & 27 deletions lib/src/screens/category/category_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import 'package:nextcloud_cookbook_flutter/src/screens/recipe_create_screen.dart
import 'package:nextcloud_cookbook_flutter/src/screens/recipe_import_screen.dart';
import 'package:nextcloud_cookbook_flutter/src/screens/recipes_list_screen.dart';
import 'package:nextcloud_cookbook_flutter/src/screens/timer_screen.dart';
import 'package:nextcloud_cookbook_flutter/src/services/services.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/authentication_cached_network_image.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/authentication_cached_network_recipe_image.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/category_card.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/recipe_image.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/user_image.dart';
import 'package:search_page/search_page.dart';

class CategoryScreen extends StatefulWidget {
Expand Down Expand Up @@ -54,13 +53,8 @@ class _CategoryScreenState extends State<CategoryScreen> {
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
),
child: Center(
child: ClipOval(
child: AuthenticationCachedNetworkImage(
url: DataRepository().getUserAvatarUrl(),
boxFit: BoxFit.fill,
),
),
child: const Center(
child: UserImage(),
),
),
ListTile(
Expand Down Expand Up @@ -155,10 +149,9 @@ class _CategoryScreenState extends State<CategoryScreen> {
],
builder: (recipe) => ListTile(
title: Text(recipe.name),
trailing: AuthenticationCachedNetworkRecipeImage(
recipeId: recipe.recipeId,
full: false,
width: 50,
trailing: RecipeImage(
id: recipe.recipeId,
size: const Size.square(50),
),
onTap: () =>
Navigator.of(context).pushReplacement(
Expand Down Expand Up @@ -290,21 +283,25 @@ class _CategoryScreenState extends State<CategoryScreen> {
mainAxisSpacing: 10,
padding: const EdgeInsets.only(top: 10),
semanticChildCount: categories.length,
children: categories
.map(
(category) => GestureDetector(
child: CategoryCard(category, recipe?.first?.imageUrl),
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return RecipesListScreen(category: category.name);
},
),
children: [
for (int i = 0; i < categories.length; i++)
GestureDetector(
child: CategoryCard(
categories.elementAt(i),
recipe?.elementAt(i)?.recipeId,
),
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return RecipesListScreen(
category: categories.elementAt(i).name,
);
},
),
),
)
.toList(),
),
],
),
);
}
Expand Down
11 changes: 4 additions & 7 deletions lib/src/screens/recipe/recipe_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import 'package:nextcloud_cookbook_flutter/src/screens/recipe_edit_screen.dart';
import 'package:nextcloud_cookbook_flutter/src/services/services.dart';
import 'package:nextcloud_cookbook_flutter/src/util/setting_keys.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/animated_time_progress_bar.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/authentication_cached_network_recipe_image.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/duration_indicator.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/recipe_image.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:wakelock/wakelock.dart';

Expand Down Expand Up @@ -165,12 +165,9 @@ class RecipeScreenState extends State<RecipeScreen> {
width: double.infinity,
height: 200,
child: Center(
child: AuthenticationCachedNetworkRecipeImage(
recipeId: recipe.id!,
full: true,
width: double.infinity,
height: 200,
boxFit: BoxFit.cover,
child: RecipeImage(
id: recipe.id,
size: const Size(double.infinity, 200),
),
),
),
Expand Down
10 changes: 4 additions & 6 deletions lib/src/screens/recipes_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter_translate/flutter_translate.dart';
import 'package:nc_cookbook_api/nc_cookbook_api.dart';
import 'package:nextcloud_cookbook_flutter/src/blocs/recipes_short/recipes_short_bloc.dart';
import 'package:nextcloud_cookbook_flutter/src/screens/recipe/recipe_screen.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/authentication_cached_network_recipe_image.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/recipe_image.dart';

class RecipesListScreen extends StatefulWidget {
final String category;
Expand Down Expand Up @@ -95,11 +95,9 @@ class RecipesListScreenState extends State<RecipesListScreen> {
ListTile _buildRecipeStubScreen(RecipeStub recipe) {
return ListTile(
title: Text(recipe.name),
trailing: AuthenticationCachedNetworkRecipeImage(
recipeId: recipe.recipeId,
full: false,
width: 60,
height: 60,
trailing: RecipeImage(
id: recipe.recipeId,
size: const Size.square(60),
),
onTap: () {
Navigator.push(
Expand Down
10 changes: 4 additions & 6 deletions lib/src/screens/timer_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:nextcloud_cookbook_flutter/src/models/timer.dart';
import 'package:nextcloud_cookbook_flutter/src/screens/recipe/recipe_screen.dart';
import 'package:nextcloud_cookbook_flutter/src/services/services.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/animated_time_progress_bar.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/authentication_cached_network_recipe_image.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/recipe_image.dart';

class TimerScreen extends StatefulWidget {
const TimerScreen({super.key});
Expand Down Expand Up @@ -66,11 +66,9 @@ class _TimerScreen extends State<TimerScreen> {
ListTile _buildListItem(Timer timer) {
return ListTile(
key: UniqueKey(),
leading: AuthenticationCachedNetworkRecipeImage(
recipeId: timer.recipeId,
full: false,
width: 60,
height: 60,
leading: RecipeImage(
id: timer.recipeId,
size: const Size.square(60),
),
title: Text(timer.title),
subtitle: AnimatedTimeProgressBar(
Expand Down
35 changes: 29 additions & 6 deletions lib/src/services/data_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ class DataRepository {
Future<RecipeStub?> _fetchCategoryMainRecipe(Category category) async {
try {
final categoryRecipes = await fetchRecipesShort(category: category.name);
if (categoryRecipes != null) {
for (final category in categoryRecipes) {
if (category.imageUrl.isNotEmpty) {
return category;
}
}
if (categoryRecipes != null && categoryRecipes.isNotEmpty) {
return categoryRecipes.first;
}
} catch (e) {
log("Could not load main recipe of Category!");
Expand All @@ -117,4 +113,31 @@ class DataRepository {

return matches?.map((e) => e.name) ?? [];
}

Future<ImageResponse?> fetchImage(String recipeId, Size size) async {
final String sizeParam;
if (size.longestSide <= 16) {
sizeParam = "thumb16";
} else if (size.longestSide <= 250) {
sizeParam = "thumb";
} else {
sizeParam = "full";
}

final response = await api.recipeApi.getImage(
id: recipeId,
headers: {
"Accept": "image/jpeg, image/svg+xml",
},
size: sizeParam,
);
if (response.data != null) {
return ImageResponse(
data: response.data!,
isSvg: response.headers.value("content-type") == "image/svg+xml",
);
}

return null;
}
}
1 change: 1 addition & 0 deletions lib/src/services/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:nc_cookbook_api/nc_cookbook_api.dart';
import 'package:nextcloud_cookbook_flutter/src/models/app_authentication.dart';
import 'package:nextcloud_cookbook_flutter/src/models/image_response.dart';
import 'package:nextcloud_cookbook_flutter/src/models/timer.dart';
import 'package:nextcloud_cookbook_flutter/src/screens/recipe_import_screen.dart';
import 'package:nextcloud_cookbook_flutter/src/util/url_validator.dart';
Expand Down
37 changes: 0 additions & 37 deletions lib/src/util/custom_cache_manager.dart

This file was deleted.

49 changes: 0 additions & 49 deletions lib/src/widget/authentication_cached_network_image.dart

This file was deleted.

42 changes: 0 additions & 42 deletions lib/src/widget/authentication_cached_network_recipe_image.dart

This file was deleted.

Loading