From 8188c179fb8b9ee73f9aa184936aca8da305a798 Mon Sep 17 00:00:00 2001 From: mike doornenbal Date: Thu, 1 Aug 2024 13:22:36 +0200 Subject: [PATCH] fix: postModel not including creator --- .../flutter_timeline_navigator_userstory.dart | 87 +++------ .../lib/src/model/timeline_poster.dart | 17 ++ .../lib/src/config/timeline_options.dart | 1 + .../timeline_post_creation_screen.dart | 5 +- .../timeline_post_overview_screen.dart | 5 + .../screens/timeline_selection_screen.dart | 179 +++++++++--------- 6 files changed, 144 insertions(+), 150 deletions(-) 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 0d90d21..b4fb0bf 100644 --- a/packages/flutter_timeline/lib/src/flutter_timeline_navigator_userstory.dart +++ b/packages/flutter_timeline/lib/src/flutter_timeline_navigator_userstory.dart @@ -23,7 +23,7 @@ Widget timeLineNavigatorUserStory({ optionsBuilder: (context) => const TimelineOptions(), ); - return _timelineScreenRoute(configuration: config, context: context); + return _timelineScreenRoute(config: config, context: context); } /// A widget function that creates a timeline screen route. @@ -33,18 +33,11 @@ Widget timeLineNavigatorUserStory({ /// parameters. If no configuration is provided, default values will be used. Widget _timelineScreenRoute({ required BuildContext context, - TimelineUserStoryConfiguration? configuration, + required TimelineUserStoryConfiguration config, + String? initalCategory, }) { - var config = configuration ?? - TimelineUserStoryConfiguration( - userId: 'test_user', - service: TimelineService( - postService: LocalTimelinePostService(), - ), - optionsBuilder: (context) => const TimelineOptions(), - ); - var timelineScreen = TimelineScreen( + timelineCategory: initalCategory, userId: config.getUserId?.call(context) ?? config.userId, allowAllDeletion: config.canDeleteAllPosts?.call(context) ?? false, onUserTap: (user) => config.onUserTap?.call(context, user), @@ -55,7 +48,7 @@ Widget _timelineScreenRoute({ Navigator.of(context).push( MaterialPageRoute( builder: (context) => _postDetailScreenRoute( - configuration: config, + config: config, context: context, post: post, ), @@ -78,7 +71,7 @@ Widget _timelineScreenRoute({ await Navigator.of(context).push( MaterialPageRoute( builder: (context) => _postCreationScreenRoute( - configuration: config, + config: config, context: context, category: selectedCategory, ), @@ -88,7 +81,7 @@ Widget _timelineScreenRoute({ await Navigator.of(context).push( MaterialPageRoute( builder: (context) => _postCategorySelectionScreen( - configuration: config, + config: config, context: context, ), ), @@ -125,17 +118,8 @@ Widget _timelineScreenRoute({ Widget _postDetailScreenRoute({ required BuildContext context, required TimelinePost post, - TimelineUserStoryConfiguration? configuration, + required TimelineUserStoryConfiguration config, }) { - var config = configuration ?? - TimelineUserStoryConfiguration( - userId: 'test_user', - service: TimelineService( - postService: LocalTimelinePostService(), - ), - optionsBuilder: (context) => const TimelineOptions(), - ); - var timelinePostScreen = TimelinePostScreen( userId: config.getUserId?.call(context) ?? config.userId, allowAllDeletion: config.canDeleteAllPosts?.call(context) ?? false, @@ -166,7 +150,7 @@ Widget _postDetailScreenRoute({ ?.call(context, timelinePostScreen, backButton, post, category) ?? Scaffold( appBar: AppBar( - leading: backButton, + iconTheme: Theme.of(context).appBarTheme.iconTheme, title: Text( category.title.toLowerCase(), style: TextStyle( @@ -188,31 +172,24 @@ Widget _postDetailScreenRoute({ Widget _postCreationScreenRoute({ required BuildContext context, required TimelineCategory category, - TimelineUserStoryConfiguration? configuration, + required TimelineUserStoryConfiguration config, }) { - var config = configuration ?? - TimelineUserStoryConfiguration( - userId: 'test_user', - service: TimelineService( - postService: LocalTimelinePostService(), - ), - optionsBuilder: (context) => const TimelineOptions(), - ); - var timelinePostCreationScreen = TimelinePostCreationScreen( userId: config.getUserId?.call(context) ?? config.userId, options: config.optionsBuilder(context), service: config.service, onPostCreated: (post) async { var newPost = await config.service.postService.createPost(post); + if (!context.mounted) return; if (config.afterPostCreationGoHome) { await Navigator.pushReplacement( context, MaterialPageRoute( builder: (context) => _timelineScreenRoute( - configuration: config, + config: config, context: context, + initalCategory: category.title, ), ), ); @@ -221,7 +198,7 @@ Widget _postCreationScreenRoute({ context, MaterialPageRoute( builder: (context) => _postOverviewScreenRoute( - configuration: config, + config: config, context: context, post: newPost, ), @@ -232,7 +209,7 @@ Widget _postCreationScreenRoute({ onPostOverview: (post) async => Navigator.of(context).push( MaterialPageRoute( builder: (context) => _postOverviewScreenRoute( - configuration: config, + config: config, context: context, post: post, ), @@ -254,6 +231,7 @@ Widget _postCreationScreenRoute({ ?.call(context, timelinePostCreationScreen, backButton) ?? Scaffold( appBar: AppBar( + iconTheme: Theme.of(context).appBarTheme.iconTheme, leading: backButton, title: Text( config.optionsBuilder(context).translations.postCreation, @@ -277,17 +255,8 @@ Widget _postCreationScreenRoute({ Widget _postOverviewScreenRoute({ required BuildContext context, required TimelinePost post, - TimelineUserStoryConfiguration? configuration, + required TimelineUserStoryConfiguration config, }) { - var config = configuration ?? - TimelineUserStoryConfiguration( - userId: 'test_user', - service: TimelineService( - postService: LocalTimelinePostService(), - ), - optionsBuilder: (context) => const TimelineOptions(), - ); - var timelinePostOverviewWidget = TimelinePostOverviewScreen( options: config.optionsBuilder(context), service: config.service, @@ -297,8 +266,11 @@ Widget _postOverviewScreenRoute({ if (context.mounted) { await Navigator.of(context).pushAndRemoveUntil( MaterialPageRoute( - builder: (context) => - _timelineScreenRoute(configuration: config, context: context), + builder: (context) => _timelineScreenRoute( + config: config, + context: context, + initalCategory: post.category, + ), ), (route) => false, ); @@ -320,6 +292,7 @@ Widget _postOverviewScreenRoute({ ) ?? Scaffold( appBar: AppBar( + iconTheme: Theme.of(context).appBarTheme.iconTheme, leading: backButton, title: Text( config.optionsBuilder(context).translations.postCreation, @@ -336,17 +309,8 @@ Widget _postOverviewScreenRoute({ Widget _postCategorySelectionScreen({ required BuildContext context, - TimelineUserStoryConfiguration? configuration, + required TimelineUserStoryConfiguration config, }) { - var config = configuration ?? - TimelineUserStoryConfiguration( - userId: 'test_user', - service: TimelineService( - postService: LocalTimelinePostService(), - ), - optionsBuilder: (context) => const TimelineOptions(), - ); - var timelineSelectionScreen = TimelineSelectionScreen( postService: config.service.postService, options: config.optionsBuilder(context), @@ -355,7 +319,7 @@ Widget _postCategorySelectionScreen({ await Navigator.of(context).push( MaterialPageRoute( builder: (context) => _postCreationScreenRoute( - configuration: config, + config: config, context: context, category: category, ), @@ -376,6 +340,7 @@ Widget _postCategorySelectionScreen({ ?.call(context, timelineSelectionScreen) ?? Scaffold( appBar: AppBar( + iconTheme: Theme.of(context).appBarTheme.iconTheme, leading: backButton, title: Text( config.optionsBuilder(context).translations.postCreation, diff --git a/packages/flutter_timeline_interface/lib/src/model/timeline_poster.dart b/packages/flutter_timeline_interface/lib/src/model/timeline_poster.dart index c652508..a07f3fd 100644 --- a/packages/flutter_timeline_interface/lib/src/model/timeline_poster.dart +++ b/packages/flutter_timeline_interface/lib/src/model/timeline_poster.dart @@ -13,11 +13,28 @@ class TimelinePosterUserModel { this.imageUrl, }); + factory TimelinePosterUserModel.fromJson( + Map json, + String userId, + ) => + TimelinePosterUserModel( + userId: userId, + firstName: json['first_name'] as String?, + lastName: json['last_name'] as String?, + imageUrl: json['image_url'] as String?, + ); + final String userId; final String? firstName; final String? lastName; final String? imageUrl; + Map toJson() => { + 'first_name': firstName, + 'last_name': lastName, + 'image_url': imageUrl, + }; + String? get fullName { var fullName = ''; 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 d3694cf..49e35f2 100644 --- a/packages/flutter_timeline_view/lib/src/config/timeline_options.dart +++ b/packages/flutter_timeline_view/lib/src/config/timeline_options.dart @@ -160,6 +160,7 @@ class TimelineOptions { BuildContext context, Function() onPressed, String text, + TimelinePost post, )? postOverviewButtonBuilder; /// Optional builder to override the default alertdialog for post deletion diff --git a/packages/flutter_timeline_view/lib/src/screens/timeline_post_creation_screen.dart b/packages/flutter_timeline_view/lib/src/screens/timeline_post_creation_screen.dart index 90286ab..0e46e58 100644 --- a/packages/flutter_timeline_view/lib/src/screens/timeline_post_creation_screen.dart +++ b/packages/flutter_timeline_view/lib/src/screens/timeline_post_creation_screen.dart @@ -61,6 +61,7 @@ class _TimelinePostCreationScreenState void initState() { titleController.addListener(_listenForInputs); contentController.addListener(_listenForInputs); + super.initState(); } @@ -77,6 +78,7 @@ class _TimelinePostCreationScreenState var imageRequired = widget.options.requireImageForPost; Future onPostCreated() async { + var user = await widget.service.userService?.getUser(widget.userId); var post = TimelinePost( id: 'Post${Random().nextInt(1000)}', creatorId: widget.userId, @@ -89,6 +91,7 @@ class _TimelinePostCreationScreenState createdAt: DateTime.now(), reactionEnabled: allowComments, image: image, + creator: user, ); if (widget.enablePostOverviewScreen) { @@ -362,7 +365,7 @@ class _TimelinePostCreationScreenState child: DefaultFilledButton( onPressed: titleIsValid && contentIsValid && - (imageRequired ? image != null : true) + (!imageRequired || image != null) ? () async { if (formkey.currentState! .validate()) { diff --git a/packages/flutter_timeline_view/lib/src/screens/timeline_post_overview_screen.dart b/packages/flutter_timeline_view/lib/src/screens/timeline_post_overview_screen.dart index 0c3f1b5..89d3e0a 100644 --- a/packages/flutter_timeline_view/lib/src/screens/timeline_post_overview_screen.dart +++ b/packages/flutter_timeline_view/lib/src/screens/timeline_post_overview_screen.dart @@ -35,13 +35,18 @@ class TimelinePostOverviewScreen extends StatelessWidget { options.postOverviewButtonBuilder?.call( context, () { + if (isSubmitted) return; + isSubmitted = true; onPostSubmit(timelinePost); }, options.translations.postIn, + timelinePost, ) ?? options.buttonBuilder?.call( context, () { + if (isSubmitted) return; + isSubmitted = true; onPostSubmit(timelinePost); }, options.translations.postIn, diff --git a/packages/flutter_timeline_view/lib/src/screens/timeline_selection_screen.dart b/packages/flutter_timeline_view/lib/src/screens/timeline_selection_screen.dart index 2ed8b36..0a855ff 100644 --- a/packages/flutter_timeline_view/lib/src/screens/timeline_selection_screen.dart +++ b/packages/flutter_timeline_view/lib/src/screens/timeline_selection_screen.dart @@ -36,105 +36,108 @@ class _TimelineSelectionScreenState extends State { padding: EdgeInsets.symmetric( horizontal: size.width * 0.05, ), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Padding( - padding: const EdgeInsets.only(top: 20, bottom: 12), - child: Text( - widget.options.translations.timelineSelectionDescription, - style: theme.textTheme.titleLarge, + child: SingleChildScrollView( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsets.only(top: 20, bottom: 12), + child: Text( + widget.options.translations.timelineSelectionDescription, + style: theme.textTheme.titleLarge, + ), ), - ), - for (var category in widget.categories.where( - (element) => element.canCreate && element.key != null, - )) ...[ - widget.options.categorySelectorButtonBuilder?.call( - context, - () { - widget.onCategorySelected.call(category); - }, - category.title, - ) ?? - InkWell( - onTap: () => widget.onCategorySelected.call(category), - child: Container( - height: 60, - width: double.infinity, - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(10), - border: Border.all( - color: widget.options.theme - .categorySelectionButtonBorderColor ?? - Theme.of(context).primaryColor, - width: 2, - ), - color: widget - .options.theme.categorySelectionButtonBackgroundColor, - ), - margin: const EdgeInsets.symmetric(vertical: 4), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Padding( - padding: const EdgeInsets.symmetric(horizontal: 12.0), - child: Text( - category.title, - style: theme.textTheme.titleMedium, - ), + for (var category in widget.categories.where( + (element) => element.canCreate && element.key != null, + )) ...[ + widget.options.categorySelectorButtonBuilder?.call( + context, + () { + widget.onCategorySelected.call(category); + }, + category.title, + ) ?? + InkWell( + onTap: () => widget.onCategorySelected.call(category), + child: Container( + height: 60, + width: double.infinity, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(10), + border: Border.all( + color: widget.options.theme + .categorySelectionButtonBorderColor ?? + Theme.of(context).primaryColor, + width: 2, ), - ], + color: widget.options.theme + .categorySelectionButtonBackgroundColor, + ), + margin: const EdgeInsets.symmetric(vertical: 4), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Padding( + padding: + const EdgeInsets.symmetric(horizontal: 12.0), + child: Text( + category.title, + style: theme.textTheme.titleMedium, + ), + ), + ], + ), ), ), + ], + InkWell( + onTap: showCategoryPopup, + child: Container( + height: 60, + width: double.infinity, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(10), + border: Border.all( + color: widget + .options.theme.categorySelectionButtonBorderColor ?? + Theme.of(context).primaryColor, + width: 2, + ), + color: widget + .options.theme.categorySelectionButtonBackgroundColor, ), - ], - InkWell( - onTap: showCategoryPopup, - child: Container( - height: 60, - width: double.infinity, - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(10), - border: Border.all( - color: - widget.options.theme.categorySelectionButtonBorderColor ?? - Theme.of(context).primaryColor, - width: 2, - ), - color: - widget.options.theme.categorySelectionButtonBackgroundColor, - ), - margin: const EdgeInsets.symmetric(vertical: 4), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Padding( - padding: const EdgeInsets.symmetric(horizontal: 12.0), - child: Row( - children: [ - Icon( - Icons.add, - color: theme.textTheme.titleMedium?.color! - .withOpacity(0.5), - ), - const SizedBox(width: 8), - Text( - widget.options.translations.addCategoryTitle, - style: theme.textTheme.titleMedium!.copyWith( + margin: const EdgeInsets.symmetric(vertical: 4), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Padding( + padding: const EdgeInsets.symmetric(horizontal: 12.0), + child: Row( + children: [ + Icon( + Icons.add, color: theme.textTheme.titleMedium?.color! .withOpacity(0.5), ), - ), - ], + const SizedBox(width: 8), + Text( + widget.options.translations.addCategoryTitle, + style: theme.textTheme.titleMedium!.copyWith( + color: theme.textTheme.titleMedium?.color! + .withOpacity(0.5), + ), + ), + ], + ), ), - ), - ], + ], + ), ), ), - ), - ], + ], + ), ), ); }