parent
1e5f75efcc
commit
72aedb6c40
@ -0,0 +1,90 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
/// Represents a bookmark category for organizing recipes.
|
||||||
|
class BookmarkCategory {
|
||||||
|
/// Unique identifier for the category.
|
||||||
|
final String id;
|
||||||
|
|
||||||
|
/// Category name.
|
||||||
|
final String name;
|
||||||
|
|
||||||
|
/// Optional color for the category (as hex string, e.g., "#FF5733").
|
||||||
|
final String? color;
|
||||||
|
|
||||||
|
/// Timestamp when category was created (milliseconds since epoch).
|
||||||
|
final int createdAt;
|
||||||
|
|
||||||
|
/// Timestamp when category was last updated (milliseconds since epoch).
|
||||||
|
final int updatedAt;
|
||||||
|
|
||||||
|
BookmarkCategory({
|
||||||
|
required this.id,
|
||||||
|
required this.name,
|
||||||
|
this.color,
|
||||||
|
int? createdAt,
|
||||||
|
int? updatedAt,
|
||||||
|
}) : createdAt = createdAt ?? DateTime.now().millisecondsSinceEpoch,
|
||||||
|
updatedAt = updatedAt ?? DateTime.now().millisecondsSinceEpoch;
|
||||||
|
|
||||||
|
factory BookmarkCategory.fromMap(Map<String, dynamic> map) {
|
||||||
|
return BookmarkCategory(
|
||||||
|
id: map['id'] as String,
|
||||||
|
name: map['name'] as String,
|
||||||
|
color: map['color'] as String?,
|
||||||
|
createdAt: map['created_at'] as int,
|
||||||
|
updatedAt: map['updated_at'] as int,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toMap() {
|
||||||
|
return {
|
||||||
|
'id': id,
|
||||||
|
'name': name,
|
||||||
|
'color': color,
|
||||||
|
'created_at': createdAt,
|
||||||
|
'updated_at': updatedAt,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
factory BookmarkCategory.fromJson(Map<String, dynamic> json) {
|
||||||
|
return BookmarkCategory(
|
||||||
|
id: json['id'] as String,
|
||||||
|
name: json['name'] as String,
|
||||||
|
color: json['color'] as String?,
|
||||||
|
createdAt: json['createdAt'] as int? ?? DateTime.now().millisecondsSinceEpoch,
|
||||||
|
updatedAt: json['updatedAt'] as int? ?? DateTime.now().millisecondsSinceEpoch,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'id': id,
|
||||||
|
'name': name,
|
||||||
|
if (color != null) 'color': color,
|
||||||
|
'createdAt': createdAt,
|
||||||
|
'updatedAt': updatedAt,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
BookmarkCategory copyWith({
|
||||||
|
String? id,
|
||||||
|
String? name,
|
||||||
|
String? color,
|
||||||
|
int? createdAt,
|
||||||
|
int? updatedAt,
|
||||||
|
}) {
|
||||||
|
return BookmarkCategory(
|
||||||
|
id: id ?? this.id,
|
||||||
|
name: name ?? this.name,
|
||||||
|
color: color ?? this.color,
|
||||||
|
createdAt: createdAt ?? this.createdAt,
|
||||||
|
updatedAt: updatedAt ?? this.updatedAt,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return 'BookmarkCategory(id: $id, name: $name)';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,406 @@
|
|||||||
|
import 'dart:typed_data';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import '../../core/service_locator.dart';
|
||||||
|
import '../../core/logger.dart';
|
||||||
|
import '../../data/recipes/recipe_service.dart';
|
||||||
|
import '../../data/recipes/models/bookmark_category_model.dart';
|
||||||
|
import '../../data/recipes/models/recipe_model.dart';
|
||||||
|
import '../add_recipe/add_recipe_screen.dart';
|
||||||
|
import '../photo_gallery/photo_gallery_screen.dart';
|
||||||
|
import '../navigation/main_navigation_scaffold.dart';
|
||||||
|
|
||||||
|
/// Bookmarks screen displaying all bookmark categories and their recipes.
|
||||||
|
class BookmarksScreen extends StatefulWidget {
|
||||||
|
const BookmarksScreen({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<BookmarksScreen> createState() => _BookmarksScreenState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _BookmarksScreenState extends State<BookmarksScreen> {
|
||||||
|
List<BookmarkCategory> _categories = [];
|
||||||
|
Map<String, List<RecipeModel>> _recipesByCategory = {};
|
||||||
|
bool _isLoading = false;
|
||||||
|
String? _errorMessage;
|
||||||
|
RecipeService? _recipeService;
|
||||||
|
bool _wasLoggedIn = false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_checkLoginState();
|
||||||
|
_initializeService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didChangeDependencies() {
|
||||||
|
super.didChangeDependencies();
|
||||||
|
_checkLoginState();
|
||||||
|
if (_recipeService != null && _wasLoggedIn) {
|
||||||
|
_loadBookmarks();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didUpdateWidget(BookmarksScreen oldWidget) {
|
||||||
|
super.didUpdateWidget(oldWidget);
|
||||||
|
_checkLoginState();
|
||||||
|
if (_recipeService != null && _wasLoggedIn) {
|
||||||
|
_loadBookmarks();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _checkLoginState() {
|
||||||
|
final sessionService = ServiceLocator.instance.sessionService;
|
||||||
|
final isLoggedIn = sessionService?.isLoggedIn ?? false;
|
||||||
|
|
||||||
|
if (isLoggedIn != _wasLoggedIn) {
|
||||||
|
_wasLoggedIn = isLoggedIn;
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {});
|
||||||
|
if (isLoggedIn && _recipeService != null) {
|
||||||
|
_loadBookmarks();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _initializeService() async {
|
||||||
|
try {
|
||||||
|
_recipeService = ServiceLocator.instance.recipeService;
|
||||||
|
if (_recipeService == null) {
|
||||||
|
throw Exception('RecipeService not available in ServiceLocator');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
Logger.error('Failed to initialize RecipeService', e);
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {
|
||||||
|
_errorMessage = 'Failed to initialize recipe service: $e';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _loadBookmarks() async {
|
||||||
|
if (_recipeService == null) return;
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_isLoading = true;
|
||||||
|
_errorMessage = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
final categories = await _recipeService!.getAllBookmarkCategories();
|
||||||
|
final recipesMap = <String, List<RecipeModel>>{};
|
||||||
|
|
||||||
|
for (final category in categories) {
|
||||||
|
final recipes = await _recipeService!.getRecipesByCategory(category.id);
|
||||||
|
recipesMap[category.id] = recipes;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {
|
||||||
|
_categories = categories;
|
||||||
|
_recipesByCategory = recipesMap;
|
||||||
|
_isLoading = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
Logger.error('Failed to load bookmarks', e);
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {
|
||||||
|
_isLoading = false;
|
||||||
|
_errorMessage = 'Failed to load bookmarks: $e';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Color _parseColor(String hexColor) {
|
||||||
|
try {
|
||||||
|
return Color(int.parse(hexColor.replaceAll('#', '0xFF')));
|
||||||
|
} catch (e) {
|
||||||
|
return Colors.blue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _viewRecipe(RecipeModel recipe) {
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => AddRecipeScreen(
|
||||||
|
recipe: recipe,
|
||||||
|
viewMode: true,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
).then((_) {
|
||||||
|
// Reload bookmarks when returning from recipe view
|
||||||
|
_loadBookmarks();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _openPhotoGallery(List<String> imageUrls, int initialIndex) {
|
||||||
|
if (imageUrls.isEmpty) return;
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => PhotoGalleryScreen(
|
||||||
|
imageUrls: imageUrls,
|
||||||
|
initialIndex: initialIndex,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final isLoggedIn = ServiceLocator.instance.sessionService?.isLoggedIn ?? false;
|
||||||
|
|
||||||
|
if (!isLoggedIn) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: const Text('Bookmarks')),
|
||||||
|
body: const Center(
|
||||||
|
child: Text('Please log in to view bookmarks'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_isLoading) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: const Text('Bookmarks')),
|
||||||
|
body: const Center(child: CircularProgressIndicator()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_errorMessage != null) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: const Text('Bookmarks')),
|
||||||
|
body: Center(
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Text(_errorMessage!),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: _loadBookmarks,
|
||||||
|
child: const Text('Retry'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_categories.isEmpty) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: const Text('Bookmarks')),
|
||||||
|
body: const Center(
|
||||||
|
child: Text(
|
||||||
|
'No bookmark categories yet.\nBookmark a recipe to get started!',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: const Text('Bookmarks'),
|
||||||
|
actions: [
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.person),
|
||||||
|
tooltip: 'User',
|
||||||
|
onPressed: () {
|
||||||
|
final scaffold = context.findAncestorStateOfType<MainNavigationScaffoldState>();
|
||||||
|
scaffold?.navigateToUser();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
body: RefreshIndicator(
|
||||||
|
onRefresh: _loadBookmarks,
|
||||||
|
child: ListView.builder(
|
||||||
|
padding: const EdgeInsets.all(16),
|
||||||
|
itemCount: _categories.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final category = _categories[index];
|
||||||
|
final recipes = _recipesByCategory[category.id] ?? [];
|
||||||
|
|
||||||
|
return Card(
|
||||||
|
margin: const EdgeInsets.only(bottom: 16),
|
||||||
|
child: ExpansionTile(
|
||||||
|
leading: category.color != null
|
||||||
|
? Container(
|
||||||
|
width: 32,
|
||||||
|
height: 32,
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: _parseColor(category.color!),
|
||||||
|
shape: BoxShape.circle,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: const Icon(Icons.bookmark),
|
||||||
|
title: Text(
|
||||||
|
category.name,
|
||||||
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
subtitle: Text('${recipes.length} recipe(s)'),
|
||||||
|
children: recipes.isEmpty
|
||||||
|
? [
|
||||||
|
const ListTile(
|
||||||
|
title: Text('No recipes in this category'),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
: recipes.map((recipe) {
|
||||||
|
return _BookmarkRecipeItem(
|
||||||
|
recipe: recipe,
|
||||||
|
onTap: () => _viewRecipe(recipe),
|
||||||
|
onPhotoTap: (index) => _openPhotoGallery(
|
||||||
|
recipe.imageUrls,
|
||||||
|
index,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Widget for displaying a recipe item in a bookmark category.
|
||||||
|
class _BookmarkRecipeItem extends StatelessWidget {
|
||||||
|
final RecipeModel recipe;
|
||||||
|
final VoidCallback onTap;
|
||||||
|
final ValueChanged<int> onPhotoTap;
|
||||||
|
|
||||||
|
const _BookmarkRecipeItem({
|
||||||
|
required this.recipe,
|
||||||
|
required this.onTap,
|
||||||
|
required this.onPhotoTap,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Builds an image widget using ImmichService for authenticated access.
|
||||||
|
Widget _buildRecipeImage(String imageUrl) {
|
||||||
|
final immichService = ServiceLocator.instance.immichService;
|
||||||
|
|
||||||
|
final assetIdMatch = RegExp(r'/api/assets/([^/]+)/').firstMatch(imageUrl);
|
||||||
|
|
||||||
|
if (assetIdMatch != null && immichService != null) {
|
||||||
|
final assetId = assetIdMatch.group(1);
|
||||||
|
if (assetId != null) {
|
||||||
|
return FutureBuilder<Uint8List?>(
|
||||||
|
future: immichService.fetchImageBytes(assetId, isThumbnail: true),
|
||||||
|
builder: (context, snapshot) {
|
||||||
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||||
|
return Container(
|
||||||
|
color: Colors.grey.shade200,
|
||||||
|
child: const Center(
|
||||||
|
child: CircularProgressIndicator(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (snapshot.hasError || !snapshot.hasData || snapshot.data == null) {
|
||||||
|
return Container(
|
||||||
|
color: Colors.grey.shade200,
|
||||||
|
child: const Icon(Icons.broken_image, size: 32),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Image.memory(
|
||||||
|
snapshot.data!,
|
||||||
|
fit: BoxFit.cover,
|
||||||
|
width: double.infinity,
|
||||||
|
height: double.infinity,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Image.network(
|
||||||
|
imageUrl,
|
||||||
|
fit: BoxFit.cover,
|
||||||
|
width: double.infinity,
|
||||||
|
height: double.infinity,
|
||||||
|
errorBuilder: (context, error, stackTrace) {
|
||||||
|
return Container(
|
||||||
|
color: Colors.grey.shade200,
|
||||||
|
child: const Icon(Icons.broken_image, size: 32),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
loadingBuilder: (context, child, loadingProgress) {
|
||||||
|
if (loadingProgress == null) return child;
|
||||||
|
return Container(
|
||||||
|
color: Colors.grey.shade200,
|
||||||
|
child: Center(
|
||||||
|
child: CircularProgressIndicator(
|
||||||
|
value: loadingProgress.expectedTotalBytes != null
|
||||||
|
? loadingProgress.cumulativeBytesLoaded /
|
||||||
|
loadingProgress.expectedTotalBytes!
|
||||||
|
: null,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return ListTile(
|
||||||
|
leading: recipe.imageUrls.isNotEmpty
|
||||||
|
? GestureDetector(
|
||||||
|
onTap: () => onPhotoTap(0),
|
||||||
|
child: ClipRRect(
|
||||||
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
child: SizedBox(
|
||||||
|
width: 60,
|
||||||
|
height: 60,
|
||||||
|
child: _buildRecipeImage(recipe.imageUrls.first),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: Container(
|
||||||
|
width: 60,
|
||||||
|
height: 60,
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.grey[300],
|
||||||
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
),
|
||||||
|
child: const Icon(Icons.restaurant_menu),
|
||||||
|
),
|
||||||
|
title: Text(
|
||||||
|
recipe.title,
|
||||||
|
style: const TextStyle(fontWeight: FontWeight.w500),
|
||||||
|
),
|
||||||
|
subtitle: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
if (recipe.description != null && recipe.description!.isNotEmpty)
|
||||||
|
Text(
|
||||||
|
recipe.description!,
|
||||||
|
maxLines: 2,
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
const Icon(Icons.star, size: 16, color: Colors.amber),
|
||||||
|
const SizedBox(width: 4),
|
||||||
|
Text(recipe.rating.toString()),
|
||||||
|
if (recipe.isFavourite) ...[
|
||||||
|
const SizedBox(width: 12),
|
||||||
|
const Icon(Icons.favorite, size: 16, color: Colors.red),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
onTap: onTap,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,227 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import '../../core/service_locator.dart';
|
||||||
|
import '../../core/logger.dart';
|
||||||
|
import '../../data/recipes/recipe_service.dart';
|
||||||
|
import '../../data/recipes/models/bookmark_category_model.dart';
|
||||||
|
|
||||||
|
/// Dialog for selecting or creating a bookmark category.
|
||||||
|
class BookmarkDialog extends StatefulWidget {
|
||||||
|
final String recipeId;
|
||||||
|
final List<BookmarkCategory> currentCategories;
|
||||||
|
|
||||||
|
const BookmarkDialog({
|
||||||
|
super.key,
|
||||||
|
required this.recipeId,
|
||||||
|
required this.currentCategories,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<BookmarkDialog> createState() => _BookmarkDialogState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _BookmarkDialogState extends State<BookmarkDialog> {
|
||||||
|
final TextEditingController _categoryNameController = TextEditingController();
|
||||||
|
List<BookmarkCategory> _allCategories = [];
|
||||||
|
bool _isLoading = false;
|
||||||
|
bool _isCreating = false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_loadCategories();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_categoryNameController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _loadCategories() async {
|
||||||
|
setState(() => _isLoading = true);
|
||||||
|
try {
|
||||||
|
final recipeService = ServiceLocator.instance.recipeService;
|
||||||
|
if (recipeService != null) {
|
||||||
|
final categories = await recipeService.getAllBookmarkCategories();
|
||||||
|
setState(() {
|
||||||
|
_allCategories = categories;
|
||||||
|
_isLoading = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
Logger.error('Failed to load bookmark categories', e);
|
||||||
|
setState(() => _isLoading = false);
|
||||||
|
if (mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('Failed to load categories: $e')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _createCategory() async {
|
||||||
|
final name = _categoryNameController.text.trim();
|
||||||
|
if (name.isEmpty) {
|
||||||
|
if (mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(content: Text('Category name cannot be empty')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setState(() => _isCreating = true);
|
||||||
|
try {
|
||||||
|
final recipeService = ServiceLocator.instance.recipeService;
|
||||||
|
if (recipeService != null) {
|
||||||
|
final category = await recipeService.createBookmarkCategory(name: name);
|
||||||
|
await recipeService.addRecipeToCategory(
|
||||||
|
recipeId: widget.recipeId,
|
||||||
|
categoryId: category.id,
|
||||||
|
);
|
||||||
|
if (mounted) {
|
||||||
|
Navigator.of(context).pop(category);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
Logger.error('Failed to create bookmark category', e);
|
||||||
|
setState(() => _isCreating = false);
|
||||||
|
if (mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('Failed to create category: $e')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _selectCategory(BookmarkCategory category) async {
|
||||||
|
try {
|
||||||
|
final recipeService = ServiceLocator.instance.recipeService;
|
||||||
|
if (recipeService != null) {
|
||||||
|
final isInCategory = widget.currentCategories
|
||||||
|
.any((c) => c.id == category.id);
|
||||||
|
|
||||||
|
if (isInCategory) {
|
||||||
|
// Remove from category
|
||||||
|
await recipeService.removeRecipeFromCategory(
|
||||||
|
recipeId: widget.recipeId,
|
||||||
|
categoryId: category.id,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Add to category
|
||||||
|
await recipeService.addRecipeToCategory(
|
||||||
|
recipeId: widget.recipeId,
|
||||||
|
categoryId: category.id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (mounted) {
|
||||||
|
Navigator.of(context).pop(category);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
Logger.error('Failed to update bookmark', e);
|
||||||
|
if (mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('Failed to update bookmark: $e')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Color _parseColor(String hexColor) {
|
||||||
|
try {
|
||||||
|
return Color(int.parse(hexColor.replaceAll('#', '0xFF')));
|
||||||
|
} catch (e) {
|
||||||
|
return Colors.blue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: const Text('Bookmark Recipe'),
|
||||||
|
content: SizedBox(
|
||||||
|
width: double.maxFinite,
|
||||||
|
child: _isLoading
|
||||||
|
? const Center(child: CircularProgressIndicator())
|
||||||
|
: SingleChildScrollView(
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
// Existing categories
|
||||||
|
if (_allCategories.isNotEmpty) ...[
|
||||||
|
const Text(
|
||||||
|
'Add to category:',
|
||||||
|
style: TextStyle(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
ConstrainedBox(
|
||||||
|
constraints: const BoxConstraints(maxHeight: 200),
|
||||||
|
child: ListView.builder(
|
||||||
|
shrinkWrap: true,
|
||||||
|
itemCount: _allCategories.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final category = _allCategories[index];
|
||||||
|
final isSelected = widget.currentCategories
|
||||||
|
.any((c) => c.id == category.id);
|
||||||
|
return CheckboxListTile(
|
||||||
|
title: Text(category.name),
|
||||||
|
value: isSelected,
|
||||||
|
onChanged: (_) => _selectCategory(category),
|
||||||
|
secondary: category.color != null
|
||||||
|
? Container(
|
||||||
|
width: 24,
|
||||||
|
height: 24,
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: _parseColor(category.color!),
|
||||||
|
shape: BoxShape.circle,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: const Icon(Icons.bookmark),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
],
|
||||||
|
// Create new category
|
||||||
|
const Text(
|
||||||
|
'Or create new category:',
|
||||||
|
style: TextStyle(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
TextField(
|
||||||
|
controller: _categoryNameController,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
hintText: 'Category name',
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
enabled: !_isCreating,
|
||||||
|
onSubmitted: (_) => _createCategory(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.of(context).pop(),
|
||||||
|
child: const Text('Cancel'),
|
||||||
|
),
|
||||||
|
if (!_isLoading)
|
||||||
|
TextButton(
|
||||||
|
onPressed: _isCreating ? null : _createCategory,
|
||||||
|
child: _isCreating
|
||||||
|
? const SizedBox(
|
||||||
|
width: 20,
|
||||||
|
height: 20,
|
||||||
|
child: CircularProgressIndicator(strokeWidth: 2),
|
||||||
|
)
|
||||||
|
: const Text('Create & Add'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in new issue