Skip to content

Commit

Permalink
Add sort to instance explorer (#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
micahmo committed May 24, 2024
1 parent 3e5c666 commit a277c92
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
16 changes: 8 additions & 8 deletions lib/instance/cubit/instance_page_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InstancePageCubit extends Cubit<InstancePageState> {
resolutionInstance: resolutionInstance,
));

Future<void> loadCommunities({int? page}) async {
Future<void> loadCommunities({int? page, required SortType sortType}) async {
if (page == 1) emit(state.copyWith(status: InstancePageStatus.loading));

try {
Expand All @@ -33,7 +33,7 @@ class InstancePageCubit extends Cubit<InstancePageState> {
q: '',
page: page ?? 1,
limit: _pageLimit,
sort: SortType.topAll,
sort: sortType,
listingType: ListingType.local,
type: SearchType.communities,
));
Expand All @@ -48,7 +48,7 @@ class InstancePageCubit extends Cubit<InstancePageState> {
}
}

Future<void> loadUsers({int? page}) async {
Future<void> loadUsers({int? page, required SortType sortType}) async {
if (page == 1) emit(state.copyWith(status: InstancePageStatus.loading));

try {
Expand All @@ -60,7 +60,7 @@ class InstancePageCubit extends Cubit<InstancePageState> {
q: '',
page: page ?? 1,
limit: _pageLimit,
sort: SortType.topAll,
sort: sortType,
listingType: ListingType.local,
type: SearchType.users,
));
Expand All @@ -75,7 +75,7 @@ class InstancePageCubit extends Cubit<InstancePageState> {
}
}

Future<void> loadPosts({int? page}) async {
Future<void> loadPosts({int? page, required SortType sortType}) async {
if (page == 1) emit(state.copyWith(status: InstancePageStatus.loading));

try {
Expand All @@ -87,7 +87,7 @@ class InstancePageCubit extends Cubit<InstancePageState> {
q: '',
page: page ?? 1,
limit: _pageLimit,
sort: SortType.topAll,
sort: sortType,
listingType: ListingType.local,
type: SearchType.posts,
));
Expand All @@ -102,7 +102,7 @@ class InstancePageCubit extends Cubit<InstancePageState> {
}
}

Future<void> loadComments({int? page}) async {
Future<void> loadComments({int? page, required SortType sortType}) async {
if (page == 1) emit(state.copyWith(status: InstancePageStatus.loading));

try {
Expand All @@ -114,7 +114,7 @@ class InstancePageCubit extends Cubit<InstancePageState> {
q: '',
page: page ?? 1,
limit: _pageLimit,
sort: SortType.topAll,
sort: sortType,
listingType: ListingType.local,
type: SearchType.comments,
));
Expand Down
61 changes: 46 additions & 15 deletions lib/instance/pages/instance_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:thunder/search/widgets/search_action_chip.dart';
import 'package:thunder/shared/error_message.dart';
import 'package:thunder/shared/persistent_header.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/shared/sort_picker.dart';
import 'package:thunder/shared/thunder_popup_menu_item.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/user/widgets/user_list_entry.dart';
Expand Down Expand Up @@ -55,6 +56,7 @@ class _InstancePageState extends State<InstancePage> {
// Use the existing SearchType enum to represent what we're showing in the instance page
// with SearchType.all representing the about page
SearchType viewType = SearchType.all;
SortType sortType = SortType.topAll;

/// Context for [_onScroll] to use
BuildContext? buildContext;
Expand Down Expand Up @@ -148,14 +150,37 @@ class _InstancePageState extends State<InstancePage> {
semanticLabel: isBlocked! ? l10n.unblockInstance : l10n.blockInstance,
),
),
IconButton(
tooltip: l10n.openInBrowser,
onPressed: () => handleLink(context, url: widget.getSiteResponse.siteView.site.actorId),
icon: Icon(
Icons.open_in_browser_rounded,
semanticLabel: l10n.openInBrowser,
if (viewType == SearchType.all)
IconButton(
tooltip: l10n.openInBrowser,
onPressed: () => handleLink(context, url: widget.getSiteResponse.siteView.site.actorId),
icon: Icon(
Icons.open_in_browser_rounded,
semanticLabel: l10n.openInBrowser,
),
),
if (viewType != SearchType.all)
IconButton(
icon: Icon(Icons.sort, semanticLabel: l10n.sortBy),
onPressed: () {
HapticFeedback.mediumImpact();

showModalBottomSheet<void>(
showDragHandle: true,
context: context,
isScrollControlled: true,
builder: (builderContext) => SortPicker(
title: l10n.sortOptions,
onSelect: (selected) async {
sortType = selected.payload;
_doLoad(context);
},
previouslySelected: sortType,
minimumVersion: LemmyClient.instance.version,
),
);
},
),
),
PopupMenuButton(
itemBuilder: (context) => [
ThunderPopupMenuItem(
Expand All @@ -171,6 +196,12 @@ class _InstancePageState extends State<InstancePage> {
icon: Icons.shield_rounded,
title: l10n.modlog,
),
if (viewType != SearchType.all)
ThunderPopupMenuItem(
onTap: () => handleLink(context, url: widget.getSiteResponse.siteView.site.actorId),
icon: Icons.open_in_browser_rounded,
title: l10n.openInBrowser,
),
],
),
],
Expand Down Expand Up @@ -199,7 +230,7 @@ class _InstancePageState extends State<InstancePage> {
],
onPressed: () async {
viewType = SearchType.communities;
await context.read<InstancePageCubit>().loadCommunities(page: 1);
await context.read<InstancePageCubit>().loadCommunities(page: 1, sortType: sortType);
WidgetsBinding.instance.addPostFrameCallback((_) => _scrollController.jumpTo(0));
},
),
Expand All @@ -214,7 +245,7 @@ class _InstancePageState extends State<InstancePage> {
],
onPressed: () async {
viewType = SearchType.users;
await context.read<InstancePageCubit>().loadUsers(page: 1);
await context.read<InstancePageCubit>().loadUsers(page: 1, sortType: sortType);
WidgetsBinding.instance.addPostFrameCallback((_) => _scrollController.jumpTo(0));
},
),
Expand All @@ -227,7 +258,7 @@ class _InstancePageState extends State<InstancePage> {
],
onPressed: () async {
viewType = SearchType.posts;
await context.read<InstancePageCubit>().loadPosts(page: 1);
await context.read<InstancePageCubit>().loadPosts(page: 1, sortType: sortType);
WidgetsBinding.instance.addPostFrameCallback((_) => _scrollController.jumpTo(0));
},
),
Expand All @@ -239,7 +270,7 @@ class _InstancePageState extends State<InstancePage> {
],
onPressed: () async {
viewType = SearchType.comments;
await context.read<InstancePageCubit>().loadComments(page: 1);
await context.read<InstancePageCubit>().loadComments(page: 1, sortType: sortType);
WidgetsBinding.instance.addPostFrameCallback((_) => _scrollController.jumpTo(0));
},
),
Expand Down Expand Up @@ -362,16 +393,16 @@ class _InstancePageState extends State<InstancePage> {

switch (viewType) {
case SearchType.communities:
await instancePageCubit.loadCommunities(page: page ?? 1);
await instancePageCubit.loadCommunities(page: page ?? 1, sortType: sortType);
break;
case SearchType.users:
await instancePageCubit.loadUsers(page: page ?? 1);
await instancePageCubit.loadUsers(page: page ?? 1, sortType: sortType);
break;
case SearchType.posts:
await instancePageCubit.loadPosts(page: page ?? 1);
await instancePageCubit.loadPosts(page: page ?? 1, sortType: sortType);
break;
case SearchType.comments:
await instancePageCubit.loadComments(page: page ?? 1);
await instancePageCubit.loadComments(page: page ?? 1, sortType: sortType);
break;
default:
break;
Expand Down

0 comments on commit a277c92

Please sign in to comment.