Files
syncrow-web/lib/pages/device_managment/water_heater/models/schedule_model.dart
2024-09-22 20:47:34 +03:00

126 lines
3.3 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
import 'package:flutter/foundation.dart';
class ScheduleModel {
final String scheduleId;
final String category;
final String time;
final Status function;
final List<String> days;
final TimeOfDay? timeOfDay;
final List<bool>? selectedDays;
ScheduleModel({
required this.category,
required this.time,
required this.function,
required this.days,
this.timeOfDay,
this.selectedDays,
this.scheduleId = '',
});
Map<String, dynamic> toMap() {
return {
'category': category,
'time': time,
'function': function.toMap(),
'days': days,
};
}
factory ScheduleModel.fromMap(Map<String, dynamic> map) {
return ScheduleModel(
scheduleId: map['scheduleId'] ?? '',
category: map['category'] ?? '',
time: map['time'] ?? '',
function: Status.fromMap(map['function']),
days: List<String>.from(map['days']),
timeOfDay:
parseTimeOfDay(map['time']),
selectedDays:
parseSelectedDays(map['days']),
);
}
String toJson() => json.encode(toMap());
factory ScheduleModel.fromJson(String source) =>
ScheduleModel.fromMap(json.decode(source));
ScheduleModel copyWith({
String? category,
String? time,
Status? function,
List<String>? days,
TimeOfDay? timeOfDay,
List<bool>? selectedDays,
String? scheduleId,
}) {
return ScheduleModel(
category: category ?? this.category,
time: time ?? this.time,
function: function ?? this.function,
days: days ?? this.days,
timeOfDay: timeOfDay ?? this.timeOfDay,
selectedDays: selectedDays ?? this.selectedDays,
scheduleId: scheduleId ?? this.scheduleId,
);
}
static TimeOfDay? parseTimeOfDay(String isoTime) {
try {
final dateTime = DateTime.parse(isoTime);
return TimeOfDay(hour: dateTime.hour, minute: dateTime.minute);
} catch (e) {
return null;
}
}
static List<bool> parseSelectedDays(List<String> days) {
const allDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
return allDays.map((day) => days.contains(day)).toList();
}
static List<String> convertSelectedDaysToStrings(List<bool> selectedDays) {
const allDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
List<String> result = [];
for (int i = 0; i < selectedDays.length; i++) {
if (selectedDays[i]) {
result.add(allDays[i]);
}
}
return result;
}
@override
String toString() {
return 'ScheduleModel(category: $category, time: $time, function: $function, days: $days, timeOfDay: $timeOfDay, selectedDays: $selectedDays)';
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is ScheduleModel &&
other.category == category &&
other.time == time &&
other.function == function &&
listEquals(other.days, days) &&
timeOfDay == other.timeOfDay &&
listEquals(other.selectedDays, selectedDays);
}
@override
int get hashCode {
return category.hashCode ^
time.hashCode ^
function.hashCode ^
days.hashCode ^
timeOfDay.hashCode ^
selectedDays.hashCode;
}
}