Skip to content

Instantly share code, notes, and snippets.

@aBuder
Created August 15, 2018 20:00
Show Gist options
  • Save aBuder/ca09a5d08ede710e032f26dbaf45560a to your computer and use it in GitHub Desktop.
Save aBuder/ca09a5d08ede710e032f26dbaf45560a to your computer and use it in GitHub Desktop.
Models
class Plan {
final String objectId;
final String name;
final String text;
final DateTime startedAt;
final DateTime endedAt;
final String createdAt;
Plan(
{this.objectId,
this.name,
this.text,
this.startedAt,
this.endedAt,
this.createdAt});
factory Plan.fromJson(Map<String, dynamic> json) {
return Plan(
objectId: json['objectId'],
name: json['name'],
text: json['text'],
startedAt: DateTime(
int.parse(json['startedAt']['iso'].substring(0, 4)),
int.parse(json['startedAt']['iso'].substring(5, 7)),
int.parse(json['startedAt']['iso'].substring(8, 10))),
endedAt: DateTime(
int.parse(json['endedAt']['iso'].substring(0, 4)),
int.parse(json['endedAt']['iso'].substring(5, 7)),
int.parse(json['endedAt']['iso'].substring(8, 10))),
createdAt: json['createdAt'],
);
}
String formattedStartedAt() {
return this.startedAt.day.toString() +
"." +
this.startedAt.month.toString() +
"." +
this.startedAt.year.toString();
}
String formattedEndedAt() {
return this.endedAt.day.toString() +
"." +
this.endedAt.month.toString() +
"." +
this.endedAt.year.toString();
}
String formattedDuration() {
Duration duration = this.endedAt.difference(this.startedAt);
int weeks = duration.inDays % 7;
return (weeks < 2) ? '${weeks} Woche' : '${weeks} Wochen';
}
}
class PlanItem {
String type;
String description;
Exercise exercise;
List<Set> sets = [];
PlanItem(this.type, this.description, this.exercise, this.sets);
factory PlanItem.fromJson(Map<String, dynamic> json) {
return PlanItem(
json['type'],
json['description'],
(json['exercise'] != null) ? Exercise.fromJson(json['exercise']) : null,
(json['sets'] as List).map((i) {
return Set.fromJson(i);
}).toList(),
);
}
/*
* Check if planItem is Day
*/
bool isDay() {
return this.type == 'DAY';
}
/*
* Check if planItem is Exercise
*/
bool isExercise() {
return this.type == 'EXERCISE';
}
/*
* Check if planItem is Text
*/
bool isText() {
return this.type == 'TEXT';
}
}
class Exercise {
String name;
String defaultImageUrl;
Exercise(this.name, this.defaultImageUrl);
// convert Json to an exercise object
factory Exercise.fromJson(Map<String, dynamic> json) {
return Exercise(
json['name'] as String,
json['defaultImageUrl'] as String,
);
}
}
class Set {
int repetitions;
int weight;
int time;
Set(this.repetitions, this.weight, this.time);
// convert Json to an exercise object
factory Set.fromJson(Map<String, dynamic> json) {
return Set(
json['repetitions'] as int,
json['weight'] as int,
json['time'] as int,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment