refactor schedule view and cleaning

This commit is contained in:
ashrafzarkanisala
2024-09-22 20:47:34 +03:00
parent b3d891b2c8
commit 3a28f0ef9a
21 changed files with 1540 additions and 902 deletions

View File

@ -1,49 +1,27 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
/*
{
"category": "kg",
"time": "2024-09-22T10:31:54Z",
"function": {
"code": "switch_1",
"value": true
},
"days": [
"Sun"
]
}
*/
import 'package:flutter/foundation.dart';
class ScheduleModel {
final String scheduleId;
final String category;
final String time;
final Status function;
final List<String> days;
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 = '',
});
ScheduleModel copyWith({
String? category,
String? time,
Status? function,
List<String>? days,
}) {
return ScheduleModel(
category: category ?? this.category,
time: time ?? this.time,
function: function ?? this.function,
days: days ?? this.days,
);
}
Map<String, dynamic> toMap() {
return {
'category': category,
@ -55,10 +33,15 @@ class ScheduleModel {
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']),
);
}
@ -67,9 +50,54 @@ class ScheduleModel {
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 'SendSchedule(category: $category, time: $time, function: $function, days: $days)';
return 'ScheduleModel(category: $category, time: $time, function: $function, days: $days, timeOfDay: $timeOfDay, selectedDays: $selectedDays)';
}
@override
@ -80,7 +108,9 @@ class ScheduleModel {
other.category == category &&
other.time == time &&
other.function == function &&
listEquals(other.days, days);
listEquals(other.days, days) &&
timeOfDay == other.timeOfDay &&
listEquals(other.selectedDays, selectedDays);
}
@override
@ -88,6 +118,8 @@ class ScheduleModel {
return category.hashCode ^
time.hashCode ^
function.hashCode ^
days.hashCode;
days.hashCode ^
timeOfDay.hashCode ^
selectedDays.hashCode;
}
}