1
+ import 'package:add_2_calendar/add_2_calendar.dart' ;
1
2
import 'package:boj_api/boj_api.dart' ;
2
3
import 'package:contest_notification_repository/contest_notification_repository.dart' ;
3
4
import 'package:contest_repository/contest_repository.dart' ;
4
5
import 'package:equatable/equatable.dart' ;
6
+ import 'package:flutter/material.dart' ;
5
7
import 'package:flutter_bloc/flutter_bloc.dart' ;
6
- import 'package:meta/meta .dart' ;
8
+ import 'package:fluttertoast/fluttertoast .dart' ;
7
9
import 'package:my_solved/features/contest_filter/bloc/contest_filter_bloc.dart' ;
8
10
import 'package:permission_handler/permission_handler.dart' ;
9
11
import 'package:shared_preferences_repository/shared_preferences_repository.dart' ;
10
12
13
+ import '../../../components/styles/color.dart' ;
14
+
11
15
part 'contest_event.dart' ;
12
16
part 'contest_state.dart' ;
13
17
@@ -31,6 +35,7 @@ class ContestBloc extends Bloc<ContestEvent, ContestState> {
31
35
on < ContestInit > (_onInit);
32
36
on < ContestSegmentedControlPressed > (_onSegmentedControlPressed);
33
37
on < ContestNotificationButtonPressed > (_onNotificationPressed);
38
+ on < ContestCalendarButtonPressed > (_onCalendarPressed);
34
39
on < ContestFilterTogglePressed > (_onFilterPressed);
35
40
}
36
41
@@ -48,9 +53,12 @@ class ContestBloc extends Bloc<ContestEvent, ContestState> {
48
53
final upcomingContests = result[ContestType .upcoming];
49
54
50
55
List <bool > isOnContestNotifications = [];
56
+ List <bool > isOnContestCalendars = [];
51
57
for (Contest contest in upcomingContests ?? []) {
52
58
isOnContestNotifications.add (await _sharedPreferencesRepository
53
59
.getIsOnContestNotification (title: contest.name));
60
+ isOnContestCalendars.add (await _sharedPreferencesRepository
61
+ .getIsOnContestCalendar (title: contest.name));
54
62
}
55
63
56
64
emit (state.copyWith (
@@ -59,6 +67,7 @@ class ContestBloc extends Bloc<ContestEvent, ContestState> {
59
67
ongoingContests: ongoingContests,
60
68
upcomingContests: upcomingContests,
61
69
isOnNotificationUpcomingContests: isOnContestNotifications,
70
+ isOnCalendarUpcomingContests: isOnContestCalendars,
62
71
));
63
72
} catch (e) {
64
73
emit (state.copyWith (status: ContestStatus .failure));
@@ -90,6 +99,15 @@ class ContestBloc extends Bloc<ContestEvent, ContestState> {
90
99
final minute =
91
100
await _sharedPreferencesRepository.getContestNotificationMinute ();
92
101
102
+ Fluttertoast .showToast (
103
+ msg: isOn ? "알람이 취소되었습니다." : "알람이 설정되었습니다." ,
104
+ toastLength: Toast .LENGTH_SHORT ,
105
+ gravity: ToastGravity .CENTER ,
106
+ timeInSecForIosWeb: 1 ,
107
+ backgroundColor: MySolvedColor .main.withOpacity (0.8 ),
108
+ textColor: Colors .white,
109
+ fontSize: 16.0 );
110
+
93
111
if (isOn) {
94
112
await _contestNotificationRepository.cancelContestNotification (
95
113
title: contest.name,
@@ -121,6 +139,63 @@ class ContestBloc extends Bloc<ContestEvent, ContestState> {
121
139
}
122
140
}
123
141
142
+ void _onCalendarPressed (
143
+ ContestCalendarButtonPressed event,
144
+ Emitter <ContestState > emit,
145
+ ) async {
146
+ emit (state.copyWith (status: ContestStatus .loading));
147
+
148
+ final contest = state.filteredUpcomingContests[event.index];
149
+ List <bool > isOnCalendar = state.isOnCalendarUpcomingContests;
150
+ final isOn = await _sharedPreferencesRepository.getIsOnContestCalendar (
151
+ title: contest.name,
152
+ );
153
+
154
+ if (isOn) {
155
+ await _sharedPreferencesRepository.setIsOnContestCalendar (
156
+ title: contest.name,
157
+ isOn: false ,
158
+ );
159
+ isOnCalendar[event.index] = false ;
160
+ emit (state.copyWith (
161
+ status: ContestStatus .success,
162
+ isOnCalendarUpcomingContests: isOnCalendar,
163
+ ));
164
+ } else {
165
+ Fluttertoast .showToast (
166
+ msg: "일정을 등록합니다." ,
167
+ toastLength: Toast .LENGTH_SHORT ,
168
+ gravity: ToastGravity .CENTER ,
169
+ timeInSecForIosWeb: 1 ,
170
+ backgroundColor: MySolvedColor .main.withOpacity (0.8 ),
171
+ textColor: Colors .white,
172
+ fontSize: 16.0 );
173
+
174
+ final Event calendarEvent = Event (
175
+ title: contest.name,
176
+ description: contest.url,
177
+ startDate: contest.startTime,
178
+ endDate: contest.endTime,
179
+ iosParams: IOSParams (
180
+ url: contest.url,
181
+ ),
182
+ );
183
+ Add2Calendar .addEvent2Cal (calendarEvent);
184
+
185
+ await _sharedPreferencesRepository.setIsOnContestCalendar (
186
+ title: contest.name,
187
+ isOn: true ,
188
+ );
189
+ isOnCalendar[event.index] = true ;
190
+ emit (state.copyWith (
191
+ status: ContestStatus .success,
192
+ isOnCalendarUpcomingContests: isOnCalendar,
193
+ ));
194
+ }
195
+
196
+ emit (state.copyWith (status: ContestStatus .success));
197
+ }
198
+
124
199
void _onSegmentedControlPressed (
125
200
ContestSegmentedControlPressed event,
126
201
Emitter <ContestState > emit,
0 commit comments