feat: add serviceBuilder for userstory configuration to fetch the service when needed

This commit is contained in:
Freek van de Ven 2024-05-09 21:45:24 +02:00
parent 93a74fb904
commit 4f7fa834e4
7 changed files with 36 additions and 27 deletions

View file

@ -1,3 +1,8 @@
## 4.0.0
- Add a serviceBuilder to the userstory configuration
## 3.0.1 ## 3.0.1
- Fixed postOverviewScreen not displaying the creators name. - Fixed postOverviewScreen not displaying the creators name.

View file

@ -28,10 +28,11 @@ List<GoRoute> getTimelineStoryRoutes({
GoRoute( GoRoute(
path: TimelineUserStoryRoutes.timelineHome, path: TimelineUserStoryRoutes.timelineHome,
pageBuilder: (context, state) { pageBuilder: (context, state) {
var service = config.serviceBuilder?.call(context) ?? config.service;
var timelineScreen = TimelineScreen( var timelineScreen = TimelineScreen(
userId: config.userId, userId: config.userId,
onUserTap: (user) => config.onUserTap?.call(context, user), onUserTap: (user) => config.onUserTap?.call(context, user),
service: config.service, service: service,
options: config.optionsBuilder(context), options: config.optionsBuilder(context),
onPostTap: (post) async => onPostTap: (post) async =>
config.onPostTap?.call(context, post) ?? config.onPostTap?.call(context, post) ??
@ -129,13 +130,13 @@ List<GoRoute> getTimelineStoryRoutes({
GoRoute( GoRoute(
path: TimelineUserStoryRoutes.timelineView, path: TimelineUserStoryRoutes.timelineView,
pageBuilder: (context, state) { pageBuilder: (context, state) {
var post = var service = config.serviceBuilder?.call(context) ?? config.service;
config.service.postService.getPost(state.pathParameters['post']!); var post = service.postService.getPost(state.pathParameters['post']!);
var timelinePostWidget = TimelinePostScreen( var timelinePostWidget = TimelinePostScreen(
userId: config.userId, userId: config.userId,
options: config.optionsBuilder(context), options: config.optionsBuilder(context),
service: config.service, service: service,
post: post!, post: post!,
onPostDelete: () => config.onPostDelete?.call(context, post), onPostDelete: () => config.onPostDelete?.call(context, post),
onUserTap: (user) => config.onUserTap?.call(context, user), onUserTap: (user) => config.onUserTap?.call(context, user),
@ -174,20 +175,20 @@ List<GoRoute> getTimelineStoryRoutes({
path: TimelineUserStoryRoutes.timelinePostCreation, path: TimelineUserStoryRoutes.timelinePostCreation,
pageBuilder: (context, state) { pageBuilder: (context, state) {
var category = state.pathParameters['category']; var category = state.pathParameters['category'];
var service = config.serviceBuilder?.call(context) ?? config.service;
var timelinePostCreationWidget = TimelinePostCreationScreen( var timelinePostCreationWidget = TimelinePostCreationScreen(
userId: config.userId, userId: config.userId,
options: config.optionsBuilder(context), options: config.optionsBuilder(context),
service: config.service, service: service,
onPostCreated: (post) async { onPostCreated: (post) async {
var newPost = await config.service.postService.createPost(post); var newPost = await service.postService.createPost(post);
if (context.mounted) { if (!context.mounted) return;
if (config.afterPostCreationGoHome) { if (config.afterPostCreationGoHome) {
context.go(TimelineUserStoryRoutes.timelineHome); context.go(TimelineUserStoryRoutes.timelineHome);
} else { } else {
await context await context
.push(TimelineUserStoryRoutes.timelineViewPath(newPost.id)); .push(TimelineUserStoryRoutes.timelineViewPath(newPost.id));
} }
}
}, },
onPostOverview: (post) async => context.push( onPostOverview: (post) async => context.push(
TimelineUserStoryRoutes.timelinePostOverview, TimelineUserStoryRoutes.timelinePostOverview,
@ -233,16 +234,15 @@ List<GoRoute> getTimelineStoryRoutes({
path: TimelineUserStoryRoutes.timelinePostOverview, path: TimelineUserStoryRoutes.timelinePostOverview,
pageBuilder: (context, state) { pageBuilder: (context, state) {
var post = state.extra! as TimelinePost; var post = state.extra! as TimelinePost;
var service = config.serviceBuilder?.call(context) ?? config.service;
var timelinePostOverviewWidget = TimelinePostOverviewScreen( var timelinePostOverviewWidget = TimelinePostOverviewScreen(
options: config.optionsBuilder(context), options: config.optionsBuilder(context),
service: config.service, service: service,
timelinePost: post, timelinePost: post,
onPostSubmit: (post) async { onPostSubmit: (post) async {
await config.service.postService.createPost(post); await service.postService.createPost(post);
if (context.mounted) { if (!context.mounted) return;
context.go(TimelineUserStoryRoutes.timelineHome); context.go(TimelineUserStoryRoutes.timelineHome);
}
}, },
); );
var backButton = IconButton( var backButton = IconButton(

View file

@ -48,6 +48,7 @@ class TimelineUserStoryConfiguration {
const TimelineUserStoryConfiguration({ const TimelineUserStoryConfiguration({
required this.service, required this.service,
required this.optionsBuilder, required this.optionsBuilder,
this.serviceBuilder,
this.userId = 'test_user', this.userId = 'test_user',
this.homeOpenPageBuilder, this.homeOpenPageBuilder,
this.postCreationOpenPageBuilder, this.postCreationOpenPageBuilder,
@ -69,6 +70,9 @@ class TimelineUserStoryConfiguration {
/// The TimelineService responsible for fetching user story data. /// The TimelineService responsible for fetching user story data.
final TimelineService service; final TimelineService service;
/// A function to get the timeline service only when needed and with a context
final TimelineService Function(BuildContext context)? serviceBuilder;
/// A function that builds TimelineOptions based on the given BuildContext. /// A function that builds TimelineOptions based on the given BuildContext.
final TimelineOptions Function(BuildContext context) optionsBuilder; final TimelineOptions Function(BuildContext context) optionsBuilder;

View file

@ -3,7 +3,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
name: flutter_timeline name: flutter_timeline
description: Visual elements and interface combined into one package description: Visual elements and interface combined into one package
version: 3.0.1 version: 4.0.0
publish_to: none publish_to: none
@ -19,13 +19,13 @@ dependencies:
git: git:
url: https://github.com/Iconica-Development/flutter_timeline url: https://github.com/Iconica-Development/flutter_timeline
path: packages/flutter_timeline_view path: packages/flutter_timeline_view
ref: 3.0.1 ref: 4.0.0
flutter_timeline_interface: flutter_timeline_interface:
git: git:
url: https://github.com/Iconica-Development/flutter_timeline url: https://github.com/Iconica-Development/flutter_timeline
path: packages/flutter_timeline_interface path: packages/flutter_timeline_interface
ref: 3.0.1 ref: 4.0.0
dev_dependencies: dev_dependencies:
flutter_lints: ^2.0.0 flutter_lints: ^2.0.0

View file

@ -4,7 +4,7 @@
name: flutter_timeline_firebase name: flutter_timeline_firebase
description: Implementation of the Flutter Timeline interface for Firebase. description: Implementation of the Flutter Timeline interface for Firebase.
version: 3.0.1 version: 4.0.0
publish_to: none publish_to: none
@ -23,7 +23,7 @@ dependencies:
git: git:
url: https://github.com/Iconica-Development/flutter_timeline url: https://github.com/Iconica-Development/flutter_timeline
path: packages/flutter_timeline_interface path: packages/flutter_timeline_interface
ref: 3.0.1 ref: 4.0.0
dev_dependencies: dev_dependencies:
flutter_lints: ^2.0.0 flutter_lints: ^2.0.0

View file

@ -4,7 +4,7 @@
name: flutter_timeline_interface name: flutter_timeline_interface
description: Interface for the service of the Flutter Timeline component description: Interface for the service of the Flutter Timeline component
version: 3.0.1 version: 4.0.0
publish_to: none publish_to: none

View file

@ -4,7 +4,7 @@
name: flutter_timeline_view name: flutter_timeline_view
description: Visual elements of the Flutter Timeline Component description: Visual elements of the Flutter Timeline Component
version: 3.0.1 version: 4.0.0
publish_to: none publish_to: none
@ -23,7 +23,7 @@ dependencies:
git: git:
url: https://github.com/Iconica-Development/flutter_timeline url: https://github.com/Iconica-Development/flutter_timeline
path: packages/flutter_timeline_interface path: packages/flutter_timeline_interface
ref: 3.0.1 ref: 4.0.0
flutter_image_picker: flutter_image_picker:
git: git:
url: https://github.com/Iconica-Development/flutter_image_picker url: https://github.com/Iconica-Development/flutter_image_picker