runable
This commit is contained in:
131
lib/features/scanner/data/models/scan_item.dart
Normal file
131
lib/features/scanner/data/models/scan_item.dart
Normal file
@@ -0,0 +1,131 @@
|
||||
import 'package:hive_ce/hive.dart';
|
||||
import '../../domain/entities/scan_entity.dart';
|
||||
|
||||
part 'scan_item.g.dart';
|
||||
|
||||
/// Data model for ScanEntity with Hive annotations for local storage
|
||||
/// This is the data layer representation that can be persisted
|
||||
@HiveType(typeId: 0)
|
||||
class ScanItem extends HiveObject {
|
||||
@HiveField(0)
|
||||
final String barcode;
|
||||
|
||||
@HiveField(1)
|
||||
final DateTime timestamp;
|
||||
|
||||
@HiveField(2)
|
||||
final String field1;
|
||||
|
||||
@HiveField(3)
|
||||
final String field2;
|
||||
|
||||
@HiveField(4)
|
||||
final String field3;
|
||||
|
||||
@HiveField(5)
|
||||
final String field4;
|
||||
|
||||
ScanItem({
|
||||
required this.barcode,
|
||||
required this.timestamp,
|
||||
this.field1 = '',
|
||||
this.field2 = '',
|
||||
this.field3 = '',
|
||||
this.field4 = '',
|
||||
});
|
||||
|
||||
/// Convert from domain entity to data model
|
||||
factory ScanItem.fromEntity(ScanEntity entity) {
|
||||
return ScanItem(
|
||||
barcode: entity.barcode,
|
||||
timestamp: entity.timestamp,
|
||||
field1: entity.field1,
|
||||
field2: entity.field2,
|
||||
field3: entity.field3,
|
||||
field4: entity.field4,
|
||||
);
|
||||
}
|
||||
|
||||
/// Convert to domain entity
|
||||
ScanEntity toEntity() {
|
||||
return ScanEntity(
|
||||
barcode: barcode,
|
||||
timestamp: timestamp,
|
||||
field1: field1,
|
||||
field2: field2,
|
||||
field3: field3,
|
||||
field4: field4,
|
||||
);
|
||||
}
|
||||
|
||||
/// Create from JSON (useful for API responses)
|
||||
factory ScanItem.fromJson(Map<String, dynamic> json) {
|
||||
return ScanItem(
|
||||
barcode: json['barcode'] ?? '',
|
||||
timestamp: json['timestamp'] != null
|
||||
? DateTime.parse(json['timestamp'])
|
||||
: DateTime.now(),
|
||||
field1: json['field1'] ?? '',
|
||||
field2: json['field2'] ?? '',
|
||||
field3: json['field3'] ?? '',
|
||||
field4: json['field4'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/// Convert to JSON (useful for API requests)
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'barcode': barcode,
|
||||
'timestamp': timestamp.toIso8601String(),
|
||||
'field1': field1,
|
||||
'field2': field2,
|
||||
'field3': field3,
|
||||
'field4': field4,
|
||||
};
|
||||
}
|
||||
|
||||
/// Create a copy with updated fields
|
||||
ScanItem copyWith({
|
||||
String? barcode,
|
||||
DateTime? timestamp,
|
||||
String? field1,
|
||||
String? field2,
|
||||
String? field3,
|
||||
String? field4,
|
||||
}) {
|
||||
return ScanItem(
|
||||
barcode: barcode ?? this.barcode,
|
||||
timestamp: timestamp ?? this.timestamp,
|
||||
field1: field1 ?? this.field1,
|
||||
field2: field2 ?? this.field2,
|
||||
field3: field3 ?? this.field3,
|
||||
field4: field4 ?? this.field4,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'ScanItem{barcode: $barcode, timestamp: $timestamp, field1: $field1, field2: $field2, field3: $field3, field4: $field4}';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is ScanItem &&
|
||||
runtimeType == other.runtimeType &&
|
||||
barcode == other.barcode &&
|
||||
timestamp == other.timestamp &&
|
||||
field1 == other.field1 &&
|
||||
field2 == other.field2 &&
|
||||
field3 == other.field3 &&
|
||||
field4 == other.field4;
|
||||
|
||||
@override
|
||||
int get hashCode =>
|
||||
barcode.hashCode ^
|
||||
timestamp.hashCode ^
|
||||
field1.hashCode ^
|
||||
field2.hashCode ^
|
||||
field3.hashCode ^
|
||||
field4.hashCode;
|
||||
}
|
||||
Reference in New Issue
Block a user