Feature
ein paar feature aber datenbank macht probleme wenn man aufträge speichern möchge
This commit is contained in:
76
services/pdf_history_service.dart
Normal file
76
services/pdf_history_service.dart
Normal file
@@ -0,0 +1,76 @@
|
||||
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<String, dynamic> toJson() => {
|
||||
'at': at.toIso8601String(),
|
||||
'title': title,
|
||||
'rechnungsnummer': rechnungsnummer,
|
||||
};
|
||||
|
||||
factory PdfHistoryEntry.fromJson(Map<String, dynamic> 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<List<PdfHistoryEntry>> 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<dynamic>;
|
||||
return list
|
||||
.map((e) => PdfHistoryEntry.fromJson(Map<String, dynamic>.from(e as Map)))
|
||||
.toList();
|
||||
} catch (_) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> 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<void> clear() async {
|
||||
final p = await SharedPreferences.getInstance();
|
||||
await p.remove(_key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user