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

Add support for new sort types as defaults #1298

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
8 changes: 4 additions & 4 deletions lib/community/widgets/community_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class FeedDrawerItems extends StatelessWidget {
isSelected: destination.listingType == feedState.postListingType,
onTap: () {
Navigator.of(context).pop();
navigateToFeedPage(context, feedType: FeedType.general, postListingType: destination.listingType, sortType: thunderState.defaultSortType);
navigateToFeedPage(context, feedType: FeedType.general, postListingType: destination.listingType, sortType: thunderState.sortTypeForInstance);
},
label: destination.label,
icon: destination.icon,
Expand Down Expand Up @@ -264,7 +264,7 @@ class FavoriteCommunities extends StatelessWidget {
context.read<FeedBloc>().add(
FeedFetchedEvent(
feedType: FeedType.community,
sortType: thunderState.defaultSortType,
sortType: thunderState.sortTypeForInstance,
communityId: community.id,
reset: true,
),
Expand Down Expand Up @@ -341,7 +341,7 @@ class SubscribedCommunities extends StatelessWidget {
context.read<FeedBloc>().add(
FeedFetchedEvent(
feedType: FeedType.community,
sortType: thunderState.defaultSortType,
sortType: thunderState.sortTypeForInstance,
communityId: community.id,
reset: true,
),
Expand Down Expand Up @@ -411,7 +411,7 @@ class ModeratedCommunities extends StatelessWidget {
context.read<FeedBloc>().add(
FeedFetchedEvent(
feedType: FeedType.community,
sortType: thunderState.defaultSortType,
sortType: thunderState.sortTypeForInstance,
communityId: community.id,
reset: true,
),
Expand Down
46 changes: 34 additions & 12 deletions lib/core/singletons/lemmy_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,50 @@ class LemmyClient {
);
}

bool supportsFeature(LemmyFeature feature) {
if (!_lemmySites.containsKey(instance.lemmyApiV3.host)) return false;
Version? get version {
if (!_lemmySites.containsKey(instance.lemmyApiV3.host)) return null;

// Parse the version
GetSiteResponse site = _lemmySites[instance.lemmyApiV3.host]!;
Version instanceVersion;
try {
instanceVersion = Version.parse(site.version);
return Version.parse(site.version);
} catch (e) {
return false;
return null;
}
}

bool supportsSortType(SortType? sortType) => switch (sortType) {
SortType.controversial => supportsFeature(LemmyFeature.sortTypeControversial),
SortType.scaled => supportsFeature(LemmyFeature.sortTypeScaled),
_ => true,
};

bool supportsCommentSortType(CommentSortType commentSortType) => switch (commentSortType) {
CommentSortType.controversial => supportsFeature(LemmyFeature.commentSortTypeControversial),
_ => true,
};

bool supportsFeature(LemmyFeature feature) {
if (version == null) return false;

// Check the feature and return whether it's supported in this version
return version! >= feature.minSupportedVersion;
}

static bool versionSupportsFeature(Version? version, LemmyFeature feature) {
if (version == null) return false;

if (version == maxVersion) return true;

// Check the feature and return whether it's supported in this version
return instanceVersion > feature.minSupportedVersion;
return version >= feature.minSupportedVersion;
}

/// This is a special Version object which simulates a "maximum possible version".
/// Note that it doesn't actually work as a max version in terms of comparison,
/// but it can be reference checked.
static Version maxVersion = Version(0, 0, 0, build: "max-version");

String generatePostUrl(int id) => 'https://${lemmyApiV3.host}/post/$id';

String generateCommentUrl(int id) => 'https://${lemmyApiV3.host}/comment/$id';
Expand Down Expand Up @@ -81,9 +109,3 @@ enum LemmyFeature {
preRelease: List.from(preRelease),
);
}

enum IncludeVersionSpecificFeature {
never,
ifSupported,
always,
}
4 changes: 2 additions & 2 deletions lib/feed/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Future<void> navigateToFeedPage(
FeedFetchedEvent(
feedType: feedType,
postListingType: postListingType,
sortType: sortType ?? thunderBloc.state.defaultSortType,
sortType: sortType ?? thunderBloc.state.sortTypeForInstance,
communityId: communityId,
communityName: communityName,
userId: userId,
Expand Down Expand Up @@ -118,7 +118,7 @@ Future<void> navigateToFeedPage(
child: Material(
child: FeedPage(
feedType: feedType,
sortType: sortType ?? thunderBloc.state.defaultSortType,
sortType: sortType ?? thunderBloc.state.sortTypeForInstance,
communityName: communityName,
communityId: communityId,
userId: userId,
Expand Down
2 changes: 1 addition & 1 deletion lib/feed/view/feed_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ class _FeedViewState extends State<FeedView> {
if (!canPop && (desiredListingType != currentListingType || communityMode)) {
feedBloc.add(
FeedFetchedEvent(
sortType: thunderBloc.state.defaultSortType,
sortType: thunderBloc.state.sortTypeForInstance,
reset: true,
postListingType: desiredListingType,
feedType: FeedType.general,
Expand Down
4 changes: 2 additions & 2 deletions lib/feed/widgets/feed_fab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:swipeable_page_route/swipeable_page_route.dart';

import 'package:thunder/account/bloc/account_bloc.dart';
import 'package:thunder/community/pages/create_post_page.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/enums/fab_action.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/feed/bloc/feed_bloc.dart';
import 'package:thunder/feed/utils/utils.dart';
import 'package:thunder/feed/view/feed_page.dart';
Expand Down Expand Up @@ -287,6 +286,7 @@ class FeedFAB extends StatelessWidget {
title: l10n.sortOptions,
onSelect: (selected) async => context.read<FeedBloc>().add(FeedChangeSortTypeEvent(selected.payload)),
previouslySelected: context.read<FeedBloc>().state.sortType,
minimumVersion: LemmyClient.instance.version,
),
);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/feed/widgets/feed_page_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:thunder/community/bloc/anonymous_subscriptions_bloc.dart';
import 'package:thunder/community/bloc/community_bloc.dart';
import 'package:thunder/community/enums/community_action.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/feed/bloc/feed_bloc.dart';
import 'package:thunder/feed/utils/community.dart';
import 'package:thunder/feed/utils/community_share.dart';
Expand Down Expand Up @@ -167,6 +168,7 @@ class FeedAppBarCommunityActions extends StatelessWidget {
title: l10n.sortOptions,
onSelect: (selected) async => feedBloc.add(FeedChangeSortTypeEvent(selected.payload)),
previouslySelected: feedBloc.state.sortType,
minimumVersion: LemmyClient.instance.version,
),
);
},
Expand Down Expand Up @@ -262,6 +264,7 @@ class FeedAppBarUserActions extends StatelessWidget {
title: l10n.sortOptions,
onSelect: (selected) async => feedBloc.add(FeedChangeSortTypeEvent(selected.payload)),
previouslySelected: feedBloc.state.sortType,
minimumVersion: LemmyClient.instance.version,
),
);
},
Expand Down Expand Up @@ -317,6 +320,7 @@ class FeedAppBarGeneralActions extends StatelessWidget {
title: l10n.sortOptions,
onSelect: (selected) async => feedBloc.add(FeedChangeSortTypeEvent(selected.payload)),
previouslySelected: feedBloc.state.sortType,
minimumVersion: LemmyClient.instance.version,
),
);
},
Expand Down
2 changes: 2 additions & 0 deletions lib/post/bloc/post_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class PostBloc extends Bloc<PostEvent, PostState> {

SharedPreferences prefs = await SharedPreferences.getInstance();
CommentSortType defaultSortType = CommentSortType.values.byName(prefs.getString(LocalSettings.defaultCommentSortType.name)?.toLowerCase() ?? DEFAULT_COMMENT_SORT_TYPE.name);
defaultSortType = LemmyClient.instance.supportsCommentSortType(defaultSortType) ? defaultSortType : DEFAULT_COMMENT_SORT_TYPE;

Account? account = await fetchActiveProfileAccount();

Expand Down Expand Up @@ -226,6 +227,7 @@ class PostBloc extends Bloc<PostEvent, PostState> {

SharedPreferences prefs = await SharedPreferences.getInstance();
CommentSortType defaultSortType = CommentSortType.values.byName(prefs.getString(LocalSettings.defaultCommentSortType.name)?.toLowerCase() ?? DEFAULT_COMMENT_SORT_TYPE.name);
defaultSortType = LemmyClient.instance.supportsCommentSortType(defaultSortType) ? defaultSortType : DEFAULT_COMMENT_SORT_TYPE;

CommentSortType sortType = event.sortType ?? (state.sortType ?? defaultSortType);

Expand Down
4 changes: 2 additions & 2 deletions lib/post/pages/post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ class _PostPageState extends State<PostPage> {
if (previousState.sortType != currentState.sortType) {
setState(() {
sortType = currentState.sortType;
final sortTypeItem = CommentSortPicker.getCommentSortTypeItems(includeVersionSpecificFeature: IncludeVersionSpecificFeature.always)
.firstWhere((sortTypeItem) => sortTypeItem.payload == currentState.sortType);
final sortTypeItem = CommentSortPicker.getCommentSortTypeItems(minimumVersion: LemmyClient.maxVersion).firstWhere((sortTypeItem) => sortTypeItem.payload == currentState.sortType);
sortTypeIcon = sortTypeItem.icon;
sortTypeLabel = sortTypeItem.label;
});
Expand Down Expand Up @@ -567,6 +566,7 @@ class _PostPageState extends State<PostPage> {
//Navigator.of(context).pop();
},
previouslySelected: sortType,
minimumVersion: LemmyClient.instance.version,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions lib/search/pages/search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@ class _SearchPageState extends State<SearchPage> with AutomaticKeepAliveClientMi
_doSearch();
},
previouslySelected: sortType,
minimumVersion: LemmyClient.instance.version,
),
);
}
Expand Down
22 changes: 10 additions & 12 deletions lib/settings/pages/general_settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import 'package:thunder/utils/bottom_sheet_list_picker.dart';
import 'package:thunder/utils/constants.dart';
import 'package:thunder/utils/language/language.dart';
import 'package:thunder/utils/links.dart';
import 'package:version/version.dart';

class GeneralSettingsPage extends StatefulWidget {
final LocalSettings? settingToHighlight;
Expand Down Expand Up @@ -329,12 +330,15 @@ class _GeneralSettingsPageState extends State<GeneralSettingsPage> with SingleTi
child: ListOption(
description: l10n.defaultFeedSortType,
value: ListPickerItem(label: defaultSortType.value, icon: Icons.local_fire_department_rounded, payload: defaultSortType),
options: [...SortPicker.getDefaultSortTypeItems(includeVersionSpecificFeature: IncludeVersionSpecificFeature.never), ...topSortTypeItems],
options: [
...SortPicker.getDefaultSortTypeItems(minimumVersion: Version(0, 19, 0, preRelease: ["rc", "1"])),
...topSortTypeItems
],
icon: Icons.sort_rounded,
onChanged: (_) async {},
isBottomModalScrollControlled: true,
customListPicker: SortPicker(
includeVersionSpecificFeature: IncludeVersionSpecificFeature.never,
minimumVersion: Version(0, 19, 0, preRelease: ["rc", "1"]),
title: l10n.defaultFeedSortType,
onSelect: (value) async {
setPreferences(LocalSettings.defaultFeedSortType, value.payload.name);
Expand All @@ -358,11 +362,11 @@ class _GeneralSettingsPageState extends State<GeneralSettingsPage> with SingleTi
child: ListOption(
description: l10n.defaultCommentSortType,
value: ListPickerItem(label: defaultCommentSortType.value, icon: Icons.local_fire_department_rounded, payload: defaultCommentSortType),
options: CommentSortPicker.getCommentSortTypeItems(includeVersionSpecificFeature: IncludeVersionSpecificFeature.never),
options: CommentSortPicker.getCommentSortTypeItems(minimumVersion: Version(0, 19, 0, preRelease: ["rc", "1"])),
icon: Icons.comment_bank_rounded,
onChanged: (_) async {},
customListPicker: CommentSortPicker(
includeVersionSpecificFeature: IncludeVersionSpecificFeature.never,
minimumVersion: Version(0, 19, 0, preRelease: ["rc", "1"]),
title: l10n.commentSortType,
onSelect: (value) async {
setPreferences(LocalSettings.defaultCommentSortType, value.payload.name);
Expand All @@ -371,16 +375,10 @@ class _GeneralSettingsPageState extends State<GeneralSettingsPage> with SingleTi
),
valueDisplay: Row(
children: [
Icon(
CommentSortPicker.getCommentSortTypeItems(includeVersionSpecificFeature: IncludeVersionSpecificFeature.always)
.firstWhere((sortTypeItem) => sortTypeItem.payload == defaultCommentSortType)
.icon,
size: 13),
Icon(CommentSortPicker.getCommentSortTypeItems(minimumVersion: LemmyClient.maxVersion).firstWhere((sortTypeItem) => sortTypeItem.payload == defaultCommentSortType).icon, size: 13),
const SizedBox(width: 4),
Text(
CommentSortPicker.getCommentSortTypeItems(includeVersionSpecificFeature: IncludeVersionSpecificFeature.always)
.firstWhere((sortTypeItem) => sortTypeItem.payload == defaultCommentSortType)
.label,
CommentSortPicker.getCommentSortTypeItems(minimumVersion: LemmyClient.maxVersion).firstWhere((sortTypeItem) => sortTypeItem.payload == defaultCommentSortType).label,
style: theme.textTheme.titleSmall,
),
],
Expand Down
36 changes: 17 additions & 19 deletions lib/shared/comment_sort_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import 'package:thunder/shared/picker_item.dart';
import 'package:thunder/utils/bottom_sheet_list_picker.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:thunder/utils/global_context.dart';
import 'package:version/version.dart';

/// Create a picker which allows selecting a valid comment sort type.
/// Specify a [minimumVersion] to determine which sort types will be displayed.
/// Pass `null` to NOT show any version-specific types (e.g., Scaled).
/// Pass [LemmyClient.maxVersion] to show ALL types.
class CommentSortPicker extends BottomSheetListPicker<CommentSortType> {
final IncludeVersionSpecificFeature includeVersionSpecificFeature;
final Version? minimumVersion;

static List<ListPickerItem<CommentSortType>> getCommentSortTypeItems({IncludeVersionSpecificFeature includeVersionSpecificFeature = IncludeVersionSpecificFeature.ifSupported}) => [
static List<ListPickerItem<CommentSortType>> getCommentSortTypeItems({required Version? minimumVersion}) => [
ListPickerItem(
payload: CommentSortType.hot,
icon: Icons.local_fire_department,
Expand All @@ -20,8 +25,7 @@ class CommentSortPicker extends BottomSheetListPicker<CommentSortType> {
icon: Icons.military_tech,
label: AppLocalizations.of(GlobalContext.context)!.top,
),
if (includeVersionSpecificFeature == IncludeVersionSpecificFeature.always ||
(includeVersionSpecificFeature == IncludeVersionSpecificFeature.ifSupported && LemmyClient.instance.supportsFeature(LemmyFeature.commentSortTypeControversial)))
if (LemmyClient.versionSupportsFeature(minimumVersion, LemmyFeature.commentSortTypeControversial))
ListPickerItem(
payload: CommentSortType.controversial,
icon: Icons.warning_rounded,
Expand All @@ -37,22 +41,16 @@ class CommentSortPicker extends BottomSheetListPicker<CommentSortType> {
icon: Icons.access_time_outlined,
label: AppLocalizations.of(GlobalContext.context)!.old,
),
//
// ListPickerItem(
// payload: CommentSortType.chat,
// icon: Icons.chat,
// label: 'Chat',
// ),
];

CommentSortPicker(
{super.key,
required super.onSelect,
required super.title,
List<ListPickerItem<CommentSortType>>? items,
super.previouslySelected,
this.includeVersionSpecificFeature = IncludeVersionSpecificFeature.ifSupported})
: super(items: items ?? CommentSortPicker.getCommentSortTypeItems(includeVersionSpecificFeature: includeVersionSpecificFeature));
CommentSortPicker({
super.key,
required super.onSelect,
required super.title,
List<ListPickerItem<CommentSortType>>? items,
super.previouslySelected,
required this.minimumVersion,
}) : super(items: items ?? CommentSortPicker.getCommentSortTypeItems(minimumVersion: minimumVersion));

@override
State<StatefulWidget> createState() => _SortPickerState();
Expand Down Expand Up @@ -96,7 +94,7 @@ class _SortPickerState extends State<CommentSortPicker> {
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
children: [
..._generateList(CommentSortPicker.getCommentSortTypeItems(includeVersionSpecificFeature: widget.includeVersionSpecificFeature), theme),
..._generateList(CommentSortPicker.getCommentSortTypeItems(minimumVersion: widget.minimumVersion), theme),
],
),
const SizedBox(height: 16.0),
Expand Down
Loading
Loading