-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to filter posts containing keyword in title/body (#974)
* initial work on keyword filtering * applied filtering to post title and body * cleaned up logic for input dialog * minor linting * checks for empty strings, and trimming whitespace * moved filter description to top of page * updated naming for dialog context
- Loading branch information
Showing
13 changed files
with
328 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
|
||
import 'package:thunder/core/enums/local_settings.dart'; | ||
import 'package:thunder/core/singletons/preferences.dart'; | ||
import 'package:thunder/settings/widgets/settings_list_tile.dart'; | ||
import 'package:thunder/shared/dialogs.dart'; | ||
import 'package:thunder/shared/input_dialogs.dart'; | ||
import 'package:thunder/thunder/bloc/thunder_bloc.dart'; | ||
|
||
class FilterSettingsPage extends StatefulWidget { | ||
const FilterSettingsPage({super.key}); | ||
|
||
@override | ||
State<FilterSettingsPage> createState() => _FilterSettingsPageState(); | ||
} | ||
|
||
class _FilterSettingsPageState extends State<FilterSettingsPage> with SingleTickerProviderStateMixin { | ||
/// The list of keyword filters to apply for posts | ||
List<String> keywordFilters = []; | ||
|
||
void setPreferences(attribute, value) async { | ||
final prefs = (await UserPreferences.instance).sharedPreferences; | ||
|
||
switch (attribute) { | ||
case LocalSettings.keywordFilters: | ||
await prefs.setStringList(LocalSettings.keywordFilters.name, value); | ||
setState(() => keywordFilters = value); | ||
break; | ||
} | ||
|
||
if (context.mounted) { | ||
context.read<ThunderBloc>().add(UserPreferencesChangeEvent()); | ||
} | ||
} | ||
|
||
void _initPreferences() async { | ||
final prefs = (await UserPreferences.instance).sharedPreferences; | ||
|
||
setState(() { | ||
keywordFilters = prefs.getStringList(LocalSettings.keywordFilters.name) ?? []; | ||
}); | ||
} | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
WidgetsBinding.instance.addPostFrameCallback((_) => _initPreferences()); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final theme = Theme.of(context); | ||
final l10n = AppLocalizations.of(context)!; | ||
|
||
return Scaffold( | ||
body: CustomScrollView( | ||
slivers: [ | ||
SliverAppBar( | ||
title: Text(l10n.filters), | ||
centerTitle: false, | ||
toolbarHeight: 70.0, | ||
pinned: true, | ||
), | ||
SliverToBoxAdapter( | ||
child: Padding( | ||
padding: const EdgeInsets.only(left: 16.0, bottom: 8.0), | ||
child: Text( | ||
l10n.keywordFilterDescription, | ||
style: theme.textTheme.bodyMedium?.copyWith( | ||
color: theme.textTheme.bodyMedium?.color?.withOpacity(0.8), | ||
), | ||
), | ||
), | ||
), | ||
SliverToBoxAdapter( | ||
child: Padding( | ||
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), | ||
child: Row( | ||
mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
children: [ | ||
Text(l10n.keywordFilters, style: theme.textTheme.titleMedium), | ||
IconButton( | ||
visualDensity: VisualDensity.compact, | ||
icon: Icon( | ||
Icons.add_rounded, | ||
semanticLabel: l10n.add, | ||
), | ||
onPressed: () => showKeywordInputDialog( | ||
context, | ||
title: l10n.addKeywordFilter, | ||
onKeywordSelected: (keyword) { | ||
setPreferences(LocalSettings.keywordFilters, [...keywordFilters, keyword]); | ||
}, | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
SliverToBoxAdapter( | ||
child: Padding( | ||
padding: const EdgeInsets.only(left: 16.0, right: 16.0), | ||
child: Align( | ||
alignment: Alignment.centerLeft, | ||
child: keywordFilters.isEmpty | ||
? Text( | ||
l10n.noKeywordFilters, | ||
style: theme.textTheme.bodyMedium?.copyWith( | ||
color: theme.textTheme.bodyMedium?.color?.withOpacity(0.8), | ||
), | ||
) | ||
: ListView.builder( | ||
padding: const EdgeInsets.only(bottom: 20), | ||
physics: const NeverScrollableScrollPhysics(), | ||
shrinkWrap: true, | ||
itemCount: keywordFilters.length, | ||
itemBuilder: (context, index) { | ||
return SettingsListTile( | ||
description: keywordFilters[index], | ||
widget: const SizedBox(height: 42.0, child: Icon(Icons.chevron_right_rounded)), | ||
onTap: () async { | ||
showThunderDialog( | ||
context: context, | ||
title: l10n.removeKeywordFilter, | ||
contentText: l10n.removeKeyword(keywordFilters[index]), | ||
primaryButtonText: l10n.remove, | ||
onPrimaryButtonPressed: (dialogContext, setPrimaryButtonEnabled) { | ||
setPreferences(LocalSettings.keywordFilters, keywordFilters.where((element) => element != keywordFilters[index]).toList()); | ||
Navigator.of(dialogContext).pop(); | ||
}, | ||
secondaryButtonText: l10n.cancel, | ||
onSecondaryButtonPressed: (dialogContext) => Navigator.of(dialogContext).pop(), | ||
); | ||
}, | ||
); | ||
}, | ||
), | ||
), | ||
), | ||
), | ||
const SliverToBoxAdapter(child: SizedBox(height: 128.0)), | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.