1+ import 'package:add_2_calendar/add_2_calendar.dart' ;
12import 'package:boj_api/boj_api.dart' ;
23import 'package:contest_notification_repository/contest_notification_repository.dart' ;
34import 'package:contest_repository/contest_repository.dart' ;
45import 'package:equatable/equatable.dart' ;
6+ import 'package:flutter/material.dart' ;
57import 'package:flutter_bloc/flutter_bloc.dart' ;
6- import 'package:meta/meta .dart' ;
8+ import 'package:fluttertoast/fluttertoast .dart' ;
79import 'package:my_solved/features/contest_filter/bloc/contest_filter_bloc.dart' ;
810import 'package:permission_handler/permission_handler.dart' ;
911import 'package:shared_preferences_repository/shared_preferences_repository.dart' ;
1012
13+ import '../../../components/styles/color.dart' ;
14+
1115part 'contest_event.dart' ;
1216part 'contest_state.dart' ;
1317
@@ -31,6 +35,7 @@ class ContestBloc extends Bloc<ContestEvent, ContestState> {
3135 on < ContestInit > (_onInit);
3236 on < ContestSegmentedControlPressed > (_onSegmentedControlPressed);
3337 on < ContestNotificationButtonPressed > (_onNotificationPressed);
38+ on < ContestCalendarButtonPressed > (_onCalendarPressed);
3439 on < ContestFilterTogglePressed > (_onFilterPressed);
3540 }
3641
@@ -48,9 +53,12 @@ class ContestBloc extends Bloc<ContestEvent, ContestState> {
4853 final upcomingContests = result[ContestType .upcoming];
4954
5055 List <bool > isOnContestNotifications = [];
56+ List <bool > isOnContestCalendars = [];
5157 for (Contest contest in upcomingContests ?? []) {
5258 isOnContestNotifications.add (await _sharedPreferencesRepository
5359 .getIsOnContestNotification (title: contest.name));
60+ isOnContestCalendars.add (await _sharedPreferencesRepository
61+ .getIsOnContestCalendar (title: contest.name));
5462 }
5563
5664 emit (state.copyWith (
@@ -59,6 +67,7 @@ class ContestBloc extends Bloc<ContestEvent, ContestState> {
5967 ongoingContests: ongoingContests,
6068 upcomingContests: upcomingContests,
6169 isOnNotificationUpcomingContests: isOnContestNotifications,
70+ isOnCalendarUpcomingContests: isOnContestCalendars,
6271 ));
6372 } catch (e) {
6473 emit (state.copyWith (status: ContestStatus .failure));
@@ -90,6 +99,15 @@ class ContestBloc extends Bloc<ContestEvent, ContestState> {
9099 final minute =
91100 await _sharedPreferencesRepository.getContestNotificationMinute ();
92101
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+
93111 if (isOn) {
94112 await _contestNotificationRepository.cancelContestNotification (
95113 title: contest.name,
@@ -121,6 +139,63 @@ class ContestBloc extends Bloc<ContestEvent, ContestState> {
121139 }
122140 }
123141
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+
124199 void _onSegmentedControlPressed (
125200 ContestSegmentedControlPressed event,
126201 Emitter <ContestState > emit,
0 commit comments