feat: compose Android app and verify native calendar and reminders
This commit is contained in:
@@ -170,40 +170,43 @@ class AppController extends ChangeNotifier {
|
||||
}
|
||||
calendarSyncError = null;
|
||||
});
|
||||
Future<void> enableCalendarFromUserAction(String calendarId) =>
|
||||
_run(() async {
|
||||
if (isDemo) throw StateError('示例课表不会导入日历');
|
||||
if (snapshot.settings.semester == null) throw StateError('请先设置学期');
|
||||
if (!await calendar.requestAccess()) {
|
||||
calendarSyncError = '日历权限未获允许';
|
||||
return;
|
||||
}
|
||||
if (snapshot.calendarCleanupPending) {
|
||||
await _cleanupCalendar();
|
||||
}
|
||||
final calendars = await calendar.listWritableCalendars();
|
||||
if (!calendars.any((c) => c.id == calendarId)) {
|
||||
calendarSyncError = '请选择可写入的日历';
|
||||
return;
|
||||
}
|
||||
final settings = snapshot.settings.copyWith(
|
||||
calendarEnabled: true,
|
||||
calendarId: calendarId,
|
||||
);
|
||||
try {
|
||||
final ids = await calendar.sync(
|
||||
ScheduleEngine.occurrences(snapshot.courses, settings.semester!),
|
||||
calendarId,
|
||||
snapshot.calendarEventIds,
|
||||
);
|
||||
await _save(
|
||||
snapshot.copyWith(settings: settings, calendarEventIds: ids),
|
||||
);
|
||||
calendarSyncError = null;
|
||||
} catch (_) {
|
||||
calendarSyncError = '日历同步失败,可重试';
|
||||
}
|
||||
});
|
||||
Future<void> enableCalendarFromUserAction(
|
||||
String calendarId,
|
||||
) => _run(() async {
|
||||
if (isDemo) throw StateError('示例课表不会导入日历');
|
||||
if (snapshot.settings.semester == null) throw StateError('请先设置学期');
|
||||
if (!await calendar.requestAccess()) {
|
||||
calendarSyncError = '日历权限未获允许';
|
||||
return;
|
||||
}
|
||||
if (snapshot.calendarCleanupPending) {
|
||||
await _cleanupCalendar();
|
||||
}
|
||||
final calendars = await calendar.listWritableCalendars();
|
||||
if (!calendars.any((c) => c.id == calendarId)) {
|
||||
calendarSyncError = '请选择可写入的日历';
|
||||
return;
|
||||
}
|
||||
final settings = snapshot.settings.copyWith(
|
||||
calendarEnabled: true,
|
||||
calendarId: calendarId,
|
||||
);
|
||||
// Keep the chosen calendar for recovery if the first native sync fails midway.
|
||||
await _save(
|
||||
snapshot.copyWith(settings: settings.copyWith(calendarEnabled: false)),
|
||||
);
|
||||
try {
|
||||
final ids = await calendar.sync(
|
||||
ScheduleEngine.occurrences(snapshot.courses, settings.semester!),
|
||||
calendarId,
|
||||
snapshot.calendarEventIds,
|
||||
);
|
||||
await _save(snapshot.copyWith(settings: settings, calendarEventIds: ids));
|
||||
calendarSyncError = null;
|
||||
} catch (_) {
|
||||
calendarSyncError = '日历同步失败,可重试';
|
||||
}
|
||||
});
|
||||
Future<void> enableRemindersFromUserAction() => _run(() async {
|
||||
if (isDemo) throw StateError('示例课表不会生成提醒');
|
||||
await notifications.requestPermissions();
|
||||
@@ -225,6 +228,10 @@ class AppController extends ChangeNotifier {
|
||||
Future<void> _cleanupCalendar() async {
|
||||
try {
|
||||
await calendar.deleteOwned(snapshot.calendarEventIds);
|
||||
final calendarId = snapshot.settings.calendarId;
|
||||
if (calendarId != null) {
|
||||
await calendar.deleteOwned(await calendar.ownedEventIds(calendarId));
|
||||
}
|
||||
await _save(
|
||||
snapshot.copyWith(calendarEventIds: {}, calendarCleanupPending: false),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user