import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; class PdfHistoryEntry { PdfHistoryEntry({ required this.at, required this.title, required this.rechnungsnummer, }); final DateTime at; final String title; final String rechnungsnummer; Map toJson() => { 'at': at.toIso8601String(), 'title': title, 'rechnungsnummer': rechnungsnummer, }; factory PdfHistoryEntry.fromJson(Map m) { return PdfHistoryEntry( at: DateTime.tryParse(m['at'] as String? ?? '') ?? DateTime.now(), title: m['title'] as String? ?? '', rechnungsnummer: m['rechnungsnummer'] as String? ?? '', ); } } /// Lokaler Verlauf geteilter/erzeugter PDFs (nur Metadaten, keine Dateiablage). class PdfHistoryService { PdfHistoryService._(); static const _key = 'handwerkpro_pdf_history_v1'; static const _max = 30; static Future> load() async { final p = await SharedPreferences.getInstance(); final raw = p.getString(_key); if (raw == null || raw.isEmpty) return []; try { final list = jsonDecode(raw) as List; return list .map((e) => PdfHistoryEntry.fromJson(Map.from(e as Map))) .toList(); } catch (_) { return []; } } static Future add({ required String title, required String rechnungsnummer, }) async { final p = await SharedPreferences.getInstance(); final existing = await load(); existing.insert( 0, PdfHistoryEntry( at: DateTime.now(), title: title, rechnungsnummer: rechnungsnummer, ), ); final trimmed = existing.take(_max).toList(); await p.setString( _key, jsonEncode(trimmed.map((e) => e.toJson()).toList()), ); } static Future clear() async { final p = await SharedPreferences.getInstance(); await p.remove(_key); } }