Skip to content

Commit

Permalink
Add instance page to long press menu (#824)
Browse files Browse the repository at this point in the history
* Add instance page

* Use new AppLocalizations style

* Remove strings from es

* Update gesture setting for instance page

* Remove alternateSiteName

* Remove unnecessary strings
  • Loading branch information
micahmo authored Oct 19, 2023
1 parent 12564cb commit b199d28
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 41 deletions.
11 changes: 11 additions & 0 deletions lib/community/utils/post_card_action_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import 'package:thunder/shared/advanced_share_sheet.dart';
import 'package:thunder/shared/picker_item.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/utils/navigate_instance.dart';
import 'package:thunder/utils/navigate_user.dart';
import 'package:lemmy_api_client/v3.dart';

Expand All @@ -28,6 +30,7 @@ import 'package:thunder/utils/global_context.dart';
enum PostCardAction {
visitProfile,
visitCommunity,
visitInstance,
sharePost,
shareMedia,
shareLink,
Expand Down Expand Up @@ -78,6 +81,11 @@ final List<ExtendedPostCardActions> postCardActionItems = [
icon: Icons.person_search_rounded,
label: AppLocalizations.of(GlobalContext.context)!.visitUserProfile,
),
ExtendedPostCardActions(
postCardAction: PostCardAction.visitInstance,
icon: Icons.language,
label: AppLocalizations.of(GlobalContext.context)!.visitInstance,
),
ExtendedPostCardActions(
postCardAction: PostCardAction.sharePost,
icon: Icons.share_rounded,
Expand Down Expand Up @@ -230,6 +238,9 @@ void onSelected(BuildContext context, PostCardAction postCardAction, PostViewMed
case PostCardAction.visitProfile:
navigateToUserPage(context, userId: postViewMedia.postView.post.creatorId);
break;
case PostCardAction.visitInstance:
navigateToInstancePage(context, instanceHost: fetchInstanceNameFromUrl(postViewMedia.postView.community.actorId)!);
break;
case PostCardAction.sharePost:
Share.share(postViewMedia.postView.post.apId);
break;
Expand Down
44 changes: 3 additions & 41 deletions lib/community/widgets/community_sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:thunder/core/enums/local_settings.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/core/singletons/preferences.dart';
import 'package:thunder/feed/bloc/feed_bloc.dart';
import 'package:thunder/instance/instance_view.dart';
import 'package:thunder/shared/common_markdown_body.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/shared/user_avatar.dart';
Expand Down Expand Up @@ -133,47 +134,8 @@ class _CommunitySidebarState extends State<CommunitySidebar> {
const SidebarSectionHeader(value: "Host Instance"),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Column(
children: [
Row(
children: [
CircleAvatar(
backgroundColor: widget.fullCommunityView!.site?.icon != null ? Colors.transparent : theme.colorScheme.secondaryContainer,
foregroundImage: widget.fullCommunityView!.site?.icon != null ? CachedNetworkImageProvider(widget.fullCommunityView!.site!.icon!) : null,
maxRadius: 24,
child: widget.fullCommunityView!.site?.icon == null
? Text(
widget.fullCommunityView!.moderators.first.moderator!.name[0].toUpperCase(),
semanticsLabel: '',
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
)
: null,
),
const SizedBox(width: 16.0),
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
widget.fullCommunityView!.site?.name ?? '',
overflow: TextOverflow.ellipsis,
maxLines: 1,
style: theme.textTheme.headlineSmall?.copyWith(fontWeight: FontWeight.w600),
),
Flexible(
child: Text(
widget.fullCommunityView!.site?.description ?? '',
style: theme.textTheme.bodyMedium,
),
),
],
),
],
),
const Divider(),
CommonMarkdownBody(body: widget.fullCommunityView!.site?.sidebar ?? ''),
],
child: InstanceView(
site: widget.fullCommunityView!.site!,
),
),
],
Expand Down
1 change: 1 addition & 0 deletions lib/community/widgets/post_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ class _PostCardState extends State<PostCard> {
context,
widget.postViewMedia,
actionsToInclude: [
PostCardAction.visitInstance,
PostCardAction.visitProfile,
PostCardAction.visitCommunity,
PostCardAction.blockCommunity,
Expand Down
1 change: 1 addition & 0 deletions lib/community/widgets/post_card_view_comfortable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class PostCardViewComfortable extends StatelessWidget {
context,
postViewMedia,
actionsToInclude: [
PostCardAction.visitInstance,
PostCardAction.visitProfile,
PostCardAction.visitCommunity,
PostCardAction.blockCommunity,
Expand Down
59 changes: 59 additions & 0 deletions lib/instance/instance_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:flutter/material.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:thunder/instance/instance_view.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/utils/links.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class InstancePage extends StatefulWidget {
final Site site;

const InstancePage({
super.key,
required this.site,
});

@override
State<InstancePage> createState() => _InstancePageState();
}

class _InstancePageState extends State<InstancePage> {
@override
Widget build(BuildContext context) {
final AppLocalizations l10n = AppLocalizations.of(context)!;
final ThemeData theme = Theme.of(context);

return Container(
color: theme.colorScheme.background,
child: SafeArea(
top: false,
child: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
title: Text(fetchInstanceNameFromUrl(widget.site.actorId) ?? ''),
actions: [
IconButton(
tooltip: l10n.openInBrowser,
onPressed: () => openLink(context, url: widget.site.actorId),
icon: Icon(
Icons.open_in_browser_rounded,
semanticLabel: l10n.openInBrowser,
),
),
],
),
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.all(20),
child: Material(
child: InstanceView(site: widget.site),
),
),
),
],
),
),
);
}
}
64 changes: 64 additions & 0 deletions lib/instance/instance_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:thunder/shared/common_markdown_body.dart';

