@@ -2,7 +2,10 @@ import 'package:equatable/equatable.dart';
22import 'package:flutter_bloc/flutter_bloc.dart' ;
33import 'package:freezed_annotation/freezed_annotation.dart' ;
44
5+ import '../../../data/constants/strings.dart' ;
6+ import '../../../data/services/local/storage_service.dart' ;
57import '../../../domain/models/contest.dart' ;
8+ import '../../../domain/models/contest_filter.dart' ;
69import '../../../domain/repositories/cp_repository.dart' ;
710
811part 'contests_event.dart' ;
@@ -17,12 +20,139 @@ class ContestsBloc extends Bloc<ContestsEvent, ContestsState> {
1720
1821 void _fetchContests (FetchContests event, Emitter <ContestsState > emit) async {
1922 final contest = await CPRepository .contestList ();
20- emit (state.copyWith (contest: contest, isLoading: false ));
23+ _ongoing = [...? contest? .ongoing];
24+ _upcoming = [...? contest? .upcoming];
25+ applyFilter ();
26+ final contests = [..._filteredUpcoming, ..._filteredOngoing];
27+ emit (state.copyWith (contests: contests, isLoading: false ));
2128 }
2229
2330 void _updateFilter (UpdateFilter event, Emitter <ContestsState > emit) {}
2431
32+ void applyFilter () {
33+ _filteredOngoing = [];
34+ _filteredUpcoming = [];
35+ if (_filter! .ongoing ?? false ) {
36+ _filteredOngoing.addAll (
37+ _ongoing.where (
38+ (element) => _filter! .check (
39+ ongoing: element,
40+ ),
41+ ),
42+ );
43+ }
44+
45+ if (_filter! .upcoming ?? false ) {
46+ _filteredUpcoming.addAll (
47+ _upcoming.where (
48+ (element) => _filter! .check (
49+ upcoming: element,
50+ ),
51+ ),
52+ );
53+ }
54+ }
55+
56+ ContestFilter ? _filter;
57+ List <Ongoing > _ongoing = [], _filteredOngoing = [];
58+ List <Upcoming > _upcoming = [], _filteredUpcoming = [];
59+
2560 void init () {
61+ final exists = StorageService .exists (AppStrings .filterKey);
62+ if (! exists) {
63+ StorageService .filter = ContestFilter (
64+ duration: 4 ,
65+ platform: [true , true , true , true , false ],
66+ startDate: DateTime .now (),
67+ ongoing: true ,
68+ upcoming: true ,
69+ );
70+ }
71+
72+ _filter = StorageService .filter;
2673 add (const FetchContests ());
2774 }
2875}
76+
77+ extension on ContestFilter {
78+ bool checkPlatform (String platformName) {
79+ switch (platformName.toLowerCase ()) {
80+ case 'codechef' :
81+ return platform? [0 ] ?? false ;
82+ case 'codeforces' :
83+ return platform? [1 ] ?? false ;
84+ case 'hackerearth' :
85+ return platform? [2 ] ?? false ;
86+ case 'hackerrank' :
87+ return platform? [3 ] ?? false ;
88+ default :
89+ return platform? [4 ] ?? false ;
90+ }
91+ }
92+
93+ bool check ({
94+ Upcoming ? upcoming,
95+ Ongoing ? ongoing,
96+ }) {
97+ assert (upcoming != null || ongoing != null , '' );
98+ final platfromCheck = checkPlatform (
99+ upcoming != null ? upcoming.platform : ongoing! .platform,
100+ );
101+
102+ if (! platfromCheck) return platfromCheck;
103+
104+ Duration _maxDuration;
105+ switch (duration) {
106+ case 0 :
107+ _maxDuration = const Duration (hours: 2 );
108+ break ;
109+ case 1 :
110+ _maxDuration = const Duration (hours: 3 );
111+ break ;
112+ case 2 :
113+ _maxDuration = const Duration (hours: 5 );
114+ break ;
115+ case 3 :
116+ _maxDuration = const Duration (days: 1 );
117+ break ;
118+ case 4 :
119+ _maxDuration = const Duration (days: 10 );
120+ break ;
121+ case 5 :
122+ _maxDuration = const Duration (days: 31 );
123+ break ;
124+ default :
125+ _maxDuration = Duration (days: 1e5 .toInt ());
126+ }
127+
128+ final durationCheck = upcoming != null
129+ ? upcoming.compareDuration (_maxDuration)
130+ : ongoing! .compareDuration (_maxDuration);
131+
132+ if (! durationCheck) return durationCheck;
133+
134+ bool ? startCheck;
135+ if (upcoming != null ) startCheck = upcoming.compareStart (startDate! );
136+
137+ startCheck ?? = true ;
138+ if (! startCheck) return startCheck;
139+
140+ return true ;
141+ }
142+ }
143+
144+ extension on Upcoming {
145+ bool compareDuration (Duration _duration) {
146+ return _duration.compareTo (endTime.difference (startTime)) >= 0 ;
147+ }
148+
149+ bool compareStart (DateTime _startDate) {
150+ return startTime.isAfter (_startDate);
151+ }
152+ }
153+
154+ extension on Ongoing {
155+ bool compareDuration (Duration _duration) {
156+ return _duration.compareTo (endTime.difference (DateTime .now ())) >= 0 ;
157+ }
158+ }
0 commit comments