103 lines
2.8 KiB
Dart
103 lines
2.8 KiB
Dart
import 'package:didvan/config/theme_data.dart';
|
|
import 'package:didvan/constants/app_icons.dart';
|
|
import 'package:didvan/models/view/action_sheet_data.dart'; // Ensure this is used or remove if not
|
|
import 'package:didvan/utils/action_sheet.dart';
|
|
import 'package:didvan/views/home/bookmarks/bookmark_service.dart';
|
|
import 'package:didvan/views/widgets/didvan/text.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class BookmarkedIcon extends StatefulWidget {
|
|
final int postId;
|
|
final void Function(bool isBookmarked)? onBookmarkChanged;
|
|
|
|
const BookmarkedIcon({
|
|
super.key,
|
|
required this.postId,
|
|
this.onBookmarkChanged,
|
|
});
|
|
|
|
@override
|
|
State<BookmarkedIcon> createState() => _BookmarkedIconState();
|
|
}
|
|
|
|
class _BookmarkedIconState extends State<BookmarkedIcon> {
|
|
bool _bookmarked = false;
|
|
bool _loading = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadStatus();
|
|
}
|
|
|
|
Future<void> _loadStatus() async {
|
|
// setState(() => _loading = true);
|
|
final bookmarks = await BookmarkService.fetchBookmarks();
|
|
if (mounted) {
|
|
setState(() {
|
|
_bookmarked = bookmarks.contains(widget.postId);
|
|
// _loading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> _toggleBookmark() async {
|
|
if (_loading) return;
|
|
|
|
if (_bookmarked) {
|
|
bool confirmAction = false;
|
|
|
|
await ActionSheetUtils(context).openDialog(
|
|
data: ActionSheetData(
|
|
content: const DidvanText(
|
|
'آیا میخواهید این محتوا از نشان شدهها حذف شود؟',
|
|
),
|
|
titleIcon: DidvanIcons.bookmark_regular,
|
|
titleColor: Theme.of(context).colorScheme.secondary,
|
|
title: 'تایید عملیات',
|
|
onConfirmed: () => confirmAction = true,
|
|
),
|
|
);
|
|
|
|
if (!confirmAction) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
setState(() => _loading = true);
|
|
try {
|
|
if (_bookmarked) {
|
|
await BookmarkService.removeBookmark(widget.postId);
|
|
} else {
|
|
await BookmarkService.addBookmark(widget.postId);
|
|
}
|
|
|
|
if (mounted) {
|
|
final newBookmarkStatus = !_bookmarked;
|
|
setState(() {
|
|
_bookmarked = newBookmarkStatus;
|
|
});
|
|
widget.onBookmarkChanged?.call(newBookmarkStatus);
|
|
}
|
|
} catch (e) {
|
|
print("Error toggling bookmark for post ${widget.postId}: $e");
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() => _loading = false);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return IconButton(
|
|
onPressed: _loading ? null : _toggleBookmark,
|
|
icon: Icon(
|
|
_bookmarked ? DidvanIcons.bookmark_solid : DidvanIcons.bookmark_regular,
|
|
color: _bookmarked
|
|
? const Color.fromARGB(255, 0, 126, 167)
|
|
: Theme.of(context).colorScheme.caption,
|
|
),
|
|
);
|
|
}
|
|
} |