Use generators to read/write JSON

This commit is contained in:
Roland Geider
2020-11-15 17:25:51 +01:00
parent cf877764bf
commit 69639bfbdc
21 changed files with 884 additions and 30 deletions

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -1,5 +1,5 @@
{
"@@last_modified": "2020-11-11T15:04:05.523531",
"@@last_modified": "2020-11-11T15:35:32.537025",
"labelWorkoutPlans": "Workout plans",
"@labelWorkoutPlans": {
"description": "Title for screen workout plans",

View File

@@ -32,6 +32,14 @@ class AppLocalizations {
desc: 'Title for screen workout plan',
);
}
String get labelDashboard {
return Intl.message(
'Dashboard',
name: 'labelDashboard',
desc: 'Title for screen dashboard',
);
}
}
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {

3
lib/models/README.txt Normal file
View File

@@ -0,0 +1,3 @@
flutter pub run build_runner build
flutter pub run build_runner watch

View File

@@ -1,11 +1,26 @@
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
part 'category.g.dart';
@JsonSerializable()
class Category {
@JsonKey(required: true)
final int id;
@JsonKey(required: true)
final String name;
Category({
@required this.id,
@required this.name,
this.id,
this.name,
});
@override
String toString() {
return 'Category $id';
}
// Boilerplate
factory Category.fromJson(Map<String, dynamic> json) => _$CategoryFromJson(json);
Map<String, dynamic> toJson() => _$CategoryToJson(this);
}

View File

@@ -0,0 +1,20 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'category.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Category _$CategoryFromJson(Map<String, dynamic> json) {
$checkKeys(json, requiredKeys: const ['id', 'name']);
return Category(
id: json['id'] as int,
name: json['name'] as String,
);
}
Map<String, dynamic> _$CategoryToJson(Category instance) => <String, dynamic>{
'id': instance.id,
'name': instance.name,
};

View File

@@ -1,11 +1,21 @@
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
part 'comment.g.dart';
@JsonSerializable()
class Comment {
@JsonKey(required: true)
final int id;
@JsonKey(required: true)
final String comment;
Comment({
@required this.id,
@required this.comment,
this.id,
this.comment,
});
// Boilerplate
factory Comment.fromJson(Map<String, dynamic> json) => _$CommentFromJson(json);
Map<String, dynamic> toJson() => _$CommentToJson(this);
}

View File

@@ -0,0 +1,20 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'comment.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Comment _$CommentFromJson(Map<String, dynamic> json) {
$checkKeys(json, requiredKeys: const ['id', 'comment']);
return Comment(
id: json['id'] as int,
comment: json['comment'] as String,
);
}
Map<String, dynamic> _$CommentToJson(Comment instance) => <String, dynamic>{
'id': instance.id,
'comment': instance.comment,
};

View File

@@ -1,11 +1,22 @@
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
part 'equipment.g.dart';
@JsonSerializable()
class Equipment {
@JsonKey(required: true)
final int id;
@JsonKey(required: true)
final String name;
Equipment({
@required this.id,
@required this.name,
});
// Boilerplate
factory Equipment.fromJson(Map<String, dynamic> json) => _$EquipmentFromJson(json);
Map<String, dynamic> toJson() => _$EquipmentToJson(this);
}

View File

@@ -0,0 +1,20 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'equipment.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Equipment _$EquipmentFromJson(Map<String, dynamic> json) {
$checkKeys(json, requiredKeys: const ['id', 'name']);
return Equipment(
id: json['id'] as int,
name: json['name'] as String,
);
}
Map<String, dynamic> _$EquipmentToJson(Equipment instance) => <String, dynamic>{
'id': instance.id,
'name': instance.name,
};

View File

@@ -1,29 +1,53 @@
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:wger/models/exercises/category.dart' as cat;
import 'package:wger/models/exercises/comment.dart';
import 'package:wger/models/exercises/equipment.dart';
import 'package:wger/models/exercises/image.dart' as img;
import 'package:wger/models/exercises/muscle.dart';
part 'exercise.g.dart';
@JsonSerializable(explicitToJson: true)
class Exercise {
@JsonKey(required: true)
final int id;
@JsonKey(required: true)
final String uuid;
@JsonKey(required: true, name: 'creation_date')
final DateTime creationDate;
@JsonKey(required: true)
final String name;
@JsonKey(required: true)
final String description;
@JsonKey(required: true)
final cat.Category category;
@JsonKey(required: false)
List<Muscle> muscles = [];
@JsonKey(required: false)
List<Muscle> musclesSecondary = [];
@JsonKey(required: false)
List<Equipment> equipment = [];
@JsonKey(required: false)
List<img.Image> images = [];
@JsonKey(required: false)
List<Comment> tips = [];
Exercise({
@required this.id,
@required this.uuid,
@required this.creationDate,
@required this.name,
@required this.description,
this.id,
this.uuid,
this.creationDate,
this.name,
this.description,
this.category,
this.muscles,
this.musclesSecondary,
@@ -35,4 +59,8 @@ class Exercise {
img.Image get getMainImage {
return images.firstWhere((image) => image.isMain);
}
// Boilerplate
factory Exercise.fromJson(Map<String, dynamic> json) => _$ExerciseFromJson(json);
Map<String, dynamic> toJson() => _$ExerciseToJson(this);
}

View File

@@ -0,0 +1,52 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'exercise.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Exercise _$ExerciseFromJson(Map<String, dynamic> json) {
$checkKeys(json,
requiredKeys: const ['id', 'uuid', 'creation_date', 'name', 'description', 'category']);
return Exercise(
id: json['id'] as int,
uuid: json['uuid'] as String,
creationDate:
json['creation_date'] == null ? null : DateTime.parse(json['creation_date'] as String),
name: json['name'] as String,
description: json['description'] as String,
category: json['category'] == null
? null
: cat.Category.fromJson(json['category'] as Map<String, dynamic>),
muscles: (json['muscles'] as List)
?.map((e) => e == null ? null : Muscle.fromJson(e as Map<String, dynamic>))
?.toList(),
musclesSecondary: (json['musclesSecondary'] as List)
?.map((e) => e == null ? null : Muscle.fromJson(e as Map<String, dynamic>))
?.toList(),
equipment: (json['equipment'] as List)
?.map((e) => e == null ? null : Equipment.fromJson(e as Map<String, dynamic>))
?.toList(),
images: (json['images'] as List)
?.map((e) => e == null ? null : img.Image.fromJson(e as Map<String, dynamic>))
?.toList(),
tips: (json['tips'] as List)
?.map((e) => e == null ? null : Comment.fromJson(e as Map<String, dynamic>))
?.toList(),
);
}
Map<String, dynamic> _$ExerciseToJson(Exercise instance) => <String, dynamic>{
'id': instance.id,
'uuid': instance.uuid,
'creation_date': instance.creationDate?.toIso8601String(),
'name': instance.name,
'description': instance.description,
'category': instance.category?.toJson(),
'muscles': instance.muscles?.map((e) => e?.toJson())?.toList(),
'musclesSecondary': instance.musclesSecondary?.map((e) => e?.toJson())?.toList(),
'equipment': instance.equipment?.map((e) => e?.toJson())?.toList(),
'images': instance.images?.map((e) => e?.toJson())?.toList(),
'tips': instance.tips?.map((e) => e?.toJson())?.toList(),
};

View File

@@ -1,11 +1,21 @@
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
part 'image.g.dart';
@JsonSerializable()
class Image {
@JsonKey(required: true)
final String url;
@JsonKey(name: 'is_main', defaultValue: false)
final bool isMain;
Image({
@required this.url,
@required this.isMain,
this.url,
this.isMain,
});
// Boilerplate
factory Image.fromJson(Map<String, dynamic> json) => _$ImageFromJson(json);
Map<String, dynamic> toJson() => _$ImageToJson(this);
}

View File

@@ -0,0 +1,20 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'image.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Image _$ImageFromJson(Map<String, dynamic> json) {
$checkKeys(json, requiredKeys: const ['url']);
return Image(
url: json['url'] as String,
isMain: json['is_main'] as bool ?? false,
);
}
Map<String, dynamic> _$ImageToJson(Image instance) => <String, dynamic>{
'url': instance.url,
'is_main': instance.isMain,
};

View File

@@ -1,13 +1,25 @@
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
part 'muscle.g.dart';
@JsonSerializable()
class Muscle {
@JsonKey(required: true)
final int id;
@JsonKey(required: true)
final String name;
@JsonKey(name: 'is_front', required: true)
final bool isFront;
Muscle({
@required this.id,
@required this.name,
@required this.isFront,
this.id,
this.name,
this.isFront,
});
// Boilerplate
factory Muscle.fromJson(Map<String, dynamic> json) => _$MuscleFromJson(json);
Map<String, dynamic> toJson() => _$MuscleToJson(this);
}

View File

@@ -0,0 +1,22 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'muscle.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Muscle _$MuscleFromJson(Map<String, dynamic> json) {
$checkKeys(json, requiredKeys: const ['id', 'name', 'is_front']);
return Muscle(
id: json['id'] as int,
name: json['name'] as String,
isFront: json['is_front'] as bool,
);
}
Map<String, dynamic> _$MuscleToJson(Muscle instance) => <String, dynamic>{
'id': instance.id,
'name': instance.name,
'is_front': instance.isFront,
};

