diff --git a/CHANGELOG.md b/CHANGELOG.md index d60b008..2d909e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Change the CategorySelectorButton to use more styling options and allow for an icon to be shown - Fix incorrect timeline reaction name - Add a dialog for post deletion confirmation +- Add a callback method to determine if a user can delete posts that gets called when needed ## 3.0.1 diff --git a/packages/flutter_timeline/example/lib/config/config.dart b/packages/flutter_timeline/example/lib/config/config.dart index ff46d66..017e672 100644 --- a/packages/flutter_timeline/example/lib/config/config.dart +++ b/packages/flutter_timeline/example/lib/config/config.dart @@ -7,6 +7,7 @@ TimelineUserStoryConfiguration getConfig(TimelineService service) { userId: 'test_user', optionsBuilder: (context) => options, enablePostOverviewScreen: false, + canDeleteAllPosts: (_) => true, ); } @@ -15,7 +16,6 @@ var options = TimelineOptions( paddings: TimelinePaddingOptions( mainPadding: const EdgeInsets.all(20).copyWith(top: 28), ), - allowAllDeletion: true, categoriesOptions: CategoriesOptions( categoriesBuilder: (context) => [ const TimelineCategory( diff --git a/packages/flutter_timeline/lib/src/flutter_timeline_gorouter_userstory.dart b/packages/flutter_timeline/lib/src/flutter_timeline_gorouter_userstory.dart index 3f4935f..7587b76 100644 --- a/packages/flutter_timeline/lib/src/flutter_timeline_gorouter_userstory.dart +++ b/packages/flutter_timeline/lib/src/flutter_timeline_gorouter_userstory.dart @@ -33,6 +33,7 @@ List getTimelineStoryRoutes({ var timelineScreen = TimelineScreen( userId: config.getUserId?.call(context) ?? config.userId, onUserTap: (user) => config.onUserTap?.call(context, user), + allowAllDeletion: config.canDeleteAllPosts?.call(context) ?? false, onRefresh: config.onRefresh, service: service, options: config.optionsBuilder(context), @@ -151,6 +152,7 @@ List getTimelineStoryRoutes({ var timelinePostWidget = TimelinePostScreen( userId: config.getUserId?.call(context) ?? config.userId, + allowAllDeletion: config.canDeleteAllPosts?.call(context) ?? false, options: config.optionsBuilder(context), service: service, post: post!, diff --git a/packages/flutter_timeline/lib/src/flutter_timeline_navigator_userstory.dart b/packages/flutter_timeline/lib/src/flutter_timeline_navigator_userstory.dart index 250fbf7..89db27a 100644 --- a/packages/flutter_timeline/lib/src/flutter_timeline_navigator_userstory.dart +++ b/packages/flutter_timeline/lib/src/flutter_timeline_navigator_userstory.dart @@ -46,6 +46,7 @@ Widget _timelineScreenRoute({ var timelineScreen = TimelineScreen( userId: config.getUserId?.call(context) ?? config.userId, + allowAllDeletion: config.canDeleteAllPosts?.call(context) ?? false, onUserTap: (user) => config.onUserTap?.call(context, user), service: config.service, options: config.optionsBuilder(context), @@ -127,6 +128,7 @@ Widget _postDetailScreenRoute({ var timelinePostScreen = TimelinePostScreen( userId: config.getUserId?.call(context) ?? config.userId, + allowAllDeletion: config.canDeleteAllPosts?.call(context) ?? false, options: config.optionsBuilder(context), service: config.service, post: post, diff --git a/packages/flutter_timeline/lib/src/models/timeline_configuration.dart b/packages/flutter_timeline/lib/src/models/timeline_configuration.dart index 3d35d6c..884bcff 100644 --- a/packages/flutter_timeline/lib/src/models/timeline_configuration.dart +++ b/packages/flutter_timeline/lib/src/models/timeline_configuration.dart @@ -50,6 +50,7 @@ class TimelineUserStoryConfiguration { required this.optionsBuilder, this.getUserId, this.serviceBuilder, + this.canDeleteAllPosts, this.userId = 'test_user', this.homeOpenPageBuilder, this.postCreationOpenPageBuilder, @@ -72,6 +73,10 @@ class TimelineUserStoryConfiguration { /// A function to get the userId only when needed and with a context final String Function(BuildContext context)? getUserId; + /// A function to determine if a user can delete posts that is called + /// when needed + final bool Function(BuildContext context)? canDeleteAllPosts; + /// The TimelineService responsible for fetching user story data. final TimelineService service; diff --git a/packages/flutter_timeline_view/lib/src/config/timeline_options.dart b/packages/flutter_timeline_view/lib/src/config/timeline_options.dart index f16c73a..e22f2b0 100644 --- a/packages/flutter_timeline_view/lib/src/config/timeline_options.dart +++ b/packages/flutter_timeline_view/lib/src/config/timeline_options.dart @@ -18,7 +18,6 @@ class TimelineOptions { this.imagePickerConfig = const ImagePickerConfig(), this.imagePickerTheme = const ImagePickerTheme(), this.timelinePostHeight, - this.allowAllDeletion = false, this.sortCommentsAscending = true, this.sortPostsAscending, this.doubleTapTolike = false, @@ -71,10 +70,6 @@ class TimelineOptions { /// Whether to sort posts ascending or descending final bool? sortPostsAscending; - /// Allow all posts to be deleted instead of - /// only the posts of the current user - final bool allowAllDeletion; - /// The height of a post in the timeline final double? timelinePostHeight; diff --git a/packages/flutter_timeline_view/lib/src/screens/timeline_post_screen.dart b/packages/flutter_timeline_view/lib/src/screens/timeline_post_screen.dart index 8bb2e40..6b857d0 100644 --- a/packages/flutter_timeline_view/lib/src/screens/timeline_post_screen.dart +++ b/packages/flutter_timeline_view/lib/src/screens/timeline_post_screen.dart @@ -23,6 +23,7 @@ class TimelinePostScreen extends StatefulWidget { required this.options, required this.post, required this.onPostDelete, + this.allowAllDeletion = false, this.isOverviewScreen = false, this.onUserTap, super.key, @@ -31,6 +32,10 @@ class TimelinePostScreen extends StatefulWidget { /// The user id of the current user final String userId; + /// Allow all posts to be deleted instead of + /// only the posts of the current user + final bool allowAllDeletion; + /// The timeline service to fetch the post details final TimelineService service; @@ -193,7 +198,7 @@ class _TimelinePostScreenState extends State { ), const Spacer(), if (!(widget.isOverviewScreen ?? false) && - (widget.options.allowAllDeletion || + (widget.allowAllDeletion || post.creator?.userId == widget.userId)) PopupMenuButton( onSelected: (value) async { @@ -422,7 +427,7 @@ class _TimelinePostScreenState extends State { GestureDetector( onLongPressStart: (details) async { if (reaction.creatorId == widget.userId || - widget.options.allowAllDeletion) { + widget.allowAllDeletion) { var overlay = Overlay.of(context) .context .findRenderObject()! as RenderBox; diff --git a/packages/flutter_timeline_view/lib/src/screens/timeline_screen.dart b/packages/flutter_timeline_view/lib/src/screens/timeline_screen.dart index 1b75303..3be9c56 100644 --- a/packages/flutter_timeline_view/lib/src/screens/timeline_screen.dart +++ b/packages/flutter_timeline_view/lib/src/screens/timeline_screen.dart @@ -21,12 +21,17 @@ class TimelineScreen extends StatefulWidget { this.timelineCategory, this.postWidgetBuilder, this.filterEnabled = false, + this.allowAllDeletion = false, super.key, }); /// The user id of the current user final String userId; + /// Allow all posts to be deleted instead of + /// only the posts of the current user + final bool allowAllDeletion; + /// The service to use for fetching and manipulating posts final TimelineService? service; @@ -151,10 +156,9 @@ class _TimelineScreenState extends State { ), if (widget.filterEnabled) ...[ Padding( - padding: EdgeInsets.symmetric( - // left: widget.options.padding.left, - // right: widget.options.padding.right, - horizontal: widget.options.paddings.mainPadding.horizontal, + padding: EdgeInsets.only( + left: widget.options.paddings.mainPadding.left, + right: widget.options.paddings.mainPadding.right, ), child: Row( crossAxisAlignment: CrossAxisAlignment.end, @@ -246,6 +250,7 @@ class _TimelineScreenState extends State { service: service, userId: widget.userId, options: widget.options, + allowAllDeletion: widget.allowAllDeletion, post: post, onTap: () async { if (widget.onPostTap != null) { diff --git a/packages/flutter_timeline_view/lib/src/widgets/timeline_post_widget.dart b/packages/flutter_timeline_view/lib/src/widgets/timeline_post_widget.dart index 20bbf1a..1051d42 100644 --- a/packages/flutter_timeline_view/lib/src/widgets/timeline_post_widget.dart +++ b/packages/flutter_timeline_view/lib/src/widgets/timeline_post_widget.dart @@ -18,12 +18,18 @@ class TimelinePostWidget extends StatefulWidget { required this.onTapUnlike, required this.onPostDelete, required this.service, + required this.allowAllDeletion, this.onUserTap, super.key, }); /// The user id of the current user final String userId; + + /// Allow all posts to be deleted instead of + /// only the posts of the current user + final bool allowAllDeletion; + final TimelineOptions options; final TimelinePost post; @@ -103,7 +109,7 @@ class _TimelinePostWidgetState extends State { ), ), const Spacer(), - if (widget.options.allowAllDeletion || + if (widget.allowAllDeletion || widget.post.creator?.userId == widget.userId) PopupMenuButton( onSelected: (value) async {