Skip to content

Commit

Permalink
Navigate to new post after creation (#1044)
Browse files Browse the repository at this point in the history
  • Loading branch information
micahmo authored Jan 17, 2024
1 parent 0a928f1 commit a6f5ded
Show file tree
Hide file tree
Showing 12 changed files with 518 additions and 401 deletions.
22 changes: 20 additions & 2 deletions lib/community/pages/create_post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/utils/debounce.dart';
import 'package:thunder/utils/image.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/utils/navigate_post.dart';

class CreatePostPage extends StatefulWidget {
final int? communityId;
Expand All @@ -61,6 +62,9 @@ class CreatePostPage extends StatefulWidget {
/// Callback function that is triggered whenever the post is successfully created or updated
final Function(PostViewMedia postViewMedia)? onPostSuccess;

// The scaffold key for the main Thunder page
final GlobalKey<ScaffoldMessengerState>? scaffoldMessengerKey;

const CreatePostPage({
super.key,
required this.communityId,
Expand All @@ -72,6 +76,7 @@ class CreatePostPage extends StatefulWidget {
this.prePopulated = false,
this.postView,
this.onPostSuccess,
this.scaffoldMessengerKey,
});

@override
Expand Down Expand Up @@ -337,9 +342,10 @@ class _CreatePostPageState extends State<CreatePostPage> {
child: IconButton(
onPressed: isSubmitButtonDisabled
? null
: () {
: () async {
draftPost.saveAsDraft = false;
context.read<CreatePostCubit>().createOrEditPost(

final int? postId = await context.read<CreatePostCubit>().createOrEditPost(
communityId: communityId!,
name: _titleTextController.text,
body: _bodyTextController.text,
Expand All @@ -348,6 +354,18 @@ class _CreatePostPageState extends State<CreatePostPage> {
postIdBeingEdited: widget.postView?.post.id,
languageId: languageId,
);

if (context.mounted && widget.scaffoldMessengerKey?.currentContext != null && widget.postView?.post.id == null && postId != null) {
showSnackbar(
context,
l10n.postCreatedSuccessfully,
trailingIcon: Icons.remove_red_eye_rounded,
trailingAction: () {
navigateToPost(widget.scaffoldMessengerKey!.currentContext!, postId: postId);
},
customState: widget.scaffoldMessengerKey?.currentState,
);
}
},
icon: Icon(
widget.postView != null ? Icons.edit_rounded : Icons.send_rounded,
Expand Down
18 changes: 14 additions & 4 deletions lib/feed/view/feed_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class FeedPage extends StatefulWidget {
this.userId,
this.username,
this.scaffoldStateKey,
this.scaffoldMessengerKey,
});

/// The type of feed to display.
Expand Down Expand Up @@ -84,6 +85,9 @@ class FeedPage extends StatefulWidget {
/// The scaffold key which holds the drawer
final GlobalKey<ScaffoldState>? scaffoldStateKey;

/// The messenger key back to the main Thunder page
final GlobalKey<ScaffoldMessengerState>? scaffoldMessengerKey;

@override
State<FeedPage> createState() => _FeedPageState();
}
Expand Down Expand Up @@ -127,7 +131,7 @@ class _FeedPageState extends State<FeedPage> with AutomaticKeepAliveClientMixin<

return BlocProvider.value(
value: bloc,
child: FeedView(scaffoldStateKey: widget.scaffoldStateKey),
child: FeedView(scaffoldStateKey: widget.scaffoldStateKey, scaffoldMessengerKey: widget.scaffoldMessengerKey),
);
}

Expand All @@ -143,17 +147,20 @@ class _FeedPageState extends State<FeedPage> with AutomaticKeepAliveClientMixin<
username: widget.username,
reset: true,
)),
child: FeedView(scaffoldStateKey: widget.scaffoldStateKey),
child: FeedView(scaffoldStateKey: widget.scaffoldStateKey, scaffoldMessengerKey: widget.scaffoldMessengerKey),
);
}
}

class FeedView extends StatefulWidget {
const FeedView({super.key, this.scaffoldStateKey});
const FeedView({super.key, this.scaffoldStateKey, this.scaffoldMessengerKey});

/// The scaffold key which holds the drawer
final GlobalKey<ScaffoldState>? scaffoldStateKey;

/// The messenger key back to the main Thunder page
final GlobalKey<ScaffoldMessengerState>? scaffoldMessengerKey;

@override
State<FeedView> createState() => _FeedViewState();
}
Expand Down Expand Up @@ -426,7 +433,10 @@ class _FeedViewState extends State<FeedView> {
curve: Curves.easeIn,
child: Container(
margin: const EdgeInsets.all(16),
child: FeedFAB(heroTag: state.communityName),
child: FeedFAB(
heroTag: state.communityName,
scaffoldMessengerKey: widget.scaffoldMessengerKey,
),
),
),
],
Expand Down
6 changes: 5 additions & 1 deletion lib/feed/widgets/feed_fab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import 'package:thunder/shared/sort_picker.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';

class FeedFAB extends StatelessWidget {
const FeedFAB({super.key, this.heroTag});
const FeedFAB({super.key, this.heroTag, this.scaffoldMessengerKey});

final String? heroTag;

/// The messenger key back to the main Thunder page
final GlobalKey<ScaffoldMessengerState>? scaffoldMessengerKey;

@override
build(BuildContext context) {
final ThunderState state = context.watch<ThunderBloc>().state;
Expand Down Expand Up @@ -287,6 +290,7 @@ class FeedFAB extends StatelessWidget {
child: CreatePostPage(
communityId: feedBloc.state.communityId,
communityView: feedBloc.state.fullCommunityView?.communityView,
scaffoldMessengerKey: scaffoldMessengerKey,
),
);
},
Expand Down
4 changes: 4 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,10 @@
"@postContentFontScale": {
"description": "Setting for post content font scale"
},
"postCreatedSuccessfully" :"Post created successfully!",
"@postCreatedSuccessfully": {
"description": "Notifying the user that their post was created successfully"
},
"postLocked": "Post locked. No replies allowed.",
"@postLocked": {},
"postNSFW": "Mark as NSFW",
Expand Down
10 changes: 7 additions & 3 deletions lib/post/cubit/create_post_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ class CreatePostCubit extends Cubit<CreatePostState> {
}
}

/// Creates or edits a post. When successful, it returns the newly created/updated post in the form of a [PostViewMedia]
Future<void> createOrEditPost({required int communityId, required String name, String? body, String? url, bool? nsfw, int? postIdBeingEdited, int? languageId}) async {
/// Creates or edits a post. When successful, it emits the newly created/updated post in the form of a [PostViewMedia]
/// and returns the newly created post id.
Future<int?> createOrEditPost({required int communityId, required String name, String? body, String? url, bool? nsfw, int? postIdBeingEdited, int? languageId}) async {
emit(state.copyWith(status: CreatePostStatus.submitting));

try {
Expand All @@ -58,8 +59,11 @@ class CreatePostCubit extends Cubit<CreatePostState> {
List<PostViewMedia> postViewMedias = await parsePostViews([postView]);

emit(state.copyWith(status: CreatePostStatus.success, postViewMedia: postViewMedias.firstOrNull));
return postViewMedias.firstOrNull?.postView.post.id;
} catch (e) {
return emit(state.copyWith(status: CreatePostStatus.error, message: getExceptionErrorMessage(e)));
emit(state.copyWith(status: CreatePostStatus.error, message: getExceptionErrorMessage(e)));
}

return null;
}
}
Loading

0 comments on commit a6f5ded

Please sign in to comment.