CalDAV and Google calendars can now be written to from the journal on
the web and in the phone app. Each change goes to the source at once
with If-Match set to the ETag the last sync read, so a change made
elsewhere is refused (409) instead of overwritten; the account is then
read again. Repeating events, iCal feeds and read-only Google calendars
stay read-only.
- calendar_events keep each event's calendar, object/event id, ETag and
whether it repeats; accounts keep their writable calendars and the
one new events go to (migration 1764800000_calendar_writes).
- Google sign-in asks for calendar.events; older read-only accounts are
offered "Connect again".
- New API: POST /calendar/events, PATCH and DELETE /calendar/events/{id}.
- Translations, Docs-Site guides and changelog updated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
385 lines
11 KiB
Dart
385 lines
11 KiB
Dart
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../api/docwell_api.dart';
|
|
import '../l10n.dart';
|
|
|
|
/// What [showEventEditor] did, so the journal knows to read its events again.
|
|
enum EventEditResult { saved, deleted, stale }
|
|
|
|
/// Opens a sheet to add an event on [day] to one of [calendars] (those whose
|
|
/// canWrite is true), or to change or delete [event] (ADR-19). The change
|
|
/// goes to the calendar at once. When it changed in its calendar since the
|
|
/// last sync, the server refuses and the sheet closes with
|
|
/// [EventEditResult.stale] after saying so.
|
|
Future<EventEditResult?> showEventEditor(
|
|
BuildContext context, {
|
|
required DocwellApi api,
|
|
required DateTime day,
|
|
required List<CalendarAccount> calendars,
|
|
CalendarEvent? event,
|
|
}) => showModalBottomSheet<EventEditResult>(
|
|
context: context,
|
|
isScrollControlled: true,
|
|
showDragHandle: true,
|
|
builder: (_) =>
|
|
EventEditor(api: api, day: day, calendars: calendars, event: event),
|
|
);
|
|
|
|
DateTime _date(DateTime d) => DateTime(d.year, d.month, d.day);
|
|
|
|
String _two(int n) => n.toString().padLeft(2, '0');
|
|
|
|
/// A date as the server takes it for all-day events.
|
|
String eventDay(DateTime d) => '${d.year}-${_two(d.month)}-${_two(d.day)}';
|
|
|
|
/// The input sent for an event: all-day ones as dates (lastDay inclusive on
|
|
/// screen, exclusive on the wire), timed ones as UTC instants.
|
|
CalendarEventInput eventInput({
|
|
String? calendar,
|
|
required String title,
|
|
required String location,
|
|
required bool allDay,
|
|
required DateTime start,
|
|
required DateTime end,
|
|
}) {
|
|
if (allDay) {
|
|
final first = _date(start);
|
|
var last = _date(end);
|
|
if (last.isBefore(first)) last = first;
|
|
return CalendarEventInput(
|
|
calendar: calendar,
|
|
title: title.trim(),
|
|
location: location.trim(),
|
|
allDay: true,
|
|
start: eventDay(first),
|
|
end: eventDay(last.add(const Duration(days: 1))),
|
|
);
|
|
}
|
|
return CalendarEventInput(
|
|
calendar: calendar,
|
|
title: title.trim(),
|
|
location: location.trim(),
|
|
allDay: false,
|
|
start: start.toUtc().toIso8601String(),
|
|
end: end.toUtc().toIso8601String(),
|
|
);
|
|
}
|
|
|
|
class EventEditor extends StatefulWidget {
|
|
const EventEditor({
|
|
super.key,
|
|
required this.api,
|
|
required this.day,
|
|
required this.calendars,
|
|
this.event,
|
|
});
|
|
|
|
final DocwellApi api;
|
|
final DateTime day;
|
|
final List<CalendarAccount> calendars;
|
|
final CalendarEvent? event;
|
|
|
|
@override
|
|
State<EventEditor> createState() => _EventEditorState();
|
|
}
|
|
|
|
class _EventEditorState extends State<EventEditor> {
|
|
late final TextEditingController _title;
|
|
late final TextEditingController _location;
|
|
late bool _allDay;
|
|
// For all-day events, _end is the last day (inclusive).
|
|
late DateTime _start;
|
|
late DateTime _end;
|
|
String? _calendar;
|
|
bool _busy = false;
|
|
String _error = '';
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final e = widget.event;
|
|
_title = TextEditingController(text: e?.title ?? '');
|
|
_location = TextEditingController(text: e?.location ?? '');
|
|
_allDay = e?.allDay ?? false;
|
|
if (e == null) {
|
|
final hour = (DateTime.now().hour + 1).clamp(0, 23);
|
|
_start = DateTime(
|
|
widget.day.year,
|
|
widget.day.month,
|
|
widget.day.day,
|
|
hour,
|
|
);
|
|
_end = _start.add(const Duration(hours: 1));
|
|
_calendar = widget.calendars.isEmpty ? null : widget.calendars.first.id;
|
|
} else if (e.allDay) {
|
|
_start = DateTime.parse(e.start);
|
|
_end = DateTime.parse(e.end).subtract(const Duration(days: 1));
|
|
} else {
|
|
_start = DateTime.parse(e.start).toLocal();
|
|
_end = DateTime.parse(e.end).toLocal();
|
|
}
|
|
_title.addListener(() => setState(() {}));
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_title.dispose();
|
|
_location.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
bool get _valid =>
|
|
_title.text.trim().isNotEmpty && (_allDay || _end.isAfter(_start));
|
|
|
|
Future<void> _pickDate(bool start) async {
|
|
final current = start ? _start : _end;
|
|
final picked = await showDatePicker(
|
|
context: context,
|
|
initialDate: current,
|
|
firstDate: DateTime(current.year - 5),
|
|
lastDate: DateTime(current.year + 5),
|
|
);
|
|
if (picked == null || !mounted) return;
|
|
setState(() {
|
|
final moved = DateTime(
|
|
picked.year,
|
|
picked.month,
|
|
picked.day,
|
|
current.hour,
|
|
current.minute,
|
|
);
|
|
if (start) {
|
|
// Keep the length when the start moves.
|
|
final length = _end.difference(_start);
|
|
_start = moved;
|
|
_end = moved.add(length);
|
|
} else {
|
|
_end = moved;
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _pickTime(bool start) async {
|
|
final current = start ? _start : _end;
|
|
final picked = await showTimePicker(
|
|
context: context,
|
|
initialTime: TimeOfDay.fromDateTime(current),
|
|
);
|
|
if (picked == null || !mounted) return;
|
|
setState(() {
|
|
final moved = DateTime(
|
|
current.year,
|
|
current.month,
|
|
current.day,
|
|
picked.hour,
|
|
picked.minute,
|
|
);
|
|
if (start) {
|
|
final length = _end.difference(_start);
|
|
_start = moved;
|
|
_end = moved.add(length);
|
|
} else {
|
|
_end = moved;
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _run(
|
|
Future<void> Function() action,
|
|
EventEditResult done,
|
|
) async {
|
|
setState(() {
|
|
_busy = true;
|
|
_error = '';
|
|
});
|
|
try {
|
|
await action();
|
|
if (mounted) Navigator.of(context).pop(done);
|
|
} on DocwellApiException catch (e) {
|
|
if (!mounted) return;
|
|
if (e.status == 409 || e.status == 404) {
|
|
// Nothing was overwritten; the journal reads the calendar again.
|
|
final messenger = ScaffoldMessenger.of(context);
|
|
Navigator.of(context).pop(EventEditResult.stale);
|
|
messenger.showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
e.explanation.isNotEmpty
|
|
? e.explanation
|
|
: tr(e.status == 409 ? 'eventStale' : 'eventGone'),
|
|
),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
setState(
|
|
() => _error = e.explanation.isNotEmpty
|
|
? e.explanation
|
|
: tr('eventSaveFailed'),
|
|
);
|
|
} catch (_) {
|
|
if (mounted) setState(() => _error = tr('errOffline'));
|
|
} finally {
|
|
if (mounted) setState(() => _busy = false);
|
|
}
|
|
}
|
|
|
|
Future<void> _save() {
|
|
final input = eventInput(
|
|
calendar: widget.event == null ? _calendar : null,
|
|
title: _title.text,
|
|
location: _location.text,
|
|
allDay: _allDay,
|
|
start: _start,
|
|
end: _end,
|
|
);
|
|
final e = widget.event;
|
|
return _run(
|
|
() => e == null
|
|
? widget.api.createCalendarEvent(input)
|
|
: widget.api.updateCalendarEvent(e.id, input),
|
|
EventEditResult.saved,
|
|
);
|
|
}
|
|
|
|
Future<void> _delete() async {
|
|
final e = widget.event!;
|
|
final ok = await showDialog<bool>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
content: Text(tr('confirmDeleteEvent', {'title': e.title})),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, false),
|
|
child: Text(tr('cancel')),
|
|
),
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, true),
|
|
child: Text(tr('deleteEvent')),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (ok != true || !mounted) return;
|
|
await _run(
|
|
() => widget.api.deleteCalendarEvent(e.id),
|
|
EventEditResult.deleted,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final loc = MaterialLocalizations.of(context);
|
|
final h24 = MediaQuery.of(context).alwaysUse24HourFormat;
|
|
String date(DateTime d) => loc.formatMediumDate(d);
|
|
String time(DateTime d) => loc.formatTimeOfDay(
|
|
TimeOfDay.fromDateTime(d),
|
|
alwaysUse24HourFormat: h24,
|
|
);
|
|
Widget row(String label, bool start) {
|
|
final d = start ? _start : _end;
|
|
return Row(
|
|
children: [
|
|
Expanded(child: Text(label)),
|
|
TextButton(
|
|
key: Key(start ? 'event-start-date' : 'event-end-date'),
|
|
onPressed: _busy ? null : () => _pickDate(start),
|
|
child: Text(date(d)),
|
|
),
|
|
if (!_allDay)
|
|
TextButton(
|
|
key: Key(start ? 'event-start-time' : 'event-end-time'),
|
|
onPressed: _busy ? null : () => _pickTime(start),
|
|
child: Text(time(d)),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
return Padding(
|
|
padding: EdgeInsets.fromLTRB(
|
|
16,
|
|
0,
|
|
16,
|
|
16 + MediaQuery.of(context).viewInsets.bottom,
|
|
),
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
key: const Key('event-editor'),
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(
|
|
tr(widget.event == null ? 'addEvent' : 'editEvent'),
|
|
style: Theme.of(context).textTheme.titleMedium,
|
|
),
|
|
const SizedBox(height: 8),
|
|
TextField(
|
|
key: const Key('event-title'),
|
|
controller: _title,
|
|
autofocus: widget.event == null,
|
|
textCapitalization: TextCapitalization.sentences,
|
|
decoration: InputDecoration(labelText: tr('eventTitle')),
|
|
),
|
|
SwitchListTile(
|
|
key: const Key('event-all-day'),
|
|
contentPadding: EdgeInsets.zero,
|
|
title: Text(tr('allDay')),
|
|
value: _allDay,
|
|
onChanged: _busy ? null : (v) => setState(() => _allDay = v),
|
|
),
|
|
row(tr('eventStart'), true),
|
|
row(tr(_allDay ? 'eventLastDay' : 'eventEnd'), false),
|
|
TextField(
|
|
controller: _location,
|
|
decoration: InputDecoration(labelText: tr('eventLocation')),
|
|
),
|
|
if (widget.event == null && widget.calendars.length > 1)
|
|
DropdownButtonFormField<String>(
|
|
initialValue: _calendar,
|
|
decoration: InputDecoration(labelText: tr('eventCalendar')),
|
|
items: [
|
|
for (final c in widget.calendars)
|
|
DropdownMenuItem(value: c.id, child: Text(c.name)),
|
|
],
|
|
onChanged: _busy ? null : (v) => setState(() => _calendar = v),
|
|
),
|
|
if (_error.isNotEmpty)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 8),
|
|
child: Text(
|
|
_error,
|
|
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
Row(
|
|
children: [
|
|
if (widget.event != null)
|
|
TextButton(
|
|
key: const Key('event-delete'),
|
|
onPressed: _busy ? null : _delete,
|
|
child: Text(tr('deleteEvent')),
|
|
),
|
|
const Spacer(),
|
|
TextButton(
|
|
onPressed: _busy ? null : () => Navigator.of(context).pop(),
|
|
child: Text(tr('cancel')),
|
|
),
|
|
const SizedBox(width: 8),
|
|
FilledButton(
|
|
key: const Key('event-save'),
|
|
onPressed: _busy || !_valid ? null : _save,
|
|
child: Text(tr('saveEvent')),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|