|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 3 | +import '../models/post_model.dart'; |
| 4 | +import '../controllers/feed_controller.dart'; |
| 5 | + |
| 6 | +void showEditPostDialog(BuildContext context, WidgetRef ref, PostModel post) { |
| 7 | + final controller = TextEditingController(text: post.caption); |
| 8 | + showDialog( |
| 9 | + context: context, |
| 10 | + builder: (ctx) => AlertDialog( |
| 11 | + title: const Text('Edit Post'), |
| 12 | + content: TextField( |
| 13 | + controller: controller, |
| 14 | + decoration: const InputDecoration(hintText: 'Enter new caption...'), |
| 15 | + maxLines: 3, |
| 16 | + ), |
| 17 | + actions: [ |
| 18 | + TextButton( |
| 19 | + onPressed: () => Navigator.pop(ctx), |
| 20 | + child: const Text('Cancel'), |
| 21 | + ), |
| 22 | + FilledButton( |
| 23 | + onPressed: () async { |
| 24 | + final success = await ref |
| 25 | + .read(feedProvider.notifier) |
| 26 | + .updatePost(postId: post.id, caption: controller.text); |
| 27 | + if (ctx.mounted) { |
| 28 | + Navigator.pop(ctx); |
| 29 | + if (success) { |
| 30 | + ScaffoldMessenger.of(context).showSnackBar( |
| 31 | + const SnackBar(content: Text('Post updated!')), |
| 32 | + ); |
| 33 | + } |
| 34 | + } |
| 35 | + }, |
| 36 | + child: const Text('Save'), |
| 37 | + ), |
| 38 | + ], |
| 39 | + ), |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +void showDeletePostDialog(BuildContext context, WidgetRef ref, PostModel post, {VoidCallback? onDeleteSuccess}) { |
| 44 | + showDialog( |
| 45 | + context: context, |
| 46 | + builder: (ctx) => AlertDialog( |
| 47 | + title: const Text('Delete Post'), |
| 48 | + content: const Text('Are you sure you want to delete this post? This action cannot be undone.'), |
| 49 | + actions: [ |
| 50 | + TextButton( |
| 51 | + onPressed: () => Navigator.pop(ctx), |
| 52 | + child: const Text('Cancel'), |
| 53 | + ), |
| 54 | + FilledButton( |
| 55 | + onPressed: () async { |
| 56 | + final success = await ref |
| 57 | + .read(feedProvider.notifier) |
| 58 | + .deletePost(post.id); |
| 59 | + if (ctx.mounted) { |
| 60 | + Navigator.pop(ctx); |
| 61 | + if (success) { |
| 62 | + ScaffoldMessenger.of(context).showSnackBar( |
| 63 | + const SnackBar(content: Text('Post deleted.')), |
| 64 | + ); |
| 65 | + onDeleteSuccess?.call(); |
| 66 | + } |
| 67 | + } |
| 68 | + }, |
| 69 | + style: FilledButton.styleFrom( |
| 70 | + backgroundColor: Theme.of(context).colorScheme.error, |
| 71 | + foregroundColor: Theme.of(context).colorScheme.onError, |
| 72 | + ), |
| 73 | + child: const Text('Delete'), |
| 74 | + ), |
| 75 | + ], |
| 76 | + ), |
| 77 | + ); |
| 78 | +} |
0 commit comments