MIF_E31222656/lib/data/models/crop_schedule_model.dart

75 lines
2.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:tugas_akhir_supabase/domain/entities/crop_schedule.dart';
class CropScheduleModel extends CropSchedule {
const CropScheduleModel({
required String id,
required String cropName,
required DateTime startDate,
required DateTime endDate,
required int plot,
required String? fieldId,
String? status,
String? notes,
required Color color,
}) : super(
id: id,
cropName: cropName,
startDate: startDate,
endDate: endDate,
plot: plot,
fieldId: fieldId,
status: status,
notes: notes,
color: color,
);
factory CropScheduleModel.fromJson(Map<String, dynamic> json) {
DateTime parseDate(dynamic value) {
if (value is String) return DateTime.tryParse(value) ?? DateTime(2000);
if (value is DateTime) return value;
return DateTime(2000);
}
return CropScheduleModel(
id: json['id'] as String,
cropName: json['crop_name'] ?? 'Tanaman',
startDate: parseDate(json['start_date']),
endDate: parseDate(json['end_date']),
plot: json['plot'] is int
? json['plot']
: int.tryParse(json['plot'].toString()) ?? 0,
fieldId: json['field_id'],
status: json['status'],
notes: json['notes'],
color: CropSchedule.generateColor(json['id'] as String),
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'crop_name': cropName,
'start_date': startDate.toIso8601String(),
'end_date': endDate.toIso8601String(),
'plot': plot,
'field_id': fieldId,
'status': status,
'notes': notes,
};
}
factory CropScheduleModel.fromEntity(CropSchedule entity) {
return CropScheduleModel(
id: entity.id,
cropName: entity.cropName,
startDate: entity.startDate,
endDate: entity.endDate,
plot: entity.plot,
fieldId: entity.fieldId,
status: entity.status,
notes: entity.notes,
color: entity.color,
);
}
}