Skip to content

Commit 114a44f

Browse files
committed
add company name filter
1 parent 3611ef8 commit 114a44f

File tree

3 files changed

+105
-8
lines changed

3 files changed

+105
-8
lines changed

lib/hive/postpreview_filter_adapter.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class PostPreviewFilterAdapter extends TypeAdapter<Filter<PostPreview>> {
1313
switch (obj['type']) {
1414
case 'nickname':
1515
return NicknameAuthorFilter(obj['value']);
16+
case 'company_name':
17+
return CompanyNameFilter(obj['value']);
1618
default:
1719
return const NoneFilter();
1820
}
@@ -25,6 +27,11 @@ class PostPreviewFilterAdapter extends TypeAdapter<Filter<PostPreview>> {
2527
'type': 'nickname',
2628
'value': obj.nickname,
2729
});
30+
} else if (obj is CompanyNameFilter) {
31+
writer.writeMap({
32+
'type': 'company_name',
33+
'value': obj.companyName,
34+
});
2835
}
2936
}
30-
}
37+
}

lib/pages/filters.dart

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ class _FiltersPageState extends State<FiltersPage> {
4343
onPressed: () => FiltersStorage().removeFilterAt(i),
4444
),
4545
);
46+
} else if (filter is CompanyNameFilter) {
47+
return ListTile(
48+
leading: const Icon(Icons.groups),
49+
title: Text(filter.companyName),
50+
trailing: IconButton(
51+
icon: const Icon(Icons.clear),
52+
onPressed: () => FiltersStorage().removeFilterAt(i),
53+
),
54+
);
4655
} else {
4756
logInfo("filter not supported");
4857
}
@@ -83,18 +92,29 @@ class _FiltersPageState extends State<FiltersPage> {
8392
case _DialogType.AuthorNickname:
8493
await _createAuthorNicknameFilter();
8594
break;
95+
case _DialogType.CompanyName:
96+
await _createCompanyNameFilter();
97+
break;
8698
default:
8799
logInfo('$type not supported');
88100
}
89101
}
90102

91103
Future<void> _createAuthorNicknameFilter() async {
92-
await showDialog<_DialogType>(
104+
await showDialog<void>(
93105
context: context,
94106
builder: (BuildContext context) {
95107
return _AuthorNicknameFilterDialog();
96108
});
97109
}
110+
111+
Future<void> _createCompanyNameFilter() async {
112+
await showDialog<void>(
113+
context: context,
114+
builder: (BuildContext context) {
115+
return _CompanyNameFilterDialog();
116+
});
117+
}
98118
}
99119

100120
enum _DialogType {
@@ -134,19 +154,20 @@ class _AuthorNicknameFilterDialogState
134154
controller: nickanameControll,
135155
autofocus: true,
136156
decoration: InputDecoration(
137-
labelText: AppLocalizations.of(context).authorNickname,
138-
hintText: AppLocalizations.of(context).authorNicknameHint),
157+
labelText: AppLocalizations.of(context).authorNickname,
158+
hintText: AppLocalizations.of(context).authorNicknameHint,
159+
),
139160
),
140161
)
141162
],
142163
),
143164
actions: <Widget>[
144-
FlatButton(
165+
TextButton(
145166
child: Text(AppLocalizations.of(context).cancel),
146167
onPressed: () {
147168
Navigator.pop(context);
148169
}),
149-
FlatButton(
170+
TextButton(
150171
child: Text(AppLocalizations.of(context).create),
151172
onPressed: () {
152173
if (nicknameValid())
@@ -158,3 +179,58 @@ class _AuthorNicknameFilterDialogState
158179
);
159180
}
160181
}
182+
183+
class _CompanyNameFilterDialog extends StatefulWidget {
184+
@override
185+
State<StatefulWidget> createState() => _CompanyNameFilterDialogState();
186+
}
187+
188+
class _CompanyNameFilterDialogState extends State<_CompanyNameFilterDialog> {
189+
TextEditingController nickanameControll;
190+
191+
@override
192+
void initState() {
193+
super.initState();
194+
nickanameControll = TextEditingController();
195+
}
196+
197+
bool nicknameValid() {
198+
return nickanameControll.text.isNotEmpty;
199+
}
200+
201+
@override
202+
Widget build(BuildContext context) {
203+
return AlertDialog(
204+
contentPadding: const EdgeInsets.all(16.0),
205+
content: Row(
206+
children: [
207+
Expanded(
208+
child: TextField(
209+
controller: nickanameControll,
210+
autofocus: true,
211+
decoration: InputDecoration(
212+
labelText: "Имя компании",
213+
hintText: "Например, RUVDS.com",
214+
),
215+
),
216+
)
217+
],
218+
),
219+
actions: <Widget>[
220+
TextButton(
221+
child: Text(AppLocalizations.of(context).cancel),
222+
onPressed: () {
223+
Navigator.pop(context);
224+
}),
225+
TextButton(
226+
child: Text(AppLocalizations.of(context).create),
227+
onPressed: () {
228+
if (nicknameValid())
229+
FiltersStorage()
230+
.addFilter(CompanyNameFilter(nickanameControll.text));
231+
Navigator.pop(context);
232+
})
233+
],
234+
);
235+
}
236+
}

lib/utils/filters/article_preview_filters.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export 'filter.dart';
66
class NicknameAuthorFilter extends Filter<PostPreview> {
77
final String nickname;
88

9-
NicknameAuthorFilter(this.nickname);
9+
const NicknameAuthorFilter(this.nickname);
1010

1111
@override
1212
bool filter(PostPreview postPreview) {
@@ -18,11 +18,25 @@ class ScoreArticleFilter extends Filter<PostPreview> {
1818
final int min;
1919
final int max;
2020

21-
ScoreArticleFilter({this.min, this.max});
21+
const ScoreArticleFilter({this.min, this.max});
2222

2323
@override
2424
bool filter(PostPreview obj) {
2525
return (min != null && obj.statistics.score < min) ||
2626
(max != null && obj.statistics.score > max);
2727
}
2828
}
29+
30+
class CompanyNameFilter extends Filter<PostPreview> {
31+
final String companyName;
32+
33+
const CompanyNameFilter(this.companyName);
34+
35+
@override
36+
bool filter(PostPreview postPreview) {
37+
final l = companyName.toLowerCase();
38+
return postPreview.hubs
39+
?.any((element) => element.toLowerCase().contains(l)) ??
40+
false;
41+
}
42+
}

0 commit comments

Comments
 (0)