Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bookmark icon functional in user profile #468

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../../core/presentation/theme/app_colors.dart';
import '../../../../article/domain/entities/article.dart';
import '../../../../article/presentation/bloc/bookmark_bloc.dart';

class Bookmark extends StatelessWidget {
final bool isBookmarked;
final Article article;

const Bookmark({
super.key,
required this.iconSize,
required this.isBookmarked,
required this.article,
});

final double iconSize;

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => _changeBookmarkStatus(context),
child: Icon(
isBookmarked ? Icons.bookmark_outlined : Icons.bookmark_border,
color: AppColors.blue,
size: iconSize,
),
);
}

void _changeBookmarkStatus(BuildContext context) {
if (isBookmarked) {
context.read<BookmarkBloc>().add(RemoveFromBookmarkEvent(article.id));
} else {
context.read<BookmarkBloc>().add(AddToBookmarkEvent(article));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import 'package:go_router/go_router.dart';
import '../../../../../core/presentation/router/routes.dart';
import '../../../../../core/presentation/theme/app_colors.dart';
import '../../../../../core/utils/time_calculator.dart';
import '../../../../../injection_container.dart';
import '../../../../article/domain/entities/article.dart';

import '../../../../article/presentation/bloc/bookmark_bloc.dart';
import '../../../../article/presentation/widgets/snackbar.dart';
import '../../../../authentication/presentation/bloc/auth_bloc.dart';
import '../../bloc/user_bloc.dart';
import 'bookmark.dart';

class SingleArticlePostGridView extends StatelessWidget {
final Article article;
Expand All @@ -21,6 +24,10 @@ class SingleArticlePostGridView extends StatelessWidget {
required this.article,
}) : super(key: key);

bool _isBookmarked(List<Article> bookmarks) {
return bookmarks.any((element) => element.id == article.id);
}

@override
Widget build(BuildContext context) {
double imageWidth = double.infinity;
Expand All @@ -39,104 +46,140 @@ class SingleArticlePostGridView extends StatelessWidget {
final timeSincePosted =
timePassedFormatter(timePassedCalculator(article.date));

return GestureDetector(
onTap: () async {
final userBloc = context.read<UserBloc>();
final authBloc = context.read<AuthBloc>();
final bookmarkBloc = context.read<BookmarkBloc>();
await context.push(Routes.articleDetail, extra: article);
userBloc.add(GetUserEvent(token: authBloc.authToken));
bookmarkBloc.add(LoadBookmarksEvent());
},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
return MultiBlocProvider(
providers: [
BlocProvider(
create: (_) =>
serviceLocator<BookmarkBloc>()..add(LoadBookmarksEvent()),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: imageWidth,
height: imageHeight,
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: CachedNetworkImage(
imageUrl: imageUrl,
fit: BoxFit.cover,
],
child: GestureDetector(
onTap: () async {
final userBloc = context.read<UserBloc>();
final authBloc = context.read<AuthBloc>();
final bookmarkBloc = context.read<BookmarkBloc>();
await context.push(Routes.articleDetail, extra: article);
userBloc.add(GetUserEvent(token: authBloc.authToken));
bookmarkBloc.add(LoadBookmarksEvent());
},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: imageWidth,
height: imageHeight,
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: CachedNetworkImage(
imageUrl: imageUrl,
fit: BoxFit.cover,
),
),
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
articleTitle,
style: TextStyle(
fontWeight: FontWeight.w100,
fontSize: titleFontSize,
color: AppColors.blue,
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
articleTitle,
style: TextStyle(
fontWeight: FontWeight.w100,
fontSize: titleFontSize,
color: AppColors.blue,
),
),
),
SizedBox(height: statsSpacing),
Text(
articleSubTitle,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: subTitleFontSize,
color: AppColors.darkerBlue,
SizedBox(height: statsSpacing),
Text(
articleSubTitle,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: subTitleFontSize,
color: AppColors.darkerBlue,
),
),
),
SizedBox(height: 8.w),
Row(
children: [
Row(
children: [
Icon(
Icons.thumb_up_alt_outlined,
color: AppColors.darkBlue,
size: iconSize,
),
SizedBox(width: statsSpacing),
Text(
likes,
style: TextStyle(
fontSize: statsFontSize,
fontWeight: FontWeight.w500,
SizedBox(height: 8.w),
Row(
children: [
Row(
children: [
Icon(
Icons.thumb_up_alt_outlined,
color: AppColors.darkBlue,
size: iconSize,
),
),
],
),
const Spacer(),
Row(
children: [
Icon(
Icons.access_time,
color: AppColors.darkBlue,
size: iconSize,
),
const SizedBox(width: 4),
Text(
timeSincePosted,
style: const TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
SizedBox(width: statsSpacing),
Text(
likes,
style: TextStyle(
fontSize: statsFontSize,
fontWeight: FontWeight.w500,
color: AppColors.darkBlue,
),
),
],
),
const Spacer(),
Row(
children: [
Icon(
Icons.access_time,
color: AppColors.darkBlue,
size: iconSize,
),
),
],
),
SizedBox(width: statsSpacing),
Icon(
Icons.bookmark_outlined,
color: AppColors.blue,
size: iconSize,
),
],
),
],
),
],
const SizedBox(width: 4),
Text(
timeSincePosted,
style: const TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
color: AppColors.darkBlue,
),
),
],
),
SizedBox(width: statsSpacing),
BlocConsumer<BookmarkBloc, BookmarkState>(
listener: (context, state) {
if (state is BookmarkErrorState) {
showError(context, state.message);
} else if (state is BookmarkAddedState) {
showSuccess(context, 'Article bookmarked');
context
.read<BookmarkBloc>()
.add(LoadBookmarksEvent());
} else if (state is BookmarkRemovedState) {
showSuccess(
context, 'Article removed from bookmarks');
context
.read<BookmarkBloc>()
.add(LoadBookmarksEvent());
}
},
builder: (context, state) {
if (state is BookmarkLoadedState) {
return Bookmark(
iconSize: iconSize,
isBookmarked: _isBookmarked(state.articles),
article: article,
);
} else {
return Bookmark(
iconSize: iconSize,
isBookmarked: false,
article: article,
);
}
},
),
],
),
],
),
],
),
),
),
);
Expand Down
Loading
Loading