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:meta/meta.dart';
import 'package:time_progress_calculator/persistence/timer_entity.dart'; import 'package:time_progress_calculator/persistence/timer_entity.dart';
import 'package:time_progress_calculator/uuid.dart';
@immutable @immutable
class Timer { class Timer {
final String id;
final DateTime startTime; final DateTime startTime;
final DateTime endTime; 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}) { Timer copyWith({String id, DateTime startTime, DateTime endTime}) {
return Timer(startTime ?? this.startTime, endTime ?? this.endTime); return Timer(
startTime ?? this.startTime,
endTime ?? this.endTime,
id: id ?? this.id,
);
} }
@override @override
int get hashCode => startTime.hashCode ^ endTime.hashCode; int get hashCode => id.hashCode ^ startTime.hashCode ^ endTime.hashCode;
@override @override
bool operator ==(Object other) => bool operator ==(Object other) =>
identical(this, other) || identical(this, other) ||
other is Timer && other is Timer &&
runtimeType == other.runtimeType && runtimeType == other.runtimeType &&
id == other.id &&
startTime == other.startTime && startTime == other.startTime &&
endTime == other.endTime; endTime == other.endTime;
TimerEntity toEntity() { TimerEntity toEntity() {
return TimerEntity(startTime, endTime); return TimerEntity(id, startTime, endTime);
} }
static Timer fromEntity(TimerEntity entity) { 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 { class TimerEntity {
final String id;
final DateTime startTime; final DateTime startTime;
final DateTime endTime; final DateTime endTime;
TimerEntity(this.startTime, this.endTime); TimerEntity(this.id, this.startTime, this.endTime);
@override @override
int get hashCode => startTime.hashCode ^ endTime.hashCode; int get hashCode => id.hashCode ^ startTime.hashCode ^ endTime.hashCode;
@override @override
bool operator ==(Object other) => bool operator ==(Object other) =>
identical(this, other) || identical(this, other) ||
other is TimerEntity && other is TimerEntity &&
runtimeType == other.runtimeType && runtimeType == other.runtimeType &&
id == other.id &&
startTime == other.startTime && startTime == other.startTime &&
endTime == other.endTime; endTime == other.endTime;
Map<String, Object> toJson() { Map<String, Object> toJson() {
return { return {
"id": id,
"startTime": startTime.millisecondsSinceEpoch, "startTime": startTime.millisecondsSinceEpoch,
"endTime": startTime.millisecondsSinceEpoch "endTime": startTime.millisecondsSinceEpoch
}; };
} }
static TimerEntity fromJson(Map<String, Object> json) { 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.fromMillisecondsSinceEpoch(json["startTime"] as int);
DateTime endTime = final DateTime endTime =
DateTime.fromMillisecondsSinceEpoch(json["endTime"] as int); DateTime.fromMillisecondsSinceEpoch(json["endTime"] as int);
return TimerEntity(startTime, endTime); return TimerEntity(id, startTime, endTime);
} }
} }