View File

@@ -2,8 +2,7 @@ import 'package:flutter/foundation.dart';
import 'package:wger/models/exercises/exercise.dart';
import 'package:wger/models/workouts/repetition_unit.dart';
import 'package:wger/models/workouts/weight_unit.dart';
import 'file:///C:/Users/Roland%20Geider/GitHub/flutter_wger/lib/models/workouts/workout_plan.dart';
import 'package:wger/models/workouts/workout_plan.dart';
class Log {
final int id;

View File

@@ -16,7 +16,6 @@ class Auth with ChangeNotifier {
// Timer _authTimer;
bool get isAuth {
// return true;
return token != null;
}
@@ -37,8 +36,7 @@ class Auth with ChangeNotifier {
// return _userId;
// }
Future<void> _authenticate(
String username, String password, String serverUrl) async {
Future<void> _authenticate(String username, String password, String serverUrl) async {
// The android emulator uses
var url = '$serverUrl/api/v2/login/';
//print(username);
@@ -91,8 +89,7 @@ class Auth with ChangeNotifier {
return _authenticate(email, password, 'signUp');
}
Future<void> signIn(
String username, String password, String serverUrl) async {
Future<void> signIn(String username, String password, String serverUrl) async {
return _authenticate(username, password, serverUrl);
}
@@ -102,8 +99,7 @@ class Auth with ChangeNotifier {
return false;
}
final extractedUserData =
json.decode(prefs.getString('userData')) as Map<String, Object>;
final extractedUserData = json.decode(prefs.getString('userData')) as Map<String, Object>;
// final expiryDate = DateTime.parse(extractedUserData['expiryDate']);
// if (expiryDate.isBefore(DateTime.now())) {

View File

@@ -0,0 +1,54 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wger/locale/locales.dart';
import 'package:wger/providers/exercises.dart';
import 'package:wger/widgets/app_drawer.dart';
class DashboardScreen extends StatefulWidget {
static const routeName = '/dashboard';
@override
_DashboardScreenState createState() => _DashboardScreenState();
}
class _DashboardScreenState extends State<DashboardScreen> {
Widget getAppBar() {
return AppBar(
title: Text(AppLocalizations.of(context).labelDashboard),
actions: [
IconButton(
icon: Icon(Icons.bar_chart),
onPressed: () {},
),
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: getAppBar(),
drawer: AppDrawer(),
body: Column(
children: [
Center(
child: Text("dashboard content here"),
),
FlatButton(
child: Text('reload'),
onPressed: () {
Provider.of<Exercises>(context, listen: false).fetchAndSetExercises();
},
),
FutureBuilder(
future: Provider.of<Exercises>(context, listen: false).fetchAndSetExercises(),
builder: (ctx, authResultSnapshot) =>
authResultSnapshot.connectionState == ConnectionState.waiting
? Center(child: CircularProgressIndicator())
: Text("all exercises loaded"),
),
],
),
);
}
}

View File

@@ -43,6 +43,62 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0-nullsafety.1"
build:
dependency: transitive
description:
name: build
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
build_config:
dependency: transitive
description:
name: build_config
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
build_daemon:
dependency: transitive
description:
name: build_daemon
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
build_resolvers:
dependency: transitive
description:
name: build_resolvers
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.11"
build_runner:
dependency: "direct dev"
description:
name: build_runner
url: "https://pub.dartlang.org"
source: hosted
version: "1.10.2"
build_runner_core:
dependency: transitive
description:
name: build_runner_core
url: "https://pub.dartlang.org"
source: hosted
version: "6.0.1"
built_collection:
dependency: transitive
description:
name: built_collection
url: "https://pub.dartlang.org"
source: hosted
version: "4.3.2"
built_value:
dependency: transitive
description:
name: built_value
url: "https://pub.dartlang.org"
source: hosted
version: "7.1.0"
characters:
dependency: transitive
description:
@@ -57,6 +113,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0-nullsafety.1"
checked_yaml:
dependency: transitive
description:
name: checked_yaml
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
cli_util:
dependency: transitive
description:
@@ -71,6 +134,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.1"
code_builder:
dependency: transitive
description:
name: code_builder
url: "https://pub.dartlang.org"
source: hosted
version: "3.5.0"
collection:
dependency: transitive
description:
@@ -134,6 +204,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "5.2.1"
fixnum:
dependency: transitive
description:
name: fixnum
url: "https://pub.dartlang.org"
source: hosted
version: "0.10.11"
flutter:
dependency: "direct main"
description: flutter
@@ -175,6 +252,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
graphs:
dependency: transitive
description:
name: graphs
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0"
html:
dependency: transitive
description:
@@ -189,6 +273,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.2"
http_multi_server:
dependency: transitive
description:
name: http_multi_server
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
http_parser:
dependency: transitive
description:
@@ -217,6 +308,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.17.10+1"
io:
dependency: transitive
description:
name: io
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.4"
js:
dependency: transitive
description:
@@ -224,6 +322,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.2"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
json_serializable:
dependency: "direct main"
description:
name: json_serializable
url: "https://pub.dartlang.org"
source: hosted
version: "3.5.0"
logging:
dependency: transitive
description:
name: logging
url: "https://pub.dartlang.org"
source: hosted
version: "0.11.4"
matcher:
dependency: transitive
description:
@@ -238,6 +357,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0-nullsafety.3"
mime:
dependency: transitive
description:
name: mime
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.7"
nested:
dependency: transitive
description:
@@ -322,6 +448,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
pool:
dependency: transitive
description:
name: pool
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.0"
process:
dependency: transitive
description:
@@ -343,6 +476,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.4"
pubspec_parse:
dependency: transitive
description:
name: pubspec_parse
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.5"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.5"
shared_preferences:
dependency: "direct main"
description:
@@ -385,11 +532,32 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.1+3"
shelf:
dependency: transitive
description:
name: shelf
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.9"
shelf_web_socket:
dependency: transitive
description:
name: shelf_web_socket
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.3"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
source_gen:
dependency: transitive
description:
name: source_gen
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.7+1"
source_span:
dependency: transitive
description:
@@ -411,6 +579,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0-nullsafety.1"
stream_transform:
dependency: transitive
description:
name: stream_transform
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
string_scanner:
dependency: transitive
description:
@@ -432,6 +607,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19-nullsafety.2"
timing:
dependency: transitive
description:
name: timing
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.1+2"
typed_data:
dependency: transitive
description:
@@ -453,6 +635,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.7+15"
web_socket_channel:
dependency: transitive
description:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
win32:
dependency: transitive
description:

View File

@@ -32,12 +32,18 @@ dependencies:
shared_preferences: ^0.5.12+4
flutter_calendar_carousel: ^1.5.1
cupertino_icons: ^1.0.0
json_serializable: ^3.5.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter_launcher_icons: ^0.8.1
intl_translation: ^0.17.10+1
json_serializable: ^3.5.0
# Note that there are incompatibilities when using a higher version. Both build
# runner and intl_translation depend on different versions of analyzer
build_runner: ^1.10.2