mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 00:17:48 +01:00
Remove the json serializable decorators, not needed anymore
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (C) 2020, 2021 wger Team
|
||||
* Copyright (c) 2020, wger Team
|
||||
*
|
||||
* wger Workout Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
@@ -16,22 +16,14 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:drift/drift.dart' as drift;
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:powersync/sqlite3.dart' as sqlite;
|
||||
import 'package:wger/helpers/json.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:wger/database/powersync/database.dart';
|
||||
|
||||
part 'weight_entry.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class WeightEntry {
|
||||
@JsonKey(required: true)
|
||||
String? id;
|
||||
|
||||
@JsonKey(required: true, fromJson: stringToNum, toJson: numToString)
|
||||
late num weight = 0;
|
||||
|
||||
@JsonKey(required: true)
|
||||
late DateTime date;
|
||||
|
||||
WeightEntry({this.id, num? weight, DateTime? date}) {
|
||||
@@ -42,50 +34,17 @@ class WeightEntry {
|
||||
}
|
||||
}
|
||||
|
||||
factory WeightEntry.fromRow(sqlite.Row row) {
|
||||
return WeightEntry(
|
||||
id: row['uuid'],
|
||||
date: DateTime.parse(row['date']),
|
||||
weight: row['weight'],
|
||||
);
|
||||
}
|
||||
|
||||
WeightEntry copyWith({String? id, int? weight, DateTime? date}) => WeightEntry(
|
||||
id: id,
|
||||
weight: weight ?? this.weight,
|
||||
date: date ?? this.date,
|
||||
);
|
||||
|
||||
factory WeightEntry.fromData(
|
||||
Map<String, dynamic> data,
|
||||
drift.GeneratedDatabase db, {
|
||||
String? prefix,
|
||||
}) {
|
||||
final effectivePrefix = prefix ?? '';
|
||||
|
||||
final rawId = data['${effectivePrefix}id'];
|
||||
final rawWeight = data['${effectivePrefix}weight'];
|
||||
final rawDate = data['${effectivePrefix}date'];
|
||||
|
||||
final parsedId = rawId as String?;
|
||||
final parsedWeight = rawWeight is num
|
||||
? rawWeight
|
||||
: (rawWeight == null ? 0 : num.tryParse(rawWeight.toString()) ?? 0);
|
||||
final parsedDate = rawDate is DateTime
|
||||
? rawDate
|
||||
: (rawDate == null
|
||||
? DateTime.now()
|
||||
: DateTime.tryParse(rawDate.toString()) ?? DateTime.now());
|
||||
|
||||
return WeightEntry(
|
||||
id: parsedId,
|
||||
weight: parsedWeight,
|
||||
date: parsedDate,
|
||||
WeightEntryTableCompanion toCompanion({bool includeId = false}) {
|
||||
return WeightEntryTableCompanion(
|
||||
id: includeId && id != null ? Value(id!) : const Value.absent(),
|
||||
date: Value(date),
|
||||
weight: Value(weight as double),
|
||||
);
|
||||
}
|
||||
|
||||
// Boilerplate
|
||||
factory WeightEntry.fromJson(Map<String, dynamic> json) => _$WeightEntryFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$WeightEntryToJson(this);
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'weight_entry.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
WeightEntry _$WeightEntryFromJson(Map<String, dynamic> json) {
|
||||
$checkKeys(json, requiredKeys: const ['id', 'weight', 'date']);
|
||||
return WeightEntry(
|
||||
id: json['id'] as String?,
|
||||
weight: stringToNum(json['weight'] as String?),
|
||||
date: json['date'] == null ? null : DateTime.parse(json['date'] as String),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$WeightEntryToJson(WeightEntry instance) => <String, dynamic>{
|
||||
'id': instance.id,
|
||||
'weight': numToString(instance.weight),
|
||||
'date': instance.date.toIso8601String(),
|
||||
};
|
||||
@@ -1,3 +1,21 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (c) 2020, wger Team
|
||||
*
|
||||
* wger Workout Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Repository for body weight network operations.
|
||||
*/
|
||||
@@ -35,23 +53,11 @@ class BodyWeightRepository {
|
||||
Future<void> updateLocalDrift(WeightEntry entry) async {
|
||||
_logger.finer('Updating local weight entry ${entry.id}');
|
||||
final stmt = _db.update(_db.weightEntryTable)..where((t) => t.id.equals(entry.id!));
|
||||
await stmt.write(
|
||||
WeightEntryTableCompanion(
|
||||
date: Value(entry.date),
|
||||
weight: Value(entry.weight as double),
|
||||
),
|
||||
);
|
||||
await stmt.write(entry.toCompanion());
|
||||
}
|
||||
|
||||
Future<void> addLocalDrift(WeightEntry entry) async {
|
||||
_logger.finer('Adding local weight entry ${entry.date}');
|
||||
await _db
|
||||
.into(_db.weightEntryTable)
|
||||
.insert(
|
||||
WeightEntryTableCompanion.insert(
|
||||
date: Value(entry.date),
|
||||
weight: entry.weight as double,
|
||||
),
|
||||
);
|
||||
await _db.into(_db.weightEntryTable).insert(entry.toCompanion());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (C) 2020, 2021 wger Team
|
||||
*
|
||||
* wger Workout Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wger Workout Manager is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:wger/models/body_weight/weight_entry.dart';
|
||||
|
||||
void main() {
|
||||
group('fetchPost', () {
|
||||
test('Test that the weight entries are correctly converted to json', () {
|
||||
expect(
|
||||
WeightEntry(id: '1', weight: 80, date: DateTime(2020, 12, 31, 12, 34)).toJson(),
|
||||
{'id': 1, 'weight': '80', 'date': '2020-12-31T12:34:00.000'},
|
||||
);
|
||||
|
||||
expect(
|
||||
WeightEntry(id: '2', weight: 70.2, date: DateTime(2020, 12, 01)).toJson(),
|
||||
{'id': 2, 'weight': '70.2', 'date': '2020-12-01T00:00:00.000'},
|
||||
);
|
||||
});
|
||||
|
||||
test('Test that the weight entries are correctly converted from json', () {
|
||||
final WeightEntry weightEntryObj = WeightEntry(
|
||||
id: '1',
|
||||
weight: 80,
|
||||
date: DateTime(2020, 12, 31),
|
||||
);
|
||||
final WeightEntry weightEntry = WeightEntry.fromJson({
|
||||
'id': 1,
|
||||
'weight': '80',
|
||||
'date': '2020-12-31',
|
||||
});
|
||||
expect(weightEntry.id, weightEntryObj.id);
|
||||
expect(weightEntry.weight, weightEntryObj.weight);
|
||||
expect(weightEntry.date, weightEntryObj.date);
|
||||
});
|
||||
});
|
||||
|
||||
group('model', () {
|
||||
test('Test the individual values from the model', () {
|
||||
WeightEntry weightModel;
|
||||
weightModel = WeightEntry(id: '1', weight: 80, date: DateTime(2020, 10, 01));
|
||||
|
||||
expect(weightModel.id, 1);
|
||||
expect(weightModel.weight, 80);
|
||||
expect(weightModel.date, DateTime(2020, 10, 01));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (C) 2020, 2021 wger Team
|
||||
* Copyright (c) 2020, wger Team
|
||||
*
|
||||
* wger Workout Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wger Workout Manager is distributed in the hope that it will be useful,
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
@@ -55,6 +55,7 @@ final testBenchPress = Exercise(
|
||||
uuid: '364f196c-881b-4839-8bfc-9e8f651521b6',
|
||||
created: DateTime(2021, 09, 01),
|
||||
lastUpdate: DateTime(2021, 09, 10),
|
||||
categoryId: 1,
|
||||
category: tCategory1,
|
||||
equipment: const [tEquipment1, tEquipment2],
|
||||
muscles: const [tMuscle1, tMuscle2],
|
||||
@@ -67,6 +68,7 @@ final testCrunches = Exercise(
|
||||
uuid: '82415754-fc4c-49ea-8ca7-1516dd36d5a0',
|
||||
created: DateTime(2021, 08, 01),
|
||||
lastUpdate: DateTime(2021, 08, 10),
|
||||
categoryId: 2,
|
||||
category: tCategory2,
|
||||
equipment: const [tEquipment2],
|
||||
muscles: const [tMuscle1],
|
||||
@@ -79,6 +81,7 @@ final testDeadLift = Exercise(
|
||||
uuid: 'ca84e2c5-5608-4d6d-ba57-6d4b6b5e7acd',
|
||||
created: DateTime(2021, 08, 01),
|
||||
lastUpdate: DateTime(2021, 08, 01),
|
||||
categoryId: 3,
|
||||
category: tCategory3,
|
||||
equipment: const [tEquipment2],
|
||||
muscles: const [tMuscle1],
|
||||
@@ -91,6 +94,7 @@ final testCurls = Exercise(
|
||||
uuid: '361f024c-fdf8-4146-b7d7-0c1b67c58141',
|
||||
created: DateTime(2021, 08, 01),
|
||||
lastUpdate: DateTime(2021, 08, 01),
|
||||
categoryId: 3,
|
||||
category: tCategory3,
|
||||
equipment: const [tEquipment2],
|
||||
muscles: const [tMuscle1],
|
||||
@@ -103,6 +107,7 @@ final testSquats = Exercise(
|
||||
uuid: '361f024c-fdf8-4146-b7d7-0c1b67c58141',
|
||||
created: DateTime(2021, 08, 01),
|
||||
lastUpdate: DateTime(2021, 08, 01),
|
||||
categoryId: 3,
|
||||
category: tCategory3,
|
||||
equipment: const [tEquipment2],
|
||||
muscles: const [tMuscle1],
|
||||
@@ -115,6 +120,7 @@ final testSideRaises = Exercise(
|
||||
uuid: '721ff972-c568-41e3-8cf5-cf1e5c5c801c',
|
||||
created: DateTime(2022, 11, 01),
|
||||
lastUpdate: DateTime(2022, 11, 01),
|
||||
categoryId: 4,
|
||||
category: tCategory4,
|
||||
equipment: const [tEquipment2],
|
||||
muscles: const [tMuscle1],
|
||||
|
||||
Reference in New Issue
Block a user