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

Use loading page for posts/comments #1311

Merged
Merged
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
43 changes: 24 additions & 19 deletions lib/comment/utils/navigate_comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/models/post_view_media.dart';
import 'package:thunder/post/bloc/post_bloc.dart';
import 'package:thunder/post/pages/post_page.dart';
import 'package:thunder/shared/pages/loading_page.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/utils/swipe.dart';
Expand All @@ -26,28 +27,32 @@ Future<void> navigateToComment(BuildContext context, CommentView commentView) as
final ThunderState state = context.read<ThunderBloc>().state;
final bool reduceAnimations = state.reduceAnimations;

// To to specific post for now, in the future, will be best to scroll to the position of the comment
await Navigator.of(context).push(
SwipeablePageRoute(
transitionDuration: reduceAnimations ? const Duration(milliseconds: 100) : null,
backGestureDetectionWidth: 45,
canOnlySwipeFromEdge: disableFullPageSwipe(isUserLoggedIn: authBloc.state.isLoggedIn, state: thunderBloc.state, isPostPage: true) || !state.enableFullScreenSwipeNavigationGesture,
builder: (context) => MultiBlocProvider(
providers: [
BlocProvider.value(value: accountBloc),
BlocProvider.value(value: authBloc),
BlocProvider.value(value: thunderBloc),
BlocProvider(create: (context) => PostBloc()),
],
child: PostPage(
selectedCommentId: commentView.comment.id,
selectedCommentPath: commentView.comment.path,
postId: commentView.post.id,
onPostUpdated: (PostViewMedia postViewMedia) => {},
),
final SwipeablePageRoute route = SwipeablePageRoute(
transitionDuration: isLoadingPageShown
? Duration.zero
: reduceAnimations
? const Duration(milliseconds: 100)
: null,
reverseTransitionDuration: reduceAnimations ? const Duration(milliseconds: 100) : const Duration(milliseconds: 500),
backGestureDetectionWidth: 45,
canOnlySwipeFromEdge: disableFullPageSwipe(isUserLoggedIn: authBloc.state.isLoggedIn, state: thunderBloc.state, isPostPage: true) || !state.enableFullScreenSwipeNavigationGesture,
builder: (context) => MultiBlocProvider(
providers: [
BlocProvider.value(value: accountBloc),
BlocProvider.value(value: authBloc),
BlocProvider.value(value: thunderBloc),
BlocProvider(create: (context) => PostBloc()),
],
child: PostPage(
selectedCommentId: commentView.comment.id,
selectedCommentPath: commentView.comment.path,
postId: commentView.post.id,
onPostUpdated: (PostViewMedia postViewMedia) => {},
),
),
);

pushOnTopOfLoadingPage(context, route);
}

Future<void> navigateToCreateCommentPage(
Expand Down
79 changes: 41 additions & 38 deletions lib/post/utils/navigate_post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:thunder/feed/bloc/feed_bloc.dart';
import 'package:thunder/instance/bloc/instance_bloc.dart';
import 'package:thunder/post/enums/post_action.dart';
import 'package:thunder/post/pages/post_page.dart';
import 'package:thunder/shared/pages/loading_page.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/utils/swipe.dart';
import 'package:thunder/post/bloc/post_bloc.dart' as post_bloc;
Expand Down Expand Up @@ -45,45 +46,47 @@ Future<void> navigateToPost(BuildContext context, {PostViewMedia? postViewMedia,

// Mark post as read when tapped
if (authBloc.state.isLoggedIn) {
int? _postId;
_postId = postViewMedia?.postView.post.id ?? postId;

feedBloc?.add(FeedItemActionedEvent(postId: _postId, postAction: PostAction.read, value: true));
feedBloc?.add(FeedItemActionedEvent(postId: postViewMedia?.postView.post.id ?? postId, postAction: PostAction.read, value: true));
}

await Navigator.of(context).push(
SwipeablePageRoute(
transitionDuration: reduceAnimations ? const Duration(milliseconds: 100) : null,
backGestureDetectionStartOffset: !kIsWeb && Platform.isAndroid ? 45 : 0,
backGestureDetectionWidth: 45,
canOnlySwipeFromEdge: disableFullPageSwipe(isUserLoggedIn: authBloc.state.isLoggedIn, state: thunderBloc.state, isPostPage: true) || !thunderBloc.state.enableFullScreenSwipeNavigationGesture,
builder: (otherContext) {
return MultiBlocProvider(
providers: [
BlocProvider.value(value: accountBloc),
BlocProvider.value(value: authBloc),
BlocProvider.value(value: thunderBloc),
BlocProvider.value(value: instanceBloc),
BlocProvider(create: (context) => post_bloc.PostBloc()),
if (communityBloc != null) BlocProvider.value(value: communityBloc),
if (anonymousSubscriptionsBloc != null) BlocProvider.value(value: anonymousSubscriptionsBloc),
],
child: PostPage(
postView: postViewMedia,
postId: postId,
selectedCommentId: selectedCommentId,
selectedCommentPath: selectedCommentPath,
onPostUpdated: (PostViewMedia postViewMedia) {
FeedBloc? feedBloc;
try {
feedBloc = context.read<FeedBloc>();
} catch (e) {}
// Manually marking the read attribute as true when navigating to post since there is a case where the API call to mark the post as read from the feed page is not completed in time
feedBloc?.add(FeedItemUpdatedEvent(postViewMedia: PostViewMedia(postView: postViewMedia.postView.copyWith(read: true), media: postViewMedia.media)));
},
),
);
},
),
final SwipeablePageRoute route = SwipeablePageRoute(
transitionDuration: isLoadingPageShown
? Duration.zero
: reduceAnimations
? const Duration(milliseconds: 100)
: null,
reverseTransitionDuration: reduceAnimations ? const Duration(milliseconds: 100) : const Duration(milliseconds: 500),
backGestureDetectionStartOffset: !kIsWeb && Platform.isAndroid ? 45 : 0,
backGestureDetectionWidth: 45,
canOnlySwipeFromEdge: disableFullPageSwipe(isUserLoggedIn: authBloc.state.isLoggedIn, state: thunderBloc.state, isPostPage: true) || !thunderBloc.state.enableFullScreenSwipeNavigationGesture,
builder: (otherContext) {
return MultiBlocProvider(
providers: [
BlocProvider.value(value: accountBloc),
BlocProvider.value(value: authBloc),
BlocProvider.value(value: thunderBloc),
BlocProvider.value(value: instanceBloc),
BlocProvider(create: (context) => post_bloc.PostBloc()),
if (communityBloc != null) BlocProvider.value(value: communityBloc),
if (anonymousSubscriptionsBloc != null) BlocProvider.value(value: anonymousSubscriptionsBloc),
],
child: PostPage(
postView: postViewMedia,
postId: postId,
selectedCommentId: selectedCommentId,
selectedCommentPath: selectedCommentPath,
onPostUpdated: (PostViewMedia postViewMedia) {
FeedBloc? feedBloc;
try {
feedBloc = context.read<FeedBloc>();
} catch (e) {}
// Manually marking the read attribute as true when navigating to post since there is a case where the API call to mark the post as read from the feed page is not completed in time
feedBloc?.add(FeedItemUpdatedEvent(postViewMedia: PostViewMedia(postView: postViewMedia.postView.copyWith(read: true), media: postViewMedia.media)));
},
),
);
},
);

pushOnTopOfLoadingPage(context, route);
}
3 changes: 2 additions & 1 deletion lib/shared/pages/loading_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LoadingPage extends StatelessWidget {
semanticLabel: MaterialLocalizations.of(context).backButtonTooltip,
)
: Icon(Icons.arrow_back_rounded, semanticLabel: MaterialLocalizations.of(context).backButtonTooltip),
onPressed: () => Navigator.of(context).maybePop(),
onPressed: null,
micahmo marked this conversation as resolved.
Show resolved Hide resolved
)),
const SliverFillRemaining(
child: Center(
Expand Down Expand Up @@ -59,6 +59,7 @@ void showLoadingPage(BuildContext context) {
transitionDuration: reduceAnimations ? const Duration(milliseconds: 100) : null,
backGestureDetectionWidth: 45,
canOnlySwipeFromEdge: !thunderBloc.state.enableFullScreenSwipeNavigationGesture,
canSwipe: false,
builder: (context) => MultiBlocProvider(
providers: [
BlocProvider.value(value: thunderBloc),
Expand Down
4 changes: 2 additions & 2 deletions lib/thunder/pages/thunder_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class _ThunderState extends State<Thunder> {
}

Future<void> _navigateToPost(String link) async {
final postId = await getLemmyPostId(link);
final postId = await getLemmyPostId(context, link);
if (context.mounted && postId != null) {
LemmyApiV3 lemmy = LemmyClient.instance.lemmyApiV3;
Account? account = await fetchActiveProfileAccount();
Expand Down Expand Up @@ -333,7 +333,7 @@ class _ThunderState extends State<Thunder> {
}

Future<void> _navigateToComment(String link) async {
final commentId = await getLemmyCommentId(link);
final commentId = await getLemmyCommentId(context, link);
if (context.mounted && commentId != null) {
LemmyApiV3 lemmy = LemmyClient.instance.lemmyApiV3;
Account? account = await fetchActiveProfileAccount();
Expand Down
12 changes: 10 additions & 2 deletions lib/utils/instance.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/instances.dart';
import 'package:thunder/shared/pages/loading_page.dart';

String? fetchInstanceNameFromUrl(String? url) {
if (url == null) {
Expand Down Expand Up @@ -97,7 +99,7 @@ Future<String?> getLemmyUser(String text) async {
}

final RegExp _post = RegExp(r'^(https?:\/\/)(.*)\/post\/([0-9]*).*$');
Future<int?> getLemmyPostId(String text) async {
Future<int?> getLemmyPostId(BuildContext context, String text) async {
LemmyApiV3 lemmy = LemmyClient.instance.lemmyApiV3;

final RegExpMatch? postMatch = _post.firstMatch(text);
Expand All @@ -110,6 +112,9 @@ Future<int?> getLemmyPostId(String text) async {
} else {
// This is a post on another instance. Try to resolve it
try {
// Show the loading page while we resolve the post
showLoadingPage(context);

final ResolveObjectResponse resolveObjectResponse = await lemmy.run(ResolveObject(q: text));
return resolveObjectResponse.post?.post.id;
} catch (e) {
Expand All @@ -123,7 +128,7 @@ Future<int?> getLemmyPostId(String text) async {
}

final RegExp _comment = RegExp(r'^(https?:\/\/)(.*)\/comment\/([0-9]*).*$');
Future<int?> getLemmyCommentId(String text) async {
Future<int?> getLemmyCommentId(BuildContext context, String text) async {
LemmyApiV3 lemmy = LemmyClient.instance.lemmyApiV3;

final RegExpMatch? commentMatch = _comment.firstMatch(text);
Expand All @@ -136,6 +141,9 @@ Future<int?> getLemmyCommentId(String text) async {
} else {
// This is a comment on another instance. Try to resolve it
try {
// Show the loading page while we resolve the post
showLoadingPage(context);

final ResolveObjectResponse resolveObjectResponse = await lemmy.run(ResolveObject(q: text));
return resolveObjectResponse.comment?.comment.id;
} catch (e) {
Expand Down
10 changes: 8 additions & 2 deletions lib/utils/links.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,12 @@ void handleLink(BuildContext context, {required String url}) async {
}

// Try navigating to post
int? postId = await getLemmyPostId(url);
int? postId = await getLemmyPostId(context, url);
if (postId != null) {
try {
// Show the loading page while we fetch the post
if (context.mounted) showLoadingPage(context);

GetPostResponse post = await lemmy.run(GetPost(
id: postId,
auth: account?.jwt,
Expand All @@ -182,9 +185,12 @@ void handleLink(BuildContext context, {required String url}) async {
}

// Try navigating to comment
int? commentId = await getLemmyCommentId(url);
int? commentId = await getLemmyCommentId(context, url);
if (commentId != null) {
try {
// Show the loading page while we fetch the comment
if (context.mounted) showLoadingPage(context);

CommentResponse fullCommentView = await lemmy.run(GetComment(
id: commentId,
auth: account?.jwt,
Expand Down
Loading