Skip to content

Commit 333ed3d

Browse files
committed
bfix: fix platform update is not reflecting
1 parent 3afa3dc commit 333ed3d

5 files changed

Lines changed: 133 additions & 88 deletions

File tree

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1+
import 'package:equatable/equatable.dart';
12
import 'package:freezed_annotation/freezed_annotation.dart';
23

34
part 'contest_filter.freezed.dart';
45
part 'contest_filter.g.dart';
56

67
@freezed
7-
class ContestFilter with _$ContestFilter {
8-
factory ContestFilter({
8+
class ContestFilter extends Equatable with _$ContestFilter {
9+
const factory ContestFilter({
910
int? duration,
1011
bool? ongoing,
1112
bool? upcoming,
1213
DateTime? startDate,
13-
List<dynamic>? platform,
14+
@Default([]) List<bool> platform,
1415
}) = _ContestFilter;
1516

1617
factory ContestFilter.fromJson(Map<String, dynamic> json) =>
1718
_$ContestFilterFromJson(json);
19+
20+
const ContestFilter._();
21+
22+
@override
23+
List<Object?> get props => [duration, ongoing, upcoming, startDate, platform];
1824
}

lib/presentation/contests/bloc/contests_bloc.dart

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:developer';
2-
31
import 'package:equatable/equatable.dart';
42
import 'package:flutter_bloc/flutter_bloc.dart';
53
import 'package:freezed_annotation/freezed_annotation.dart';
@@ -19,6 +17,7 @@ class ContestsBloc extends Bloc<ContestsEvent, ContestsState> {
1917
on<FetchContests>(_fetchContests);
2018
on<UpdateFilter>(_updateFilter);
2119
on<FilterButton>(_filterButton);
20+
on<UpdateContestsList>(_updateContestsList);
2221
}
2322

2423
void _fetchContests(FetchContests event, Emitter<ContestsState> emit) async {
@@ -35,21 +34,37 @@ class ContestsBloc extends Bloc<ContestsEvent, ContestsState> {
3534
}
3635

3736
void _filterButton(FilterButton event, Emitter<ContestsState> emit) {
38-
_updatedfilter = _filter;
37+
_filter = StorageService.filter;
3938

40-
emit(state.copyWith(duration: _updatedfilter?.duration));
39+
emit(state.copyWith(
40+
duration: _filter?.duration,
41+
filter: _filter,
42+
updateIdx: state.updateIdx + 1,
43+
));
4144
}
4245

4346
void _updateFilter(UpdateFilter event, Emitter<ContestsState> emit) {
44-
if (event.updatedFilter != null) _updatedfilter = event.updatedFilter;
47+
_filter = event.updatedFilter ?? _filter;
4548
emit(
4649
state.copyWith(
47-
duration: event.duration ?? _updatedfilter?.duration,
48-
filter: event.updatedFilter ?? _updatedfilter,
50+
duration: event.duration ?? _filter?.duration,
51+
filter: event.updatedFilter ?? _filter,
52+
updateIdx: state.updateIdx + 1,
4953
),
5054
);
5155
}
5256

57+
void _updateContestsList(
58+
UpdateContestsList event,
59+
Emitter<ContestsState> emit,
60+
) {
61+
applyFilter();
62+
final contests = [..._filteredUpcoming, ..._filteredOngoing];
63+
emit(state.copyWith(
64+
contests: contests,
65+
));
66+
}
67+
5368
void applyFilter() {
5469
_filteredOngoing = [];
5570
_filteredUpcoming = [];
@@ -74,7 +89,7 @@ class ContestsBloc extends Bloc<ContestsEvent, ContestsState> {
7489
}
7590
}
7691

77-
ContestFilter? _filter, _updatedfilter;
92+
ContestFilter? _filter;
7893
List<Ongoing> _ongoing = [], _filteredOngoing = [];
7994
List<Upcoming> _upcoming = [], _filteredUpcoming = [];
8095

@@ -83,37 +98,36 @@ class ContestsBloc extends Bloc<ContestsEvent, ContestsState> {
8398
if (!exists) {
8499
StorageService.filter = ContestFilter(
85100
duration: 4,
86-
platform: [true, true, true, true, false],
101+
platform: const [true, true, true, true, false],
87102
startDate: DateTime.now(),
88103
ongoing: true,
89104
upcoming: true,
90105
);
91106
}
92107

93108
_filter = StorageService.filter;
94-
_updatedfilter = _filter;
95109
add(const FetchContests());
96110
}
97111

98112
void saveFilter() {
99-
log('save filter is called');
100-
_filter = _updatedfilter;
113+
StorageService.filter = _filter;
114+
add(const UpdateContestsList());
101115
}
102116
}
103117

104118
extension on ContestFilter {
105119
bool checkPlatform(String platformName) {
106120
switch (platformName.toLowerCase()) {
107121
case 'codechef':
108-
return platform?[0] ?? false;
122+
return platform[0];
109123
case 'codeforces':
110-
return platform?[1] ?? false;
124+
return platform[1];
111125
case 'hackerearth':
112-
return platform?[2] ?? false;
126+
return platform[2];
113127
case 'hackerrank':
114-
return platform?[3] ?? false;
128+
return platform[3];
115129
default:
116-
return platform?[4] ?? false;
130+
return platform[4];
117131
}
118132
}
119133

lib/presentation/contests/bloc/contests_event.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ class UpdateFilter extends ContestsEvent {
3232
@override
3333
List<Object?> get props => [updatedFilter, duration];
3434
}
35+
36+
class UpdateContestsList extends ContestsEvent {
37+
const UpdateContestsList();
38+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
part of 'contests_bloc.dart';
22

33
@freezed
4-
class ContestsState with _$ContestsState {
4+
class ContestsState extends Equatable with _$ContestsState {
55
const factory ContestsState({
66
@Default(true) bool isLoading,
77
@Default([]) List contests,
88
ContestFilter? filter,
99
int? duration,
10+
@Default(0) int updateIdx,
1011
}) = _ContestsState;
1112

1213
const ContestsState._();
14+
15+
@override
16+
List<Object?> get props => [isLoading, contests, filter, duration, updateIdx];
1317
}

lib/presentation/contests/widgets/filter_sheet.dart

Lines changed: 84 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class FilterSheet extends StatelessWidget {
5757
mainAxisSize: MainAxisSize.min,
5858
children: [
5959
Container(
60-
color: const Color(0xFFF3F4F7),
60+
color: AppColors.grey7,
6161
child: Row(
6262
mainAxisAlignment: MainAxisAlignment.spaceBetween,
6363
children: <Widget>[
@@ -73,7 +73,7 @@ class FilterSheet extends StatelessWidget {
7373
TextButton(
7474
onPressed: () {
7575
context.read<ContestsBloc>().saveFilter();
76-
Get.back();
76+
Get.back(result: true);
7777
},
7878
child: Text(
7979
'Apply',
@@ -105,7 +105,7 @@ class FilterSheet extends StatelessWidget {
105105
return _buildPlatfromIcon(
106106
platfromIcons[index],
107107
() {
108-
final updatedPlatform = state.filter!.platform!;
108+
final updatedPlatform = state.filter!.platform;
109109
updatedPlatform[index] = !updatedPlatform[index];
110110
context.read<ContestsBloc>().add(
111111
UpdateFilter(
@@ -115,7 +115,7 @@ class FilterSheet extends StatelessWidget {
115115
),
116116
);
117117
},
118-
state.filter!.platform![index],
118+
state.filter!.platform[index],
119119
);
120120
},
121121
),
@@ -183,11 +183,17 @@ class FilterSheet extends StatelessWidget {
183183
),
184184
),
185185
),
186-
Row(
187-
children: <Widget>[
188-
GestureDetector(
189-
onTap: () {},
190-
child: Padding(
186+
GestureDetector(
187+
onTap: () {
188+
final ongoing = state.filter!.ongoing!;
189+
context.read<ContestsBloc>().add(UpdateFilter(
190+
updatedFilter: state.filter!.copyWith(
191+
ongoing: !ongoing,
192+
)));
193+
},
194+
child: Row(
195+
children: <Widget>[
196+
Padding(
191197
padding: const EdgeInsets.fromLTRB(25, 10, 15, 10),
192198
child: SvgPicture.asset(
193199
state.filter!.ongoing!
@@ -197,74 +203,85 @@ class FilterSheet extends StatelessWidget {
197203
width: 28,
198204
),
199205
),
200-
),
201-
GestureDetector(
202-
onTap: () {},
203-
child: const Text(
206+
const Text(
204207
'Ongoing',
205208
style: TextStyle(
206209
fontSize: 16,
207210
),
208211
),
209-
)
210-
],
212+
],
213+
),
211214
),
212-
Row(
213-
children: <Widget>[
214-
GestureDetector(
215-
onTap: () {},
216-
child: Padding(
217-
padding: const EdgeInsets.fromLTRB(25, 10, 15, 10),
218-
child: SvgPicture.asset(
219-
state.filter!.upcoming!
220-
? AppAssets.trueRadioButton
221-
: AppAssets.falseRadioButton,
222-
height: 28,
223-
width: 28,
215+
GestureDetector(
216+
onTap: () {
217+
final upcoming = state.filter!.upcoming!;
218+
context.read<ContestsBloc>().add(UpdateFilter(
219+
updatedFilter: state.filter!.copyWith(
220+
upcoming: !upcoming,
221+
startDate: !upcoming ? DateTime.now() : null,
222+
)));
223+
},
224+
child: Row(
225+
children: <Widget>[
226+
Padding(
227+
padding: const EdgeInsets.fromLTRB(25, 10, 15, 10),
228+
child: SvgPicture.asset(
229+
state.filter!.upcoming!
230+
? AppAssets.trueRadioButton
231+
: AppAssets.falseRadioButton,
232+
height: 28,
233+
width: 28,
234+
),
235+
),
236+
const Text(
237+
'Upcoming',
238+
style: TextStyle(
239+
fontSize: 16,
240+
),
241+
),
242+
TextButton(
243+
onPressed: () async {
244+
showDatePicker(
245+
builder: (BuildContext context, Widget? child) {
246+
return Theme(
247+
data: Theme.of(context).copyWith(
248+
colorScheme:
249+
const ColorScheme.light().copyWith(
250+
primary: AppColors.primary,
251+
),
252+
),
253+
child: child!,
254+
);
255+
},
256+
context: context,
257+
initialDate: DateTime.now(),
258+
firstDate: DateTime(2020),
259+
lastDate: DateTime(2025),
260+
).then((val) {
261+
context.read<ContestsBloc>().add(UpdateFilter(
262+
updatedFilter: state.filter!.copyWith(
263+
upcoming: true,
264+
startDate: val ??= DateTime.now(),
265+
),
266+
));
267+
});
268+
},
269+
child: Container(
270+
margin: const EdgeInsets.symmetric(horizontal: 15),
271+
padding: const EdgeInsets.all(5),
272+
decoration: BoxDecoration(
273+
borderRadius: BorderRadius.circular(5),
224274
),
225-
)),
226-
GestureDetector(
227-
onTap: () {},
228-
child: const Text(
229-
'Upcoming',
230-
style: TextStyle(
231-
fontSize: 16,
275+
child: Text(
276+
(state.filter!.startDate != null)
277+
? DateFormat('d MMMM, yyyy')
278+
.format(state.filter!.startDate!)
279+
: 'Select Date',
232280
),
233-
)),
234-
TextButton(
235-
onPressed: () async {
236-
showDatePicker(
237-
builder: (BuildContext context, Widget? child) {
238-
return Theme(
239-
data: Theme.of(context).copyWith(
240-
colorScheme: const ColorScheme.light().copyWith(
241-
primary: AppColors.primary,
242-
),
243-
),
244-
child: child!,
245-
);
246-
},
247-
context: context,
248-
initialDate: DateTime.now(),
249-
firstDate: DateTime(2020),
250-
lastDate: DateTime(2025),
251-
).then((val) {});
252-
},
253-
child: Container(
254-
margin: const EdgeInsets.symmetric(horizontal: 15),
255-
padding: const EdgeInsets.all(5),
256-
decoration: BoxDecoration(
257-
borderRadius: BorderRadius.circular(5),
258-
),
259-
child: Text(
260-
(state.filter!.startDate != null)
261-
? DateFormat('d MMMM, yyyy')
262-
.format(state.filter!.startDate!)
263-
: 'Select Date',
264281
),
265282
),
266-
),
267-
],
283+
],
284+
),
268285
),
269286
],
270287
),

0 commit comments

Comments
 (0)