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 separate default sort for posts feed. #10

Merged
merged 1 commit into from
Jan 29, 2024
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
9 changes: 8 additions & 1 deletion lib/src/screens/feed_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class _FeedScreenState extends State<FeedScreen> {
? context.read<SettingsController>().defaultFeedMode
: FeedMode.entries;
_sort = widget.source == null
? context.read<SettingsController>().defaultFeedSort
? _mode == FeedMode.entries
? context.read<SettingsController>().defaultEntriesFeedSort
: context.read<SettingsController>().defaultPostsFeedSort
: context.read<SettingsController>().defaultExploreFeedSort;
}

Expand Down Expand Up @@ -85,6 +87,11 @@ class _FeedScreenState extends State<FeedScreen> {
onSelectionChanged: (Set<FeedMode> newSelection) {
setState(() {
_mode = newSelection.first;
_sort = widget.source == null
? _mode == FeedMode.entries
? context.read<SettingsController>().defaultEntriesFeedSort
: context.read<SettingsController>().defaultPostsFeedSort
: context.read<SettingsController>().defaultExploreFeedSort;
});
},
),
Expand Down
32 changes: 25 additions & 7 deletions lib/src/screens/settings/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class SettingsController with ChangeNotifier {
ThemeMode get themeMode => _themeMode;
late FeedMode _defaultFeedMode;
FeedMode get defaultFeedMode => _defaultFeedMode;
late FeedSort _defaultFeedSort;
FeedSort get defaultFeedSort => _defaultFeedSort;
late FeedSort _defaultEntriesFeedSort;
FeedSort get defaultEntriesFeedSort => _defaultEntriesFeedSort;
late FeedSort _defaultPostsFeedSort;
FeedSort get defaultPostsFeedSort => _defaultPostsFeedSort;
late FeedSort _defaultExploreFeedSort;
FeedSort get defaultExploreFeedSort => _defaultExploreFeedSort;
late CommentSort _defaultCommentSort;
Expand All @@ -42,8 +44,11 @@ class SettingsController with ChangeNotifier {
_defaultFeedMode = prefs.getString('defaultFeedMode') != null
? FeedMode.values.byName(prefs.getString("defaultFeedMode")!)
: FeedMode.entries;
_defaultFeedSort = prefs.getString('defaultFeedSort') != null
? FeedSort.values.byName(prefs.getString("defaultFeedSort")!)
_defaultEntriesFeedSort = prefs.getString('defaultEntriesFeedSort') != null
? FeedSort.values.byName(prefs.getString("defaultEntriesFeedSort")!)
: FeedSort.hot;
_defaultPostsFeedSort = prefs.getString('defaultPostsFeedSort') != null
? FeedSort.values.byName(prefs.getString("defaultPostsFeedSort")!)
: FeedSort.hot;
_defaultExploreFeedSort = prefs.getString('defaultExploreFeedSort') != null
? FeedSort.values.byName(prefs.getString("defaultExploreFeedSort")!)
Expand Down Expand Up @@ -92,19 +97,32 @@ class SettingsController with ChangeNotifier {
await prefs.setString('defaultFeedMode', newDefaultFeedMode.name);
}

Future<void> updateDefaultFeedSort(FeedSort? newDefaultFeedSort) async {
Future<void> updateDefaultEntriesFeedSort(FeedSort? newDefaultFeedSort) async {
if (newDefaultFeedSort == null) return;

if (newDefaultFeedSort == _defaultFeedSort) return;
if (newDefaultFeedSort == _defaultEntriesFeedSort) return;

_defaultFeedSort = newDefaultFeedSort;
_defaultEntriesFeedSort = newDefaultFeedSort;

notifyListeners();

final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('defaultFeedSort', newDefaultFeedSort.name);
}

Future<void> updateDefaultPostsFeedSort(FeedSort? newDefaultFeedSort) async {
if (newDefaultFeedSort == null) return;

if (newDefaultFeedSort == _defaultPostsFeedSort) return;

_defaultPostsFeedSort = newDefaultFeedSort;

notifyListeners();

final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('defaultPostsFeedSort', newDefaultFeedSort.name);
}

Future<void> updateDefaultExploreFeedSort(
FeedSort? newDefaultExploreFeedSort,
) async {
Expand Down
37 changes: 30 additions & 7 deletions lib/src/screens/settings/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class SettingsScreen extends StatelessWidget {
final currentDefaultFeedMode = feedModeSelect.options.firstWhere(
(option) => option.value == controller.defaultFeedMode,
);
final currentDefaultFeedSort = feedSortSelect.options.firstWhere(
(option) => option.value == controller.defaultFeedSort,
final currentDefaultEntriesFeedSort = feedSortSelect.options.firstWhere(
(option) => option.value == controller.defaultEntriesFeedSort,
);
final currentDefaultPostsFeedSort = feedSortSelect.options.firstWhere(
(option) => option.value == controller.defaultPostsFeedSort,
);
final currentDefaultExploreFeedSort = feedSortSelect.options.firstWhere(
(option) => option.value == controller.defaultExploreFeedSort,
Expand Down Expand Up @@ -86,22 +89,42 @@ class SettingsScreen extends StatelessWidget {
),
),
ListTile(
title: const Text('Default Feed Sort'),
title: const Text('Default Threads Feed Sort'),
leading: const Icon(Icons.sort),
onTap: () async {
controller.updateDefaultEntriesFeedSort(
await feedSortSelect.inquireSelection(
context,
currentDefaultEntriesFeedSort.value,
),
);
},
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(currentDefaultEntriesFeedSort.icon),
const SizedBox(width: 4),
Text(currentDefaultEntriesFeedSort.title),
],
),
),
ListTile(
title: const Text('Default Posts Feed Sort'),
leading: const Icon(Icons.sort),
onTap: () async {
controller.updateDefaultFeedSort(
controller.updateDefaultPostsFeedSort(
await feedSortSelect.inquireSelection(
context,
currentDefaultFeedSort.value,
currentDefaultPostsFeedSort.value,
),
);
},
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(currentDefaultFeedSort.icon),
Icon(currentDefaultPostsFeedSort.icon),
const SizedBox(width: 4),
Text(currentDefaultFeedSort.title),
Text(currentDefaultPostsFeedSort.title),
],
),
),
Expand Down