import 'dart:convert'; /// Data model representing a single checklist item for a trip. /// /// Each item stores a unique [id], the [label] to display, its completion /// status via [isDone], and optional timestamps to support ordering or /// future reminders. The model includes JSON helpers to simplify /// persistence with `SharedPreferences`. class ChecklistItem { /// Unique identifier of the checklist item. final String id; /// Human‑readable text describing the task to complete. final String label; /// Indicates whether the task has been completed. final bool isDone; /// Creation timestamp used to keep a stable order in the list UI. final DateTime createdAt; /// Optional due date for the task; can be leveraged by reminders later. final DateTime? dueDate; /// Creates a checklist item. const ChecklistItem({ required this.id, required this.label, required this.isDone, required this.createdAt, this.dueDate, }); /// Builds a new item in the pending state with the current timestamp. factory ChecklistItem.newItem({ required String id, required String label, DateTime? dueDate, }) { return ChecklistItem( id: id, label: label, isDone: false, createdAt: DateTime.now().toUtc(), dueDate: dueDate, ); } /// Creates a copy with updated fields while keeping immutability. ChecklistItem copyWith({ String? id, String? label, bool? isDone, DateTime? createdAt, DateTime? dueDate, }) { return ChecklistItem( id: id ?? this.id, label: label ?? this.label, isDone: isDone ?? this.isDone, createdAt: createdAt ?? this.createdAt, dueDate: dueDate ?? this.dueDate, ); } /// Serializes the item to JSON for storage. Map toJson() { return { 'id': id, 'label': label, 'isDone': isDone, 'createdAt': createdAt.toIso8601String(), 'dueDate': dueDate?.toIso8601String(), }; } /// Deserializes a checklist item from JSON. factory ChecklistItem.fromJson(Map json) { return ChecklistItem( id: json['id'] as String, label: json['label'] as String, isDone: json['isDone'] as bool? ?? false, createdAt: DateTime.parse(json['createdAt'] as String), dueDate: json['dueDate'] != null ? DateTime.tryParse(json['dueDate'] as String) : null, ); } /// Encodes a list of checklist items to a JSON string. static String encodeList(List items) { final jsonList = items.map((item) => item.toJson()).toList(); return json.encode(jsonList); } /// Decodes a list of checklist items from a JSON string. static List decodeList(String jsonString) { final decoded = json.decode(jsonString) as List; return decoded .cast>() .map(ChecklistItem.fromJson) .toList(); } }