-
Notifications
You must be signed in to change notification settings - Fork 12
/
calendar_selection_viewmodel.dart
82 lines (71 loc) · 2.25 KB
/
calendar_selection_viewmodel.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import 'dart:collection';
import 'package:device_calendar/device_calendar.dart';
import 'package:ets_api_clients/models.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:notredame/core/managers/course_repository.dart';
import 'package:notredame/core/utils/calendar_utils.dart';
import 'package:notredame/locator.dart';
class CalendarSelectionViewModel {
final AppIntl translations;
late UnmodifiableListView<Calendar>? _calendars;
final CourseRepository courseRepository = locator<CourseRepository>();
String? selectedCalendarId;
News? news;
CalendarSelectionViewModel({
required this.translations,
this.news,
});
Future<void> fetchCalendars() async {
_calendars = await CalendarUtils.nativeCalendars;
}
List<DropdownMenuItem<String>> getDropdownItems() {
if (_calendars == null) return [];
return _calendars!
.map<DropdownMenuItem<String>>(
(Calendar value) => DropdownMenuItem<String>(
value: value.name,
child: Text(value.name ?? ""),
),
)
.toList();
}
Future<void> exportCalendar(BuildContext context) async {
if (selectedCalendarId == null || selectedCalendarId?.isEmpty == true) {
_showToast(translations.calendar_select);
return;
}
Navigator.of(context).pop();
if (news != null) {
_exportNews(news!, selectedCalendarId!);
} else {
_exportCourses(selectedCalendarId!);
}
}
void _exportNews(News news, String selectedCalendarId) {
CalendarUtils.exportNews(news, selectedCalendarId).then((value) {
if (value) {
_showToast(translations.news_export_success);
} else {
_showToast(translations.news_export_error);
}
});
}
void _exportCourses(String selectedCalendarId) {
CalendarUtils.export(
courseRepository.coursesActivities!, selectedCalendarId)
.then((value) {
if (value) {
_showToast(translations.calendar_export_success);
} else {
_showToast(translations.calendar_export_error);
}
});
}
void _showToast(String message) {
Fluttertoast.showToast(
msg: message,
);
}
}