phase 1 complete - local storage and aching
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
/// Data model representing an item stored in local storage.
|
||||
///
|
||||
/// This model is used for storing items (e.g., image metadata or records)
|
||||
/// in the local database. Each item has a unique ID and can contain
|
||||
/// arbitrary JSON data.
|
||||
class Item {
|
||||
/// Unique identifier for the item.
|
||||
final String id;
|
||||
|
||||
/// JSON-serializable data stored with the item.
|
||||
final Map<String, dynamic> data;
|
||||
|
||||
/// Timestamp when the item was created (milliseconds since epoch).
|
||||
final int createdAt;
|
||||
|
||||
/// Timestamp when the item was last updated (milliseconds since epoch).
|
||||
final int updatedAt;
|
||||
|
||||
/// Creates an [Item] instance.
|
||||
///
|
||||
/// [id] - Unique identifier for the item.
|
||||
/// [data] - JSON-serializable data to store.
|
||||
/// [createdAt] - Creation timestamp (defaults to current time).
|
||||
/// [updatedAt] - Update timestamp (defaults to current time).
|
||||
Item({
|
||||
required this.id,
|
||||
required this.data,
|
||||
int? createdAt,
|
||||
int? updatedAt,
|
||||
}) : createdAt = createdAt ?? DateTime.now().millisecondsSinceEpoch,
|
||||
updatedAt = updatedAt ?? DateTime.now().millisecondsSinceEpoch;
|
||||
|
||||
/// Creates an [Item] from a database row (Map).
|
||||
factory Item.fromMap(Map<String, dynamic> map) {
|
||||
return Item(
|
||||
id: map['id'] as String,
|
||||
data: map['data'] as Map<String, dynamic>,
|
||||
createdAt: map['created_at'] as int,
|
||||
updatedAt: map['updated_at'] as int,
|
||||
);
|
||||
}
|
||||
|
||||
/// Converts the [Item] to a Map for database storage.
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'id': id,
|
||||
'data': data,
|
||||
'created_at': createdAt,
|
||||
'updated_at': updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/// Creates a copy of this [Item] with updated fields.
|
||||
Item copyWith({
|
||||
String? id,
|
||||
Map<String, dynamic>? data,
|
||||
int? createdAt,
|
||||
int? updatedAt,
|
||||
}) {
|
||||
return Item(
|
||||
id: id ?? this.id,
|
||||
data: data ?? this.data,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
updatedAt: updatedAt ?? this.updatedAt,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Item(id: $id, data: $data, createdAt: $createdAt, updatedAt: $updatedAt)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
return other is Item &&
|
||||
other.id == id &&
|
||||
other.createdAt == createdAt &&
|
||||
other.updatedAt == updatedAt;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => id.hashCode ^ createdAt.hashCode ^ updatedAt.hashCode;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user