58 lines
2.5 KiB
Dart
58 lines
2.5 KiB
Dart
import 'dart:io';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:share_plus/share_plus.dart'; // Ensure share_plus is in dependencies or use printing/share mechanism
|
|
import '../models/cycle_entry.dart';
|
|
|
|
// Since we might not have share_plus in the pubspec explicitly seen earlier (user plan said adding dependencies),
|
|
// keeping it safe. The pubspec had 'pdf', 'printing', 'path_provider', 'universal_html'.
|
|
// 'share_plus' was not explicitly in the list I viewed in Step 258, but 'printing' can share PDF.
|
|
// For ICS, we need a way to share the file. 'printing' relies on pdf.
|
|
// Wait, Step 258 pubspec content lines 9-48...
|
|
// I don't see `share_plus`.
|
|
// I'll check `pubspec.yaml` again to be absolutely sure or add it via `flutter pub add`.
|
|
// Actually, `printing` has a share method but it's specific to PDF bytes usually? No, `Printing.sharePdf`.
|
|
// I should use `share_plus` if I want to share a text/ics file.
|
|
// Or I can just write to file and open it with `open_filex`.
|
|
|
|
import 'package:open_filex/open_filex.dart';
|
|
|
|
class IcsService {
|
|
static Future<void> generateCycleCalendar(List<CycleEntry> entries) async {
|
|
final buffer = StringBuffer();
|
|
buffer.writeln('BEGIN:VCALENDAR');
|
|
buffer.writeln('VERSION:2.0');
|
|
buffer.writeln('PRODID:-//Christian Period Tracker//Cycle Calendar//EN');
|
|
|
|
// Sort entries
|
|
entries.sort((a, b) => a.date.compareTo(b.date));
|
|
|
|
for (var entry in entries) {
|
|
if (entry.isPeriodDay) {
|
|
final dateStr = DateFormat('yyyyMMdd').format(entry.date);
|
|
buffer.writeln('BEGIN:VEVENT');
|
|
buffer.writeln('UID:${entry.id}');
|
|
buffer.writeln('DTSTAMP:${DateFormat('yyyyMMddTHHmmss').format(DateTime.now())}Z');
|
|
buffer.writeln('DTSTART;VALUE=DATE:$dateStr'); // All day event
|
|
buffer.writeln('DTEND;VALUE=DATE:${DateFormat('yyyyMMdd').format(entry.date.add(const Duration(days: 1)))}');
|
|
buffer.writeln('SUMMARY:Period');
|
|
buffer.writeln('DESCRIPTION:Logged period day.');
|
|
buffer.writeln('END:VEVENT');
|
|
}
|
|
}
|
|
|
|
buffer.writeln('END:VCALENDAR');
|
|
|
|
// Save to file
|
|
final directory = await getApplicationDocumentsDirectory();
|
|
final file = File('${directory.path}/cycle_calendar.ics');
|
|
await file.writeAsString(buffer.toString());
|
|
|
|
// Open/Share file
|
|
final result = await OpenFilex.open(file.path);
|
|
if (result.type != ResultType.done) {
|
|
throw 'Could not open file: ${result.message}';
|
|
}
|
|
}
|
|
}
|