mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-03 02:24:30 +01:00
More work on workout serialization
Also, it is now possible to delete workouts from the DB
This commit is contained in:
@@ -1,16 +1,25 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:wger/models/workouts/set.dart';
|
||||
|
||||
part 'day.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class Day {
|
||||
@JsonKey(required: true)
|
||||
final int id;
|
||||
|
||||
@JsonKey(required: true)
|
||||
final String description;
|
||||
|
||||
@JsonKey(required: true, name: 'days_of_week')
|
||||
List<int> daysOfWeek = [];
|
||||
|
||||
@JsonKey(required: true)
|
||||
List<Set> sets = [];
|
||||
|
||||
Day({
|
||||
@required this.id,
|
||||
@required this.description,
|
||||
this.id,
|
||||
this.description,
|
||||
this.daysOfWeek,
|
||||
this.sets,
|
||||
});
|
||||
@@ -32,4 +41,8 @@ class Day {
|
||||
String get getAllDays {
|
||||
return daysOfWeek.map((e) => getDayName(e)).join(', ');
|
||||
}
|
||||
|
||||
// Boilerplate
|
||||
factory Day.fromJson(Map<String, dynamic> json) => _$DayFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$DayToJson(this);
|
||||
}
|
||||
|
||||
27
lib/models/workouts/day.g.dart
Normal file
27
lib/models/workouts/day.g.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'day.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
Day _$DayFromJson(Map<String, dynamic> json) {
|
||||
$checkKeys(json,
|
||||
requiredKeys: const ['id', 'description', 'days_of_week', 'sets']);
|
||||
return Day(
|
||||
id: json['id'] as int,
|
||||
description: json['description'] as String,
|
||||
daysOfWeek: (json['days_of_week'] as List)?.map((e) => e as int)?.toList(),
|
||||
sets: (json['sets'] as List)
|
||||
?.map((e) => e == null ? null : Set.fromJson(e as Map<String, dynamic>))
|
||||
?.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$DayToJson(Day instance) => <String, dynamic>{
|
||||
'id': instance.id,
|
||||
'description': instance.description,
|
||||
'days_of_week': instance.daysOfWeek,
|
||||
'sets': instance.sets,
|
||||
};
|
||||
@@ -1,11 +1,22 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'repetition_unit.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class RepetitionUnit {
|
||||
@JsonKey(required: true)
|
||||
final int id;
|
||||
|
||||
@JsonKey(required: true)
|
||||
final String name;
|
||||
|
||||
RepetitionUnit({
|
||||
@required this.id,
|
||||
@required this.name,
|
||||
});
|
||||
|
||||
// Boilerplate
|
||||
factory RepetitionUnit.fromJson(Map<String, dynamic> json) => _$RepetitionUnitFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$RepetitionUnitToJson(this);
|
||||
}
|
||||
|
||||
21
lib/models/workouts/repetition_unit.g.dart
Normal file
21
lib/models/workouts/repetition_unit.g.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'repetition_unit.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
RepetitionUnit _$RepetitionUnitFromJson(Map<String, dynamic> json) {
|
||||
$checkKeys(json, requiredKeys: const ['id', 'name']);
|
||||
return RepetitionUnit(
|
||||
id: json['id'] as int,
|
||||
name: json['name'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$RepetitionUnitToJson(RepetitionUnit instance) =>
|
||||
<String, dynamic>{
|
||||
'id': instance.id,
|
||||
'name': instance.name,
|
||||
};
|
||||
@@ -1,19 +1,35 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/workouts/setting.dart';
|
||||
|
||||
part 'set.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class Set {
|
||||
@JsonKey(required: true)
|
||||
final int id;
|
||||
|
||||
@JsonKey(required: true)
|
||||
final int sets;
|
||||
|
||||
@JsonKey(required: true)
|
||||
final int order;
|
||||
|
||||
@JsonKey(required: true)
|
||||
List<Exercise> exercises = [];
|
||||
|
||||
@JsonKey(required: true)
|
||||
List<Setting> settings = [];
|
||||
|
||||
Set({
|
||||
@required this.id,
|
||||
@required this.sets,
|
||||
@required this.order,
|
||||
this.id,
|
||||
this.sets,
|
||||
this.order,
|
||||
this.exercises,
|
||||
this.settings,
|
||||
});
|
||||
|
||||
// Boilerplate
|
||||
factory Set.fromJson(Map<String, dynamic> json) => _$SetFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$SetToJson(this);
|
||||
}
|
||||
|
||||
33
lib/models/workouts/set.g.dart
Normal file
33
lib/models/workouts/set.g.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'set.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
Set _$SetFromJson(Map<String, dynamic> json) {
|
||||
$checkKeys(json,
|
||||
requiredKeys: const ['id', 'sets', 'order', 'exercises', 'settings']);
|
||||
return Set(
|
||||
id: json['id'] as int,
|
||||
sets: json['sets'] as int,
|
||||
order: json['order'] as int,
|
||||
exercises: (json['exercises'] as List)
|
||||
?.map((e) =>
|
||||
e == null ? null : Exercise.fromJson(e as Map<String, dynamic>))
|
||||
?.toList(),
|
||||
settings: (json['settings'] as List)
|
||||
?.map((e) =>
|
||||
e == null ? null : Setting.fromJson(e as Map<String, dynamic>))
|
||||
?.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$SetToJson(Set instance) => <String, dynamic>{
|
||||
'id': instance.id,
|
||||
'sets': instance.sets,
|
||||
'order': instance.order,
|
||||
'exercises': instance.exercises,
|
||||
'settings': instance.settings,
|
||||
};
|
||||
@@ -1,26 +1,49 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/workouts//weight_unit.dart';
|
||||
import 'package:wger/models/workouts/repetition_unit.dart';
|
||||
|
||||
part 'setting.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class Setting {
|
||||
@JsonKey(required: true)
|
||||
final int id;
|
||||
|
||||
@JsonKey(required: true)
|
||||
final Exercise exercise;
|
||||
//final RepetitionUnit repetitionUnit;
|
||||
|
||||
@JsonKey(required: true, name: 'repetition_unit')
|
||||
final RepetitionUnit repetitionUnit;
|
||||
|
||||
@JsonKey(required: true)
|
||||
final int reps;
|
||||
|
||||
@JsonKey(required: true)
|
||||
final double weight;
|
||||
//final WeightUnit weightUnit;
|
||||
|
||||
@JsonKey(required: true, name: 'weight_unit')
|
||||
final WeightUnit weightUnit;
|
||||
|
||||
@JsonKey(required: true)
|
||||
final String comment;
|
||||
|
||||
// Generated by Server
|
||||
@JsonKey(required: true)
|
||||
final String repsText;
|
||||
|
||||
Setting({
|
||||
@required this.id,
|
||||
this.id,
|
||||
this.exercise,
|
||||
//this.repetitionUnit,
|
||||
@required this.reps,
|
||||
@required this.weight,
|
||||
//this.weightUnit,
|
||||
@required this.comment,
|
||||
this.repetitionUnit,
|
||||
this.reps,
|
||||
this.weight,
|
||||
this.weightUnit,
|
||||
this.comment,
|
||||
this.repsText,
|
||||
});
|
||||
|
||||
// Boilerplate
|
||||
factory Setting.fromJson(Map<String, dynamic> json) => _$SettingFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$SettingToJson(this);
|
||||
}
|
||||
|
||||
48
lib/models/workouts/setting.g.dart
Normal file
48
lib/models/workouts/setting.g.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'setting.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
Setting _$SettingFromJson(Map<String, dynamic> json) {
|
||||
$checkKeys(json, requiredKeys: const [
|
||||
'id',
|
||||
'exercise',
|
||||
'repetition_unit',
|
||||
'reps',
|
||||
'weight',
|
||||
'weight_unit',
|
||||
'comment',
|
||||
'repsText'
|
||||
]);
|
||||
return Setting(
|
||||
id: json['id'] as int,
|
||||
exercise: json['exercise'] == null
|
||||
? null
|
||||
: Exercise.fromJson(json['exercise'] as Map<String, dynamic>),
|
||||
repetitionUnit: json['repetition_unit'] == null
|
||||
? null
|
||||
: RepetitionUnit.fromJson(
|
||||
json['repetition_unit'] as Map<String, dynamic>),
|
||||
reps: json['reps'] as int,
|
||||
weight: (json['weight'] as num)?.toDouble(),
|
||||
weightUnit: json['weight_unit'] == null
|
||||
? null
|
||||
: WeightUnit.fromJson(json['weight_unit'] as Map<String, dynamic>),
|
||||
comment: json['comment'] as String,
|
||||
repsText: json['repsText'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$SettingToJson(Setting instance) => <String, dynamic>{
|
||||
'id': instance.id,
|
||||
'exercise': instance.exercise,
|
||||
'repetition_unit': instance.repetitionUnit,
|
||||
'reps': instance.reps,
|
||||
'weight': instance.weight,
|
||||
'weight_unit': instance.weightUnit,
|
||||
'comment': instance.comment,
|
||||
'repsText': instance.repsText,
|
||||
};
|
||||
@@ -1,11 +1,21 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'weight_unit.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class WeightUnit {
|
||||
@JsonKey(required: true)
|
||||
final int id;
|
||||
|
||||
@JsonKey(required: true)
|
||||
final String name;
|
||||
|
||||
WeightUnit({
|
||||
@required this.id,
|
||||
@required this.name,
|
||||
this.id,
|
||||
this.name,
|
||||
});
|
||||
|
||||
// Boilerplate
|
||||
factory WeightUnit.fromJson(Map<String, dynamic> json) => _$WeightUnitFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$WeightUnitToJson(this);
|
||||
}
|
||||
|
||||
21
lib/models/workouts/weight_unit.g.dart
Normal file
21
lib/models/workouts/weight_unit.g.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'weight_unit.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
WeightUnit _$WeightUnitFromJson(Map<String, dynamic> json) {
|
||||
$checkKeys(json, requiredKeys: const ['id', 'name']);
|
||||
return WeightUnit(
|
||||
id: json['id'] as int,
|
||||
name: json['name'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$WeightUnitToJson(WeightUnit instance) =>
|
||||
<String, dynamic>{
|
||||
'id': instance.id,
|
||||
'name': instance.name,
|
||||
};
|
||||
@@ -1,9 +1,20 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
|
||||
part 'workout_plan.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class WorkoutPlan {
|
||||
@JsonKey(required: true)
|
||||
final int id;
|
||||
|
||||
@JsonKey(required: true, name: 'creation_date')
|
||||
DateTime creationDate;
|
||||
|
||||
@JsonKey(required: true)
|
||||
String description;
|
||||
|
||||
@JsonKey(required: true, name: 'days')
|
||||
List<Day> days = [];
|
||||
|
||||
WorkoutPlan({
|
||||
@@ -12,4 +23,8 @@ class WorkoutPlan {
|
||||
this.description,
|
||||
this.days,
|
||||
});
|
||||
|
||||
// Boilerplate
|
||||
factory WorkoutPlan.fromJson(Map<String, dynamic> json) => _$WorkoutPlanFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$WorkoutPlanToJson(this);
|
||||
}
|
||||
|
||||
30
lib/models/workouts/workout_plan.g.dart
Normal file
30
lib/models/workouts/workout_plan.g.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'workout_plan.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
WorkoutPlan _$WorkoutPlanFromJson(Map<String, dynamic> json) {
|
||||
$checkKeys(json,
|
||||
requiredKeys: const ['id', 'creation_date', 'description', 'days']);
|
||||
return WorkoutPlan(
|
||||
id: json['id'] as int,
|
||||
creationDate: json['creation_date'] == null
|
||||
? null
|
||||
: DateTime.parse(json['creation_date'] as String),
|
||||
description: json['description'] as String,
|
||||
days: (json['days'] as List)
|
||||
?.map((e) => e == null ? null : Day.fromJson(e as Map<String, dynamic>))
|
||||
?.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$WorkoutPlanToJson(WorkoutPlan instance) =>
|
||||
<String, dynamic>{
|
||||
'id': instance.id,
|
||||
'creation_date': instance.creationDate?.toIso8601String(),
|
||||
'description': instance.description,
|
||||
'days': instance.days,
|
||||
};
|
||||
@@ -144,24 +144,15 @@ class WorkoutPlans with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> addProduct(WorkoutPlan product) async {
|
||||
final productsUrl =
|
||||
'https://flutter-shop-a2335.firebaseio.com/products.json?auth=${_auth.token}';
|
||||
Future<void> addWorkout(WorkoutPlan workout) async {
|
||||
try {
|
||||
final response = await http.post(
|
||||
productsUrl,
|
||||
body: json.encode(
|
||||
{
|
||||
'description': product.description,
|
||||
},
|
||||
),
|
||||
_url,
|
||||
body: json.encode(workout.toJson()),
|
||||
);
|
||||
final newProduct = WorkoutPlan(
|
||||
id: json.decode(response.body)['name'],
|
||||
creationDate: json.decode(response.body)['creation_date'],
|
||||
description: product.description,
|
||||
);
|
||||
_entries.add(newProduct);
|
||||
print(json.decode(response.body));
|
||||
final newWorkout = WorkoutPlan.fromJson(json.decode(response.body));
|
||||
_entries.add(newWorkout);
|
||||
notifyListeners();
|
||||
} catch (error) {
|
||||
print(error);
|
||||
@@ -182,19 +173,22 @@ class WorkoutPlans with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteProduct(int id) async {
|
||||
final url = 'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=${_auth.token}';
|
||||
final existingProductIndex = _entries.indexWhere((element) => element.id == id);
|
||||
var existingProduct = _entries[existingProductIndex];
|
||||
_entries.removeAt(existingProductIndex);
|
||||
Future<void> deleteWorkout(int id) async {
|
||||
final url = '$_url$id/';
|
||||
final existingWorkoutIndex = _entries.indexWhere((element) => element.id == id);
|
||||
var existingWorkout = _entries[existingWorkoutIndex];
|
||||
_entries.removeAt(existingWorkoutIndex);
|
||||
notifyListeners();
|
||||
|
||||
final response = await http.delete(url);
|
||||
final response = await http.delete(
|
||||
url,
|
||||
headers: <String, String>{'Authorization': 'Token ${_auth.token}'},
|
||||
);
|
||||
if (response.statusCode >= 400) {
|
||||
_entries.insert(existingProductIndex, existingProduct);
|
||||
_entries.insert(existingWorkoutIndex, existingWorkout);
|
||||
notifyListeners();
|
||||
//throw HttpException();
|
||||
}
|
||||
existingProduct = null;
|
||||
existingWorkout = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,19 +11,53 @@ class WorkoutPlansList extends StatelessWidget {
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.all(10.0),
|
||||
itemCount: workoutPlansData.items.length,
|
||||
itemBuilder: (context, index) => Card(
|
||||
child: ListTile(
|
||||
onTap: () => Navigator.of(context).pushNamed(
|
||||
WorkoutPlanScreen.routeName,
|
||||
arguments: workoutPlansData.items[index],
|
||||
itemBuilder: (context, index) {
|
||||
final currentWorkout = workoutPlansData.items[index];
|
||||
return Dismissible(
|
||||
key: Key(currentWorkout.id.toString()),
|
||||
onDismissed: (direction) {
|
||||
// Delete workout from DB
|
||||
Provider.of<WorkoutPlans>(context, listen: false).deleteWorkout(currentWorkout.id);
|
||||
|
||||
// and inform the user
|
||||
Scaffold.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"Workout ${currentWorkout.id} deleted",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
background: Container(
|
||||
color: Theme.of(context).errorColor,
|
||||
alignment: Alignment.centerRight,
|
||||
padding: EdgeInsets.only(right: 20),
|
||||
margin: EdgeInsets.symmetric(
|
||||
horizontal: 15,
|
||||
vertical: 4,
|
||||
),
|
||||
child: Icon(
|
||||
Icons.delete,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
leading: Icon(Icons.edit),
|
||||
title: Text(
|
||||
DateFormat('dd.MM.yyyy').format(workoutPlansData.items[index].creationDate).toString(),
|
||||
direction: DismissDirection.endToStart,
|
||||
child: Card(
|
||||
child: ListTile(
|
||||
onTap: () => Navigator.of(context).pushNamed(
|
||||
WorkoutPlanScreen.routeName,
|
||||
arguments: currentWorkout,
|
||||
),
|
||||
leading: Icon(Icons.edit),
|
||||
title: Text(
|
||||
DateFormat('dd.MM.yyyy').format(currentWorkout.creationDate).toString(),
|
||||
),
|
||||
subtitle: Text(currentWorkout.description),
|
||||
),
|
||||
),
|
||||
subtitle: Text(workoutPlansData.items[index].description),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user