Added timer id to Timer and TimerEntity

Signed-off-by: Andreas Fahrecker <AndreasFahrecker@gmail.com>
This commit is contained in:
Andreas Fahrecker 2020-10-13 15:03:46 +02:00
parent 88a9026966
commit e4c0a50ece
2 changed files with 27 additions and 11 deletions

View File

@ -1,33 +1,45 @@
import 'package:meta/meta.dart';
import 'package:time_progress_calculator/persistence/timer_entity.dart';
import 'package:time_progress_calculator/uuid.dart';
@immutable
class Timer {
final String id;
final DateTime startTime;
final DateTime endTime;
Timer(this.startTime, this.endTime);
Timer(this.startTime, this.endTime, {String id})
: id = id ?? Uuid().generateV4();
Timer copyWith({DateTime startTime, DateTime endTime}) {
return Timer(startTime ?? this.startTime, endTime ?? this.endTime);
Timer copyWith({String id, DateTime startTime, DateTime endTime}) {
return Timer(
startTime ?? this.startTime,
endTime ?? this.endTime,
id: id ?? this.id,
);
}
@override
int get hashCode => startTime.hashCode ^ endTime.hashCode;
int get hashCode => id.hashCode ^ startTime.hashCode ^ endTime.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Timer &&
runtimeType == other.runtimeType &&
id == other.id &&
startTime == other.startTime &&
endTime == other.endTime;
TimerEntity toEntity() {
return TimerEntity(startTime, endTime);
return TimerEntity(id, startTime, endTime);
}
static Timer fromEntity(TimerEntity entity) {
return Timer(entity.startTime, entity.endTime);
return Timer(
entity.startTime,
entity.endTime,
id: entity.id ?? Uuid().generateV4(),
);
}
}

View File

@ -1,32 +1,36 @@
class TimerEntity {
final String id;
final DateTime startTime;
final DateTime endTime;
TimerEntity(this.startTime, this.endTime);
TimerEntity(this.id, this.startTime, this.endTime);
@override
int get hashCode => startTime.hashCode ^ endTime.hashCode;
int get hashCode => id.hashCode ^ startTime.hashCode ^ endTime.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is TimerEntity &&
runtimeType == other.runtimeType &&
id == other.id &&
startTime == other.startTime &&
endTime == other.endTime;
Map<String, Object> toJson() {
return {
"id": id,
"startTime": startTime.millisecondsSinceEpoch,
"endTime": startTime.millisecondsSinceEpoch
};
}
static TimerEntity fromJson(Map<String, Object> json) {
DateTime startTime =
final String id = json["id"] as String;
final DateTime startTime =
DateTime.fromMillisecondsSinceEpoch(json["startTime"] as int);
DateTime endTime =
final DateTime endTime =
DateTime.fromMillisecondsSinceEpoch(json["endTime"] as int);
return TimerEntity(startTime, endTime);
return TimerEntity(id, startTime, endTime);
}
}