class InstanceView extends StatelessWidget {
final Site site;

const InstanceView({super.key, required this.site});

@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);

return Column(
children: [
Row(
children: [
CircleAvatar(
backgroundColor: site.icon != null ? Colors.transparent : theme.colorScheme.secondaryContainer,
foregroundImage: site.icon != null ? CachedNetworkImageProvider(site.icon!) : null,
maxRadius: 24,
child: site.icon == null
? Text(
site.name[0],
semanticsLabel: '',
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
)
: null,
),
const SizedBox(width: 16.0),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
site.name,
overflow: TextOverflow.ellipsis,
maxLines: 1,
style: theme.textTheme.headlineSmall?.copyWith(fontWeight: FontWeight.w600),
),
Row(
children: [
Flexible(
child: Text(
site.description ?? '',
style: theme.textTheme.bodyMedium,
),
),
],
),
],
),
),
],
),
const Divider(),
CommonMarkdownBody(body: site.sidebar ?? ''),
],
);
}
}
2 changes: 2 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@
"@comment": {},
"comments": "Comments",
"@comments": {},
"visitInstance": "Visit Instance",
"openInBrowser": "Open in Browser",
"postLocked": "Post locked. No replies allowed.",
"@postLocked": {},
"replyingTo": "Replying to {author}",
Expand Down
38 changes: 38 additions & 0 deletions lib/utils/navigate_instance.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:swipeable_page_route/swipeable_page_route.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/instance/instance_page.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/utils/swipe.dart';

Future<void> navigateToInstancePage(BuildContext context, {required String instanceHost}) async {
ThunderBloc thunderBloc = context.read<ThunderBloc>();
AuthBloc authBloc = context.read<AuthBloc>();

final bool reduceAnimations = thunderBloc.state.reduceAnimations;

FullSiteView? fullSiteView;
try {
fullSiteView = await LemmyApiV3(instanceHost).run(const GetSite()).timeout(const Duration(seconds: 5));
} catch (e) {}

if (fullSiteView?.siteView?.site != null && context.mounted) {
Navigator.of(context).push(
SwipeablePageRoute(
transitionDuration: reduceAnimations ? const Duration(milliseconds: 100) : null,
backGestureDetectionWidth: 45,
canOnlySwipeFromEdge: !thunderBloc.state.enableFullScreenSwipeNavigationGesture,
builder: (context) => MultiBlocProvider(
providers: [
BlocProvider.value(value: thunderBloc),
],
child: InstancePage(
site: fullSiteView!.siteView!.site,
),
),
),
);
}
}

0 comments on commit b199d28

Please sign in to comment.