Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: save lectures to device calendar #1483

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions packages/uni_app/lib/view/schedule/schedule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import 'package:uni/view/common_widgets/pages_layouts/secondary/secondary.dart';
import 'package:uni/view/lazy_consumer.dart';
import 'package:uni/view/locale_notifier.dart';
import 'package:uni/view/schedule/widgets/schedule_slot.dart';
import 'package:device_calendar/device_calendar.dart';
import 'package:flutter/services.dart';
import 'package:timezone/timezone.dart' as tz;

class SchedulePage extends StatefulWidget {
SchedulePage({super.key, DateTime? now}) : now = now ?? DateTime.now();
Expand Down Expand Up @@ -60,6 +63,16 @@ class SchedulePageViewState extends State<SchedulePageView>
TabController? tabController;
late final List<Lecture> lecturesThisWeek;

late DeviceCalendarPlugin _deviceCalendarPlugin;
List<Calendar> _calendars = [];
Calendar? _selectedCalendar;

List<Calendar> get _writableCalendars =>
_calendars.where((c) => c.isReadOnly == false).toList();

List<Calendar> get _readOnlyCalendars =>
_calendars.where((c) => c.isReadOnly == true).toList();

@override
void initState() {
super.initState();
Expand All @@ -70,6 +83,8 @@ class SchedulePageViewState extends State<SchedulePageView>

var weekDay = widget.currentWeek.start.weekday;

_deviceCalendarPlugin = DeviceCalendarPlugin();

lecturesThisWeek = <Lecture>[];
widget.currentWeek.weekdays.take(6).forEach((day) {
final lectures = lecturesOfDay(widget.lectures, day);
Expand Down Expand Up @@ -110,6 +125,62 @@ class SchedulePageViewState extends State<SchedulePageView>
final queryData = MediaQuery.of(context);
return Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly, // Adjust alignment as needed
children: <Widget>[
ElevatedButton(
onPressed: () async {
print('retrieve calendars button Pressed');
_retrieveCalendars();
},
child: const Text('Retrieve calendars'),
),
ElevatedButton(
onPressed: () async {
print('add to calendar button Pressed');

if (_calendars.isEmpty) {
print('no calendars on calendar list');
} else {
_selectedCalendar = _calendars.last;

final DateTime tomorrow =
DateTime.now().add(Duration(days: 1));

addLecturesToCalendar(_selectedCalendar!, widget.lectures);
}
},
child: const Text('Add to calendar'),
),
],
),
),
if (_calendars.isNotEmpty)
Padding(
padding: const EdgeInsets.all(8),
child: SizedBox(
height: 150, // Adjust height as needed
child: ListView.builder(
shrinkWrap: true,
itemCount: _calendars.length,
itemBuilder: (context, index) {
final calendar = _calendars[index];
return ListTile(
title: Text(calendar.name ?? 'Unnamed Calendar'),
onTap: () {
setState(() {
_selectedCalendar = calendar;
});
print('Selected Calendar: ${calendar.name}');
},
);
},
),
),
),
TabBar(
controller: tabController,
isScrollable: true,
Expand Down Expand Up @@ -210,4 +281,49 @@ class SchedulePageViewState extends State<SchedulePageView>
),
);
}

void _retrieveCalendars() async {
print("retrieve calendars called");
try {
var permissionsGranted = await _deviceCalendarPlugin.hasPermissions();
print("permissions checked");
if (permissionsGranted.isSuccess &&
(permissionsGranted.data == null ||
permissionsGranted.data == false)) {
permissionsGranted = await _deviceCalendarPlugin.requestPermissions();

if (!permissionsGranted.isSuccess ||
permissionsGranted.data == null ||
permissionsGranted.data == false) {
return;
}
}

final calendarsResult = await _deviceCalendarPlugin.retrieveCalendars();
setState(() {
_calendars = calendarsResult.data as List<Calendar>;
});
} on PlatformException catch (e, s) {
debugPrint('RETRIEVE_CALENDARS: $e, $s');
}
}

void addLecturesToCalendar(
Calendar selectedCalendar, List<Lecture> lectures) {
final now = DateTime.now();

lectures
.where((lecture) => lecture.endTime.isAfter(now))
.forEach((lecture) {
final event = Event(
selectedCalendar.id,
title: '${lecture.subject} (${lecture.typeClass})',
location: lecture.room,
start: tz.TZDateTime.from(lecture.startTime, tz.local),
end: tz.TZDateTime.from(lecture.endTime, tz.local),
);

_deviceCalendarPlugin.createOrUpdateEvent(event);
});
}
}
1 change: 1 addition & 0 deletions packages/uni_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies:
crypto: ^3.0.1
cupertino_icons: ^1.0.2
currency_text_input_formatter: ^2.1.5
device_calendar: ^4.3.3
diacritic: ^0.1.5
email_validator: ^2.0.1
expandable: ^5.0.1
Expand Down
Loading