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

Support new sort types #829

Merged
merged 9 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Added support for lemmy 0.19.x authentication - contribution from @micahmo
- Added support for accessibility profiles in settings - contribution from @micahmo
- Added option to enable/disable full screen navigation swipe gesture to go back (applies when LTR gestures are disabled)
- Support new scaled and controversial sort types - contribution from @micahmo

### Changed
- Collapsed comments are easier to expand - contribution from @micahmo
Expand Down
51 changes: 51 additions & 0 deletions lib/core/singletons/lemmy_client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'package:lemmy_api_client/v3.dart';
import 'package:thunder/account/models/account.dart';
import 'package:thunder/core/auth/helpers/fetch_account.dart';
import 'package:version/version.dart';

class LemmyClient {
LemmyApiV3 lemmyApiV3 = const LemmyApiV3('');
Expand All @@ -7,9 +10,57 @@ class LemmyClient {

void changeBaseUrl(String baseUrl) {
lemmyApiV3 = LemmyApiV3(baseUrl);
_populateSiteInfo(); // Do NOT await this. Let it populate in the background.
}

static final LemmyClient _instance = LemmyClient._initialize();

static LemmyClient get instance => _instance;

Future<void> _populateSiteInfo() async {
if (!_lemmySites.containsKey(instance.lemmyApiV3.host)) {
// Retrieve the site so we can look up metadata about it later
Account? account = await fetchActiveProfileAccount();

_lemmySites[instance.lemmyApiV3.host] = await instance.lemmyApiV3.run(
GetSite(
auth: account?.jwt,
),
);
}
}

bool supportsFeature(LemmyFeature feature) {
if (_lemmySites.containsKey(instance.lemmyApiV3.host)) {
// Parse the version
FullSiteView site = _lemmySites[instance.lemmyApiV3.host]!;
Version version;
try {
version = Version.parse(site.version);
} catch (e) {
return false;
}

// Check the feature and return whether it's supported in this version
return switch (feature) {
LemmyFeature.sortTypeControversial || LemmyFeature.sortTypeScaled || LemmyFeature.commentSortTypeControversial => version >= Version(0, 19, 0, preRelease: ["rc", "1"]),
};
}

return false;
}

static final Map<String, FullSiteView> _lemmySites = <String, FullSiteView>{};
}

enum LemmyFeature {
sortTypeControversial,
sortTypeScaled,
commentSortTypeControversial,
}

enum IncludeVersionSpecificFeature {
never,
ifSupported,
always,
}
1 change: 1 addition & 0 deletions lib/feed/widgets/feed_fab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class FeedFAB extends StatelessWidget {
showModalBottomSheet<void>(
showDragHandle: true,
context: context,
isScrollControlled: true,
builder: (builderContext) => SortPicker(
title: l10n.sortOptions,
onSelect: (selected) => context.read<FeedBloc>().add(FeedChangeSortTypeEvent(selected.payload)),
Expand Down
1 change: 1 addition & 0 deletions lib/feed/widgets/feed_page_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class FeedPageAppBar extends StatelessWidget {
showModalBottomSheet<void>(
showDragHandle: true,
context: context,
isScrollControlled: true,
builder: (builderContext) => SortPicker(
title: l10n.sortOptions,
onSelect: (selected) => feedBloc.add(FeedChangeSortTypeEvent(selected.payload)),
Expand Down
2 changes: 2 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@
"shareLink": "Share Link",
"upvote": "Upvote",
"downvote": "Downvote",
"scaled": "Scaled",
"controversial": "Controversial",
"edit": "Edit",
"restore": "Restore",
"delete": "Delete",
Expand Down
2 changes: 2 additions & 0 deletions lib/l10n/app_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@
"shareLink": "Share Link",
"upvote": "Upvote",
"downvote": "Downvote",
"scaled": "Scaled",
"controversial": "Controversial",
"edit": "Edit",
"restore": "Restore",
"delete": "Delete",
Expand Down
2 changes: 2 additions & 0 deletions lib/l10n/app_fi.arb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@
"shareLink": "Share Link",
"upvote": "Upvote",
"downvote": "Downvote",
"scaled": "Scaled",
"controversial": "Controversial",
"edit": "Edit",
"restore": "Restore",
"delete": "Delete",
Expand Down
2 changes: 2 additions & 0 deletions lib/l10n/app_pl.arb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@
"edit": "Edit",
"restore": "Restore",
"delete": "Delete",
"scaled": "Scaled",
"controversial": "Controversial",
"inReplyTo": "In reply to {post} in {community}",
"xUpvotes": "{x} upvotes",
"xDownvotes": "{x} downvotes",
Expand Down
2 changes: 2 additions & 0 deletions lib/l10n/app_sv.arb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@
"shareLink": "Share Link",
"upvote": "Upvote",
"downvote": "Downvote",
"scaled": "Scaled",
"controversial": "Controversial",
"edit": "Edit",
"restore": "Restore",
"delete": "Delete",
Expand Down
4 changes: 3 additions & 1 deletion lib/post/pages/post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/enums/fab_action.dart';
import 'package:thunder/core/enums/local_settings.dart';
import 'package:thunder/core/models/post_view_media.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/core/singletons/preferences.dart';
import 'package:thunder/post/bloc/post_bloc.dart';
import 'package:thunder/post/pages/post_page_success.dart';
Expand Down Expand Up @@ -122,7 +123,8 @@ class _PostPageState extends State<PostPage> {
if (previousState.sortType != currentState.sortType) {
setState(() {
sortType = currentState.sortType;
final sortTypeItem = commentSortTypeItems.firstWhere((sortTypeItem) => sortTypeItem.payload == currentState.sortType);
final sortTypeItem = CommentSortPicker.getCommentSortTypeItems(includeVersionSpecificFeature: IncludeVersionSpecificFeature.always)
.firstWhere((sortTypeItem) => sortTypeItem.payload == currentState.sortType);
sortTypeIcon = sortTypeItem.icon;
sortTypeLabel = sortTypeItem.label;
});
Expand Down
17 changes: 13 additions & 4 deletions lib/settings/pages/general_settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:lemmy_api_client/v3.dart';

import 'package:thunder/core/enums/local_settings.dart';
import 'package:thunder/core/enums/nested_comment_indicator.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/core/singletons/preferences.dart';
import 'package:thunder/settings/widgets/list_option.dart';
import 'package:thunder/settings/widgets/toggle_option.dart';
Expand Down Expand Up @@ -405,11 +406,12 @@ class _GeneralSettingsPageState extends State<GeneralSettingsPage> with SingleTi
ListOption(
description: LocalSettings.defaultFeedSortType.label,
value: ListPickerItem(label: defaultSortType.value, icon: Icons.local_fire_department_rounded, payload: defaultSortType),
options: allSortTypeItems,
options: [...SortPicker.getDefaultSortTypeItems(includeVersionSpecificFeature: IncludeVersionSpecificFeature.never), ...topSortTypeItems],
icon: Icons.sort_rounded,
onChanged: (_) {},
isBottomModalScrollControlled: true,
customListPicker: SortPicker(
includeVersionSpecificFeature: IncludeVersionSpecificFeature.never,
title: LocalSettings.defaultFeedSortType.label,
onSelect: (value) {
setPreferences(LocalSettings.defaultFeedSortType, value.payload.name);
Expand Down Expand Up @@ -616,10 +618,11 @@ class _GeneralSettingsPageState extends State<GeneralSettingsPage> with SingleTi
ListOption(
description: LocalSettings.defaultCommentSortType.label,
value: ListPickerItem(label: defaultCommentSortType.value, icon: Icons.local_fire_department_rounded, payload: defaultCommentSortType),
options: commentSortTypeItems,
options: CommentSortPicker.getCommentSortTypeItems(includeVersionSpecificFeature: IncludeVersionSpecificFeature.never),
icon: Icons.comment_bank_rounded,
onChanged: (_) {},
customListPicker: CommentSortPicker(
includeVersionSpecificFeature: IncludeVersionSpecificFeature.never,
title: 'Comment Sort Type',
onSelect: (value) {
setPreferences(LocalSettings.defaultCommentSortType, value.payload.name);
Expand All @@ -628,10 +631,16 @@ class _GeneralSettingsPageState extends State<GeneralSettingsPage> with SingleTi
),
valueDisplay: Row(
children: [
Icon(commentSortTypeItems.firstWhere((sortTypeItem) => sortTypeItem.payload == defaultCommentSortType).icon, size: 13),
Icon(
CommentSortPicker.getCommentSortTypeItems(includeVersionSpecificFeature: IncludeVersionSpecificFeature.always)
.firstWhere((sortTypeItem) => sortTypeItem.payload == defaultCommentSortType)
.icon,
size: 13),
const SizedBox(width: 4),
Text(
commentSortTypeItems.firstWhere((sortTypeItem) => sortTypeItem.payload == defaultCommentSortType).label,
CommentSortPicker.getCommentSortTypeItems(includeVersionSpecificFeature: IncludeVersionSpecificFeature.always)
.firstWhere((sortTypeItem) => sortTypeItem.payload == defaultCommentSortType)
.label,
style: theme.textTheme.titleSmall,
),
],
Expand Down
79 changes: 48 additions & 31 deletions lib/shared/comment_sort_picker.dart
Original file line number Diff line number Diff line change
@@ -1,41 +1,58 @@
import 'package:flutter/material.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
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';

List<ListPickerItem<CommentSortType>> commentSortTypeItems = [
ListPickerItem(
payload: CommentSortType.top,
icon: Icons.military_tech,
label: AppLocalizations.of(GlobalContext.context)!.top,
),
ListPickerItem(
payload: CommentSortType.old,
icon: Icons.access_time_outlined,
label: AppLocalizations.of(GlobalContext.context)!.old,
),
ListPickerItem(
payload: CommentSortType.new_,
icon: Icons.auto_awesome_rounded,
label: AppLocalizations.of(GlobalContext.context)!.new_,
),
ListPickerItem(
payload: CommentSortType.hot,
icon: Icons.local_fire_department,
label: AppLocalizations.of(GlobalContext.context)!.hot,
),
//
// ListPickerItem(
// payload: CommentSortType.chat,
// icon: Icons.chat,
// label: 'Chat',
// ),
];

class CommentSortPicker extends BottomSheetListPicker<CommentSortType> {
CommentSortPicker({super.key, required super.onSelect, required super.title, List<ListPickerItem<CommentSortType>>? items, super.previouslySelected}) : super(items: items ?? commentSortTypeItems);
final IncludeVersionSpecificFeature includeVersionSpecificFeature;

static List<ListPickerItem<CommentSortType>> getCommentSortTypeItems({IncludeVersionSpecificFeature includeVersionSpecificFeature = IncludeVersionSpecificFeature.ifSupported}) => [
ListPickerItem(
payload: CommentSortType.top,
icon: Icons.military_tech,
label: AppLocalizations.of(GlobalContext.context)!.top,
),
ListPickerItem(
payload: CommentSortType.old,
icon: Icons.access_time_outlined,
label: AppLocalizations.of(GlobalContext.context)!.old,
),
if (includeVersionSpecificFeature == IncludeVersionSpecificFeature.always ||
(includeVersionSpecificFeature == IncludeVersionSpecificFeature.ifSupported && LemmyClient.instance.supportsFeature(LemmyFeature.commentSortTypeControversial)))
ListPickerItem(
payload: CommentSortType.controversial,
icon: Icons.warning_rounded,
label: AppLocalizations.of(GlobalContext.context)!.controversial,
),
ListPickerItem(
payload: CommentSortType.new_,
icon: Icons.auto_awesome_rounded,
label: AppLocalizations.of(GlobalContext.context)!.new_,
),
ListPickerItem(
payload: CommentSortType.hot,
icon: Icons.local_fire_department,
label: AppLocalizations.of(GlobalContext.context)!.hot,
),
//
// 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));

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